<!-- 字典项编辑弹窗 -->
<template>
  <ele-modal
    width="500px"
    :visible="visible"
    :close-on-click-modal="true"
    :title="isUpdate ? '编辑分类信息' : '新增分类信息'"
    @update:visible="updateVisible"
  >
    <el-form :model="formData" ref="formRef" :rules="rules" label-width="96px">
      <el-form-item label="父级节点:" prop="code">
        <el-input
          clearable
          :maxlength="20"
          v-model="formData.code"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="层级全览:" prop="name">
        <el-input
          clearable
          :maxlength="20"
          v-model="formData.name"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="节点编码:" prop="type">
        <el-select
          clearable
          :maxlength="20"
          v-model="formData.type"
          placeholder="请选择"
        >
          <el-option
            v-for="item in typeList"
            :key="item.code"
            :label="item.label"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="节点名称:" prop="remark">
        <el-input
          clearable
          type="textarea"
          :maxlength="20"
          v-model="formData.remark"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="与下层分隔:" prop="remark">
        <el-input
          clearable
          type="textarea"
          :maxlength="20"
          v-model="formData.remark"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="本层排序:" prop="remark">
        <el-input
          clearable
          type="textarea"
          :maxlength="20"
          v-model="formData.remark"
          placeholder="请输入"
        />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
      <el-button @click="updateVisible(false)">返回 </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import { saveOrUpdate } from '@/api/classifyManage';

  export default {
    props: {},
    data() {
      const defaultForm = {
        name: '',
        code: '',
        type: '',
        remark: '',
        parentId: 0
      };
      return {
        visible: false,
        defaultForm,
        // 表单数据
        formData: { ...defaultForm },
        typeList: [{ label: '产品分类', code: 3 }],
        // 表单验证规则
        rules: {
          name: [
            {
              required: true,
              message: '请输入类型名称',
              trigger: 'blur'
            }
          ],
          code: [
            {
              required: true,
              message: '请输入类型编码',
              trigger: 'blur'
            }
          ],
          type: [
            {
              required: true,
              message: '请选择类型用途',
              trigger: 'change'
            }
          ]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        parentData: {}
      };
    },
    methods: {
      open(parentData, data) {
        this.parentData = parentData;
        this.data = data;
        this.visible = true;
      },
      /* 保存编辑 */
      save() {
        this.$refs.formRef.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          saveOrUpdate({
            ...this.formData
          })
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              // this.$message.error(e.message);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.visible = value;
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            this.$util.assignObject(this.formData, this.data);
            this.isUpdate = true;
          } else {
            this.isUpdate = false;
          }
        } else {
          this.$refs.formRef.clearValidate();
          this.formData = { ...this.defaultForm };
        }
      }
    }
  };
</script>
