<!-- 用户编辑弹窗 -->
<template>
  <el-dialog
    class="ele-dialog-form"
    :title="title"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    width="1200px"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="8" v-if="title != '新增'">
          <el-form-item label="编码:">
            <el-input :maxlength="20" v-model="form.code" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="title != '新增'">
          <el-form-item label="工单号:">
            <el-input :maxlength="20" v-model="form.workOrderCode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="类型:" prop="qualityType">
            <DictSelection
              dictName="质检计划类型"
              v-if="title != '详情'"
              v-model="form.qualityType"
            ></DictSelection>
            <el-input
              :value="getDictValue('质检计划类型', form.qualityType)"
              disabled
              v-if="title == '详情'"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="取样类型:" prop="qualityMode">
            <DictSelection
              dictName="取样类型"
              v-model="form.qualityMode"
              clearable
              v-if="title != '详情'"
            ></DictSelection>
            <el-input
              :value="getDictValue('取样类型', form.qualityMode)"
              disabled
              v-if="title == '详情'"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="工艺路线:">
            <el-input
              @click.native="openVersion"
              clearable
              v-model="form.produceRoutingName"
              v-if="title != '详情'"
            />
            <el-input
              v-model="form.produceRoutingName"
              disabled
              v-if="title == '详情'"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="工序:" v-if="!form.qualityPlanId">
            <el-select
              style="width: 100%"
              v-model="form.produceTaskId"
              placeholder="请选择"
              clearable
              @change="produceTaskChange"
              v-if="title != '详情'"
            >
              <el-option
                v-for="item in produceTaskList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
            <el-input
              v-model="form.produceTaskName"
              disabled
              v-if="title == '详情'"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="产品名称:" prop="productName">
            <el-input
              v-model="form.productName"
              @click.native="addProduct"
              readonly
              v-if="title == '新增'"
            />
            <el-input
              v-model="form.productName"
              disabled
              v-if="title != '新增'"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="总数量:">
            <el-input v-model="form.total" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="合格数:" prop="qualifiedNumber">
            <el-input
              v-model="form.qualifiedNumber"
              @input="inputValue"
              :disabled="title == '详情'"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="不合格数:" prop="noQualifiedNumber">
            <el-input
              v-model="form.noQualifiedNumber"
              @input="inputValue"
              :disabled="title == '详情'"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="工时(h):" prop="hours">
            <el-input
              v-model="form.hours"
              :disabled="title == '详情'"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="质检时间:" prop="qualityTime">
            <el-date-picker
              class="w100"
              v-model="form.qualityTime"
              type="date"
              value-format="yyyy-MM-dd"
              :disabled="title == '详情'"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="质检部门" prop="groupId">
            <deptSelect
              :disabled="title == '详情'"
              v-model="form.groupId"
              @changeGroup="searchDeptNodeClick"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="质检人" prop="qualityId">
            <el-select
              :disabled="title == '详情'"
              v-model="form.qualityId"
              @change="changeExecutor"
              size="small"
              style="width: 100%"
              filterable
            >
              <el-option
                v-for="item in executorList"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="附件:" prop="accessory">
            <fileUpload
              v-model="form.accessory"
             
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="title" v-if="title != '新增'">质检内容</div>

    <el-tabs
      v-model="tabTaskId"
      class="tab-box"
      type="border-card"
    >
      <el-tab-pane
        v-for="(item, index) in form.categoryParamList"
        :label="item.name"
        :name="item.id"
        :key="index"
      >
        <term
          :qualityParam="item.qualityParam"
          :isDetails="title == '详情'"
          ref="qualityParamRef"
        ></term>
      </el-tab-pane>
    </el-tabs>

    <div class="title" v-if="title != '新增'">不良品列表</div>
    <el-table :data="form.voList" border v-if="title != '新增'" height="25vh">
      <el-table-column label="序号" align="center" width="60">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="不良品编码" align="center" prop="code">
      </el-table-column>
      <el-table-column label="数量" align="center" prop="unqualifiedQuantity">
      </el-table-column>
      <el-table-column label="原因" align="center" prop="unqualifiedReason">
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="reviewerName">
      </el-table-column>
      <el-table-column label="处置类型" align="center" prop="type">
        <template slot-scope="scope">
          {{ getDictValue('不良品处理类型', scope.row.type + '') }}
        </template>
      </el-table-column>
    </el-table>

    <template v-slot:footer>
      <el-button @click="handleClose">返回</el-button>
      <el-button
        type="primary"
        :loading="loading"
        @click="update"
        v-if="title == '报工'"
      >
        报工
      </el-button>
      <el-button
        type="primary"
        :loading="loading"
        @click="save"
        v-if="title == '新增'"
      >
        保存
      </el-button>
    </template>
    <ProductionVersion ref="versionRefs" @changeProduct="changeProduct" />
    <EquipmentDialog ref="equipmentRefs" @choose="choose" />
  </el-dialog>
</template>

<script>
  import { save, update, getById } from '@/api/inspectionWork';
  import { getProduceTaskList } from '@/api/aps';
  import EquipmentDialog from './EquipmentDialog.vue';
  import ProductionVersion from './ProductionVersion.vue';
  import dictMixins from '@/mixins/dictMixins';
  import deptSelect from '@/components/CommomSelect/dept-select.vue';
  import { getUserPage } from '@/api/system/organization';
  // import fileUpload from '@/components/upload/fileUpload';
  import fileUpload from '@/components/addDoc/index.vue';

  import term from './term';

  export default {
    components: {
      ProductionVersion,
      EquipmentDialog,
      deptSelect,
      fileUpload,
      term
    },
    mixins: [dictMixins],

    data() {
      const defaultForm = function () {
        return {
          produceRoutingName: '',
          produceRoutingId: '',
          produceTaskId: '',
          produceTaskName: '',
          code: '',
          workOrderCode: '',
          batchNo: '',
          productName: '',
          productCode: '',
          specification: '',
          brandNo: '',
          qualityType: '',
          total: '',
          qualifiedNumber: '',
          noQualifiedNumber: '',
          hours: '',
          qualityTime: '',
          qualityName: '',
          qualityId: '',
          voList: [],
          groupId: '',
          groupName: '',
          id: '',
          accessory: [],
          qualityMode: ''
        };
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm() },

        // 表单验证规则
        rules: {
          productName: [
            { required: true, message: '请输入', trigger: 'change' }
          ],
          qualifiedNumber: [
            { required: true, message: '请输入', trigger: 'blur' }
          ],
          noQualifiedNumber: [
          { required: true, message: '请输入', trigger: 'blur' }
          ],
          qualityTime: [{ required: true, message: '请输入', trigger: 'blur' }],
          groupId: [{ required: true, message: '请输入', trigger: 'change' }],
          qualityId: [{ required: true, message: '请输入', trigger: 'change' }]
        },
        visible: false,
        type: null,
        title: null,
        loading: false,
        produceTaskList: [],
        executorList: '',

        tabTaskId: null
      };
    },

    created() {},
    methods: {
      open(type, row) {
        this.title = type == 'add' ? '新增' : type == 'edit' ? '报工' : '详情';
        this.type = type;
        if (this.type == 'detail') {
          row.accessory = row.accessory || [];

          this.form = JSON.parse(JSON.stringify(row));
          this.tabTaskId =
            this.form.categoryParamList[0] && this.form.categoryParamList[0].id;
          if (this.form.groupId) {
            this.getUserList({ groupId: this.form.groupId });
          }
          this.getProduceTaskList();
        } else if (type == 'edit') {
          getById(row.id).then((res) => {
            row.accessory = row.accessory || [];

            this.form = res.data;
            this.tabTaskId =
              this.form.categoryParamList[0] &&
              this.form.categoryParamList[0].id;
            if (this.form.groupId) {
              this.getUserList({ groupId: this.form.groupId });
            }
            this.getProduceTaskList();
          });
        }
        this.visible = true;
      },
      inputValue() {
        if(!this.form.qualityPlanId) {
          this.form.total =
          Number(this.form.qualifiedNumber) +
          Number(this.form.noQualifiedNumber);
        }
  
      },
      addProduct() {
        this.$refs.equipmentRefs.open();
      },
      openVersion() {
        this.$refs.versionRefs.open();
      },
      //工艺路线回调
      changeProduct(data) {
        this.form.produceRoutingId = data.id;
        this.form.produceRoutingName = data.name;
        this.form.produceTaskId = '';
        this.form.produceTaskName = '';
        this.getProduceTaskList();
      },
      async getProduceTaskList() {
        if (!this.form.produceRoutingId) {
          return;
        }
        const res = await getProduceTaskList({
          isDetail: true,
          pageNum: 1,
          routingId: this.form.produceRoutingId,
          size: -1
        });
        this.produceTaskList = res.list;
      },
      produceTaskChange() {
        if (!this.form.produceTaskId) {
          return;
        }
        this.form.produceTaskName = this.produceTaskList.find(
          (item) => item.id == this.form.produceTaskId
        ).name;
      },
      choose(data) {
        this.form.productName = data[0].name;
        this.form.productCode = data[0].code;
        this.form.specification = data[0].specification;
        this.form.brandNo = data[0].brandNum;
      },
      //选择部门(搜索)
      searchDeptNodeClick(info, row) {
        if (info) {
          const params = { groupId: info };
          this.getUserList(params);
          this.form.groupName = row.name;
        } else {
          this.form.executeGroupId = null;
        }
      },
      // 获取审核人列表、巡点检人员
      async getUserList(params) {
        try {
          let data = { pageNum: 1, size: -1 };
          // 如果传了参数就是获取巡点检人员数据
          if (params) {
            data = Object.assign(data, params);
          }
          const res = await getUserPage(data);
          this.executorList = res.list;
        } catch (error) {}
      },
      // 执行人选择
      changeExecutor(val) {
        if (val) {
          this.form.qualityId = val;
          this.form.qualityName = this.executorList.filter(
            (item) => item.id === val
          )[0].name;
        }
      },
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          save(this.form)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.handleClose();
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
            });
        });
      },
      update() {
        this.loading = true;
        this.form['status'] = 1;
        update({ ...this.form })
          .then((msg) => {
            this.loading = false;
            this.$message.success(msg);
            this.handleClose();
            this.$emit('done');
          })
          .catch((e) => {
            this.loading = false;
          });
      },
      restForm() {
        this.form = { ...this.defaultForm() };
        this.$nextTick(() => {
          this.$refs.form.clearValidate();
        });
      },
      handleClose() {
        this.restForm();
        this.visible = false;
      }
    }
  };
</script>

<style lang="scss" scoped>
  .title {
    font-size: 16px;
    line-height: 45px;
  }
  .location-warp {
    display: flex;

    .detail {
      margin-left: 10px;
    }
  }

  :deep(
      .el-dialog:not(.ele-dialog-form)
        .el-dialog__body
        .el-form
        .el-form-item:last-child
    ) {
    margin-bottom: 22px;
  }
  :deep(
      .el-dialog:not(.ele-dialog-form)
        .el-dialog__body
        .el-form
        .el-table__body
        .el-table__row
        .el-form-item:last-child
    ) {
    margin-bottom: 0 !important;
  }
  .add-product {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    font-size: 30px;
    color: #1890ff;
    margin: 10px 0;
    cursor: pointer;
  }
</style>
