<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="960px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="false"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改工作中心' : '添加工作中心'"
    @update:visible="updateVisible"
    :maxable="true"
  >
    <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-col :span="8">
          <el-form-item label="所属工厂:" prop="factoryId">
            <el-select
              v-model="form.factoryId"
              filterable
              placeholder="所属工厂"
            >
              <el-option
                v-for="item in flist"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="工作中心类别:" prop="categoryType">
            <el-select
              v-model="form.categoryType"
              filterable
              placeholder="工作中心类别"
            >
              <el-option
                v-for="item in categoryTypes"
                :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="leaderUserId">
            <el-select
              v-model="form.leaderUserId"
              filterable
              placeholder="负责人"
            >
              <el-option
                v-for="item in userList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </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 { emailReg, phoneReg } from 'ele-admin';
  import work from '@/api/technology/work';

  import { addUsers, putUsers } from '@/api/system/user';

  export default {
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
      categoryTypes: Array,
      flist: Array,
      userList: Array
    },
    data() {
      const defaultForm = {
        id: null,
        code: '',
        name: '',
        factoryId: '',
        categoryType: '',
        leaderUserId: ''
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        timeType: [
          { code: 1, label: '分' },
          { code: 2, label: '时' }
        ],
        tacticsType: [
          { code: 1, label: '标准时间' },
          { code: 2, label: '最短时间' }
        ],

        // 表单验证规则
        rules: {
          name: [
            { required: true, message: '请输入工序名称', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '工作中心编码', trigger: 'change' }
          ],
          factoryId: [
            { required: true, message: '请选择所属工厂', trigger: 'blur' }
          ],
          categoryType: [
            { required: true, message: '请选择工作中心类别', trigger: 'blur' }
          ],
          leaderUserId: [
            { 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;
          }
          if (!this.isUpdate) {
            delete this.form.id;
          }
          this.loading = true;
          const saveOrUpdate = this.isUpdate ? work.update : work.save; // 保存或更新工厂函数名称
          let title = this.isUpdate ? '修改' : '保存';
          saveOrUpdate(this.form)
            .then((msg) => {
              console.log(999, 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);
            if (this.data.type == 'edit') {
              this.isUpdate = true;
            } else {
              this.isUpdate = false;
              this.form.code=''
            }
          } else {
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
