<!-- 用户编辑弹窗 -->
<template>
  <ele-modal width="60%" :visible="visible" :append-to-body="true" :close-on-click-modal="true"
    custom-class="ele-dialog-form" :title="isUpdate ? '修改质检' : '新增质检'" @update:visible="updateVisible">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">

      <el-row>
        <el-col :span="12">
          <el-form-item label="质检类型:" prop="categoryLevelName">
            <el-input v-model="form.categoryLevelName" @click.native="openCategory" readonly />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="质检名称:" prop="name">
            <el-input clearable v-model="form.name" placeholder="请输入质检名称" />
          </el-form-item>
        </el-col>





        <el-col :span="12">
          <el-form-item label="工序:" prop="specification">
            <el-input clearable v-model="form.specification" />
          </el-form-item>
        </el-col>




        <el-col :span="12">
          <el-form-item label="产品类型:"></el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="产品类型编码:" prop="code">
            <el-input clearable v-model="form.code"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="状态:" prop="status">
            <el-switch v-model="form.status" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
              :inactive-value="0">
            </el-switch>
          </el-form-item>
        </el-col>

        
        <el-col :span="12">
          <el-form-item label="备注:" prop="remark">
            <el-input clearable v-model="form.remark"></el-input>
          </el-form-item>
        </el-col>




      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>


    <CategoryDialog ref="categoryRefs" @chooseCategory="changeCategory"></CategoryDialog>
  </ele-modal>
</template>

<script>





import { save, update } from '@/api/inspectionClassify/index';

export default {
  components: {

  },
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
    rootId: [Number, String],
  },
  data() {
    const defaultForm = {
      code: '',
      name: '',
      categoryLevelName: '',
      categoryLevelId: '',
      status: '',
      region: '',
      specification: ''
    };
    return {
      defaultForm,
      // 表单数据
      form: { ...defaultForm },
      // 表单验证规则
      rules: {
        name: [
          { required: true, message: '请输入干燥区名称', trigger: 'blur' }
        ],
        categoryLevelName: [
          { required: true, message: '请选择分类', trigger: 'change' }
        ],

        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ],


        region: [
          { required: true, message: '请输入位置', trigger: 'blur' }
        ],


      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },


  computed: {
    // 是否开启响应式布局
    styleResponsive() {
      return this.$store.state.theme.styleResponsive;
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.loading = true;
        if (!this.isUpdate) {
          delete this.form.id;
        }
        const data = {
          ...this.form
        };
        const saveOrUpdate = this.isUpdate ? update : save;
        saveOrUpdate(data)
          .then((msg) => {
            this.loading = false;
            this.$message.success(msg);
            this.updateVisible(false);
            this.$emit('done');
          })
          .catch((e) => {
            this.loading = false;

          });
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },

    openCategory() {
      this.$refs.categoryRefs.open(this.rootId, '选择分类')
    },


    changeCategory(row) {
      this.$set(this.form, 'categoryLevelName', row.name)
      this.$set(this.form, 'categoryLevelId', row.id)
    },



  },
  watch: {
    visible(visible) {
      if (visible) {
        if (this.data) {
          this.$util.assignObject(this.form, {
            ...this.data,
          });
          this.isUpdate = true;
        } else {
          this.isUpdate = false;

        }
      } else {
        this.$refs.form.clearValidate();
        this.form = { ...this.defaultForm };
      }
    }
  }
};
</script>
