<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="600px"
    @close="close"
  >
    <el-form
      ref="form"
      label-position="left"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('选择分类')" prop="category_id">
        <el-select
          v-model="form.category_id"
          clearable
          :disabled="form.isEdit"
          :placeholder="__('请选择')"
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="item in categoryOptions"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="__('分类别名')" prop="category_alias">
        <el-input
          v-model="form.category_alias"
          clearable
          :placeholder="__('请输入分类别名')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('模板分类')" prop="temp_type">
        <el-radio-group v-model="form.temp_type" size="medium">
          <el-radio
            v-for="(item, index) in temp_typeOptions"
            :key="index"
            :disabled="item.disabled"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="__('分类图片')" prop="category_image" required>
        <upload
          height="64px"
          :image="form.category_image"
          width="64px"
          @upImage="
            (image) => {
              form.category_image = image
            }
          "
        />
      </el-form-item>
      <el-form-item :label="__('排序')" prop="category_order">
        <el-input
          v-model="form.category_order"
          clearable
          :placeholder="__('请输入排序')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import Upload from '@/components/upload'
  import { doEdit } from '@/api/base/product/category/nav'
  import { getTree } from '@/api/base/product/category'

  export default {
    name: 'BaseProductCategoryNavEdit',
    components: { Upload },
    data() {
      return {
        categoryOptions: [],
        title: '',
        dialogFormVisible: false,
        form: {},
        rules: {
          category_id: [
            {
              required: true,
              message: '请输入选择分类',
              trigger: 'blur',
            },
          ],
          category_alias: [
            {
              required: true,
              message: '请输入分类别名',
              trigger: 'blur',
            },
          ],
          temp_type: [
            {
              required: true,
              message: '模板分类不能为空',
              trigger: 'change',
            },
          ],
          category_order: [
            {
              required: true,
              message: '请输入排序',
              trigger: 'blur',
            },
          ],
        },
        temp_typeOptions: [
          {
            label: this.__('分类模版'),
            value: 1,
          },
          {
            label: this.__('商品模版'),
            value: 2,
          },
        ],
      }
    },
    created() {
      this.categoryTree()
    },
    methods: {
      async categoryTree() {
        const { data } = await getTree({ category_parent_id: 0 })
        this.categoryOptions = data
      },
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('新增')
          this.form.isEdit = false
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
          this.form.isEdit = true
        }
        this.dialogFormVisible = true
      },
      close() {
        this.dialogFormVisible = false
        this.form = {
          isEdit: false,
          category_image: '',
        }
        this.$refs['form'].resetFields()
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            this.addCategoryName()
            this.form.sub = []
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      addCategoryName() {
        let category_id = this.form.category_id
        if (!category_id) return
        let obj = this.categoryOptions.find((item) => item.id === category_id)
        this.form.category_name = obj.label
      },
    },
  }
</script>
