<template>
  <div>
    <el-dialog :title="exData.id ? '编辑': '新增'"
               :visible.sync="dialog"
               width="600px"
               :close-on-click-modal="false"
               top="50px">
      <el-form ref="form"
               :model="form"
               :rules="formRules"
               label-width="160px">
        <el-form-item prop="category_name"
                      :rules="formRules.required"
                      label="分类名称：">
          <el-col :span="16">
            <el-input v-model="form.category_name"
                      placeholder="请输入分类名称"
                      clearable></el-input>
          </el-col>
        </el-form-item>
        <el-form-item prop="category_sort_id"
                      :rules="{required: true, message: '必填', trigger: 'blur'}"
                      label="分类排序：">
          <el-col :span="16">
            <el-input v-model="form.category_sort_id"
                      type="number"
                      placeholder="请输入分类排序序号(数字)"
                      clearable></el-input>
          </el-col>
        </el-form-item>
        <el-form-item prop="category_status"
                      :rules="formRules.required"
                      label="分类状态：">
          <el-radio v-model="form.category_status" label="1">上架</el-radio>
          <el-radio v-model="form.category_status" label="0">下架</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer text-center">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="primary" :disabled="booLock" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    value: {
      type: Boolean,
      default: true
    },
    exData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      dialog: !!this.value,
      form: {
        category_name: '', // 分类名称
        category_sort_id: '', // 分类排序
        category_status: '1' // 分类状态(0下架1上架)
      },
      booLock: false
    }
  },
  methods: {
    handleSubmit () {
      const url = this.exData.id ? '/v1/bar/product/category/modify' : '/v1/bar/product/category/add'
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.booLock = true
          const data = await this.$fetch(url, {
            ...this.form
          })
          this.booLock = false
          if (data.code === 200) {
            this.$message.success('提交成功')
            this.$emit('success')
            this.$store.dispatch('common/fetchCategory')
            this.dialog = false
          }
        }
      })
    }
  },
  mounted () {
    if (this.exData.id) {
      this.$set(this.form, 'id', this.exData.id)
      for (const key in this.exData) {
        if (this.form.hasOwnProperty(key)) {
          let value = this.exData[key]
          if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') {
            if (key === 'category_status') {
              value = value.toString()
            }
            this.$set(this.form, key, value)
          }
        }
      }
    }
  },
  watch: {
    dialog (val) {
      if (!val) this.$emit('input', val)
    }
  }
}
</script>

<style lang="scss" scoped>
.top-tip {
  margin-top: -20px;
  margin-bottom: 20px;
}
</style>
