<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-06-20 15:27:51
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <div>
    <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item prop="factoryId" :label="$t('code.factoryName')">
            <el-select v-model="dataForm.factoryId" :placeholder="$t('code.factoryName')">
              <el-option v-for="item in factoryList" :key="item.id" :label="item.factoryName" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="inspectionTime" :label="$t('code.inspectionTime')">
            <el-date-picker v-model="dataForm.inspectionTime" type="datetime" :placeholder="$t('code.inspectionTime')"
              format='yyyy-MM-dd HH:mm:ss' valueFormat='yyyy-MM-ddTHH:mm:ss'>
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="orderNumber" :label="$t('code.orderNumber')">
            <el-input v-model="dataForm.orderNumber" :placeholder="$t('code.orderNumber')"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item prop="batchNo" :label="$t('code.batchNo')">
            <el-input v-model="dataForm.batchNo" :placeholder="$t('code.batchNo')"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="inspectionType" :label="$t('code.inspectionType')">
            <el-select v-model="dataForm.inspectionType" :placeholder="$t('code.inspectionType')">
              <el-option v-for="item in inspectionTypeList" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="recheck" :label="$t('code.recheck')">
            <el-switch v-model="dataForm.recheck" :active-value="1" :inactive-value="0">
            </el-switch>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item prop="productId" :label="$t('code.productName')">
            <el-select v-model="dataForm.productId" :placeholder="$t('code.productName')">
              <el-option v-for="item in productList" :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 prop="supplierId" :label="$t('code.supplierName')">
            <el-select v-model="dataForm.supplierId" :placeholder="$t('code.supplierName')">
              <el-option v-for="item in supplierList" :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 prop="batchQuantity" :label="$t('code.batchQuantity')">
            <el-input v-model="dataForm.batchQuantity" :placeholder="$t('code.batchQuantity')"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item prop="inspectionQuantity" :label="$t('code.inspectionQuantity')">
            <el-input v-model="dataForm.inspectionQuantity" :placeholder="$t('code.inspectionQuantity')"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="batchSize" :label="$t('code.batchSize')">
            <el-input v-model="dataForm.batchSize" :placeholder="$t('code.batchSize')"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="samplesQuantity" :label="$t('code.samplesQuantity')">
            <el-input v-model="dataForm.samplesQuantity" :placeholder="$t('code.samplesQuantity')"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item prop="companyId" :label="$t('code.companyName')">
            <el-select v-model="dataForm.companyId" :placeholder="$t('code.companyName')">
              <el-option v-for="item in unitList" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="userId" :label="$t('code.username')">
            <el-select v-model="dataForm.userId" :placeholder="$t('code.username')">
              <el-option v-for="item in unitList" :key="item.id" :label="item.realName" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="inspectionStatus" :label="$t('code.inspectionStatus')">
            <el-select v-model="dataForm.inspectionStatus" :placeholder="$t('code.inspectionStatus')">
              <el-option v-for="item in inspectionStatusList" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item prop="customSamplingId" :label="$t('code.customSampling')">
            <el-select v-model="dataForm.customSamplingId" :placeholder="$t('code.customSampling')">
              <el-option v-for="item in samplingPlanList" :key="item.id" :label="item.sampleRange" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-divider content-position="center">GBT2828.1</el-divider>
    <el-form :model="GBTDataForm" ref="GBTDataForm" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="4">
          <el-form-item prop="critical" label="CR" label-width="50px">
            <el-input v-model="GBTDataForm.critical" placeholder="CR"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="criticalAccept" label="Accept">
            <el-input v-model="GBTDataForm.criticalAccept" placeholder="Accept"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="criticalReject" label="Reject" label-width="60px">
            <el-input v-model="GBTDataForm.criticalReject" placeholder="Reject"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="criticalNumber" :label="$t('code.defectsNumber')">
            <el-input v-model="GBTDataForm.criticalNumber" :placeholder="$t('code.defectsNumber')"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="criticalDefectiveNumber" :label="$t('code.defectiveQuantity')">
            <el-input v-model="GBTDataForm.criticalDefectiveNumber" :placeholder="$t('code.defectiveQuantity')">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="4">
          <el-form-item prop="major" label="MA" label-width="50px">
            <el-input v-model="GBTDataForm.major" placeholder="CR"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="majorAccept" label="Accept">
            <el-input v-model="GBTDataForm.majorAccept" placeholder="Accept"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="majorReject" label="Reject" label-width="60px">
            <el-input v-model="GBTDataForm.majorReject" placeholder="Reject"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="majorNumber" :label="$t('code.defectsNumber')">
            <el-input v-model="GBTDataForm.majorNumber" :placeholder="$t('code.defectsNumber')"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="majorDefectiveNumber" :label="$t('code.defectiveQuantity')">
            <el-input v-model="GBTDataForm.majorDefectiveNumber" :placeholder="$t('code.defectiveQuantity')">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="4">
          <el-form-item prop="minor" label="MI" label-width="50px">
            <el-input v-model="GBTDataForm.minor" placeholder="CR"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="minorAccept" label="Accept">
            <el-input v-model="GBTDataForm.minorAccept" placeholder="Accept"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="minorReject" label="Reject" label-width="60px">
            <el-input v-model="GBTDataForm.minorReject" placeholder="Reject"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="minorNumber" :label="$t('code.defectsNumber')">
            <el-input v-model="GBTDataForm.minorNumber" :placeholder="$t('code.defectsNumber')"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="minorDefectiveNumber" :label="$t('code.defectiveQuantity')">
            <el-input v-model="GBTDataForm.minorDefectiveNumber" :placeholder="$t('code.defectiveQuantity')">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="4">
          <el-form-item prop="total" label="total" label-width="50px">
            <el-input v-model="GBTDataForm.total" placeholder="CR"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="totalAccept" label="Accept">
            <el-input v-model="GBTDataForm.totalAccept" placeholder="Accept"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="totalReject" label="Reject" label-width="60px">
            <el-input v-model="GBTDataForm.totalReject" placeholder="Reject"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="totalNumber" :label="$t('code.defectsNumber')">
            <el-input v-model="GBTDataForm.totalNumber" :placeholder="$t('code.defectsNumber')"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item prop="totalDefectiveNumber" :label="$t('code.defectiveQuantity')">
            <el-input v-model="GBTDataForm.totalDefectiveNumber" :placeholder="$t('code.defectiveQuantity')">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import debounce from "lodash/debounce";
import basicAdd from "@/mixins/basic-add";
export default {
  mixins: [basicAdd],
  data() {
    return {
      urlOptions: {
        GBTSubmitURL: "/code/shipmentRegistration",
        submitURL: "/code/shipmentRegistration",  
        infoURL: "/code/shipmentRegistration/{id}",
        getFactoryURL: '/code/factory/page',
        getProductURL: '/basic/product/page',
        getSupplierURL: '/supplier/qmsSupplier/page',
        getDictDataList: '/sys/dict/data/page',
        getUserURL: '/sys/user/page',
        getSamplingPlanURL: '/quality/qmsSamplingPlan/page'
      },
      supplierList: [],
      userList: [],
      samplingPlanList:[],
      inspectionStatusList: [
        {
          id: 0,
          name: '未登记'
        },
        {
          id: 1,
          name: '已登记'
        }
      ],
      unitList:[],
      productList: [],
      factoryList:[],
      visible: false,
      listQuery: {
        limit: 999,
        page:1
      },
      inspectionTypeList: [{
        value: 0,
        label: '监控'
      },
      {
        value: 1,
        label: '电芯来料检验'
      },
      {
        value: 2,
        label: 'IQC抽检'
      },
      {
        value: 3,
        label: 'IQC抽检2'
      },
      {
        value: 4,
        label: '原料抽检'
      },
      {
        value: 5,
        label: '进货外观检验'
      },
      {
        value: 6,
        label: '库内原料检验'
      },
      {
        value: 7,
        label: '来料检验'
      },
      {
        value: 8,
        label: '胶片'
      },
      {
        value: 9,
        label: '抽检'
      },
      {
        value: 10,
        label: '巡检'
      }, {
        value: 11,
        label: '首检'
      },
      {
        value: 12,
        label: '末检'
      },
      {
        value: 13,
        label: '实时监测'
      },
      {
        value: 14,
        label: 'FQC抽检'
      },
      {
        value: 15,
        label: 'OQC抽检'
      },
      ],
      GBTDataForm: {
        critical:null,
        criticalAccept: null,
        criticalDefectiveNumber: null,
        criticalNumber: null,
        criticalReject: null,
        id: null,
        major: null,
        majorAccept: null,
        majorDefectiveNumber: null,
        majorNumber: null,
        majorReject: null,
        minor: null,
        minorAccept: null,
        minorDefectiveNumber: null,
        minorNumber: null,
        minorReject: null,
        purchaseRegistrationId: null,
        total: null,
        totalAccept: null,
        totalDefectiveNumber: null,
        totalNumber: null,
        totalReject: null,
      },
      dataForm: {
        batchNo: null,
        batchQuantity: null,
        batchSize: null,
        checkoutTime: null,
        companyId: null,
        customSamplingId: null,
        defectiveQuantity: null,
        defectsNumber: null,
        distributionUserId: null,
        factoryId: null,
        id: null,
        inspectionQuantity: null,
        inspectionTime: null,
        inspectionType: null,
        orderNumber: null,
        parameterName: null,
        processingMethod: null,
        processingResults: null,
        productId: null,
        recordQuantity: null,
        remark: null,
        shiftId: null,
        submit: null,
        supplierId: null,
        teamId: null,
        upload: null,
        userId: null,
      },
    };
  },
  computed: {
    dataRule() {
      return {
        dictLabel: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        dictValue: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        sort: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
      };
    },
  },
  created() {
    this.getDict()
  },
  methods: {
    init(id, ) {
      this.dataForm.id = id || ""
      // this.dataForm.dictTypeId = dictTypeId || "";
      this.visible = true
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/code/shipmentRegistration/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
        })
        .catch(() => { })
      this.$http
        .get(`/code/purchaseRegistrationNationalStandard/getById/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.GBTDataForm = {
            ...this.GBTDataForm,
            ...res.data,
          };
        })
        .catch(() => { })
    },
    getDict() {
      this.$http
        .post(this.urlOptions.getSamplingPlanURL, {
          params: this.listQuery
        })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res);
            this.samplingPlanList = res.data.list
          }
        })
        .catch(() => {
        })
      this.$http
        .post(this.urlOptions.getUserURL, {
          params: this.listQuery
        })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res);
            this.userList = res.data.list
          }
        })
        .catch(() => {
        })
      this.$http
        .post(this.urlOptions.getProductURL, {
          params: this.listQuery
        })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res);
            this.productList = res.data.list
          }
        })
        .catch(() => {
        })
      this.$http
        .post(this.urlOptions.getDictDataList, {
          params: {
            limit: 999,
            page: 1,
            dictTypeId: '1665966021935190018'
          }
        })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res);
            this.unitList = res.data.list
          }
        })
        .catch(() => {
        })
      this.$http
        .post(this.urlOptions.getSupplierURL, {
          params: this.listQuery
        })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res);
            this.supplierList = res.data.list
          }
        })
        .catch(() => {
        })
      this.$http
        .post(this.urlOptions.getFactoryURL, {
          params: this.listQuery
        })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res);
            this.factoryList = res.data.list
          }
        })
        .catch(() => {
        })
      // console.log(111111);
    },
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (!valid) {
          return false;
        }
        if (this.dataForm.id) {
            this.$http.put(this.urlOptions.submitURL, this.dataForm)
              .then(({ data: res }) => {
                if (res.code !== 0) {
                  return this.$message.error(res.msg);
                }
                this.GBTDataForm.purchaseRegistrationId = res.data
                this.$http.post(this.urlOptions.GBTSubmitURL, this.parameterData)
                  .then(({ data: res }) => {
                    if (res.code !== 0) {
                      return this.$message.error(res.msg);
                    }
                    this.$message({
                      message: this.$t("prompt.success"),
                      type: "success",
                      duration: 500,
                      onClose: () => {
                        this.visible = false;
                        this.$emit("refreshDataList")
                      }
                    })
                  })

                this.$message({
                  message: this.$t("prompt.success"),
                  type: "success",
                  duration: 500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList")
                  }
                })
              })
              .catch(() => { });
        } else {
          this.$http.post(this.urlOptions.submitURL, this.dataForm)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.GBTDataForm.purchaseRegistrationId = res.data
              this.$http.post(this.urlOptions.GBTSubmitURL, this.parameterData)
                .then(({ data: res }) => {
                  if (res.code !== 0) {
                    return this.$message.error(res.msg);
                  }
                  this.$message({
                    message: this.$t("prompt.success"),
                    type: "success",
                    duration: 500,
                    onClose: () => {
                      this.visible = false;
                      this.$emit("refreshDataList")
                    }
                  })
                })

              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList")
                }
              })
            })
            .catch(() => { });
        }
      });
    },
    // 表单提交
  },
};
</script>
