<template>
  <div>
    <div class="title_box rx-bc mt6">
      <div class="name">{{ title }}({{ list.length || 0 }})个 </div>

      <!-- isEngrave: {
        type: Boolean,
        default: false
      },
      isDisposal: {
        type: Boolean,
        default: false
      } -->

      <div class="title_box rx-bc mt6">
        <div
          v-if="!isDetails && isEngrave"
          class="rx-bc"
          style="margin-left: 20px"
        >
          <el-button type="text" size="mini" @click="batchEngrave()"
            >批量填写刻码</el-button
          >
        </div>

        <div
          v-if="!isDetails && isDisposal"
          class="rx-bc"
          style="margin-left: 20px"
        >
          <el-button type="text" size="mini" @click="batchDisposal()"
            >批量处置</el-button
          >
        </div>

        <div
          class="rx-bc"
          v-if="!isDetails && isChoose"
          style="margin-left: 20px"
          ><el-button type="text" size="mini" @click="batchDelete()"
            >批量删除</el-button
          >
        </div>
      </div>
    </div>

    <ele-pro-table
      ref="table"
      :needPage="false"
      :columns="columns"
      :datasource="list"
      class="time-form"
      max-height="600"
      @columns-change="handleColumnChange"
      :cache-key="cacheKeyUrl"
      :selection.sync="selection"
      row-key="id"
    >
      <template v-slot:engrave="{ row, $index }">
        <span v-if="isDetails">{{ row.extInfo.engrave }}</span>
        <el-input
          v-else
          size="mini"
          class="content_num"
          v-model="row.extInfo.engrave"
          placeholder="刻码"
        />
      </template>
      <template v-slot:sendCode="{ row, $index }">
        <el-input
          size="mini"
          class="content_num"
          v-model="row.extInfo.sendCode"
          placeholder="发货条码"
          :disabled="isDetails"
        />
      </template>
      <template v-slot:batchNo="{ row, $index }">
        <el-input
          size="mini"
          class="content_num"
          v-model="row.batchNo"
          placeholder="批次号"
          :disabled="isDetails"
        />
      </template>

      <template v-slot:materielCode="{ row, $index }">
        <span v-if="isDetails">{{ row.extInfo.materielCode }}</span>
        <el-input
          v-else
          size="mini"
          class="content_num"
          v-model="row.extInfo.materielCode"
          placeholder="物料代号"
        />
      </template>
      <template v-slot:clientCode="{ row, $index }">
        <span v-if="isDetails"> {{ row.extInfo.clientCode }}</span>
        <el-input
          v-else
          size="mini"
          class="content_num"
          v-model="row.extInfo.clientCode"
          placeholder="请输入客户代号"
        />
      </template>
      <template v-slot:deviceId="{ row, $index }">
        <el-select
          v-if="deviceList?.length > 0"
          class="content_num"
          v-model="row.deviceId"
          filterable
          placeholder="请选择"
          @change="(e) => selectVal(e, row, $index)"
          size="mini"
          :disabled="isDetails"
        >
          <el-option
            v-for="item in deviceList"
            :label="item.name + '-' + item.codeNumber"
            :value="item.id || item.instanceId"
            :key="item.id || item.instanceId"
          >
          </el-option>
        </el-select>
      </template>
      <template v-slot:heatNumber="{ row, $index }">
        <el-input
          size="mini"
          class="content_num"
          v-model="row.extInfo.heatNumber"
          placeholder="请输入炉次号"
          :disabled="isDetails"
        />
      </template>
      <template v-slot:feedQuantity="{ row, $index }">
        <el-input
          size="mini"
          class="content_num"
          v-model="row.feedQuantity"
          disabled
          placeholder="数量"
        />
      </template>
      <template v-slot:position="{ row, $index }">
        <span v-if="isDetails">{{ row.extInfo.position }}</span>
        <el-input
          v-else
          size="mini"
          class="content_num"
          v-model="row.extInfo.position"
          placeholder="位置"
        />
      </template>
      <template v-slot:weight="{ row, $index }">
        <div style="display: flex; align-items: center">
          <div class="tag_box">{{ row.extInfo.newWeight ? '上' : '原' }}</div>
          <span
            v-if="
              Object.prototype.hasOwnProperty.call(row.extInfo, 'newWeight')
            "
            >{{ row.extInfo.newWeight }}</span
          >

          <el-input
            v-if="
              !Object.prototype.hasOwnProperty.call(row.extInfo, 'newWeight')
            "
            size="mini"
            class="content_num2"
            style="width: 100px"
            v-model="row.extInfo.weight"
            :disabled="isDetails"
            prop="digit"
          />
          <div style="width: 55px">{{ row.weightUnit }}</div>
        </div>
      </template>
      <template v-slot:reportWeight="{ row, $index }">
        <div style="display: flex; align-items: center">
          <span v-if="isDetails">{{ row.extInfo.reportWeight }}</span>
          <el-input
            v-else
            size="mini"
            class="content_num"
            type="number"
            v-model="row.extInfo.reportWeight"
            placeholder="报工重量"
            prop="digit"
            @input="handleInput"
          />
          <div style="width: 55px">{{ row.weightUnit }}</div>
        </div>
      </template>
      <template v-slot:isQualified="{ row, $index }">
        <el-select
          size="mini"
          class="content_num"
          v-model="row.extInfo.isQualified"
          @change="handlerSelect"
          filterable
          placeholder="请选择"
          :disabled="
            isDetails ||
            taskObj.type == 2 ||
            taskObj.type == 3 ||
            taskObj.type == 6
          "
        >
          <el-option
            v-for="item in isQualifiedList"
            :label="item.name"
            :value="item.code"
            :key="item.code"
          >
          </el-option>
        </el-select>
      </template>
      <!-- :disabled="isDetails || isDisable" -->
      <template v-slot:notType="{ row, $index }">
        <el-select
          v-if="row.extInfo.isQualified && row.extInfo.isQualified == 2"
          size="mini"
          class="content_num"
          v-model="row.extInfo.notType"
          filterable
          placeholder="请选择"
          :disabled="isDetails"
        >
          <el-option
            v-for="item in notTypeList"
            :label="item.name"
            :value="item.code"
            :key="item.code"
          >
          </el-option>
        </el-select>
      </template>
      <template v-slot:taskNameHeader>
        <div>
          不良原因<span style="color: #f00; padding: 0 2px">/</span>处置
        </div>
      </template>
      <template v-slot:taskName="{ row, $index }">
        <div
          v-if="
            row.extInfo.isQualified != 1 &&
            row.extInfo.notType &&
            row.extInfo.notType != 5 &&
            row.extInfo.notType != 8
          "
        >
          <span v-if="isDetails">{{ row.extInfo.notReason }}</span>
          <el-input
            v-else
            size="mini"
            class="content_num"
            v-model="row.extInfo.notReason"
            placeholder="原因"
          />
        </div>
        <div
          v-if="![1, 2, 3, 4, 6, 9, 7].includes(Number(row.extInfo.notType))"
        >
          <span v-if="isDetails">{{ row.extInfo.taskName }}</span>
          <el-select
            v-else
            size="mini"
            class="content_num"
            v-model="row.extInfo.taskId"
            filterable
            placeholder="请选择"
            @change="(e) => selectName(e, $index)"
          >
            <el-option
              v-for="item in stepsList"
              :label="item.taskTypeName"
              :value="item.taskId"
              :key="item.taskId"
            >
            </el-option>
          </el-select>
        </div>
      </template>
      <!-- <template v-slot:index="{ row, $index }">
        <div
          class="tag_box"
          v-if="
            Object.prototype.hasOwnProperty.call(row, 'extInfo') &&
            Object.prototype.hasOwnProperty.call(
              row.extInfo,
              'productionTimes'
            ) &&
            Object.prototype.hasOwnProperty.call(
              row.extInfo.productionTimes,
              item.currentTaskDiagram.taskId
            )
          "
        >
          {{ row.extInfo.productionTimes[item.currentTaskDiagram.taskId] }}
        </div>

        <div class="tag_box" v-if="row.isCache">缓</div>
      </template> -->
      <!-- 操作列 -->
      <template v-slot:action="{ row, $index }" v-if="!isDetails">
        <el-link
          type="success"
          @click="getOk($index)"
          v-if="row.confirm == 0 || !row.confirm"
          >确认</el-link
        >
        <el-link type="warning" @click="getCancle($index)" v-else>取消</el-link>
        <el-link type="danger" @click="getDelete($index)" v-if="!isChoose"
          >删除</el-link
        >
      </template>
    </ele-pro-table>

    <el-dialog
      title="工序计算列表"
      :visible.sync="show"
      :before-close="cancel"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
      width="800px"
    >
      <div class="material">
        <div class="btn_end">
          <el-button type="primary" size="mini" @click="handAddParam"
            >添加</el-button
          >
        </div>
        <div class="content_table2">
          <div class="head row rx-sc">
            <div class="item ww30">工序名称</div>
            <div class="item ww20">坯体密度</div>
            <div class="item ww20">密度系数</div>
            <div class="item ww20">重量</div>
            <div class="item ww20">操作</div>
          </div>

          <div class="table">
            <div class="tr row rx-sc" v-for="(it, idx) in paramList" :key="idx">
              <div class="item ww30">
                <el-select
                  size="mini"
                  class="content_num"
                  filterable
                  v-model="it.taskId"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in stepsList"
                    :label="item.taskTypeName"
                    :value="item.taskId"
                    :key="item.taskId"
                  >
                  </el-option>
                </el-select>
              </div>

              <div class="item ww20">
                <el-input
                  class="content_num"
                  size="mini"
                  v-model="it.billet"
                  type="digit"
                  @input="changeInp(it, idx)"
                ></el-input>
              </div>

              <div class="item ww20">
                <el-input
                  class="content_num"
                  size="mini"
                  v-model="it.density"
                  type="digit"
                  @input="changeInp(it, idx)"
                ></el-input>
              </div>

              <div
                class="item ww20"
                v-if="idx == 0 || idx != paramList.length - 1"
              >
                < {{ it.CVIWeight }}
              </div>

              <div
                class="item ww20"
                v-if="paramList.length > 1 && idx == paramList.length - 1"
              >
                ≥ {{ it.CVIWeight }}
              </div>

              <div class="item ww20 rx-cc" @click="getDelete2(idx)">
                <el-link type="danger">删除</el-link>
              </div>
            </div>
          </div>
        </div>

        <div class="formula_box">计算公式： 物料重量/坯体密度*密度系数 </div>

        <div class="reportWeight rx-sc">
          物料重量: {{ weight }} ; 报工重量: {{ reportWeight }}
        </div>
      </div>

      <template slot="footer">
        <el-button size="mini" type="primary" @click="paramSave"
          >保存并计算</el-button
        >
        <el-button size="mini" type="primary" @click="save">计算</el-button>
        <el-button size="mini" @click="cancel">取消</el-button>
      </template>
    </el-dialog>

    <el-dialog
      title="处置类型"
      v-if="dialogVisible"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="处置类型">
          <el-select
            size="mini"
            filterable
            v-model="form.taskId"
            placeholder="请选择"
          >
            <el-option
              v-for="item in stepsList"
              :label="item.taskTypeName"
              :value="item.taskId"
              :key="item.taskId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="batchConfirm()">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="刻码"
      v-if="dialogEngrave"
      :visible.sync="dialogEngrave"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="90px">
        <el-form-item label="刻码区间">
          <el-input
            size="mini"
            type="number"
            v-model="form.minNumber"
            placeholder="请输入刻码最小值"
            prop="digit"
            style="width: 180px"
          />
          -
          <el-input
            size="mini"
            type="number"
            v-model="form.maxNumber"
            placeholder="请输入刻码最大值"
            prop="digit"
            style="width: 180px"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogEngrave = false">取 消</el-button>
        <el-button type="primary" @click="batchEngraveConfirm()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getTaskInstanceList,
    getComputeParam,
    saveParam
  } from '@/api/produce/job';
  import tabMixins from '@/mixins/tableColumnsMixin';
  import { parameterGetByCode } from '@/api/system/dictionary-data';

  export default {
    name: 'semiProductJobBom',
    mixins: [tabMixins],
    props: {
      list: {
        type: Array,
        default: () => []
      },
      equipmentList: {
        type: Array,
        default: () => []
      },

      item: {
        type: Object,
        default: () => {}
      },
      isDetails: {
        type: Boolean,
        default: false
      },
      singleReport: {
        default: null
      },
      isQualified: {
        type: Boolean,
        default: false
      },
      title: {
        type: String,
        default: '报工信息'
      },
      isChoose: {
        type: Boolean,
        default: false
      },
      isEngrave: {
        type: Boolean,
        default: false
      },
      isDisposal: {
        type: Boolean,
        default: false
      }
    },

    data() {
      return {
        deviceList: [],
        newList: [],
        stepsList: [],
        selection: [],
        cacheKeyUrl: 'mes_produce_jobBooking_semiProductJobBom',
        weight: 0,
        reportWeight: 0,

        paramList: [],

        resultObj: {},
        resultIdx: 0,

        show: false,

        isQualifiedList: [
          {
            code: 1,
            name: '合格'
          },
          {
            code: 2,
            name: '不合格'
          }
        ],

        notTypeList: [
          {
            code: 1,
            name: '返工'
          },
          {
            code: 2,
            name: '返修'
          },
          {
            code: 3,
            name: '报废'
          },
          {
            code: 4,
            name: '降级使用'
          },
          {
            code: 5,
            name: '让步接收'
          },
          {
            code: 6,
            name: '留样'
          },
          {
            code: 7,
            name: '消耗'
          },
          {
            code: 8,
            name: '回用'
          },
          {
            code: 9,
            name: '转试销'
          }
        ],
        selectArr: [],
        isDisable: false,
        chooseData: [],
        oldList: [],
        form: {
          taskId: '',
          minNumber: '',
          maxNumber: ''
        },
        dialogVisible: false,
        dialogEngrave: false
      };
    },

    watch: {
      equipmentList: {
        immediate: true,
        deep: true,
        handler(newVal) {
          this.deviceList = newVal;
          console.log(newVal, '1111121547646549896498');
          this.changeHeatNumber();
          this.getCodeData();
        }
      },
      list: {
        immediate: true,
        deep: true,
        handler(newVal) {
          console.log('aaaaaa', newVal);
          if (newVal.length) {
            console.log(newVal);
            this.newList = newVal;
            // if(this.taskObj.type != 4){
            this.sumweight(newVal);
            this.sunTj();
            // }
          }
        }
      }
    },
    computed: {
      taskObj() {
        return this.$store.state.user.taskObj;
      },
      columns() {
        let areaCodeColumns = {
          width: 55,
          show: false
        };

        let selection = {
          width: 45,
          show: false
        };

        if (this.isQualified) {
          areaCodeColumns = {
            minWidth: 120,
            prop: 'areaCode',
            label: '货位',
            align: 'center',
            showOverflowTooltip: true,
            show: true
          };
        }

        if (!this.isChoose) {
          selection = {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center',
            fixed: 'left',
            reserveSelection: true
          };
        }
        return [
          // selection,
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center',
            fixed: 'left',
            reserveSelection: true
          },
          {
            width: 55,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            label: '序号',
            fixed: 'left'
          },
          // areaCodeColumns,
          {
            minWidth: 120,
            prop: 'code',
            label: '编码',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            minWidth: 140,
            prop: 'name',
            label: '名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            minWidth: 120,
            prop: 'batchNo',
            slot: 'batchNo',
            label: '批次号',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            minWidth: 100,
            prop: 'rootCategoryLevelId',
            label: '类型',
            align: 'center',
            showOverflowTooltip: true,
            formatter: (row) => {
              return row.rootCategoryLevelId == 2
                ? '在制品'
                : row.rootCategoryLevelId == 23
                ? '半成品'
                : row.rootCategoryLevelId == 9
                ? '产品'
                : item.rootCategoryLevelId == 28
                ? '废品'
                : '';
            }
          },
          {
            minWidth: 120,
            prop: 'extInfo.engrave',
            label: '刻码',
            slot: 'engrave',
            align: 'center'
          },
          {
            minWidth: 80,
            prop: 'extInfo.productSequence',
            label: '序列号',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            minWidth: 120,
            prop: 'sendCode',
            label: '发货条码',
            slot: 'sendCode',
            align: 'center'
          },
          {
            minWidth: 160,
            prop: 'materielCode',
            label: '物料代号',
            align: 'center',
            slot: 'materielCode'
          },
          {
            minWidth: 100,
            prop: 'clientCode',
            label: '客户代号',
            align: 'center',
            slot: 'clientCode'
          },
          {
            minWidth: 150,
            prop: 'deviceId',
            label: '设备',
            align: 'center',
            slot: 'deviceId'
          },
          {
            minWidth: 100,
            prop: 'heatNumber',
            label: '炉次号',
            slot: 'heatNumber',
            align: 'center'
          },
          {
            width: 100,
            prop: 'feedQuantity',
            label: '数量',
            slot: 'feedQuantity',
            align: 'center'
          },
          {
            width: 110,
            prop: 'position',
            label: '位置',
            slot: 'position',
            align: 'center'
          },
          {
            width: 180,
            prop: 'weight',
            label: '上道工序重量',
            slot: 'weight',
            align: 'center'
          },
          {
            width: 150,
            prop: 'reportWeight',
            label: '报工重量',
            slot: 'reportWeight',
            align: 'center'
          },
          {
            width: 110,
            prop: 'isQualified',
            label: '是否合格',
            slot: 'isQualified',
            align: 'center'
          },
          {
            width: 160,
            prop: 'notType',
            label: '不合格品处置类型',
            align: 'center',
            slot: 'notType'
          },
          {
            width: 140,
            prop: 'taskName',
            label: '不良原因处置',
            slot: 'taskName',
            headerSlot: 'taskNameHeader',
            align: 'center'
          },

          {
            width: 80,
            prop: 'index',
            label: '次数',
            align: 'center',
            slot: 'index'
          },
          {
            width: 150,
            prop: 'modelType',
            label: '型号',
            slot: 'modelType',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            width: 150,
            prop: 'specification',
            label: '规格',
            slot: 'specification',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            width: 150,
            prop: 'brandNum',
            label: '牌号',
            slot: 'brandNum',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 120,
            align: 'center',
            resizable: false,
            slot: 'action',
            fixed: 'right',
            showOverflowTooltip: true
          }
        ];
      },
      clientEnvironmentId() {
        return this.$store.state.user.info.clientEnvironmentId;
      }
    },
    created() {
      this.getTaskFn();
      this.getCodeData();

      console.log(this.list, 'list1111111111111111111111111');
    },

    mounted() {
      if (this.list.length > 0) {
        this.oldList = this.list;
      }
      // this.list.forEach((item, index) => {
      //   if (!item.extInfo.engrave) {
      //     item.extInfo.engrave = index + 1;
      //   }
      // });
      console.log(this.item, 'this.itemitemitems');
    },

    // item

    methods: {
      sunTj() {
        console.log(this.newList, 'workReportInfo.formedNum');
        if (this.item.workReportInfo) {
          this.item.workReportInfo.formedNum = this.newList.filter(
            (v) => v.extInfo.isQualified == 1
          ).length;
          this.item.workReportInfo.notFormedNum = this.newList.filter(
            (v) => v.extInfo.isQualified !== 1
          ).length;
        }
      },
      sumweight(arr) {
        console.log(arr, 6666667777);
        let formedWeight = 0;
        let noFormedWeight = 0;
        arr.map((s, i) => {
          if (s.extInfo.reportWeight) {
            if (s.extInfo.isQualified == 1) {
              formedWeight += s.extInfo.reportWeight * 1;
            } else {
              noFormedWeight += s.extInfo.reportWeight * 1;
            }
          } else {
            s.extInfo.reportWeight = null;
          }
          this.$set(
            this.list[i].extInfo,
            'reportWeight',
            s.extInfo.reportWeight
          );
        });

        if (this.item.workReportInfo) {
          this.item.workReportInfo.formedWeight = parseFloat(
            formedWeight.toFixed(2)
          );
          this.item.workReportInfo.notFormedWeight = parseFloat(
            noFormedWeight.toFixed(2)
          );
        }
      },

      async getCodeData() {
        await parameterGetByCode({ code: 'enable_quality_plus' }).then(
          (res) => {
            if (res.value == '1') {
              console.log('请求code');
              this.isDisable = true;
            } else {
              this.isDisable = false;
            }
          }
        );
      },

      handlerSelect() {
        this.sunTj();
        this.sumweight(this.list);
      },

      handleSelectionChange(data) {
        // this.chooseData = data;
        // console.log(data, '选择的数据');
        // this.$emit('chooseData', data);
        // if (data.length > 0) {
        //   console.log(data, 'data');
        //   data.forEach((item) => {
        //     this.list.forEach((o) => {
        //       if (item.id == o.id) {
        //         console.log(11111, 'data');
        //         o.confirm = 1;
        //       } else {
        //         console.log('0000000', 'data');
        //         o.confirm = 0;
        //       }
        //     });
        //   });
        // } else {
        //   console.log(data, 'data');
        //   this.list.forEach((item) => {
        //     item.confirm = 0;
        //   });
        // }
      },

      handleInput() {
        console.log(12354987498498);
        let arr = JSON.parse(JSON.stringify(this.list));
        this.sumweight(arr);
        this.sunTj();
        // console.log('arr11111112222',arr)
        // console.log('this.list33333',this.list)
      },

      batchDisposal() {
        this.form.taskId = '';
        this.dialogVisible = true;
      },

      batchConfirm() {
        if (!this.form.taskId) {
          this.$message({ message: '请选择处置类型', type: 'warning' });
          return false;
        }

        this.list.forEach((item, index) => {
          this.$set(this.list[index].extInfo, 'taskId', this.form.taskId);
        });

        this.$forceUpdate();

        this.dialogVisible = false;
      },

      handleClose() {
        this.dialogVisible = false;
        this.dialogEngrave = false;
      },

      batchEngrave() {
        this.form.minNumber = '';
        this.form.maxNumber = '';
        this.dialogEngrave = true;
      },

      batchEngraveConfirm() {
        if (!this.form.minNumber) {
          this.$message({ message: '请输入最小刻码', type: 'warning' });
          return false;
        }

        if (!this.form.maxNumber) {
          this.$message({ message: '请输入最大刻码', type: 'warning' });
          return false;
        }

        if (Number(this.form.minNumber) > Number(this.form.maxNumber)) {
          this.$message({
            message: '最小刻码不能大于最大刻码',
            type: 'warning'
          });
          return false;
        }

        this.list.forEach((item, index) => {
          this.$set(this.list[index].extInfo, 'engrave', '');
        });

        const engraveNumber =
          Number(this.form.maxNumber) - Number(this.form.minNumber) + 1;

        console.log(engraveNumber, '刻码数量');

        for (let i = 0; i < engraveNumber; i++) {
          if (this.list.length < i) return;

          if (this.list.length > i) {
            this.$set(
              this.list[i].extInfo,
              'engrave',
              Number(this.form.minNumber) + i
            );
          } else {
            break;
          }
        }

        console.log('222222222222222');

        this.dialogEngrave = false;
      },

      // changeHeatNumber() {
      //   if (this.deviceList.length > 0) {
      //     const device = this.deviceList[0];
      //     this.$emit('update:heatNumber', device.heatNumber);
      //   }
      // },

      getTaskFn() {
        getTaskInstanceList(this.item.workOrderId).then((res) => {
          // this.stepsList = res;
          this.stepsList = res.filter((item) => {
            return item.taskId != -2;
          });

          console.log(this.stepsList, 'this.stepsList');
        });
      },
      selectVal(e, item, idx) {
        let obj = this.deviceList.find((f) => f.id == e);

        this.$set(
          this.list[idx],
          'deviceName',
          obj.name + '-' + obj.codeNumber
        );
        this.$set(
          this.list[idx]['extInfo'],
          'heatNumber',
          obj.extInfo.heatNumber
        );
      },

      changeHeatNumber() {
        this.deviceList.forEach((f) => {
          this.list.forEach((o, index) => {
            if (
              o.deviceId &&
              f.instanceId == o.deviceId &&
              this.deviceList.length > 1
            ) {
              // o.extInfo.heatNumber = f.extInfo.heatNumber;
              this.$set(
                this.list[index].extInfo,
                'heatNumber',
                f.extInfo.heatNumber
              );
              this.$forceUpdate();
            } else if (this.deviceList.length == 1) {
              o['deviceId'] = this.deviceList[0].id || f.instanceId;
              console.log('111111', this.deviceList[0].id);
              // o.extInfo.heatNumber = this.deviceList[0].extInfo.heatNumber;
              // this.$set(this.list[index],'heatNumber',this.deviceList[0].extInfo.heatNumber)
              this.$set(
                this.list[index].extInfo,
                'heatNumber',
                this.deviceList[0].extInfo.heatNumber
              );
              this.$forceUpdate();
            }
          });
        });
      },

      selectName(value, idx) {
        const taskObj = this.stepsList.find((item) => item.taskId === value);
        this.$set(this.list[idx].extInfo, 'taskName', taskObj.taskTypeName);
        this.$forceUpdate();
      },

      openNumerate(row, idx) {
        if (!row.extInfo.reportWeight) {
          this.$message({ message: '请先输入报工重量', type: 'warning' });
          return false;
        }
        this.weight = row.extInfo.weight;
        this.reportWeight = row.extInfo.reportWeight;
        this.resultIdx = idx;

        this.show = true;
      },

      getCompute() {
        console.log(this.item, '列表数据789');
        if (this.item.currentTaskDiagram) {
          getComputeParam(
            this.item.workOrderId,
            this.item.currentTaskDiagram.taskId
          ).then((res) => {
            this.paramList = [];
            this.paramList = (res && res.paramList) || [];
            this.paramObj = res || {};
          });
        }
      },

      handAddParam() {
        this.paramList.push({
          taskId: null,
          taskName: null
        });
      },
      getDelete(idx) {
        this.list.splice(idx, 1);
      },

      getOk(index) {
        this.list[index].confirm = 1;
      },

      getCancle(index) {
        this.list[index].confirm = 0;
      },

      getDelete2(idx) {
        this.paramList.splice(idx, 1);
      },

      changeInp(item, index) {
        if (Number(item.billet) && Number(item.density)) {
          this.paramList[index].CVIWeight = (
            (Number(this.weight) / item.billet) *
            item.density
          ).toFixed(2);
        }
      },

      cancel() {
        this.show = false;
        this.reportWeight = 0;
        this.weight = 0;
      },

      paramSave() {
        if (this.paramList.length > 0) {
          let bol;
          let _i;
          bol = this.paramList.every((e, i) => {
            _i = i + 1;
            return e.taskId && e.billet && e.density;
          });

          if (!bol) {
            this.$message({ message: `第${_i}行数据不全`, type: 'warning' });

            return false;
          }

          if (this.paramList.length < 1) {
            this.$message({
              message: '请输入二个以上计算数据',
              type: 'warning'
            });

            return false;
          }
        }
        let param = {};

        if (!Object.prototype.hasOwnProperty.call(this.paramObj, 'id')) {
          param = {
            paramList: this.paramList,
            workOrderId: this.item.workOrderId,
            taskId: this.item.currentTaskDiagram.taskId
          };
        } else {
          this.paramObj.paramList = this.paramList;
          param = this.paramObj;
        }

        saveParam(param).then((res) => {
          this.$message({ message: '保存成功', type: 'success' });
          this.getCompute();

          this.save();
        });
      },

      save() {
        if (this.item?.currentTaskDiagram?.isFirstTask == 1) {
          this.resultObj = {};

          if (Number(this.reportWeight) < Number(this.paramList[0].CVIWeight)) {
            this.resultObj = this.paramList[0];
            this.setDataSave();
            return false;
          }

          if (
            Number(this.reportWeight) >=
            this.paramList[this.paramList.length - 1].CVIWeight
          ) {
            this.resultObj = this.paramList[this.paramList.length - 1];
            this.setDataSave();
            return false;
          }

          for (let i = 1; i < this.paramList.length; i++) {
            console.log(
              11,
              Number(this.paramList[i - 1].CVIWeight),
              Number(this.reportWeight)
            );
            if (
              Number(this.paramList[i - 1].CVIWeight) <
                Number(this.reportWeight) &&
              Number(this.reportWeight) < Number(this.paramList[i].CVIWeight)
            ) {
              this.resultObj = this.paramList[i];
              break;
            }
          }
          this.setDataSave();
        }
      },

      setDataSave() {
        this.$set(
          this.list[this.resultIdx].extInfo,
          'taskId',
          this.resultObj.taskId
        );
        this.$set(
          this.list[this.resultIdx].extInfo,
          'density',
          this.resultObj.density
        );
        this.$set(
          this.list[this.resultIdx].extInfo,
          'billet',
          this.resultObj.billet
        );
        this.$set(
          this.list[this.resultIdx].extInfo,
          'taskName',
          this.resultObj.taskName
        );

        this.cancel();
      },
      // 多选
      // tableDataSelectChange(val) {
      //   let arr = [];
      //   this.selectArr = [];
      //   if (val.length > 0) {
      //     val.map((item) => {
      //       arr.push(item.id || item.instanceId);
      //     });
      //     this.selectArr = arr;
      //   }
      //   console.log(arr);
      // },
      //批量删除
      batchDelete() {
        if (!this.selection.length) {
          this.$message.warning('请至少选择一条数据');
          return;
        }
        let ids = this.selection.map((item) => item.id || item.instanceId);
        this.$confirm('确定要删除选中的报工信息吗?', '提示', {
          type: 'warning'
        })
          .then(() => {
            ids.forEach((sitem, sindex) => {
              this.list.forEach((item, index) => {
                let id = item.id || item.instanceId;
                if (id == sitem) {
                  this.list.splice(index, 1);
                }
              });
            });
          })
          .catch(() => {});
      }
    },

    created() {
      console.log(this.item, '列表数据123');
      this.getTaskFn();
      this.getCompute();
    }
  };
</script>

<style lang="scss" scoped>
  .tag_box {
    display: inline-block;
    padding: 1px 6px;
    margin-right: 6px;
    background: #e6a23c;
    font-size: 11px;
    color: #fff;
    border-radius: 2px;
  }

  .content_num2 {
    --input-background-color: #f0f8f2;
  }

  .numerate {
    font-size: 22rpx;
    color: #157a2c;
    cursor: pointer;
  }

  .material {
    width: 100%;
  }

  .btn_end {
    text-align: right;
    margin-bottom: 10px;
  }

  .content_table2 {
    width: 100%;

    .row {
      width: 100%;

      .item {
        color: #404446;
        font-size: 14px;
        padding-left: 6px;
      }

      .color157 {
        color: #157a2c;
      }

      .ww20 {
        width: 20%;
      }

      .ww30 {
        width: 30%;
      }

      .ww25 {
        width: 25%;
      }

      .ww35 {
        width: 35%;
      }

      .ww10 {
        width: 10%;
      }
    }

    .head {
      height: 32px;
      background: #f7f9fa;
      border-top: 1px solid #e3e5e5;
      border-left: 1px solid #e3e5e5;

      .item {
        height: 32px;
        line-height: 32px;
        border-right: 1px solid #e3e5e5;
        box-sizing: border-box;
      }
    }

    .tr {
      border-top: 1px solid #e3e5e5;
      border-left: 1px solid #e3e5e5;

      .item {
        font-size: 12px;
        min-height: 32px;
        display: flex;
        align-items: center;
        border-right: 1px solid #e3e5e5;
        box-sizing: border-box;
        white-space: normal;
        word-break: break-all;
      }

      &:last-child {
        border-bottom: 1px solid #e3e5e5;
      }
    }
  }

  .formula_box {
    font-size: 12px;
    font-style: normal;
    font-weight: 400;
    margin-top: 15px;
    color: #157a2c;
  }

  .reportWeight {
    font-size: 12px;
    font-style: normal;
    font-weight: 400;
    margin-top: 15px;
    color: #157a2c;
  }

  .content_num {
    display: flex;
    align-items: center;
    padding: 2px;
    --input-background-color: #f0f8f2;
  }
</style>
