<template>
  <view class="commonBody">
    <view class="button-bar">
      <!-- <view class="button-frame" @click="addQC('QCMSG')">
        <view class="shortcut-icon icon-color01">
          <image class="icon-button" src="/static/icons/png/pro.png"/>
        </view>
        <view class="grid-text">检查通知</view>
      </view> -->
      <!-- <view class="button-frame" @click="addQC('SJQR')">
        <view class="shortcut-icon icon-color01">
          <image class="icon-button" src="/static/icons/png/pro.png"/>
        </view>
        <view class="grid-text">首检确认</view>
      </view> -->
      <view class="button-frame" :class="{ active: qcType.value === curQcType }" v-for="qcType in dict.type.mes_ipqc_type" @click="handleClickBtn(qcType.value)">
        <!-- <view class="shortcut-icon icon-color01">
          <image class="icon-button" src="/static/icons/png/pro.png"/>
        </view> -->
        <view class="grid-text">{{ qcType.label }}</view>
      </view>
      <!-- <view class="button-frame" :class="{'active': !curQcType }" @click="handleClickBtn('')">
        <view class="grid-text">全部</view>
      </view>  -->
    </view>

    <view class="list-bar">
      <!-- 新增 -->
      <view class="btnbox">
        <u-button v-if="getAddShow()" type="success" size="medium" @click="handleAddClick">新增</u-button>
      </view>
      <view class="tableListClass">
        <!-- style="{ height: this.screenHeight - 280 + 'px' }" -->
        <zb-table :columns="tableColumn" :data="qcList" :stripe="true">
          <template slot="checkResult" slot-scope="scope">
            <dict-tag :options="dict.type.mes_qc_result" :value="scope.row.checkResult" />
          </template>
          <template slot="status" slot-scope="scope">
            <dict-tag :options="dict.type.mes_order_status" :value="scope.row.status" />
          </template>
          <template slot="operation" slot-scope="scope">
            <view style="padding-top: 10rpx">
              <!-- 首检按钮判断FIRST开始 -->
              <button
                class="uni-button"
                size="mini"
                type="primary"
                v-if="curQcType === 'FIRST' && scope.row.ipqcType === 'FIRST' && (scope.row.status === 'PREPARE' || scope.row.status === 'PENDING')"
                @click="qcLineClick(scope.row, true, 'firstEdit')"
              >
                查看编辑
              </button>
              <button
                class="uni-button"
                size="mini"
                type="primary"
                v-if="(curQcType === 'FIRST' && scope.row.ipqcType === 'FIRST' && scope.row.status === 'CONFIRMED') || curQcType === 'PATROL'"
                @click="qcLineClick(scope.row, false, 'firstView')"
              >
                查看
              </button>
              <!-- 首检按钮判断FIRST结束 -->
              <!-- 首检确认按钮判断SJQR开始 -->
              <button class="uni-button" size="mini" type="primary" v-if="curQcType === 'SJQR'" @click="qcLineClick(scope.row, true, 'sjqrFinish')">查看确认</button>
              <!-- 首检确认按钮判断SJQR结束 -->
            </view>
          </template>
        </zb-table>
      </view>
    </view>
		<addDialog v-if="addQuFlag" :visible.sync="addQuFlag" :currentItem="qcForm" :btnType="btnType" :curQcType="curQcType" :showIpqcConfirmButton="showIpqcConfirmButton" ref="addDialog" @confirm="submitIpqc" :qcAddIndex.sync = "qcAddIndex" :zjVisible.sync = "zjVisible" @close="closemodel" />
    <u-modal width="90%" @confirm="submitIpqc" @cancel="closemodel" v-model="qcModalFlag" :showConfirmButton="showIpqcConfirmButton" :showCancelButton="true" title="请填写检验单" content="操作内容">
      <u-form ref="qcForm" label-width="90px" class="addForm">
        <u-row>
          <u-col span="4">
            <u-form-item label="任务编号">
              <SearchInput v-model="qcForm.taskCode" placeholder="请输入任务编号" hassearchbtn="true" @serach="searchTask" />
            </u-form-item>
          </u-col>
          <u-col span="4">
            <u-form-item label="检验单名称" label-width="90px">
              <u-input v-model="qcForm.ipqcName" placeholder="请输入检验单名称"></u-input>
            </u-form-item>
          </u-col>
          <u-col span="4">
            <u-form-item label="检测结果" prop="checkResult">
              <u-radio-group v-model="qcForm.checkResult">
                <u-radio :name="item.value" v-for="item in dict.type.mes_qc_result" :key="item.value">
                  {{ item.label }}
                </u-radio>
              </u-radio-group>
            </u-form-item>
          </u-col>
        </u-row>
        <u-row>
          <u-col span="4">
            <u-form-item label="检测数量" prop="quantityCheck">
              <u-number-box disabled v-model="qcForm.quantityCheck"></u-number-box>
            </u-form-item>
          </u-col>
          <u-col span="4">
            <u-form-item label="合格数量" prop="quantityQualified">
              <u-number-box :min="0" v-model="qcForm.quantityQualified" @change="handleChangeQuantityCheck"></u-number-box>
            </u-form-item>
          </u-col>
          <u-col span="4">
            <u-form-item label="不合格数量" prop="quantityUnqualified">
              <u-number-box :min="0" v-model="qcForm.quantityUnqualified" @change="handleChangeQuantityCheck"></u-number-box>
            </u-form-item>
          </u-col>
        </u-row>
        <u-row>
          <u-col span="4">
            <u-form-item label="检测人员" prop="inspectorNickName">
              <!--                <u-input v-model="qcForm.inspector"></u-input>-->
              <!-- <zxz-uni-data-select v-model="qcForm.inspector" filterable multiple dataKey="label" dataValue="value" :localdata="qcUserList" @change="change"></zxz-uni-data-select> -->
              <u-input disabled v-model="qcForm.inspectorNickName"></u-input>
              <u-button v-if="btnType !== 'firstView'" @tap="userTempVisible = true" type="success" size="mini">选择人员</u-button>
            </u-form-item>
          </u-col>
        </u-row>
      </u-form>
      <!-- <scroll-view scroll-y="true" scroll-x="true" class="line-list"> -->
        <view class="btnbox">
          <u-button v-if="btnType === 'firstEdit'" type="success" size="medium" @click="handleAddFirstEdit">新增</u-button>
        </view>
        <view class="line-content-item">
          <uni-table ref="qcLineTable" class="line-table" border stripe :loading="loading" emptyText="未查询到数据">
            <uni-tr>
              <uni-th width="100px" align="center">质检项目</uni-th>
              <uni-th width="100px" align="center">检验要求</uni-th>
              <uni-th width="150px" align="center">检验结果</uni-th>
            </uni-tr>
            <uni-tr v-for="(line, index) in qcLines" :key="index + 'index'">
              <uni-td align="center">{{ line.indexName }}</uni-td>
              <uni-td align="center">{{ line.checkMethod }}</uni-td>
              <uni-td width="150px" align="center">
                <!-- {{ line.qualityCheckResults v-if="line.inputType == 2" }} -->
                <uni-data-checkbox v-if="line.inputType == 2" multiple v-model="line.qualityCheckResults" :localdata="line.selectValues"></uni-data-checkbox>
                <uni-easyinput v-else v-model="line.qualityCheckResults"></uni-easyinput>
              </uni-td>
            </uni-tr>
            <uni-tr v-for="(line, index) in qcAddList" :key="index + 'add'">
              <uni-td align="center">
                <view style="display:flex;">
                  <u-input disabled v-model="line.indexName" placeholder="选择质检项目"></u-input>
                  <u-button @tap="zjVisible = true, qcAddIndex = index" type="success" size="mini">选择质检项目</u-button>
                </view>
              </uni-td>
              <uni-td align="center"></uni-td>
              <uni-td width="150px" align="center">
                <uni-easyinput v-model="line.qualityCheckResults"></uni-easyinput>
              </uni-td>
            </uni-tr>
          </uni-table>
        </view>
      <!-- </scroll-view> -->
    </u-modal>
    <u-modal
      title="选择人员"
      @confirm="userDetermine()"
      show-cancel-button
      :title-style="{ 'background-color': '#1E3770', color: '#FFFFFF', 'line-height': '37px', 'padding-top': '0px' }"
      v-model="userTempVisible"
      width="1300rpx"
    >
      <view class="feedback-card">
        <user ref="userRef" v-if="userTempVisible" />
      </view>
    </u-modal>
    <u-modal
      title="选择质检项目"
      @confirm="handleSubmitZj"
      show-cancel-button
      :title-style="{ 'background-color': '#1E3770', color: '#FFFFFF', 'line-height': '37px', 'padding-top': '0px' }"
      v-model="zjVisible"
      width="1300rpx"
    >
      <view class="feedback-card">
        <zjList ref="zjListRef" v-if="zjVisible" />
      </view>
    </u-modal>

    <u-modal width="80%" v-model="qcMsgVisible" :showConfirmButton="false" :showCancelButton="true" title="检验通知" content="操作内容">
      <scroll-view scroll-y="true" scroll-x="true" class="line-list">
        <view class="line-content">
          <uni-table class="line-table" border stripe :loading="loading" emptyText="未查询到数据">
            <uni-tr>
              <uni-th width="60px" align="center">单号</uni-th>
              <uni-th width="90px" align="center">作业单元</uni-th>
              <uni-th width="100px" align="center">用户</uni-th>
              <uni-th width="90px" align="center">作业单元</uni-th>
              <uni-th width="100px" align="center">单位</uni-th>
              <uni-th width="60px" align="center">状态</uni-th>
              <uni-th width="90px" align="center">作业人</uni-th>
              <uni-th width="110px" align="center">请求检验时间</uni-th>
              <uni-th width="90px" align="center">检验人</uni-th>
            </uni-tr>
            <uni-tr v-for="(line, index) in qcLines" :key="index">
              <uni-td align="center">{{ line.indexName }}</uni-td>
              <uni-td align="center">{{ line.qcTool }}</uni-td>
              <uni-td>{{ line.checkMethod }}</uni-td>
              <uni-td align="center">{{ line.standerVal }}</uni-td>
              <uni-td align="center">{{ line.unitOfMeasure }}</uni-td>
              <uni-td align="center">{{ line.thresholdMax }}</uni-td>
              <uni-td align="center">{{ '0' }}</uni-td>
              <uni-td>
                <view class="uni-group">
                  <button class="uni-button" size="mini" type="primary">缺陷登记</button>
                </view>
              </uni-td>
            </uni-tr>
          </uni-table>
        </view>
      </scroll-view>
    </u-modal>

    <u-modal
      width="80%"
      v-model="qcFirstVisible"
      @close="getIpqcList"
      @cancel="getIpqcList"
      :showConfirmButton="false"
      :showCancelButton="true"
      title="首检确认"
      content="操作内容"
    >
      <scroll-view scroll-y="true" scroll-x="true" class="line-list">
        <view class="line-content">
          <uni-table style="min-height: 900rpx">
            <uni-tr>
              <uni-th width="160px" align="center">检验单编号</uni-th>
              <uni-th width="160px" align="center">检验单名称</uni-th>
              <uni-th width="150px" align="center">检验类型</uni-th>
              <uni-th width="150px" align="center">工单编号</uni-th>
              <uni-th width="100px" align="center">产品物料编码</uni-th>
              <uni-th width="110px" align="center">产品物料名称</uni-th>
              <uni-th width="100px" align="center">规格型号</uni-th>
              <uni-th width="100px" align="center">单位</uni-th>
              <uni-th width="100px" align="center">检测数量</uni-th>
              <uni-th width="100px" align="center">检测结果</uni-th>
              <uni-th width="120px" align="center">检测日期</uni-th>
              <uni-th width="100px" align="center">检测人员</uni-th>
              <uni-th width="150px" align="center">单据状态</uni-th>
              <uni-th width="150px" align="center">操作</uni-th>
            </uni-tr>
            <uni-tr v-for="(line, index) in qcFirstLines" :key="index">
              <uni-td align="center">{{ line.ipqcCode }}</uni-td>
              <uni-td align="center">{{ line.ipqcName }}</uni-td>
              <uni-td align="center">
                <dict-tag :options="dict.type.mes_ipqc_type" :value="line.ipqcType" />
              </uni-td>
              <uni-td>{{ line.workorderCode }}</uni-td>
              <uni-td align="center">{{ line.itemCode }}</uni-td>
              <uni-td align="center">{{ line.itemName }}</uni-td>
              <uni-td align="center">{{ line.specification }}</uni-td>
              <uni-td align="center">{{ line.quantityCheck }}</uni-td>
              <uni-td align="center">
                <dict-tag :options="dict.type.mes_qc_result" :value="line.checkResult" />
              </uni-td>
              <uni-td align="center">{{ line.unitOfMeasure }}</uni-td>

              <uni-td align="center">{{ line.inspectDate }}</uni-td>
              <uni-td align="center">{{ line.inspector }}</uni-td>
              <uni-td align="center">
                <dict-tag :options="dict.type.mes_order_status" :value="line.status" />
              </uni-td>
              <uni-td>
                <view class="uni-group">
                  <button class="uni-button" size="mini" type="primary" v-if="line.quantityCheck >= line.maxNum" @click="firstCONFIRMED(line)">确认</button>
                </view>
              </uni-td>
            </uni-tr>
          </uni-table>
        </view>
      </scroll-view>
    </u-modal>

    <u-modal width="760px" v-model="qcEditModalFlag" :showConfirmButton="false" :showCancelButton="true" title="请填写出货检验单" content="操作内容">
      <u-form ref="qcEditForm" label-width="100px" :model="qcEditForm" :rules="qcEditRules"></u-form>
    </u-modal>

    <u-modal
      width="80%"
      v-model="deffectModalFlag"
      @confirm="subQcdeffect"
      @cancel="getTemplateLineList"
      confirm-text="提交"
      :showConfirmButton="true"
      :showCancelButton="true"
      title="缺陷登记"
    >
      <u-button @click="addQcdeffect">新增</u-button>
      <uni-card v-for="(item, index) in defectrecordList" :key="index">
        <u-form label-width="100px">
          <u-row>
            <u-col>
              <u-form-item label="缺陷描述">
                <u-input v-model="item.defectName" placeholder="请输入缺陷描述"></u-input>
              </u-form-item>
            </u-col>
            <u-col>
              <u-form-item label="缺陷产品数量">
                <u-number-box v-model="item.defectQuantity"></u-number-box>
              </u-form-item>
            </u-col>
            <u-col>
              <u-form-item label="缺陷等级">
                <u-radio-group v-model="item.defectLevel">
                  <u-radio :name="v.value" v-for="v in dict.type.mes_defect_level" :key="v.value">
                    {{ v.label }}
                  </u-radio>
                </u-radio-group>
              </u-form-item>
            </u-col>
          </u-row>
        </u-form>
      </uni-card>
    </u-modal>
		
  </view>
</template>

<script>
import SearchInput from '@/components/ScanInput/index.vue';
import UButton from '../../../uview-ui/components/u-button/u-button.vue';
import UniEasyinput from '../../../uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.vue';
import User from '@/components/User/User.vue';
import zjList from './components/zjList.vue';
import addDialog from './components/addDialog.vue';

export default {
  name: 'QcContent',
  components: {
    SearchInput,
    UButton,
    UniEasyinput,
    User,
    zjList,
		addDialog
  },
  dicts: ['mes_ipqc_type', 'mes_qc_result', 'mes_order_status', 'mes_defect_level'],
  computed: {
    tableColumn() {
      if (this.curQcType === 'FIRST' || this.curQcType === 'SJQR') {
        return this.tableSjColumn;
      } else {
        return this.tableDefaultColumn;
      }
    }
  },
  data() {
    return {
      btnType:'',
      qcAddIndex: null,
      zjVisible: false,
			addQuFlag: false,
      qcAddList: [],
      tableSjColumn: [
        {
          name: 'ipqcCode',
          label: '检验单编号',
          align: 'center',
          width: 200
        },
        {
          name: 'arrangeCode',
          label: '编排单号',
          align: 'center',
          width: 200
        },
        {
          name: 'quantityCheck',
          label: '检测数量',
          align: 'center',
          width: 80
        },
        {
          name: 'checkResult',
          label: '检测结果',
          align: 'center',
          type: 'slot',
          slot: 'checkResult',
          width: 150
        },
        {
          name: 'inspectDate',
          label: '检测日期',
          align: 'center',
          width: 80
        },
        {
          name: 'inspectorNickName',
          label: '检测人员',
          align: 'center',
          width: 100
        },
        {
          name: 'status',
          label: '单据状态',
          align: 'center',
          width: 80,
          type: 'slot',
          slot: 'status'
        },
        {
          name: 'operation',
          label: '操作',
          slot: 'operation',
          type: 'slot',
          width: 80
        }
      ],
      tableDefaultColumn: [
        {
          name: 'ipqcCode',
          label: '检验单编号',
          align: 'center',
          width: 120
        },
        {
          name: 'arrangeCode',
          label: '编排单号',
          align: 'center',
          width: 130
        },
        {
          name: 'itemCode',
          label: '产品物料编码',
          align: 'center',
          width: 110
        },
        {
          name: 'itemName',
          label: '产品物料名称',
          align: 'center',
          width: 130
        },
        {
          name: 'specification',
          label: '规格型号',
          align: 'center',
          width: 100
        },
        {
          name: 'quantityCheck',
          label: '检测数量',
          align: 'center',
          width: 80
        },
        {
          name: 'checkResult',
          label: '检测结果',
          align: 'center',
          type: 'slot',
          slot: 'checkResult',
          width: 80
        },
        {
          name: 'inspectDate',
          label: '检测日期',
          align: 'center',
          width: 80
        },
        {
          name: 'inspectorNickName',
          label: '检测人员',
          align: 'center',
          width: 100
        },
        {
          name: 'status',
          label: '单据状态',
          align: 'center',
          width: 80,
          type: 'slot',
          slot: 'status'
        },
        {
          name: 'operation',
          label: '操作',
          slot: 'operation',
          type: 'slot',
          width: 80
        }
      ],
      userTempVisible: false,
      screenHeight: 768,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        pageCurrent: 1
      },
      loading: false,
      //新增弹窗
      qcModalFlag: false,
      qcForm: {},
      qcRules: [],
      qcLines: [],
      //编辑弹窗
      qcEditModalFlag: false,
      qcEditForm: {},
      qcEditRules: [],
      qcEditLines: [],
      qcMsgVisible: false,
      //所有检测单的列表
      qcList: [],
      qcUserList: [
        // {
        //   value: 0,
        //   label: 'admin'
        // },
        // {
        //   value: 1,
        //   label: '张三'
        // },
        // {
        //   value: 2,
        //   label: '李四'
        // },
        // {
        //   value: 3,
        //   label: '王五'
        // }
      ],
      taskInfo: {},
      curQcType: 'FIRST',
      defectrecordList: [],
      qcLineItem: {},
      deffectModalFlag: false,
      qcFirstVisible: false,
      qcFirstLines: [],
      showIpqcConfirmButton: true
    };
  },
  created() {
    //获取屏幕高度
    uni.getSystemInfo({
      success(res) {
        console.log('windowHeight:' + res.windowHeight);
        this.screenHeight = res.windowHeight;
      }
    });
    this.getIpqcList('FIRST');
  },
  mounted() {},
  methods: {
    async firstCONFIRMED(item) {
      const res = await this.$u.api.changeState({ ipqcId: item.ipqcId, status: 'CONFIRMED' });
      if (res.code === 200) {
        this.getIpqcList();
      }
    },
    handleAddFirstEdit() {
      this.qcAddList.push({
        indexName: '',
        qualityCheckResults: ''
      })
    },
    handleSubmitZj() {
      if (!this.$refs.zjListRef.tableSelectData[0]?.indexName) {
        this.zjVisible = true
        this.$u.toast('请选择质检项目')
        return false
      }
      // 必传 qcTemplateIndexId inputType:1 ipqcId indexId indexCode indexName indexType qcTool
      console.log(this.$refs.zjListRef.tableSelectData, 'this.$refs.zjListRef.tableSelectData');
			if(this.addQuFlag) {
				this.$refs.addDialog.handleSubmitZj({inputType:1, ipqcId: this.qcForm.ipqcId, indexId: this.$refs.zjListRef.tableSelectData[0].indexId, indexCode: this.$refs.zjListRef.tableSelectData[0].indexCode, indexName: this.$refs.zjListRef.tableSelectData[0].indexName, indexType: this.$refs.zjListRef.tableSelectData[0].indexType, qcTool: this.$refs.zjListRef.tableSelectData[0].qcTool})
			} else {
				this.$set(this.qcAddList[this.qcAddIndex], 'inputType', 1)
				this.$set(this.qcAddList[this.qcAddIndex], 'ipqcId', this.qcForm.ipqcId)
				this.$set(this.qcAddList[this.qcAddIndex], 'indexId', this.$refs.zjListRef.tableSelectData[0]?.indexId)
				this.$set(this.qcAddList[this.qcAddIndex], 'indexCode', this.$refs.zjListRef.tableSelectData[0]?.indexCode)
				this.$set(this.qcAddList[this.qcAddIndex], 'indexName', this.$refs.zjListRef.tableSelectData[0]?.indexName)
				this.$set(this.qcAddList[this.qcAddIndex], 'indexType', this.$refs.zjListRef.tableSelectData[0]?.indexType)
				this.$set(this.qcAddList[this.qcAddIndex], 'qcTool', this.$refs.zjListRef.tableSelectData[0]?.qcTool)
			}
      
      console.log(this.qcAddList)
    },
    userDetermine() {
      console.log(this.$refs.userRef.tableSelectData, 'this.$refs.userRef.tableSelectData');
      this.qcForm.inspectorNickName = this.$refs.userRef.tableSelectData[0]?.nickName;
      this.qcForm.inspector = this.$refs.userRef.tableSelectData[0]?.userName;
    },
    async getIpqcList(type) {
      let params = { ipqcType: type || this.curQcType };
      if (this.curQcType == 'SJQR') {
        params = { status: 'PENDING', ipqcType: 'FIRST' };
      }
      const res = await this.$u.api.ipqcList(params);
      if (res.code === 200) {
        this.qcList = res.rows;
      } else {
				this.qcList = []
			}
    },
    async getQcFirstLines() {
      const res = await this.$u.api.ipqcList({ status: 'PREPARE' });
      if (res.code === 200) {
        this.qcFirstLines = res.rows;
      }
    },
    async searchTask(val) {
      const res = await this.$u.api.getTaskInfoByTaskCode({
        taskCode: val
      });
      if (res.code === 200) {
        if (!res.data) {
          this.$u.toast('暂未查询到任务订单信息！');
        }
        this.taskInfo = res.data;
      }
    },

    async submitIpqc(params={}) {
			console.log('params', params)
           // if (this.btnType === 'sjqrFinish') {
        //   this.firstCONFIRMED(this.qcForm);
        //   return;
        // }
      if (!this.taskInfo && !params) {
        this.$u.toast('请输入任务单号！');
        return;
      }
      let qcIpqclineList = (params.qcLines || this.qcLines).map((item) => {
        let selectValues = item.selectValues;
        let qualityCheckResults = item.qualityCheckResults;
        if (item.inputType == 2) {
          selectValues = item.selectValues.map((item) => {
            return {
              name: item.value
            };
          });
          selectValues = JSON.stringify(selectValues);
          qualityCheckResults = JSON.stringify(item.qualityCheckResults);
        }
        return {
          ...item,
          selectValues,
          qualityCheckResults,
        };
      });
      if (this.btnType === 'firstEdit') {
        qcIpqclineList = qcIpqclineList.concat(this.qcAddList)
      } else if(params.qcAddList) {
				qcIpqclineList = qcIpqclineList.concat(params.qcAddList)
			}
      const ipqcCode = await this.$u.api.getIpqcCode();
      const param = {
        ipqcId: params.ipqcId || this.qcForm.ipqcId,
        ipqcCode: ipqcCode,
				arrangeCode: params.arrangeCode || null,
        ipqcName: params.ipqcName || this.qcForm.ipqcName,
        ipqcType: this.curQcType,
        templateId: params.templateId || this.qcForm.templateId,
        workorderId: params.workorderId || this.taskInfo.workorderId,
        workorderCode: params.workorderCode || this.taskInfo.workorderCode,
        workorderName: params.workorderName || this.taskInfo.workorderName,
        taskId: params.taskId || this.taskInfo.taskId,
        taskCode: params.taskCode || this.taskInfo.taskCode,
        taskName: params.taskName || this.taskInfo.taskName,
				workorderProcessId: params.workorderProcessId || null,
        workstationId: params.workstationId || this.taskInfo.workstationId,
        workstationCode: params.workstationCode || this.taskInfo.workstationCode,
        workstationName: params.workstationName || this.taskInfo.workstationName,
        processId: params.processId || this.taskInfo.processId,
        processCode: params.processCode || this.taskInfo.processCode,
        processName: params.processName || this.taskInfo.processName,
        itemId:  params.productId || this.taskInfo.itemId,
        itemCode: params.productCode || this.taskInfo.itemCode,
        itemName: params.productName || this.taskInfo.itemName,
        specification: params.specification || this.taskInfo.specification,
        unitOfMeasure: params.unitOfMeasure || this.taskInfo.unitOfMeasure,
        quantityCheck: params.quantityCheck || this.qcForm.quantityCheck,
        quantityUnqualified: params.quantityUnqualified || this.qcForm.quantityUnqualified,
        quantityQualified: params.quantityQualified || this.qcForm.quantityQualified,
        crRate: null,
        majRate: null,
        minRate: null,
        crQuantity: null,
        majQuantity: null,
        minQuantity: null,
        checkResult: params.checkResult || this.qcForm.checkResult,
        inspectDate: new Date(),
        inspector: params.inspector || this.qcForm.inspector,
        status: this.btnType === 'sjqrFinish'? 'CONFIRMED': 'PREPARE',
        qcIpqclineList
      };
      if (this.btnType === 'sjqrFinish') {
        delete param.ipqcType
      }
      const res = await this.$u.api.addIpqc(param, (this.btnType === 'firstEdit' || this.btnType === 'sjqrFinish') ? 'putJson' : 'postJson');
      if (res.code === 200) {
        this.getIpqcList(this.curQcType);
        this.taskInfo = {};
        this.qcForm = param;
        this.qcForm.ipqcId = res.data;
        this.getTemplateLineList();
        this.$u.toast('创建质检单成功！');
      } else {
        this.$u.toast(res.msg);
      }
			this.closemodel()
    },
    getTemplateLineList() {
      this.$u.api
        .getIpqcLineList({
          ipqcId: this.qcForm.ipqcId
        })
        .then((res) => {
          if (res.code === 200) {
            console.log(res.rows, 'rows88999655');
            this.qcLines = res.rows.map((item) => {
              let selectValues = item.selectValues;
              let qualityCheckResults = item.qualityCheckResults;
              if (item.inputType == 2) {
                // selectValues = selectValues? JSON.parse(item.selectValues) : []
                if (selectValues) {
                  const data = JSON.parse(item.selectValues);
                  selectValues = data.map((item) => {
                    return {
                      text: item.name,
                      value: item.name
                    };
                  });
                } else {
                  selectValues = [];
                }
                qualityCheckResults = qualityCheckResults ? JSON.parse(item.qualityCheckResults) : [];
              }
              return {
                ...item,
                selectValues,
                qualityCheckResults
              };
            });
            console.log(this.qcLines, 'this.qcLines');
          }
        });
    },
    handleAddClick() {
      this.qcForm = {
        quantityCheck: 0
      };
			if(this.curQcType !== 'FINAL') {
				this.addQuFlag = true
			} else {
				this.qcModalFlag = true;
			}
			this.showIpqcConfirmButton = true
      if (this.curQcType === 'FIRST') {
        this.setDefaultQcForm();
      }
    },
    setDefaultQcForm() {
      this.qcForm.checkResult = 'REJECT';
      this.qcForm.quantityCheck = 1;
      this.qcForm.quantityQualified = 1;
      this.qcForm.quantityUnqualified = 0;
    },
    handleChangeQuantityCheck() {
      const sum = this.qcForm.quantityQualified + this.qcForm.quantityUnqualified;
      this.$set(this.qcForm, 'quantityCheck', sum);
    },
    getAddShow() {
      const type = this.curQcType;
      switch (type) {
        case 'FIRST': // 首检
          return false;
        case 'FINAL': // 末检
          return true;
        case 'PATROL': // 巡检
          return true;
        case 'FQC': // 终检
          return true;
        default:
          return false;
      }
    },
    handleClickBtn(type) {
      this.qcForm = {};
      this.curQcType = type;
      this.getIpqcList(type);
      console.log(type, 'type');
      // if (type === 'QCMSG') {
      //   this.qcMsgVisible = true
      // } else if (type === 'SJQR') {
      //   this.getQcFirstLines()
      //   this.qcFirstVisible = true
      // } else {
      //   this.qcModalFlag = true;
      // }
    },
    qcLineClick(item, flag, btnType) {
      this.qcAddList = []
      this.btnType = btnType
			if(this.curQcType !== 'FINAL') {
				this.addQuFlag = true
			} else {
				this.qcModalFlag = true;
			}
      this.qcForm = Object.assign({}, item);
      console.log(item, ' itme33344');
      if (btnType === 'sjqrFinish') {
        this.qcForm.inspector = this.qcForm.inspector? this.qcForm.inspector: this.vuex_user.userName;
        this.qcForm.inspectorNickName = this.qcForm.inspectorNickName? this.qcForm.inspectorNickName: this.vuex_user.nickName;
      }
      this.showIpqcConfirmButton = flag === false ? false : true;
			if(this.curQcType === 'FINAL') {
				this.getTemplateLineList();
			}
    },
    qcLineItemClick(item) {
      this.qcLineItem = item;
      this.getDefectrecordList();
      this.deffectModalFlag = true;
    },
    getDefectrecordList(ipqcId, lineId) {
      this.$u.api
        .getDefectrecordList({
          qcType: this.qcForm.ipqcType,
          qcId: this.qcLineItem.ipqcId,
          lineId: this.qcLineItem.lineId
        })
        .then((res) => {
          if (res.code === 200) {
            this.defectrecordList = res.rows;
          }
        });
    },
		closemodel(){
			this.btnType = ''
			this.addQuFlag = false
		},
    addQcdeffect() {
      this.defectrecordList.push({
        recordId: null,
        qcId: this.qcLineItem.ipqcId,
        qcType: this.qcForm.ipqcType,
        lineId: this.qcLineItem.lineId,
        defectName: '',
        defectLevel: 'CR',
        defectQuantity: 1,
        flag: 'add',
        remark: null
      });
    },
    subQcdeffect() {
      console.log(this.defectrecordList);
      this.$u.api.addDefectrecord(this.defectrecordList).then((res) => {
        if (res.code === 200) {
          this.$u.toast('提交缺陷成功！');
          this.getDefectrecordList();
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.commonBody {
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: #fefeff;
}

.content {
  margin: 10px;
}

.list-bar {
  width: calc(100vw - 130px);
}

.button-bar {
  display: flex;
  justify-content: space-around;
  padding-top: 10px;
  padding-bottom: 10px;
}

.tableListClass {
  height: calc(100vh - 300rpx);
}

.button-frame {
  height: 80rpx;
  padding: 40rpx;
  background-color: #e5eeff;
  border-radius: 10px;
  box-shadow: 2px 2px 3px #888888;
  display: flex;
  // flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  color: #0256ff;
  // place-items: center;
  &.active {
    background-color: rgb(2, 86, 255);
    color: #fff;
  }
}

.btnbox {
  // display:flex;
  margin-bottom: 20rpx;
  padding-left: 40rpx;
  .u-btn {
    width: 150rpx;
  }
}

.icon-button {
  width: 48px;
  height: 48px;
}

.shortcut-icon {
  width: 100%;
  height: 48px;
  align-items: center;
  border-radius: 10px;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  color: #ffffff;
  font-size: 26px;
}

.shortcut-icon i {
  font-size: 26px;
}

.line-list {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  height: 300px;
}

.line-content {
  width: 200%;
  height: 300px;
  display: flex;
  flex-wrap: nowrap;
}

.line-content-item {
  width: 100%;
	padding: 0 10px;
  height: 300px;
  display: flex;
  flex-wrap: nowrap;
}

.line-table {
  display: inline-block;
}

.uni-group {
  display: flex;
  justify-content: center;
}

.ellipis_text {
  overflow: hidden;
  word-break: break-all;
  /* break-all(允许在单词内换行。) */
  text-overflow: ellipsis;
  /* 超出部分省略号 */
  display: -webkit-box;
  /** 对象作为伸缩盒子模型显示 **/
  -webkit-box-orient: vertical;
  /** 设置或检索伸缩盒对象的子元素的排列方式 **/
  -webkit-line-clamp: 4;
  /** 显示的行数 **/
}

.submit-qc {
  width: 30%;
  margin: 1rpx auto;
  display: block;
}
.addForm {
	padding: 0 20px;
}
</style>
