<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="ipqclineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="质检项目" width="120" align="center" prop="indexName" />
      <el-table-column label="检验要求" align="center" prop="checkMethod" />
      <el-table-column label="检验结果" width="300" align="center" prop="qualityCheckResults">
        <template slot-scope="scope">
          <div>
            <el-radio-group v-if="scope.row.inputType == 2" v-model="scope.row.qualityCheckResults">
              <el-radio v-for="(item, index) in scope.row.selectValues" :key="index" :label="item.name"></el-radio>
            </el-radio-group>
            <el-input v-else v-model="scope.row.qualityCheckResults" placeholder="请输入检验结果"></el-input>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作"  align="center" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDefect(scope.row)"
            v-hasPermi="['mes:qc:ipqc:edit']"
            v-if="optType !='view'"
          >缺陷记录</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <Defectrecord ref="defectDialog" :qcId="defect_qcid" :lineId="defect_qclineid" :qcType="defect_qctype" :optType="optType"></Defectrecord>
  </div>
</template>

<script>
import { listIpqcline, getIpqcline, delIpqcline, addIpqcline, updateIpqcline } from "@/api/mes/qc/ipqcline";
import Defectrecord from "../defectrecord/index.vue"
export default {
  name: "Ipqcline",
  dicts: ['mes_index_type'],
  props: {
    ipqcId: null,
    optType: undefined
  },
  components: {Defectrecord},
  data() {
    return {
      defect_qcid: null,
      defect_qclineid: null,
      defect_qctype: 'IPQC',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 过程检验单行表格数据
      ipqclineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ipqcId: this.ipqcId,
        indexId: null,
        indexCode: null,
        indexName: null,
        indexType: null,
        qcTool: null,
        checkMethod: null,
        standerVal: null,
        unitOfMeasure: null,
        thresholdMax: null,
        thresholdMin: null,
        crQuantity: null,
        majQuantity: null,
        minQuantity: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ipqcId: [
          { required: true, message: "检验单ID不能为空", trigger: "blur" }
        ],
        indexId: [
          { required: true, message: "检测项ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询过程检验单行列表 */
    getList() {
      this.loading = true;
      listIpqcline(this.queryParams).then(response => {
        // console.log(response, 'response88')
        this.ipqclineList = response.rows.map(item => {
          let selectValues = item.selectValues
          let qualityCheckResults = item.qualityCheckResults
          if (item.inputType == 2) {
            // selectValues = selectValues? JSON.parse(item.selectValues) : []
            if (selectValues) {
              selectValues = JSON.parse(item.selectValues)
            } else {
              selectValues = []
            }
            qualityCheckResults = qualityCheckResults? JSON.parse(item.qualityCheckResults) : []
          }
          return {
            ...item,
            selectValues,
            qualityCheckResults
          }
        })
        // console.log(this.ipqclineList, 'this.ipqclineList')
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        lineId: null,
        ipqcId: null,
        indexId: null,
        indexCode: null,
        indexName: null,
        indexType: null,
        qcTool: null,
        checkMethod: null,
        standerVal: null,
        unitOfMeasure: null,
        thresholdMax: null,
        thresholdMin: null,
        crQuantity: null,
        majQuantity: null,
        minQuantity: null,
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.lineId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //缺陷记录
    handleDefect(row){
      this.defect_qcid = row.ipqcId;
      this.defect_qclineid = row.lineId;
      this.$nextTick(() => {
        this.$refs.defectDialog.showFlag = true;
        this.$refs.defectDialog.getList();
      })
    }

  }
};
</script>
