<template>
  <!-- 制样任务-中控 -->
  <div class="examination">
    <div class="common-head-search">
      <el-form :inline="true" @submit.native.prevent>
        <el-form-item label="物料名称">
          <el-input v-model="search.matName" @keyup.enter.native="searchData()" size="small" placeholder="请输入查询内容"
            style="width: 200px; margin-right: 10px"></el-input>
        </el-form-item>
        <el-form-item label="任务日期">
          <el-date-picker v-model="search.entrTime[0]" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date"
            placeholder="开始日期" size="mini" style="width: 140px">
          </el-date-picker>
          <span>至</span>
          <el-date-picker v-model="search.entrTime[1]" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date"
            placeholder="结束日期" size="mini" style="width: 140px">
          </el-date-picker>
          <!-- <el-date-picker
            v-model="search.entrTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="small"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker> -->
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="goon" size="mini" @click="searchData">查询</el-button>
          <el-button icon="el-icon-refresh" type="info" plain size="mini" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row>
      <el-col :span="9">
        <el-tabs type="border-card" v-model="activeName" @tab-click="tabClick">
          <el-tab-pane label="待接任务" name="first">
            <span slot="label">待接任务<el-badge :max="10" :hidden="leftPage.totalPage == 0" style="margin-top: -18px"
                :value="leftPage.totalPage"></el-badge></span>
            <div class="common-title-div" style="height: auto">
              <div class="common-title-name">
                <img style="width: 25px; height: 25px" src="../../../../assets/img/imgScreen/logo.png" />
                未接收
              </div>
              <div>
                <el-button icon="el-icon-check" type="goon" size="mini" :loading="saveLoading"
                  v-privilege="activeMenu + 'receive'" @click="saveData">接收</el-button>
              </div>
            </div>
            <div class="common-table-div">
              <el-table v-loading="allDataLoading" @selection-change="clickRowData" border :height="theight3"
                highlight-current-row :data="table.data" :icore-filter-flag="icoreFilterFlag"
                :header-cell-style="tableHeaderCellStyle" :summary-method="getSummaries" show-summary>
                <el-table-column type="index" label="NO" align="center"></el-table-column>
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="matName" label="物料名称"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="taskDate" label="任务日期"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="sampleTypeName"
                  label="样品类型" align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="suppName" label="发货单位"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="acceptName" label="收货单位"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="testTypeName"
                  label="检验类型" align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="carNo" label="车号"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="stdName" label="制样方法"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="entrTypeName"
                  label="委托类别" align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="pointName" label="发货地点"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="createMan" label="创建人"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="createTime" label="创建时间"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="updateMan" label="修改人"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="updateTime" label="修改时间"
                  align="center"></el-table-column>
              </el-table>
            </div>
            <div class="common-foot-style">
              <el-pagination @size-change="leftPageSize" @current-change="leftPageCurrent"
                :current-page="leftPage.pageNum" :page-sizes="[100, 200, 500, 800]" :page-size="leftPage.pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="leftPage.totalPage" background>
              </el-pagination>
            </div>
          </el-tab-pane>
          <el-tab-pane label="已接任务" name="second">
            <div class="common-title-div" style="height: auto">
              <div class="common-title-name" style="margin-right: 10px">
                <img style="width: 25px; height: 25px" src="../../../../assets/img/imgScreen/logo.png" />
                已接收
              </div>
              <div>
                <el-button icon="el-icon-back" type="danger" size="mini" :loading="backLoading"
                  v-privilege="activeMenu + 'back'" @click="back">退回</el-button>
                <el-button icon="el-icon-check" type="goon" size="mini" :loading="subLoading"
                  v-privilege="activeMenu + 'submit'" @click="submitData">提交</el-button>
              </div>
            </div>
            <div class="common-table-div">
              <el-table id="excelTable" v-loading="dataLoading" ref="dataTable" @cell-click="searchFollow" border
                :height="theight1" highlight-current-row :data="table.list" :icore-filter-flag="icoreFilterFlag"
                :header-cell-style="tableHeaderCellStyle" :cell-class-name="tableRowClassName"
                :summary-method="getSummaries" show-summary>
                <el-table-column type="index" label="NO" align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="matName" label="物料名称"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="stateFlagName" label="状态"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="taskDate" label="任务日期"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="prepTime" label="制样时间"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="sampleNo" label="样号"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="suppName" label="发货单位"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="acceptName" label="收货单位"
                  align="center"></el-table-column>
                <!-- <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="validFlagName" label="数据状态" align="center"></el-table-column> -->
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="sampleTypeName"
                  label="样品类型" align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="testTypeName"
                  label="检验类型" align="center"></el-table-column>
                <!-- <el-table-column sortable width="100px" prop="" label="到货批次" align="center"></el-table-column> -->
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="carNo" label="车号"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="stdName" label="制样方法"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="entrTypeName"
                  label="委托类别" align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="pointName" label="发货地点"
                  align="center"></el-table-column>
                <!-- <el-table-column sortable width="100px" prop="" label="重量(t)" align="center"></el-table-column>
                <el-table-column sortable width="100px" prop="" label="是否商检" align="center"></el-table-column>
                <el-table-column sortable width="100px" prop="" label="是否匹配" align="center"></el-table-column> -->
                <!-- <el-table-column sortable width="100px" prop="ruleId" label="抽样规则" align="center"></el-table-column> -->
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="envId" label="环境信息"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="createMan" label="创建人"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="createTime" label="创建时间"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="updateMan" label="修改人"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="updateTime" label="修改时间"
                  align="center"></el-table-column>
              </el-table>
            </div>
            <div class="common-foot-style">
              <el-pagination @size-change="pageSizeChange" @current-change="pageCurrentChange"
                :current-page="page.pageNum" :page-sizes="[10, 20, 50, 100, 200]" :page-size="page.pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="page.totalPage" background>
              </el-pagination>
            </div>
          </el-tab-pane>
          <el-tab-pane label="已提交" name="thirdly">
            <div class="common-title-div" style="height: auto">
              <div class="common-title-name" style="margin-right: 10px">
                <img style="width: 25px; height: 25px" src="../../../../assets/img/imgScreen/logo.png" />
                已提交
              </div>
              <div>
                <el-input v-model="values" ref="autoInput" size="mini" style="display: inline-block; width: 150px"
                  @input="autoSubmit()" placeholder="扫码接收" clearable :disabled="inputLoading"></el-input>
                <el-button icon="el-icon-printer" type="goon" size="mini" :loading="printLoading"
                  v-privilege="activeMenu + 'print'" @click="printQRCode">打印条形码</el-button>
              </div>
            </div>
            <div class="common-table-div">
              <el-table id="excelTable" v-loading="dataLoading" ref="dataTable" @cell-click="searchSubData" border
                :height="theight1" highlight-current-row :data="table.subList" :icore-filter-flag="icoreFilterFlag"
                :header-cell-style="tableHeaderCellStyle" :cell-class-name="tableRowClassName"
                :summary-method="getSummaries" show-summary>
                <el-table-column type="index" label="NO" align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="matName" label="物料名称"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="stateFlagName" label="状态"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="taskDate" label="任务日期"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="prepTime" label="制样时间"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="sampleNo" label="样号"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="suppName" label="发货单位"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="acceptName" label="收货单位"
                  align="center"></el-table-column>
                <!-- <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="validFlagName" label="数据状态" align="center"></el-table-column> -->
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="sampleTypeName"
                  label="样品类型" align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="testTypeName"
                  label="检验类型" align="center"></el-table-column>
                <!-- <el-table-column sortable width="100px" prop="" label="到货批次" align="center"></el-table-column> -->
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="carNo" label="车号"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="stdName" label="制样方法"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="entrTypeName"
                  label="委托类别" align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="pointName" label="发货地点"
                  align="center"></el-table-column>
                <!-- <el-table-column sortable width="100px" prop="" label="重量(t)" align="center"></el-table-column>
                <el-table-column sortable width="100px" prop="" label="是否商检" align="center"></el-table-column>
                <el-table-column sortable width="100px" prop="" label="是否匹配" align="center"></el-table-column> -->
                <!-- <el-table-column sortable width="100px" prop="ruleId" label="抽样规则" align="center"></el-table-column> -->
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="envId" label="环境信息"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="createMan" label="创建人"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="createTime" label="创建时间"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="updateMan" label="修改人"
                  align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="updateTime" label="修改时间"
                  align="center"></el-table-column>
              </el-table>
            </div>
            <div class="common-foot-style">
              <el-pagination @size-change="subPageSizeChange" @current-change="subPageCurrentChange"
                :current-page="subPage.pageNum" :page-sizes="[10, 20, 50, 100, 200]" :page-size="subPage.pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="subPage.totalPage" background>
              </el-pagination>
            </div>
          </el-tab-pane>
        </el-tabs>
      </el-col>
      <el-col :span="15">
        <div class="common-title-div" style="margin-top: 40px; height: auto">
          <div class="common-title-name">
            <img style="width: 25px; height: 25px" src="../../../../assets/img/imgScreen/logo.png" />
            任务项目
          </div>
          <div>
            <el-button icon="el-icon-check" type="goon" size="mini" :loading="saveDataLoading"
              v-privilege="activeMenu + 'save'" @click="saveDepData">保存</el-button>
          </div>
        </div>
        <div class="common-table-div">
          <el-table v-loading="dataLoading2" ref="dataTable2" border :height="theight2" @row-dblclick="showCell"
            :row-class-name="tableRowName" highlight-current-row :data="table.list2"
            :icore-filter-flag="icoreFilterFlag" :header-cell-style="tableHeaderCellStyle"
            :cell-class-name="tableRowClassName2" :summary-method="getSummaries" show-summary>
            <el-table-column type="index" label="NO" align="center"></el-table-column>
            <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="anlyItemName" label="分析项目名称"
              align="center"></el-table-column>
            <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="anlyValue" label="项目值"
              align="center">
              <template slot-scope="{ row }">
                <el-select :disabled="saveRow.stateFlag == 1" v-if="
                  row.index === tabClickIndex &&
                  !row.showInput &&
                  row.anlyItemName !== '日期'
                " clearable allow-create v-model="row.anlyValue" filterable collapse-tags size="small">
                  <el-option v-for="(item, index) in row.defaultList" :key="index" :label="item" :value="item">
                  </el-option>
                </el-select>
                <el-date-picker v-model="row.anlyValue" type="date" size="small"
                  @change="dateChange(row, row.anlyValue)" v-if="
                    row.index === tabClickIndex &&
                    !row.showInput &&
                    row.anlyItemName === '日期'
                  ">
                </el-date-picker>
                <el-input :disabled="saveRow.stateFlag == 1" type="number" :min="0" :controls="false"
                  oninput="value=value.replace(/[^0-9.]/g,'')" v-if="
                    row.index === tabClickIndex &&
                    row.showInput &&
                    row.anlyItemName !== '日期'
                  " @blur="saveCell" v-model="row.anlyValue" @keyup.enter.native="saveCell" size="small"
                  style="width: 100%"></el-input>
                <span v-else>{{ row.anlyValue }}</span>
              </template>
            </el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="100" align="center" prop="unitName"
              label="单位"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable min-width="100" align="center" prop="formulaName"
              label="计算公式"></el-table-column>
            <!-- <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="defaultValue" label="默认值" align="center"></el-table-column> -->
            <!-- <el-table-column sortable width="140px" prop="savePath1" label="上传图片1" align="center"></el-table-column>
            <el-table-column sortable width="100px" prop="savePath2" label="上传图片2" align="center"></el-table-column>
            <el-table-column sortable width="120px" prop="savePath3" label="上传图片3"  align="center"></el-table-column> -->
            <!-- <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="memo" label="备注" align="center"></el-table-column>
            <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="validFlag1" label="数据状态" align="center"></el-table-column> -->
            <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="createMan" label="创建人"
              align="center"></el-table-column>
            <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="createTime" label="创建时间"
              align="center"></el-table-column>
            <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="updateMan" label="修改人"
              align="center"></el-table-column>
            <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="updateTime" label="修改时间"
              align="center"></el-table-column>
          </el-table>
        </div>
        <!-- <div class="common-foot-style">
          <el-pagination  @size-change="pageSizeChange2" @current-change="pageCurrentChange2"
            :current-page="page2.pageNum" :page-sizes="[10, 20, 50, 100, 200]" :page-size="page2.pageSize" layout="total, sizes, prev, pager, next, jumper"
            :total="page2.totalPage" background>
          </el-pagination>
        </div> -->
      </el-col>
    </el-row>
    <div id="printContainer2" style="margin-left: 20px; margin-top: 3px" v-show="false">
      <div id="printContent">
        <div v-for="(item, index) in printData" :key="item.id"
          style="width: 394px; border: 1px solid #fff; position: relative">
          <!-- <div style="text-align: center; margin-top:40px">
              <strong style="font-size:20px;border:1px solid #333;height: 260px; ">广西北港新材料有限公司</strong><br/>
          </div> -->
          <div style="margin-top: 3px">
            <div style="font-size: 18px; margin-left: 10px">
              物料名称：{{ item.matName + "(" + item.smpDeliName + ")" }}
            </div>
            <div style="font-size: 18px; margin-left: 10px">
              样品编号：<span style="font-weight: 800; font-size: 18px">{{
                item.newSampleNo
              }}</span>
            </div>
            <div style="font-size: 18px; margin-left: 10px">
              生产信息：{{
                (item.atime ? item.atime : item.createTime) +
                "/" +
                item.classVal +
                "/" +
                item.classify
              }}
            </div>
            <div style="font-size: 18px; margin-left: 10px">
              备注信息：{{
                (item.carNo ? item.carNo : "") +
                "/" +
                (item.lots ? item.lots : "")
              }}
            </div>
            <!-- <div style="font-size: 18px; margin-left: 10px">
              生产批号：{{ item.productNo }}
            </div> -->
            <div v-if="item.anlyName && item.anlyName.length > 80" style="
                margin-left: 10px;
                line-height: 17px;
                word-wrap: break-word;
                word-break: normal;
                font-size: 16px;
              ">
              <span style="font-size: 17px">检验元素：</span>{{ item.anlyName }}
            </div>
            <div v-else style="
                margin-left: 10px;
                word-wrap: break-word;
                word-break: normal;
              " :id="'anlyName' + index">
              {{ item.printAnlyName ? createData(item.printAnlyName, "anlyName" + index) : '检验元素：' }}
            </div>
          </div>
          <div style="text-align: center; margin-bottom: 0">
            <img width="88%" :id="`barcode${item.id}`" />
          </div>
          <div style="page-break-after: always"></div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  getCookie,
  formatDate
} from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js'
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
import JsBarcode from 'jsbarcode'
export default {
  data() {
    return {
      table: {
        list: [],
        list2: [],
        data: [],
        subList: []
      },
      tabClickIndex: null,
      allDataLoading: false,
      backLoading: false,
      dataLoading: false,
      dataLoading2: false,
      printLoading: false,
      icoreFilterFlag: true,
      saveRow: {},
      allData: [],
      saveDataLoading: false,
      saveLoading: false,
      subLoading: false,
      stopLoading: false,
      startLoading: false,
      leftPage: {
        pageSize: 200,
        pageNum: 1,
        totalPage: 0
      },
      subPage: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0
      },
      page: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0
      },
      page2: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0
      },
      list: [],
      scId: '',
      showFlag: false,
      Params: {},
      showFlag2: false,
      Params2: {},
      entrOrgNameType: [],
      sampleTypeNameType: [],
      multipleSelection3: [],
      multipleSelection4: [],
      theight1: 0,
      theight2: 0,
      theight3: 0,
      search: {
        entrTime: '',
        matName: ''
      },
      saveSubRow: {},
      printData: [],
      activeName: 'first',
      activeMenu: '',
      inputLoading: false,
      values: ''
    }
  },
  created() {
    this.theight2 = window.innerHeight - 185 < 249 ? 249 : window.innerHeight - 185
    this.theight3 = window.innerHeight - 230
    this.theight1 = window.innerHeight - 230
    this.search.entrTime = [this.getYNM(new Date()), this.getYNM(new Date())]
    this.activeMenu = window.top.localStorage.getItem('activeMenu')
  },
  mounted() {
    this.searchData()
    // this.addScreen();
    // this.addScreen2()
  },
  methods: {
    getYNM(date) {
      let year = date.getFullYear().toString();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      month = month < 10 ? "0" + month : month;
      day = day < 10 ? "0" + day : day;
      return `${year}-${month}-${day}`;
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
        }
        if (index === 1) {
          sums[index] = data.length;
        }
      });
      return sums;
    },
    tabClick(tab, event) {
      this.table.list2 = []
    },
    // 保存
    saveData() {
      if (this.allData.length < 1) {
        this.$message.warning('请点击勾选中未接收制样任务中的数据')
        return
      }
      this.saveLoading = true
      this.axios.post('pass/testManagement/v1/limssamplepreps/saveLimsSamplePrep', this.allData, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message);
          this.searchData()
        } else {
          this.$message.error(res.message)
        }
        this.saveLoading = false
      }).catch(e => {
        this.saveLoading = false
        this.$message.error(e.message)
      })
    },
    // 退回
    back() {
      if (JSON.stringify(this.saveRow) === "{}") {
        this.$message.warning('请点击选中一条您要退回的数据')
        return
      }
      if (this.saveRow.stateFlag != 0) {
        this.$message.warning('该状态不能退回，请选择状态为已处理的数据')
        return
      }
      this.backLoading = true
      this.axios.post('pass/testManagement/v1/limssamplepreps/sendBack?id=' + this.saveRow.id, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message);
          this.searchData()
        } else {
          this.$message.error(res.message)
        }
        this.backLoading = false
      }).catch(e => {
        this.backLoading = false
        this.$message.error(e.message)
      })
    },
    // 获取任务项目
    getAnlyItem(list) {
      let classVal = '', classify = '', date1 = '', className = ''
      if (list.length > 0) {
        let val = list.find(y => y.anlyItemName === '班别');
        let groupClass = list.find(u => u.anlyItemName === '班次');
        let date = list.find(y => y.anlyItemName === '日期');
        let batchClass = list.find(y => y.anlyItemName === '炉次');
        if (val) {
          classVal = val['anlyValue']
        }
        if (groupClass) {
          classify = groupClass['anlyValue']
        }
        if (date) {
          date1 = date['anlyValue']
        }
        if (batchClass) {
          className = batchClass['anlyValue']
        }
      }
      return {
        classVal,
        classify,
        date1,
        className
      }

    },
    // 扫码获取任务项目
    async getAnlyItem1(id) {
      try {
        const { data: res } = await this.axios.post('pass/testManagement/v1/limssampleprepds/queryLimsSamplePrepD?smId=' + id)
        let classVal = '', classify = '', lots1 = '', date = ''
        if (res && res.length > 0) {
          let val = res.find(y => y.anlyItemName === '班别');
          let groupClass = res.find(u => u.anlyItemName === '班次');
          let heatClass = res.find(u => u.anlyItemName === '炉次');
          let dateClass = res.find(u => u.anlyItemName === '日期');
          if (val) {
            classVal = val['anlyValue']
          }
          if (groupClass) {
            classify = groupClass['anlyValue']
          }
          if (heatClass) {
            lots1 = heatClass['anlyValue']
          }
          if (dateClass) {
            date = dateClass['anlyValue']
          }
        }
        return {
          classVal,
          classify,
          lots1,
          date
        }
      } catch (e) {
        this.$message.warning('请求异常，请重试')
      }
    },
    //提交
    submitData() {
      if (JSON.stringify(this.saveRow) === "{}") {
        this.$message.warning('请点击选中一条您要提交的数据')
        return
      }
      if (this.saveRow.stateFlag != 0) {
        this.$message.warning('请选择状态为已处理的数据提交')
        return
      }
      this.subLoading = true
      this.saveDataLoading = true
      this.axios.post('pass/testManagement/v1/limssampleprepds/updateLimsSamplePrepDs', this.table.list2, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.axios.post('pass/testManagement/v1/limssamplepreps/submit?id=' + this.saveRow.smId).then(res => {
            if (res.code == 0 || res.code == 200) {
              this.$message.success(res.message);
              this.searchData()
            } else {
              this.$message.error(res.message)
            }
            this.subLoading = false
          }).catch(e => {
            this.subLoading = false
            this.$message.error(e.message)
          })
        } else {
          this.subLoading = false
          this.$message.error(res.message)
        }
        this.saveDataLoading = false
      }).catch(e => {
        this.subLoading = false
        this.saveDataLoading = false
        this.$message.error(e.message)
      })
    },
    declarationData() {
      this.$message.warning('此操作为跳转操作，后期完善')
    },
    samplingInspection() {

    },
    // 打印条形码
    async printQRCode() {
      if (JSON.stringify(this.saveSubRow) === "{}") {
        this.$message.warning('请点击您要打印的数据')
        return
      }
      if (this.saveSubRow.stateFlag != 1) {
        this.$message.warning('未提交的数据不能进行打印操作，请选择已提交的数据')
        return
      }
      this.printLoading = true
      let sampleFlag = true
      this.axios.post('pass/testManagement/v1/limssamplepreps/saveLimsSamplePrepToScan', { scanCode: this.saveSubRow.markNo, scanName: 'markNo' }, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message);
          const { classVal, classify, date1, className } = this.getAnlyItem([...this.table.list2])
          let { shift, team, lots, atime } = res.data.limsSamplePreps
          this.printData = res.data.limsSampleInfoDBySampleNo
          this.printData.forEach(item => {
            // item.anlyName = res.data.limsTestEntrust.anlyName
            item.carNo = res.data.limsTestEntrust.carNo
            item.smpDeliName = res.data.limsTestEntrust.smpDeliName
            item.lots = lots
            item.atime = atime
            if (!shift) {
              item.classify = classify
            } else {
              item.classify = shift
            }
            if (!team) {
              item.classVal = classVal
            } else {
              item.classVal = team
            }
            if (className) {
              item.lots = className
            }
            if (date1) {
              item.atime = date1
            }
            if (item.anlyName) {
              if (item.anlyName.length < 80) {
                item.printAnlyName = item.anlyName.split('').map(item => {
                  return `<span style="font-size: 23px;">${item}</span>`
                }).join('')
              } else {
                item.printAnlyName = item.anlyName
              }
            } else {
              sampleFlag = false
            }
          })
          this.printData = this.handleSampleNo(this.printData)
          this.runPrint(this.printData)
          this.searchData()
          this.depData(res.data.limsSamplePreps.id)
        } else {
          this.$message.error(res.message)
        }
        this.printLoading = false
      }).catch(e => {
        console.log(e)
        this.printLoading = false
      })
    },
    // 日期选中
    dateChange(item, val) {
      if (val) {
        item.anlyValue = formatDate(val, 'yyyy-MM-dd')
      }
    },
    //删除
    abolishData() {
      if (JSON.stringify(this.saveRow) === "{}") {
        this.$message.warning('请点击选中一条您要停用的数据')
        return
      }
      this.stopLoading = true
      this.axios.post('pass/testManagement/v1/limssamplepreps/inValid?id=' + this.saveRow.smId).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success('停用成功');
          this.searchData()
        } else {
          this.$message.error(res.message)
        }
        this.stopLoading = false
      }).catch(e => {
        this.stopLoading = false
        this.$message.error(e.message)
      })
    },
    // 启用
    recoverData() {
      if (JSON.stringify(this.saveRow) === "{}") {
        this.$message.warning('请点击选中一条您要启用的数据')
        return
      }
      this.startLoading = true
      this.axios.post('pass/testManagement/v1/limssamplepreps/valid?id=' + this.saveRow.smId).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success('启用成功');
          this.searchData()
        } else {
          this.$message.error(res.message)
        }
        this.startLoading = false
      }).catch(e => {
        this.startLoading = false
        this.$message.error(e.message)
      })
    },
    //点击选中数据
    clickRowData(val) {
      this.allData = val
    },
    showCell(row) {
      this.tabClickIndex = row.index
    },
    saveCell(row) {
      this.tabClickIndex = null
    },
    tableRowName({ row, rowIndex }) {
      row.index = rowIndex
    },
    // 保存任务项目数据
    saveDepData() {
      if (this.table.list2.length < 1) {
        this.$message.warning('任务项目表中无数据，不可进行保存');
        return
      }
      if (this.saveSubRow.stateFlag == 1) {
        this.$message.warning('数据已提交，不能进行保存操作，请选择制样任务表中状态为未提交的数据')
        return
      }
      this.saveDataLoading = true
      this.axios.post('pass/testManagement/v1/limssampleprepds/updateLimsSamplePrepDs', this.table.list2, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message);
          this.depData(this.scId);
        } else {
          this.$message.error(res.message)
        }
        this.saveDataLoading = false
      }).catch(e => {
        this.saveDataLoading = false
        this.$message.error(e.message)
      })
    },
    //查询
    searchData() {
      this.page.pageNum = 1;
      this.page.pageSize = 50
      this.subPage.pageNum = 1
      this.subPage.pageSize = 50
      this.leftPage.pageNum = 1
      this.leftPage.pageSize = 200
      this.mainData()
      this.subData()
      this.leftMainData()
    },
    leftMainData() {
      // this.allData = {}
      this.allDataLoading = true;
      let startTime = ''
      let endTime = ''
      if (!this.search.entrTime) {
        startTime = ''
        endTime = ''
      } else {
        startTime = this.search.entrTime[0] + ' 00:00:00'
        endTime = this.search.entrTime[1] + ' 23:59:59'
      }
      let obj = {
        "object": {
          "matName": this.search.matName,
          'sampleTypeCode': '480102'
        },
        list: [-1],
        'mode': userId,
        'startTime': startTime,
        'endTime': endTime,
        'pageIndex': this.leftPage.pageNum,
        'pageSize': this.leftPage.pageSize,
      }
      this.axios.post('pass/testManagement/v1/limssamplepreps/queryLimsSamplePrepPage/', obj, { individualType: 'json' }).then(res => {
        this.allDataLoading = false;
        if (res.code == 0 || res.code == 200) {
          this.table.data = res.data.list;
          this.leftPage.totalPage = res.data.total;
        } else {
          this.$message.error(res.message)
        }
      }).catch(e => {
        this.allDataLoading = false;
        this.$message.error(e.message)
      });
    },
    mainData() {
      this.saveRow = {}
      this.dataLoading = true;
      let startTime = ''
      let endTime = ''
      if (!this.search.entrTime) {
        startTime = ''
        endTime = ''
      } else {
        startTime = this.search.entrTime[0] + ' 00:00:00'
        endTime = this.search.entrTime[1] + ' 23:59:59'
      }
      let obj = {
        "object": {
          "matName": this.search.matName,
          'sampleTypeCode': '480102'
        },
        list: [0],
        'mode': userId,
        'startTime': startTime,
        'endTime': endTime,
        'pageIndex': this.page.pageNum,
        'pageSize': this.page.pageSize,

      }
      this.axios.post('pass/testManagement/v1/limssamplepreps/queryLimsSamplePrepPage/', obj, { individualType: 'json' }).then(res => {
        this.dataLoading = false;
        if (res.code == 0 || res.code == 200) {
          this.table.list = res.data.list;
          this.table.list.forEach(item => {
            item.stateFlagName = item.stateFlag === '-1' ? '未处理' : item.stateFlag === '0' ? '已处理' : item.stateFlag === '1' ? '提交' : ''
            item.validFlagName = item.validFlag == 1 ? '有效' : item.validFlag == 0 ? '无效' : ''
            item.entrTime1 = formatDate(item.entrTime, 'yyyy-MM-dd')
          })
          // if (this.table.list.length > 0) {
          //   this.$refs.dataTable.setCurrentRow(this.table.list[0])
          //   this.searchFollow(this.table.list[0])
          // }
          this.page.totalPage = res.data.total;
        } else {
          this.$message.error(res.message)
        }
      }).catch(e => {
        this.dataLoading = false;
        this.$message.error(e.message)
      });
    },
    subData() {
      this.saveSubRow = {}
      this.dataLoading = true;
      let startTime = ''
      let endTime = ''
      if (!this.search.entrTime) {
        startTime = ''
        endTime = ''
      } else {
        startTime = this.search.entrTime[0] + ' 00:00:00'
        endTime = this.search.entrTime[1] + ' 23:59:59'
      }
      let obj = {
        "object": {
          "matName": this.search.matName,
          'sampleTypeCode': '480102'
        },
        list: [1],
        'mode': userId,
        'startTime': startTime,
        'endTime': endTime,
        'pageIndex': this.subPage.pageNum,
        'pageSize': this.subPage.pageSize,

      }
      this.axios.post('pass/testManagement/v1/limssamplepreps/queryLimsSamplePrepPage/', obj, { individualType: 'json' }).then(res => {
        this.dataLoading = false;
        if (res.code == 0 || res.code == 200) {
          this.table.subList = res.data.list;
          this.table.subList.forEach(item => {
            item.stateFlagName = item.stateFlag === '-1' ? '未处理' : item.stateFlag === '0' ? '已处理' : item.stateFlag === '1' ? '提交' : ''
            item.validFlagName = item.validFlag == 1 ? '有效' : item.validFlag == 0 ? '无效' : ''
            item.entrTime1 = formatDate(item.entrTime, 'yyyy-MM-dd')
          })
          this.subPage.totalPage = res.data.total;
        } else {
          this.$message.error(res.message)
        }
      }).catch(e => {
        this.dataLoading = false;
        this.$message.error(e.message)
      });
    },
    printCode() {
      var iframe = document.createElement('iframe');
      document.body.appendChild(iframe);
      var doc = iframe.contentWindow.document;
      doc.write('<div>' + $('#printContent').html() + '</div>');
      doc.close();
      iframe.contentWindow.focus();
      iframe.contentWindow.document.execCommand('print');
      document.body.removeChild(iframe);
      var t = setTimeout(() => {
        this.$refs.autoInput.focus();
        clearTimeout(t)
      }, 500)
    },
    searchFollow(row) {
      this.saveRow = row
      this.saveSubRow = {}
      this.table.list2 = []
      this.scId = row.smId ? row.smId : ''
      if (!this.scId) {
        return
      }
      this.tabClickIndex = null
      this.page2.pageNum = 1
      this.page2.pageSize = 50
      this.depData(this.scId)
    },
    searchSubData(row) {
      this.saveSubRow = row
      this.saveRow = {}
      this.table.list2 = []
      let scId = row.smId ? row.smId : ''
      if (!scId) {
        return
      }
      this.tabClickIndex = null
      this.page2.pageNum = 1
      this.page2.pageSize = 50
      this.depData(scId)
    },
    depData(id) {
      this.dataLoading2 = true;
      this.axios.post('pass/testManagement/v1/limssampleprepds/queryLimsSamplePrepD?smId=' + id).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.table.list2 = res.data
          this.table.list2.forEach(item => {
            if (item.anlyItemName === '日期' && item.anlyValue - 0 === 0) {
              item.anlyValue = formatDate(new Date(), 'yyyy-MM-dd')
            }
            item.showInput = item.unitName == '无' ? false : true
            item.defaultList = item.defaultValue ? item.defaultValue.split('#') : []
            item.anlyValue = item.anlyValue ? item.anlyValue : item.defaultList[0]
            item.validFlag1 = item.validFlag === '0' ? '已停用' : item.validFlag === '1' ? '已启用' : ''
          })
          this.page2.totalPage = res.data.total;
        } else {
          this.$message.error(res.message)
        }
        this.dataLoading2 = false;
      }).catch(e => {
        this.dataLoading2 = false;
        this.$message.error(e.message)
      });
    },
    createData(data, id) {
      $('#' + id).html('<span style="font-size: 16px;">检验元素：</span>' + data)
    },
    // 扫码打印
    autoSubmit(val) {
      if (!this.values) {
        this.$message.warning('请先用扫码枪扫描标签')
        return
      }
      if (!this.timeId) {
        this.timeId = setTimeout(() => {
          this.inputLoading = true
          let sampleFlag = true
          this.axios.post('pass/testManagement/v1/limssamplepreps/saveLimsSamplePrepToScan', { scanCode: this.values, scanName: 'markNo' }, { individualType: 'json' }).then(async res => {
            if (res.code == 0 || res.code == 200) {
              const { classVal, classify, lots1, date } = await this.getAnlyItem1(res.data.limsSamplePreps.id)
              this.$message.success(res.message);
              this.printData = res.data.limsSampleInfoDBySampleNo
              let { shift, team, lots, atime } = res.data.limsSamplePreps
              this.printData.forEach(item => {
                // item.anlyName = res.data.limsTestEntrust.anlyName
                item.smpDeliName = res.data.limsTestEntrust.smpDeliName
                item.lots = lots
                item.atime = atime
                if (!shift) {
                  item.classify = classify
                } else {
                  item.classify = shift
                }
                if (!team) {
                  item.classVal = classVal
                } else {
                  item.classVal = team
                }
                if (lots1 && lots1 - 0 !== 0) {
                  item.lots = lots1
                }
                if (date) {
                  item.atime = date
                }
                // item.classVal = classVal
                // item.classify = classify
                if (item.anlyName) {
                  if (item.anlyName.length < 80) {
                    item.printAnlyName = item.anlyName.split('').map(item => {
                      // let reg1 = /[A-Za-z]/
                      // if (reg1.test(item)) {
                      return `<span style="font-size: 23px;">${item}</span>`
                      // } else {
                      //   return `<span style="font-size: 16px;">${item}</span>`
                      // }
                    }).join('')
                  } else {
                    item.printAnlyName = item.anlyName
                  }
                } else {
                  sampleFlag = false
                }
              })
              // if (sampleFlag) {
              // 处理样品编号
              this.printData = this.handleSampleNo([...this.printData])
              this.runPrint(this.printData)
              this.searchData()
              this.depData(res.data.limsSamplePreps.id)
              // } else {
              //   this.$message.error('存在分析元素为空的数据')
              // }
            } else {
              this.$message.error(res.message)
            }
            this.values = ''
            this.inputLoading = false
          })
          this.timeId = null;
        }, 1000);
      }
    },
    // 处理样品编号
    handleSampleNo(arr) {
      arr.forEach(item => {
        item.createTime = item.createTime ? formatDate(item.createTime, 'yyyy-MM-dd') : '';
        console.log(item.sampleNo, '替换前的样号')
        let newStr = item.sampleNo.replace('-U', '(筛上)').replace('-D', '(筛下)').replace('-B', '(边)')
          .replace('-Z', '(中)').replace('-T', '(2mm)').replace('-H', '(合金)').replace('-R', '(熔渣)');
        item.newSampleNo = newStr
        console.log(newStr, '替换后的样号')
      })
      return arr
    },
    runPrint(arr) {
      setTimeout(() => {
        arr.forEach(item => {
          JsBarcode(`#barcode${item.id}`, item.sampleNo, {
            format: 'CODE128',
            lineColor: '#000',
            background: '#fff',
            width: 1,
            height: 35,
            displayValue: false
          })
        })
        setTimeout(() => {
          // this.printPage('printContent');
          this.printCode()
        }, 1500)
      }, 100);
    },
    reset() {
      this.search.matName = '';
      this.search.entrTime = [this.getYNM(new Date()), this.getYNM(new Date())]
      this.searchData();
    },
    handleSelectionChange(val) {
      this.multipleSelection3 = val;
    },
    leftPageSize(size) {
      this.leftPage.pageNum = 1
      this.leftPage.pageSize = size;
      this.leftMainData()
    },
    leftPageCurrent(index) {
      this.leftPage.pageNum = index;
      this.leftMainData()
    },
    pageSizeChange(size) {
      this.page.pageNum = 1
      this.page.pageSize = size;
      this.mainData();
    },
    pageCurrentChange(index) {
      this.page.pageNum = index;
      this.mainData();
    },
    subPageSizeChange(size) {
      this.subPage.pageNum = 1
      this.subPage.pageSize = size;
      this.subData();
    },
    subPageCurrentChange(index) {
      this.subPage.pageNum = index;
      this.subData();
    },
    pageSizeChange2(size) {
      this.page2.pageNum = 1
      this.page2.pageSize = size;
      this.depData(this.scId);
    },
    pageCurrentChange2(index) {
      this.page2.pageNum = index;
      this.depData(this.scId);
    },
    addScreen() {
      this.$nextTick(() => {
        let ss = document.createElement('div');
        ss.innerHTML = '筛选';
        ss.style = 'text-align: center;'
        this.$refs.dataTable.$el.getElementsByClassName('el-table__fixed')[0].getElementsByTagName('td')[0].appendChild(ss);
      });
    },
    addScreen2() {
      this.$nextTick(() => {
        let ss = document.createElement('div');
        ss.innerHTML = '筛选';
        ss.style = 'text-align: center;'
        this.$refs.dataTable2.$el.getElementsByClassName('el-table__fixed')[0].getElementsByTagName('td')[0].appendChild(ss);
      });
    },
    tableRowClassName({
      row, column, rowIndex, columnIndex
    }) {
      if (columnIndex === 5) {
        if (row.validFlag !== '1') {
          return 'error-row';
        }
      }
      if (columnIndex === 1) {
        if (row.stateFlag === '1') {
          return 'error-green';
        }
        return '';
      }
    },
    tableRowClassName2({
      row, column, rowIndex, columnIndex
    }) {
      if (columnIndex === 5) {
        if (row.validFlag !== '1') {
          return 'error-row';
        }
        return '';
      }
    },
    tableHeaderCellStyle() {
      return 'color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;'
    }
  }
}
</script>
<style scoped></style>
