<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="960px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改工序控制码' : '添加工序控制码'"
    @update:visible="updateVisible"
  >
    <header-title title="基本信息"></header-title>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="编码:" prop="code">
            <el-input
              clearable
             
              v-model="form.code"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="名称:" prop="name">
            <el-input v-model="form.name" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <header-title title="业务控制"></header-title>
    <el-form ref="form1" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="检验方式:" prop="checkMethod">
            <el-select filterable v-model="form.checkMethod" placeholder="请选择">
              <el-option
                v-for="item in checkMethodList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
<!--        <el-col :span="8" v-if="form.checkMethod==2">
          <el-form-item label="互检:" prop="huj">
             <el-switch
               v-model="form.huj"
               :active-value='1'
               :inactive-value='0'
             >
             </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="form.checkMethod==2">
          <el-form-item label="专检:" prop="zj">
              <el-switch
                v-model="form.zj"
                :active-value='1'
                :inactive-value='0'
              >
              </el-switch>
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
          <el-form-item label="报工方式:" prop="submitMethod">
            <el-select filterable v-model="form.submitMethod" placeholder="请选择">
              <el-option
                v-for="item in submitMethodList"
                :key="item.code"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="加工方式:" prop="produceMethod">
            <el-select filterable v-model="form.produceMethod" placeholder="请选择">
              <el-option
                v-for="item in produceMethodList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </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>
  </ele-modal>
</template>

<script>
  import control from '@/api/technology/control';
  export default {
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
      submitMethodList: Array,
      checkMethodList: Array,
      produceMethodList: Array
    },
    data() {
      const defaultForm = {
        id: null,
        code: '',
        name: '',
        checkMethod: 2,
        submitMethod: '',
        produceMethod: ''
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },

        // 表单验证规则
        rules: {
          name: [
            { required: true, message: '请输入控制码名称', trigger: 'blur' }
          ],
          code: [{ required: true, trigger: 'change' }],
          // checkMethod: [
          //   { required: true, message: '请选择检验方式', trigger: 'blur' }
          // ],
          submitMethod: [
            { required: true, message: '请选择报工方式', trigger: 'blur' }
          ],
          // produceMethod: [
          //   { required: true, message: '请选择加工方式', trigger: 'blur' }
          // ]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          this.$refs.form1.validate((valid1) => {
            console.log(valid1, valid);
            if (!valid || !valid1) {
              return false;
            }
            if (!this.isUpdate) {
              delete this.form.id;
            }

            this.loading = true;
            const saveOrUpdate = this.isUpdate ? control.update : control.save; // 保存或更新工厂函数名称
            let title = this.isUpdate ? '修改' : '保存';
            saveOrUpdate(this.form)
              .then((msg) => {
                this.form = {};
                this.loading = false;
                this.$message.success(title + msg);
                this.updateVisible(false);
                this.$emit('done');
              })
              .catch((e) => {
                this.loading = false;
                // this.$message.error(e.message);
              });
          });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    },

    watch: {
      async visible(visible) {
        if (visible) {
          if (this.data) {
        
            // const res = await work.getById(this.data.id);
            this.$util.assignObject(this.form, this.data);
            this.isUpdate = true;
          } else {
           
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
