<!-- 制样管理- 加权任务-外检 -->
<template>
  <div class="examination">
    <div class="common-head-search">
      <el-form
        :inline="true"
        @submit.native.prevent
        ref="searchForm"
        :model="searchForm"
        class="demo-form-inline sui-search-form"
      >
        <el-form-item label="样品名称">
          <el-input
            v-model="searchForm.matName"
            @keyup.enter.native="searchData()"
            size="small"
            placeholder="请输入查询内容"
            style="width: 150px"
          ></el-input>
        </el-form-item>
        <el-form-item label="样品类型">
          <el-select
            v-model="searchForm.sampleTypeCode"
            clearable
            filterable
            collapse-tags
            size="small"
            style="width: 150px"
            placeholder="请选择查询内容"
          >
            <el-option
              v-for="item in sampleTypeNameType"
              :key="item.baseName"
              :label="item.baseName"
              :value="item.baseCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="送样日期">
          <el-date-picker v-model="searchForm.times[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="searchForm.times[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="searchForm.times"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="small"
            style="width: 300px"
            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="resetData"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-row>
      <el-col :span="10">
        <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="total_main == 0"
                style="margin-top: -18px"
                :value="total_main"
              ></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"
                  :loading="sampleLoading"
                  v-privilege="activeMenu + 'receive'"
                  @click="recSample"
                  size="mini"
                  >接任务</el-button
                >
              </div>
            </div>
            <div class="common-table-div">
              <el-table
                ref="table1"
                :height="theight1"
                v-loading="tableLoading1"
                :data="tableData1"
                :summary-method="getSummaries2"
                show-summary
                border
                @selection-change="searchFollow"
                :header-cell-style="tableHeaderCellStyle"
                highlight-current-row
                :icorePanelShown="100"
                :icore-filter-flag="icoreFilterFlag"
                :exchangeFilterMap="{
                  lock_flag: { '0': '未锁定', '1': '已锁定' },
                }"
                style="width: 100%"
              >
                <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
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="150"
                  align="center"
                  prop="markNo"
                  label="样品号"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="matName"
                  label="物料名称"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="80"
                  align="center"
                  prop="testItemName"
                  label="任务名称"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="80"
                  align="center"
                  prop="sampleTypeName"
                  label="样品类型"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="80"
                  align="center"
                  prop="functionName"
                  label="任务类型"
                ></el-table-column>
              </el-table>
            </div>
            <div class="common-foot-style">
              <el-pagination
                @size-change="handleSizeChangeMain"
                @current-change="handleCurrentChangeMain"
                :current-page="pageIndex"
                :page-sizes="[10, 20, 30, 40, 100, 200]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total_main"
                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"
                  :loading="submitLoading"
                  @click="subData"
                  v-privilege="activeMenu + 'submit'"
                  size="mini"
                  >提交</el-button
                >
                <el-input
                  v-model="values"
                  ref="autoInput"
                  size="mini"
                  style="display: inline-block; width: 120px"
                  clearable
                  @input="autoSubmit()"
                  placeholder="扫码接样"
                  :disabled="inputLoading"
                ></el-input>
              </div>
            </div>
            <div class="common-table-div">
              <el-table
                v-loading="tableLoading2"
                ref="table2"
                :cell-class-name="tableRowClassName"
                border
                @row-click="searchAnalyze"
                :height="theight2"
                highlight-current-row
                :data="tableData2"
                :icore-filter-flag="icoreFilterFlag"
                :header-cell-style="tableHeaderCellStyle"
                :summary-method="getSummaries2"
                show-summary
              >
                <el-table-column
                  type="index"
                  label="NO"
                  fixed
                  align="center"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  fixed
                  sortable
                  min-width="150"
                  align="center"
                  prop="markNo"
                  label="样品号"
                ></el-table-column>
                <!-- <el-table-column :show-overflow-tooltip="true" sortable min-width="100" align="center" prop="id" label="编号"></el-table-column> -->
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="matName"
                  label="物料名称"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="carNo"
                  label="车号"
                >
                  <template slot-scope="{row}">
                    {{ (row.smpDeliCode === '481833') ? row.carNo : ''}}
                  </template>
                </el-table-column>
                <!-- <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="lotsNo"
                  label="批次号"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="heatNo"
                  label="炉号"
                ></el-table-column> -->
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="80"
                  align="center"
                  prop="sampleTypeName"
                  label="样品类型"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="80"
                  align="center"
                  prop="stateFlagName"
                  label="状态"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="80"
                  align="center"
                  prop="testItemName"
                  label="任务名称"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="testItemName"
                  label="任务名称"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="stdName"
                  label="方法"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="eqpName"
                  label="仪器类型"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="testPostName"
                  label="分析岗位"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="testDirection"
                  label="测试部门"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="memo"
                  label="备注"
                ></el-table-column>
              </el-table>
            </div>
            <div class="common-foot-style">
              <el-pagination
                @size-change="handleSizeChangeDep"
                @current-change="handleCurrentChangeDep"
                :current-page="pageIndex_dep"
                :page-sizes="[10, 20, 30, 40, 100, 200]"
                :page-size="pageSize_dep"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total_dep"
                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-button
                  icon="el-icon-back"
                  type="danger"
                  size="mini"
                  :loading="withdrawLoading"
                  @click="backWithdraw"
                  >撤回</el-button
                >
                <el-button
                  icon="el-icon-printer"
                  type="goon"
                  size="mini"
                  :loading="printLoading"
                  v-privilege="activeMenu + 'print'"
                  @click="printQRCode"
                  >打印条形码</el-button
                >
                <el-button
                  icon="el-icon-printer"
                  type="goon"
                  size="mini"
                  :loading="printLoading"
                  @click="yPrint"
                  >Y码打印</el-button
                >
              </div>
            </div>
            <div class="common-table-div">
              <el-table
                v-loading="tableLoading2"
                ref="table2"
                @selection-change="SelectionValues4"
                :cell-class-name="tableRowClassName"
                border
                @row-click="searchSubData"
                :height="theight2"
                highlight-current-row
                :data="subList"
                :icore-filter-flag="icoreFilterFlag"
                :header-cell-style="tableHeaderCellStyle"
                :summary-method="getSummaries2"
                show-summary
              >
                <el-table-column
                  type="index"
                  label="NO"
                  fixed
                  align="center"
                ></el-table-column>
                <el-table-column
                  type="selection"
                  width="45"
                  fixed
                  align="center"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="150"
                  align="center"
                  prop="markNo"
                  label="样品号"
                ></el-table-column>
                <!-- <el-table-column :show-overflow-tooltip="true" sortable min-width="100" align="center" prop="id" label="编号"></el-table-column> -->
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="matName"
                  label="物料名称"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="carNo"
                  label="车号"
                >
                  <template slot-scope="{row}">
                    {{ (row.smpDeliCode === '481833') ? row.carNo : ''}}
                  </template>
                </el-table-column>
                <!-- <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="lotsNo"
                  label="批次号"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="heatNo"
                  label="炉号"
                ></el-table-column> -->
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="80"
                  align="center"
                  prop="sampleTypeName"
                  label="样品类型"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="80"
                  align="center"
                  prop="stateFlagName"
                  label="状态"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="80"
                  align="center"
                  prop="testItemName"
                  label="任务名称"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="testItemName"
                  label="任务名称"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="stdName"
                  label="方法"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="eqpName"
                  label="仪器类型"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="testPostName"
                  label="分析岗位"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="testDirection"
                  label="测试部门"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="memo"
                  label="备注"
                ></el-table-column>
              </el-table>
            </div>
            <div class="common-foot-style">
              <el-pagination
                @size-change="subSizeChangeDep"
                @current-change="subCurrentChangeDep"
                :current-page="subPageIndex"
                :page-sizes="[10, 20, 30, 40, 100, 200]"
                :page-size="subPageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="subTotal"
                background
              ></el-pagination>
            </div>
          </el-tab-pane>
        </el-tabs>
      </el-col>
      <el-col :span="14">
        <el-tabs type="border-card" v-model="activeName2">
          <el-tab-pane
            :label="
              analyzeVal.matName
                ? '【 ' +
                  analyzeVal.matName +
                  ' 】 - 【 ' +
                  analyzeVal.testItemName +
                  ' 】'
                : '未选择数据'
            "
            name="first2"
          >
            <el-form
              :inline="true"
              :model="autoCollData"
              ref="autoCollData"
              :rules="autoColl"
            >
              <!-- <el-form-item prop='eqpTypeCode' label='设备类型'>
                <el-select v-model="autoCollData.eqpTypeCode" @change='selectAuto' filterable collapse-tags size="small" style="width:200px;" placeholder="">
                  <el-option
                  v-for="item in facilityType"
                  :key="item.baseCode"
                  :label="item.baseName"
                  :value="item.baseCode">
                  </el-option>
                </el-select>
              </el-form-item> -->
              <el-form-item prop="eqpNo" label="设备名称">
                <el-select
                  v-model="autoCollData.eqpNo"
                  clearable
                  filterable
                  collapse-tags
                  size="small"
                  style="width: 200px"
                  placeholder=""
                >
                  <el-option
                    v-for="item in autoEqpType"
                    :key="item.eqpNo"
                    :label="item.eqpName + ` (${item.memo})`"
                    :value="item.eqpNo"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-input
                  v-model="scale"
                  ref="autoBalance"
                  size="mini"
                  style="display: inline-block; width: 120px"
                  clearable
                  @input="autoScale()"
                  placeholder="扫码选天平"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  icon="el-icon-check"
                  type="goon"
                  size="mini"
                  :loading="autoColletLoading"
                  @click="autoCollet"
                  >手动采集</el-button
                >
              </el-form-item>
            </el-form>
            <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"
                  :loading="saveLoading"
                  @click="saveData"
                  type="goon"
                  v-privilege="activeMenu + 'save'"
                  size="mini"
                  >保存</el-button
                >
              </div>
              <div></div>
            </div>
            <div class="common-table-div" style="margin-bottom: 0">
              <el-table
                v-loading="tableLoading3"
                border
                :height="theight3"
                highlight-current-row
                :data="tableData3"
                :icore-filter-flag="icoreFilterFlag"
                :header-cell-style="tableHeaderCellStyle"
                :cell-class-name="fxTableRowClassName"
                @row-click="selectAnalyze"
                @row-dblclick="showCell"
                :row-class-name="tableRowName"
                :summary-method="getSummaries2"
                show-summary
              >
                <el-table-column
                  type="index"
                  label="NO"
                  align="center"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="testItemName"
                  label="任务名称"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="anlyItemName"
                  label="分析项目"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="anlyValue"
                  label="报出值"
                >
                  <!-- <template slot-scope="{row}">
                    <el-input :disabled='analyzeVal.stateFlag != 0' type='number' :min="0" :controls="false" oninput ="value=value.replace(/[^0-9.]/g,'')" v-if="row.index === tabClickIndex && row.dataTypeCode == '482502'" @blur="saveCell" v-model="row.anlyValue" @keyup.enter.native="saveCell" size="small" style="width: 100%;"></el-input>
                    <el-input :disabled='analyzeVal.stateFlag != 0' :min="0" :controls="false" v-else-if="row.index === tabClickIndex && row.dataTypeCode !== '482502'" @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="reviseValue"
                  label="录入值"
                >
                  <template slot-scope="{ row }">
                    <el-input
                      :disabled="analyzeVal.stateFlag != 0"
                      type="number"
                      :min="0"
                      :controls="false"
                      oninput="value=value.replace(/[^0-9.]/g,'')"
                      v-if="
                        row.index === tabClickIndex &&
                        row.dataTypeCode == '482502'
                      "
                      @blur="saveCell"
                      v-model="row.reviseValue"
                      @keyup.enter.native="saveCell"
                      size="small"
                      style="width: 100%"
                    ></el-input>
                    <el-input
                      :disabled="analyzeVal.stateFlag != 0"
                      :min="0"
                      :controls="false"
                      v-else-if="
                        row.index === tabClickIndex &&
                        row.dataTypeCode !== '482502'
                      "
                      @blur="saveCell"
                      v-model="row.reviseValue"
                      @keyup.enter.native="saveCell"
                      size="small"
                      style="width: 100%"
                    ></el-input>
                    <span v-else>{{ row.reviseValue }}</span>
                  </template>
                </el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="dasValue"
                  label="采集值"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="cptValue"
                  label="计算值"
                ></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
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="reviseRuleName"
                  label="修约规则"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="isReportName"
                  label="是否报出"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  sortable
                  min-width="100"
                  align="center"
                  prop="isAlarmName"
                  label="是否报警"
                ></el-table-column>
              </el-table>
            </div>
          </el-tab-pane>
        </el-tabs>
      </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="index"
          style="height: 260px; width: 374px; border: 1px solid #333"
        >
          <div style="text-align: center; margin-top: 40px">
            <strong style="font-size: 20px">广西北港新材料有限公司</strong><br />
          </div>
          <div style="margin-top: 6px">
            <div style="font-size: 16px; line-height: 18px; margin-left: 35px">
              物料名称：{{ item.matName }}
            </div>
            <div style="font-size: 16px; line-height: 18px; margin-left: 35px">
              加密编号：{{ item.newSampleNo }}
            </div>
          </div>
          <div style="text-align: center; margin-top: 10px">
            <img width="88%" :id="`barcode${item.id}`" />
          </div>
        </div>
      </div>
    </div>
    <div
      id="printContainer3"
      style="margin-left: 20px; margin-top: 3px"
      v-show="false"
    >
      <div id="printContent3">
        <div
          v-for="(item, index) in printData3"
          :key="item.id"
          style="width: 394px; border: 1px solid #fff"
        >
          <!-- <div style="text-align: center;margin-top:40px">
            <strong style="font-size:20px;">广西北港新材料有限公司</strong><br/>
          </div> -->
          <div style="margin-top: 3px">
            <div style="font-size: 19px; line-height: 21px; margin-left: 15px">
              物料名称：{{ item.matName }}
            </div>
            <div style="font-size: 18px; line-height: 21px; margin-left: 15px">
              样品编号：<span style="font-weight: 800;font-size: 17px;">{{
                item.newSampleNo
              }}</span>
            </div>
            <div style="font-size: 18px; line-height: 21px; margin-left: 15px">
              制样时间：{{ item.createTime }}
            </div>
            <div
              v-if="item.anlyName.length > 80"
              style="
                margin-left: 15px;
                line-height: 17px;
                word-wrap: break-word;
                word-break: normal;
              "
            >
              <span style="font-size: 17px">检验元素：</span>{{ item.anlyName }}
            </div>
            <div
              v-else
              style="
                margin-left: 15px;
                word-wrap: break-word;
                word-break: normal;
              "
              :id="'anlyName' + index"
            >
              {{ 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 {
      searchForm: {
        times: '',
        matName: '',
        sampleTypeCode: '',
        eqpNo: ''
      },
      autoCollData: {
        eqpTypeCode: '480820',
        eqpNo: ''
      },
      checked: false,
      withdrawLoading: false,
      values: '',
      printLoading: false,
      inputLoading: false,
      autoColl: {
        eqpTypeCode: [{ required: true, message: '不能为空', trigger: 'change' },],
        eqpNo: [{ required: true, message: '不能为空', trigger: 'change' }],
      },
      autoColletLoading: false,
      autoEqpType: [],
      facilityType: [],
      saveValue: '',
      saveMainData: [],
      sampleLoading: false,
      saveLoading: false,
      submitLoading: false,
      sampleTypeNameType: [],
      activeName2: 'first2',
      analyzeVal: {},
      analyzeValues: {},
      dialogTableVisible: false,
      theight1: 0,
      theight2: 0,
      theight3: 0,
      tableLoading1: false,
      tableLoading2: false,
      tableLoading3: false,
      tableLoading4: false,
      tabClickIndex: null,
      func: '',
      subList: [],
      subTotal: 0,
      subPageSize: 50,
      subPageIndex: 1,
      originalData: [],
      tableData1: [],
      tableData2: [],
      tableData3: [],
      tableData4: [],
      activeName: 'first',
      pageIndex: 1,
      pageSize: 100,
      total_main: 0,
      pageIndex_dep: 1,
      pageSize_dep: 100,
      total_dep: 0,
      selectVal4: [],
      icoreFilterFlag: true,
      activeMenu: '',
      backLoading: false,
      timeId: null,
      interval: null,
      intervalOne: null,
      sectionNo: '',
      scale: '',
      scaleId: null,
      clearFlag: false,
      printData: [],
      printData3: [] // Y码打印数据
    }
  },
  created () {
    this.theight2 = window.innerHeight - 230
    this.theight3 = window.innerHeight - 230
    this.theight1 = window.innerHeight - 230
    this.searchForm.times = [this.getYNM(new Date()), this.getYNM(new Date())]
    this.activeMenu = window.top.localStorage.getItem('activeMenu')
    this.getCompany()
  },
  mounted () {
    this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode", { validFlag: 1, baseCode: 4808 }, { individualType: 'json' }).then(res => {
      this.facilityType = res.data
    })
    this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode", { validFlag: 1, baseCode: 4801 }, { individualType: 'json' }).then(res => {
      this.sampleTypeNameType = res.data
    })
    this.searchData()
  },
  methods: {
    //获取个人所属哪个公司
    getCompany () {
      this.axios.get('pass/baseManagement/v1/limslrcemps/' + userId).then(res => {
        if (res.code == 0 || res.code == 200) {
          // this.classNo = res.data ? res.data.postNo : ''
          // this.deptNo = res.data ? res.data.deptNo : ''
          this.sectionNo = res.data ? res.data.sectionNo : ''
          this.clearFlag = true
          this.selectAuto('480820')
          this.clearFlag = false
        } else {
          this.$message.error(e.message)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    },
    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}`;
    },
    getSummaries2 (param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "总计";
        }
        if (index === 1) {
          sums[index] = data.length;
        } else if (column.property === "quantity") {
          let val = 0;
          for (let i in data) {
            val = this.floatComputed(
              data[i].quantity ? data[i].quantity : 0,
              val,
              "+"
            );
          }
          sums[index] = val;
        } else if (column.property === "factweight") {
          let val = 0;
          for (let i in data) {
            val = this.floatComputed(
              data[i].factweight ? data[i].factweight : 0,
              val,
              "+"
            );
          }
          sums[index] = val;
        }
      });
      return sums;
    },
    selectAuto (val) {
      this.autoEqpType = []
      this.autoCollData.eqpNo = ''
      if (!val) {
        return
      }
      this.axios.post('pass/baseManagement/v1/limslrceqpinfos/queryeqpName', { eqpTypeCode: val, sectionNo: this.sectionNo }, { individualType: 'json' }).then(res => {
        this.autoEqpType = res.data;
      })
    },
    autoSubmit (val) {
      if (!this.values) {
        this.$message.warning('请先用扫码枪扫描标签')
        return
      }
      if (!this.timeId) {
        this.timeId = setTimeout(() => {
          this.inputLoading = true
          this.axios.post('pass/testManagement/v1/limssampletests/pickUpSampleScanByParmAndCode', { functionCode: '484004', scanCode: this.values, scanName: 'markNo' }, { 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.$nextTick(() => {
              this.$refs.autoInput.focus()
            })
            this.values = ''
            this.inputLoading = false
          }).catch(e => {
            this.$nextTick(() => {
              this.$refs.autoInput.focus()
            })
            this.values = ''
            this.inputLoading = false
          }).catch(e => {
            this.$nextTick(() => {
              this.$refs.autoInput.focus()
            })
            this.values = ''
            this.inputLoading = false
            this.$message.error(e.message)
          })
          this.timeId = null;
        }, 1000);
      }
    },
    autoScale (val) {
      if (!this.scale) {
        this.$message.warning('请先用扫码枪扫描二维码')
        return
      }
      if (!this.scaleId) {
        this.scaleId = setTimeout(() => {
          this.autoCollData.eqpNo = this.scale
          this.scaleId = null;
        }, 1000);
      }
    },
    stopall () {
      clearInterval(this.interval)
      clearInterval(this.intervalOne)
      this.autoColletLoading = false
    },
    //手动采集数据
    autoCollet () {
      let that = this
      if (JSON.stringify(this.analyzeValues) === "{}") {
        this.$message.warning("请先点击选中一条需要采集的分析项目数据！");
        return;
      }
      if (!this.analyzeValues.unitNo == 'UN010' || this.analyzeValues.formulaNo) {
        this.$message.warning("单位不为g 或有计算公式的数据不能进行手动采集操作！");
        return;
      }
      if (this.analyzeVal.stateFlag == 3) {
        this.$message.warning("已提交的数据不能进行自动采集,请选择状态为未处理的数据！");
        return;
      }
      this.$refs.autoCollData.validate((valid) => {
        if (valid) {
          that.autoColletLoading = true
          that.axios.post('pass/testManagement/v1/limsdatacollectds/queryLimsDataCollectDToTime', { eqpNo: that.autoCollData.eqpNo, isAuto: 0 }, { individualType: 'json' }).then(res => {
            if (res.code == 0 || res.code == 200) {
              if (res.data.length > 0) {
                that.analyzeValues.dasValue = that.analyzeValues.reviseValue = that.analyzeValues.anlyValue = res.data.length > 0 ? res.data[0].testValue : ''
                that.analyzeValues.isAdd = 1
                that.$message.success(res.message)
              } else {
                this.$message.warning('本次采集的数据为空！')
              }
            } else {
              that.$message.error(res.message)
            }
            that.autoColletLoading = false
          }).catch(err => {
            that.autoColletLoading = false
            that.$message.error(err.message)
          })
        }
      })
    },
    // 已提交多选
    SelectionValues4 (val) {
      this.selectVal4 = val
    },
    //撤回已提交数据
    backWithdraw () {
      if (this.selectVal4.length < 1) {
        this.$message.warning('请先勾选您要撤回的数据')
        return
      }
      this.withdrawLoading = true
      this.axios.post('pass/testManagement/v1/limssampletests/withdraw', this.selectVal4, { 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.withdrawLoading = false
      }).catch(err => {
        this.withdrawLoading = false
        this.$message.error(err.message)
      })
    },
    resetData () {
      this.searchForm.matName = ''
      this.searchForm.sampleTypeCode = ''
      this.searchForm.eqpNo = ''
      this.searchForm.times = [this.getYNM(new Date()), this.getYNM(new Date())]
      this.searchData()
    },
    //点击选中样品信息数据
    searchFollow (val) {
      this.saveMainData = val
    },
    //接样
    recSample () {
      if (this.saveMainData.length < 1) {
        this.$message.warning('请先勾选您要接样的数据')
        return
      }
      this.sampleLoading = true
      this.axios.post('pass/testManagement/v1/limssampletests/pickUpSample', this.saveMainData, { 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.sampleLoading = false
      }).catch(err => {
        this.sampleLoading = false
        this.$message.error(err.message)
      })
    },
    //提交
    async subData () {
      if (JSON.stringify(this.analyzeVal) === "{}") {
        this.$message.warning("请先点击选中一条测试项目数据");
        return;
      }
      if (this.analyzeVal.stateFlag == 3) {
        this.$message.warning("已提交的数据不能进行提交操作,请选择状态为未处理的数据进行操作");
        return;
      }
      this.stopall()
      this.checked = false
      this.submitLoading = true
      this.saveLoading = true
      let result = await this.axios.post('pass/testManagement/v1/limssampletestds/formulacom', this.tableData3, { individualType: 'json' })
      if (result.code != 0) {
        this.submitLoading = false
        this.saveLoading = false
        this.$message.warning("分析项目无数据或无法计算,无法提交");
        return
      }
      this.tableData3 = result.data
      // limssampletestds/updateLimsSampleTestDList
      this.axios.post('pass/testManagement/v1/limssampletestds/updateLimsSampleTestDList', this.tableData3, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.axios.post('pass/testManagement/v1/limssampletests/submit?stId=' + this.analyzeVal.stId, { 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.submitLoading = false
          }).catch(err => {
            this.submitLoading = false
            this.$message.error(err.message)
          })
        } else {
          this.submitLoading = false
          this.$message.error(res.message)
        }
        this.saveLoading = false
      }).catch(err => {
        this.saveLoading = false
        this.submitLoading = false
        this.$message.error(err.message)
      })
    },
    // 退回
    back () {
      if (JSON.stringify(this.analyzeVal) === "{}") {
        this.$message.warning('请点击选中一条您要退回的数据')
        return
      }
      if (this.analyzeVal.stateFlag != 0) {
        this.$message.warning('该状态不能退回，请选择状态为未处理的数据')
        return
      }
      this.backLoading = true
      this.axios.post('pass/testManagement/v1/limssampletests/sendBack?id=' + this.analyzeVal.id, { individualType: 'json' }).then(res => {
        if (res.succeed) {
          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)
      })
    },
    tabClick (tab, event) {
      this.tableData3 = []
      this.analyzeVal = {}
      this.stopall()
    },
    //保存
    saveData () {
      if (this.tableData3.length < 1) {
        this.$message.warning("表格中无数据，无法进行保存");
        return;
      }
      if (this.analyzeVal.stateFlag == 3) {
        this.$message.warning("已提交的数据不能进行保存,请选择状态为未处理的数据进行操作");
        return;
      }
      this.analyzeValues = {}
      this.stopall()
      this.checked = false
      this.saveLoading = true
      this.axios.post('pass/testManagement/v1/limssampletestds/formulacom', this.tableData3, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message)
          this.depData(this.analyzeVal.id)
        } else {
          this.$message.error(res.message)
        }
        this.saveLoading = false
      }).catch(err => {
        this.saveLoading = false
        this.$message.error(err.message)
      })
    },
    showCell (row) {
      clearInterval(this.intervalOne)
      this.autoColletLoading = false
      this.tabClickIndex = row.index
    },
    saveCell (row) {
      this.tabClickIndex = null
    },
    tableRowName ({ row, rowIndex }) {
      row.index = rowIndex
    },
    //查询操作
    searchData () {
      this.pageIndex = 1
      this.pageSize = 100
      this.subPageIndex = 1
      this.subPageSize = 50
      this.pageSize_dep = 100
      this.pageIndex_dep = 1
      this.stopall()
      this.subDataDep()
      this.DataMain()
      this.DataDep()
    },
    //查询样品信息表格
    DataMain () {
      // this.saveMainData = {}
      let startTime = ''
      let endTime = ''
      if (!this.searchForm.times) {
        startTime = null
        endTime = null
      } else {
        startTime = this.searchForm.times[0] + ' 00:00:00'
        endTime = this.searchForm.times[1] + ' 23:59:59'
      }
      let obj = {
        'object': {
          'sampleTypeCode': this.searchForm.sampleTypeCode,
          'functionCode': '484004',
          'isCtrl': 1,
          'stateFlag': 1,
          'optNo': userId
        },
        mode3: this.searchForm.matName, // 样品名称
        "pageSize": this.pageSize,
        "pageIndex": this.pageIndex,
        'startTime': startTime,
        'endTime': endTime
      }
      this.tableLoading1 = true;
      this.axios.post('pass/testManagement/v1/limstasktests/queryLimsTaskTestPage', obj, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.tableData1 = res.data.list
          this.total_main = res.data.total
        } else {
          this.$message.error(res.message)
        }
        this.tableLoading1 = false;
      }).catch(err => {
        this.tableLoading1 = false;
        this.$message.error(err.message)
      })
    },
    //查询测试项目表格
    DataDep () {
      this.tableData3 = []
      let startTime = ''
      let endTime = ''
      if (!this.searchForm.times) {
        startTime = ''
        endTime = ''
      } else {
        startTime = this.searchForm.times[0] + ' 00:00:00'
        endTime = this.searchForm.times[1] + ' 23:59:59'
      }
      let obj = {
        'object': {
          'matName': this.searchForm.matName,
          'sampleTypeCode': this.searchForm.sampleTypeCode,
          'isCtrl': 1,
          'createNo': userId
        },
        'list': [0],
        'list2': [484004],
        "pageSize": this.pageSize_dep,
        "pageIndex": this.pageIndex_dep,
        'startTime': startTime,
        'endTime': endTime
      }
      this.tableLoading2 = true;
      this.axios.post('pass/testManagement/v1/limssampletests/queryLimsSampleTestPage', obj, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          res.data.list.forEach(item => {
            if (item.smpDeliCode !== '481833') {
              item.carNo = ''
            }
          })
          this.tableData2 = res.data.list
          this.total_dep = res.data.total
          this.tableData2.forEach(item => {
            item.stateFlagName = item.stateFlag == '0' ? '未处理' : item.stateFlag == '3' ? '已提交' : ''
          })

          // if (this.tableData2.length > 0) {
          //   this.$refs.table2.setCurrentRow(this.tableData2[0])
          //   this.searchAnalyze(this.tableData2[0])
          // }
        } else {
          this.$message.error(res.message)
        }
        this.tableLoading2 = false;
      }).catch(err => {
        this.tableLoading2 = false;
        this.$message.error(err.message)
      })
    },
    subDataDep () {
      this.tableData3 = []
      this.analyzeValues = {}
      let startTime = ''
      let endTime = ''
      if (!this.searchForm.times) {
        startTime = ''
        endTime = ''
      } else {
        startTime = this.searchForm.times[0] + ' 00:00:00'
        endTime = this.searchForm.times[1] + ' 23:59:59'
      }
      let obj = {
        'object': {
          'matName': this.searchForm.matName,
          'sampleTypeCode': this.searchForm.sampleTypeCode,
          'isCtrl': 1,
          'createNo': userId
        },
        'list': [3],
        'list2': [484004],
        "pageSize": this.subPageSize,
        "pageIndex": this.subPageIndex,
        'startTime': startTime,
        'endTime': endTime
      }
      this.tableLoading2 = true;
      this.axios.post('pass/testManagement/v1/limssampletests/queryLimsSampleTestPage', obj, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          res.data.list.forEach(item => {
            if (item.smpDeliCode !== '481833') {
              item.carNo = ''
            }
          })
          this.subList = res.data.list
          this.subTotal = res.data.total
          this.subList.forEach(item => {
            item.stateFlagName = item.stateFlag == '0' ? '未处理' : item.stateFlag == '3' ? '已提交' : ''
          })
        } else {
          this.$message.error(res.message)
        }
        this.tableLoading2 = false;
      }).catch(err => {
        this.tableLoading2 = false;
        this.$message.error(err.message)
      })
    },
    createData (data, id) {
      $('#' + id).html('<span style="font-size: 16px;">检验元素：</span>' + data)
    },
    //点击测试项目，查询分析项目
    searchAnalyze (row) {
      //保存点击的样品信息数据，保存、提交操作时刷新
      this.analyzeVal = row
      this.stopall()
      this.depData(this.analyzeVal.id)
    },
    searchSubData (row) {
      //保存点击的样品信息数据，保存、提交操作时刷新
      this.analyzeVal = row
      this.depData(this.analyzeVal.id)
      this.stopall()
    },
    depData (id) {
      this.tableLoading3 = true;
      this.analyzeValues = {}
      this.axios.post('pass/testManagement/v1/limssampletestds/queryLimsSampleTestDList?stId=' + id).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.tableData3 = res.data
          this.tableData3.forEach(item => {
            item.anlyValue = item.anlyValue ? item.anlyValue : item.defaultValue
            item.isReportName = item.isReport == 0 ? '否' : item.isReport == 1 ? '是' : ''
            item.isAlarmName = item.isAlarm == 0 ? '否' : item.isAlarm == 1 ? '是' : ''
          })
        } else {
          this.$message.error(res.message)
        }
        this.tableLoading3 = false;
      }).catch(err => {
        this.tableLoading3 = false;
        this.$message.error(err.message)
      })
    },
    //点击选中分析项目的值
    selectAnalyze (val) {
      this.analyzeValues = val
      clearInterval(this.intervalOne)
      this.autoCollet()
      this.saveValue = this.analyzeValues.reviseValue
    },
    handleSizeChangeMain (pageSize) {
      this.pageIndex = 1;
      this.pageSize = pageSize;
      this.DataMain();
    },
    handleCurrentChangeMain (pageIndex) {
      this.pageIndex = pageIndex;
      this.DataMain();
    },
    handleSizeChangeDep (pageSize) {
      this.pageIndex_dep = 1;
      this.pageSize_dep = pageSize;
      this.DataDep();
    },
    handleCurrentChangeDep (pageIndex) {
      this.pageIndex_dep = pageIndex;
      this.DataDep();
    },
    subSizeChangeDep (pageSize) {
      this.subPageIndex = 1;
      this.subPageSize = pageSize;
      this.subDataDep();
    },
    subCurrentChangeDep (pageIndex) {
      this.subPageIndex = pageIndex;
      this.subDataDep();
    },
    tableRowClassName ({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 5) {
        if (row.stateFlag === '3') {
          return 'error-green';
        }
        return '';
      }
    },
    fxTableRowClassName ({ row, column, rowIndex, columnIndex }) {
      if (column.property === 'reviseValue') {
        return 'error-blue';
      }
    },
    tableHeaderCellStyle () {
      return 'color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;'
    },
    // 打印条形码
    printQRCode () {
      if (JSON.stringify(this.analyzeVal) === "{}") {
        this.$message.warning('请点击您要打印的数据')
        return
      }
      this.printLoading = true
      this.analyzeVal.printCode = this.analyzeVal.sampleTypeCode == '480101' ? this.analyzeVal.markNo : this.analyzeVal.sampleNo
      this.analyzeVal.newSampleNo = this.analyzeVal.printCode.replace('-U', '(筛上)').replace('-D', '(筛下)').replace('-B', '(边)')
        .replace('-Z', '(中)').replace('-T', '(2mm)').replace('-H', '(合金)').replace('-R', '(熔渣)');
      this.printData = []
      this.printData.push(this.analyzeVal)
      setTimeout(() => {
        this.printData.forEach(item => {
          JsBarcode(`#barcode${item.id}`, item.printCode, {
            format: 'CODE128',
            lineColor: '#000',
            background: '#fff',
            width: 1,
            height: 15,
            displayValue: false
          })
        })
        setTimeout(() => {
          this.printLoading = false
          this.printPage('printContent');
        }, 1500)
      }, 100);
    },
    // Y码打印
    yPrint () {
      if (JSON.stringify(this.analyzeVal) === "{}") {
        this.$message.warning('请点击您要打印的数据')
        return true;
      }
      if (this.analyzeVal.markNo) {
        this.printLoading = true
        this.axios.post('pass/testManagement/v1/limssamplepreps/saveLimsSamplePrepToScan', { scanCode: this.analyzeVal.markNo, scanName: 'markNo' }, { individualType: 'json' }).then(res => {
          if (res.code == 0 || res.code == 200) {
            this.printData3 = res.data.limsSampleInfoDBySampleNo
            this.printData3.forEach(item => {
              // item.anlyName = res.data.limsTestEntrust.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: 24px;">${item}</span>`
                }).join('')
              } else {
                item.printAnlyName = item.anlyName
              }
            })
            setTimeout(() => {
              this.runPrint(this.printData3)
            }, 100)
          } else {
            this.printLoading = false
            this.$message.error(res.message)
          }
        }).catch(e => {
          this.printLoading = false
          this.$message.error(e.message)
        })
      } else {
        this.$message.warning('样品N码不存在')
        return true
      }
    },
    runPrint (arr) {
      arr.forEach(item => {
        item.createTime = item.createTime ? formatDate(item.createTime, 'yyyy-MM-dd') : '';
        let newStr = item.sampleNo.replace('-U', '(筛上)').replace('-D', '(筛下)').replace('-B', '(边)')
          .replace('-Z', '(中)').replace('-T', '(2mm)').replace('-H', '(合金)').replace('-R', '(熔渣)')
        item.newSampleNo = newStr
        JsBarcode(`#barcode${item.id}`, item.sampleNo, {
          format: 'CODE128',
          lineColor: '#000',
          background: '#fff',
          width: 1,
          height: 15,
          displayValue: false
        })
      })
      setTimeout(() => {
        this.printLoading = false
        this.printPage('printContent3');
      }, 1000)
    }
  },
  beforeDestroy () {
    clearInterval(this.interval)
    clearInterval(this.intervalOne)
  }
}
</script>
<style  scoped>
</style>
