<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="700px"
    @close="close"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item :label="__('分类名称')" prop="store_product_cat_name">
        <el-input
          v-model="form.store_product_cat_name"
          :placeholder="__('分类名称')"
        />
      </el-form-item>
      <el-form-item :label="__('排序')" prop="store_product_cat_order">
        <el-input
          v-model="form.store_product_cat_order"
          :placeholder="__('排序')"
        />
      </el-form-item>
      <el-form-item :label="__('父编号')" prop="parent_id">
        <el-input
          v-model="form.parent_id"
          :disabled="isEdit"
          :placeholder="__('父编号')"
        />
      </el-form-item>
      <el-form-item
        :label="__('统计购物车数量')"
        prop="store_product_cat_num_enable"
      >
        <el-switch
          v-model="form.store_product_cat_num_enable"

          active-text="启用"
          :active-value="1"

          inactive-text="禁用"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item :label="__('分类图片')" prop="store_product_cat_image">
        <upload
          height="100px"
          :image="form.store_product_cat_image"
          width="100px"
          @upImage="
            (image) => {
              form.store_product_cat_image = image
            }
          "
        />
      </el-form-item>
      <el-form-item :label="__('是否启用')" prop="store_product_cat_enable">
        <el-switch
          v-model="form.store_product_cat_enable"

          active-text="启用"
          :active-value="1"

          inactive-text="禁用"
          :inactive-value="0"
        />
      </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/store/product/category'
  import { URL } from '@/config'

  export default {
    name: 'StoreProductCategoryEdit',
    components: { Upload },
    data() {
      return {
        action: URL.action,
        isEdit: false,
        form: {
          store_product_cat_image: '',
          store_product_cat_num_enable: 1,
          store_product_cat_enable: 1,
        },
        rules: {
          store_product_cat_name: [
            { required: true, trigger: 'blur', message: this.__('分类名称') },
          ],
          store_id: [
            { required: true, trigger: 'blur', message: this.__('所属店铺') },
          ],
          store_product_cat_enable: [
            { required: true, trigger: 'blur', message: this.__('是否启用') },
          ],
          store_product_cat_level: [
            { required: true, trigger: 'blur', message: this.__('分类等级') },
          ],
          store_product_cat_num_enable: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('是否统计购物车数量'),
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row, store_product_cat_id) {
        if (!row) {
          this.title = this.__('新增')
          if (store_product_cat_id != null) {
            this.form.parent_id = store_product_cat_id
            this.isEdit = true
          } else {
            this.form.parent_id = 0
            this.isEdit = true
          }
        } else {
          this.title = this.__('编辑')
          this.isEdit = true
          const obj = Object.assign({}, row)

          this.form = {
            store_product_cat_id: obj.store_product_cat_id,
            parent_id: obj.parent_id,
            store_product_cat_image: obj.store_product_cat_image,
            store_product_cat_enable: obj.store_product_cat_enable,
            store_product_cat_num_enable: obj.store_product_cat_num_enable,
            store_product_cat_order: obj.store_product_cat_order,
            store_product_cat_name: obj.store_product_cat_name,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          store_product_cat_image: '',
          store_product_cat_num_enable: 1,
          store_product_cat_enable: 1,
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            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
          }
        })
      },
    },
  }
</script>
