<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: zwq
 * @LastEditTime: 2023-04-20 16:33:43
 * @Description:
-->
<template>
  <div>
    <div style="margin-left:20px">
      <el-button size="small" type="primary" @click="addSample()">
        <svg class="icon-svg"><use xlink:href="#icon-新建"></use></svg>
        添加样本组
      </el-button>
      <el-button size="small" type="primary" @click="Submit()">
        提交
      </el-button>
    </div>
    <el-table
      :data="dataList"
      border
      :stripe="true"
      :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        height: '56px',
      }"
      :height="'80vh'"
      v-loading="dataListLoading"
      style="width: 100%;"
    >
      <el-table-column
        v-for="item in tableProps"
        :label="item.label"
        :key="item.prop"
        align="center"
      >
        <template slot-scope="scope">
          <span v-if="scope.$index > 6">
            <el-checkbox
              v-if="item.fType === 2"
              v-model="scope.row[item.prop]"
              :class="scope.row[item.prop] === '0' ? 'redbg' : ''"
              true-label="1.0000"
              false-label="0.0000"
            />
            <el-input
              v-else
              v-model="scope.row[item.prop]"
              :class="
                (item.usl < scope.row[item.prop] || scope.row[item.prop] < item.lsl) &&
                !(item.label === '取样时间' || item.label === '样本号' || item.label === '批次号')
                  ? 'redFont'
                  : ''
              "
              size="small"
              clearable
              :readonly="
                item.label === '取样时间' || item.label === '样本号' || item.label === '批次号'
              "
              :placeholder="item.label"
            />
          </span>
          <span v-else>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  props: {
    featuresList: {
      type: Array,
      default: () => {
        [];
      },
    },
    workingProcedureName: {
      type: String,
    },
    inspectionSheetId: {
      type: String,
    },
    numberOfGroupedSamples: {
      type: Number,
      default: 1,
    },
    batchNum: {
      type: String,
    },
  },
  data() {
    return {
      dataListLoading: false,
      dataList: [],
      tableProps: [],
      sampleNum: 1,
    };
  },
  methods: {
    init() {
      this.dataList.splice(0, this.dataList.length);
      this.dataList = [
        {
          sampleNumber: "工序",
        },
        {
          sampleNumber: "特性类型",
        },
        {
          sampleNumber: "规格下限",
        },
        {
          sampleNumber: "规格上限",
        },
        {
          sampleNumber: "抽样数量",
        },
        {
          sampleNumber: "参数规格",
        },
        {
          sampleNumber: "频率",
        },
      ];
      this.tableProps = [
        {
          prop: "sampleNumber",
          label: "样本号",
        },
        {
          prop: "sampleTime1",
          label: "取样时间",
        },
        // {
        //   prop: "batchNum",
        //   label: "批次号",
        // },
      ];
      this.featuresList.forEach((item) => {
        let obj = {
          prop: item.name,
          label: item.name,
          fType: item.type,
          lsl: item.lsl,
          usl: item.usl,
        };
        this.tableProps.push(obj);
        this.dataList[0][item.name] = this.workingProcedureName
          ? this.workingProcedureName
          : item.workingProcedureName;
        this.dataList[1][item.name] = { 1: "计量型", 2: "计数型" }[item.type];
        this.dataList[2][item.name] = item.lsl;
        this.dataList[3][item.name] = item.usl;
        this.dataList[4][item.name] = item.sampleSize;
        this.dataList[5][item.name] = item.specifications;
        this.dataList[6][item.name] = item.samplingFrequency;
      });
      this.getInfluxdb();
      this.dataListLoading = false;
      this.$nextTick(() => {});
    },
    getInfluxdb() {
      this.$http
        .post(
          `/processInspection/processData/getProcessDataList?inspectionSheetId=${this.inspectionSheetId}`
        )
        .then((res) => {
          res.data.forEach((item) => {
            item.sampleTime1 = this.formatDateTime(item.sampleTime);
            item.sampleParamList.forEach((item1) => {
              item[item1.argName] = item1.argValue;
            });
            this.dataList.push(item);
          });
        })
        .catch(() => {});
    },
    addSample() {
      let arr = [];
      for (let i = 1; i <= parseInt(this.numberOfGroupedSamples); i++) {
        let sampleNum = "";
        if (i < 10) {
          sampleNum = this.getSampleNum("0" + i);
        } else {
          sampleNum = this.getSampleNum(i);
        }
        this.featuresList.forEach((item) => {
          let obj = {
            inspectionSheetId: this.inspectionSheetId,
            eqName: this.workingProcedureName,
            sampleNumber: sampleNum,
            time: new Date(),
            batchNum: this.batchNum,
          };
          obj.eqName = this.workingProcedureName
            ? this.workingProcedureName
            : item.workingProcedureName;
          if (item.type === 1) {
            obj.argName = item.name;
            obj.argValue = '';
          } else {
            obj.argName = item.name;
            obj.argValue = 1;
          }
          arr.push(obj);
        });
        // obj.jsonData = JSON.stringify(obj.jsonData);
      }
      this.$http
        .post("/processInspection/processData/saveProcessDataList", arr)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$nextTick(() => {
            this.$emit("refreshDataForm", this.inspectionSheetId);
          });
        })
        .catch(() => {});
    },
    Submit() {
      let arr = JSON.parse(JSON.stringify(this.dataList.slice(7)));
      let arr1 = []
      arr.forEach((item) => {
        (item.workingProcedureName = this.workingProcedureName), (item.sampleParamList = []);
        
        this.featuresList.forEach((item1) => {
        let obj = JSON.parse(JSON.stringify(item))
          obj.eqName= this.workingProcedureName
              ? this.workingProcedureName
              : item1.workingProcedureName
          obj.argName = item1.name;
          obj.argValue = item[item1.name];
          console.log(obj)
          arr1.push(obj)
        });
      });
      this.$http
        .post("/processInspection/processData/saveProcessDataList", arr1)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.init();
          this.$nextTick(() => {
            this.$emit("refreshDataForm", this.inspectionSheetId);
          });
        })
        .catch(() => {});
    },
    getSampleNum(num) {
      this.sampleNum = Math.ceil(this.dataList.slice(7).length / this.numberOfGroupedSamples) + 1;
      return `00${this.sampleNum}${num}`;
    },
    formatDateTime(val) {
      let date = "";
      if (val) {
        date = new Date(val);
      } else {
        date = new Date();
      }
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? "0" + MM : MM;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      let h = date.getHours();
      h = h < 10 ? "0" + h : h;
      let m = date.getMinutes();
      m = m < 10 ? "0" + m : m;
      let s = date.getSeconds();
      s = s < 10 ? "0" + s : s;
      return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
    },
  },
};
</script>

<style scoped>
.redFont >>> .el-input__inner {
  color: red;
}
.redbg >>> .el-checkbox__inner {
  background: red;
}
</style>
