<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="150px" class="order_form">
      <el-form-item label="分类名称" prop="ClassName">
        <el-input v-model="ruleForm.ClassName" placeholder="请输入分类名称"/>
      </el-form-item>
      <el-form-item label="分类简写" prop="ClassShortName">
        <el-input v-model="ruleForm.ClassShortName" placeholder="请输入分类简写"/>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="ruleForm.sort" placeholder="请输入排序"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCommodityType, addCommodityType, editCommodityType } from '@/api/commodityManagement/commodityType'
import { validateTel } from '@/utils/validate'
import { filterContent } from '@/utils'

export default {
  name: 'AddOrEdit',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      props: { // 自定义省市下拉数据的key值
        value: 'code',
        label: 'name',
        children: 'entityJson'
      },
      sourceOptions: [], // 工单来源下拉数据
      ruleForm: {
        ClassId: '', //分类id
        ClassName: '', //分类名称
        ClassShortName: '', //分类简写
        sort: '', //排序
        ParentId: 0, //顶级
      },
      rules: {
        ClassName: [{
          required: true,
          trigger: 'blur',
          message: '请输入分类名称',
        }],
        ClassShortName: [{
          required: true,
          trigger: 'blur',
          message: '请输入分类简写',
        }]
      },
      loading: false
    }
  },
  created() {
    Promise.all([])
      .then(() => {
        if (this.rowid) {
          this.ruleForm.ClassId = this.rowid
          this.getDetail(this.rowid)
        }
      })
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.ruleForm.content = filterContent.delHtmlTag(this.ruleForm.content)// 保存内容时去掉 html 标签
          this.loading = true
          // 添加
          if (!this.rowid) {
            addCommodityType(this.ruleForm).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$layer.close(this.layerid)
                this.$parent.getList() // 重新加载父级数据
                this.$message.success('恭喜你，添加成功!')
              }
            }).catch(() => {
              this.loading = false
            })
            return
          }
          // 编辑
          editCommodityType(this.ruleForm).then(response => {
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getList() // 重新加载父级数据
              this.$message.success('恭喜你，编辑成功!')
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    },
    // 详情
    getDetail(rid) {
      const data = {
        ClassId: rid
      }
      getCommodityType(data).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data[0]
          this.ruleForm.ClassName = res.text //分类名称
          this.ruleForm.ClassShortName = res.code //分类简写
          this.ruleForm.sort = res.iconcls //分类简写
        }
      })
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.order_form .form_select{
	width: 100%;
}
</style>