<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-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="24">
        <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='buttonLoading'  @click="recoverData">确认来样</el-button>
            <el-button icon="el-icon-back"  type="danger" size="mini" :loading='buttonLoading'  @click="recoverDataFo">取消确认</el-button>
          </div>
          <div class="common-button-div">
            <el-button
              icon="el-icon-download"
              type="goon"
              size="mini"
              :loading="tableExportLoading"
              @click="doExport"
            >导出</el-button>
          </div>
        </div>
        <div class="common-table-div" id="topTable">
          <el-table
            ref="el_table_1"
            :height="theight1"
            v-loading="tableLoading1"
            :data="tableData1"
            :summary-method="getSummaries2"
            show-summary
            border
            @row-click="saveClickRow"
            @selection-change="searchFollow"
            :header-cell-style="tableHeaderCellStyle"
            highlight-current-row
            :cell-class-name="cellClassName"
            :icorePanelShown="100"
            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="120"
              align="center"
              prop="SAMPLE_NO"
              label="中心编号"
            ></el-table-column>
            <el-table-column
              :show-overflow-tooltip="true"
              sortable
              min-width="150"
              align="center"
              prop="MAT_NAME"
              label="物料名称"
            ></el-table-column>
            <el-table-column
              :show-overflow-tooltip="true"
              sortable
              min-width="120"
              align="center"
              prop="MAT_SHAPE_NAME"
              label="样品规则"
            ></el-table-column>
            <el-table-column
              :show-overflow-tooltip="true"
              sortable
              min-width="180"
              align="center"
              prop="CAR_NO"
              label="委托编号"
            ></el-table-column>
            <el-table-column sortable :show-overflow-tooltip="true" prop="ARCHIVE_FLAG" min-width="80" label="样品检验" align="center">
              <template slot-scope="scope">
                <span v-if="scope.row.ARCHIVE_FLAG == 1">√</span>
                <span v-else></span>
              </template>
            </el-table-column>
            <el-table-column
              :show-overflow-tooltip="true"
              sortable
              min-width="300"
              align="center"
              prop="ANLY_NAME"
              label="委托检测项目"
            ></el-table-column>
            <el-table-column
              :show-overflow-tooltip="true"
              sortable
              min-width="100"
              align="center"
              prop="CREATE_MAN"
              label="送样人"
            ></el-table-column>
            <el-table-column
              :show-overflow-tooltip="true"
              sortable
              min-width="150"
              align="center"
              prop="CREATE_TIME"
              label="送样日期"
            ></el-table-column>
            <el-table-column
              :show-overflow-tooltip="true"
              sortable
              min-width="100"
              align="center"
              prop="KEEP_MAN"
              label="返还人"
            ></el-table-column>
            <el-table-column
              :show-overflow-tooltip="true"
              sortable
              min-width="150"
              align="center"
              prop="KEEP_TIME"
              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="[100, 200, 500, 1000 , 2000]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total_main"
            background
          ></el-pagination>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { getCookie, formatDate } from "@/utils/util.js";
import { cookieUserId, cookieUserName } from "@/config/config.js";
import { exportToExcel } from '../../../utils/utils.js'
import JsBarcode from "jsbarcode";
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  data () {
    return {
      searchForm: {
        times: "",
        matName: ""
      },
      saveDepRow: {},
      clickRow: {},
      total_main: 0,
      pageIndex: 1,
      pageSize: 50,
      saveMainData: [],
      theight1: 0,
      tableLoading1: false,
      tableData1: [],
      icoreFilterFlag: true,
      activeMenu: "",
      buttonLoading: false,
      tableExportLoading: false,
    };
  },
  created () {
    this.theight2 = window.innerHeight - 230;
    this.theight3 = window.innerHeight - 230;
    this.theight1 = window.innerHeight - 190;
    this.searchForm.times = [this.getYNM(new Date()), this.getYNM(new Date())];
    this.activeMenu = window.top.localStorage.getItem("activeMenu");
  },
  mounted () {
    this.searchData();
  },
  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}`;
    },
    recoverData(){
      if(this.saveMainData.length < 1){
        this.$message.warning('请勾选您要确认的数据！')
        return
      }
      this.buttonLoading = true
      let list = [];
      this.saveMainData.forEach(s=>{
        list.push(s.ENTR_ID)
      })
      this.axios.post('pass/testManagement/v1/limstestentrusts/updateArchiveFlag', list, {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.buttonLoading = false
      }).catch(err =>{
        this.buttonLoading = false
        this.$message.error(err.message);
      })
    },
    recoverDataFo(){
      if(this.saveMainData.length < 1){
        this.$message.warning('请勾选您要取消确认的数据！')
        return
      }
      this.buttonLoading = true
      let list = [];
      this.saveMainData.forEach(s=>{
        list.push(s.ENTR_ID)
      })
      this.axios.post('pass/testManagement/v1/limstestentrusts/updateArchiveFlagFo', list, {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.buttonLoading = false
      }).catch(err =>{
        this.buttonLoading = false
        this.$message.error(err.message);
      })
    },
    // 导出
    doExport(){
      if(this.tableData1.length < 1){
        this.$message.warning('导出无数据！')
        return
      }
      let tHeader = [];
      let filterVal = [];
      let list = this.tableData1;
      list.forEach(s=>{
        if(s.ARCHIVE_FLAG == "1"){
          s.ARCHIVE_FLAG = "已确认"
        }else{
          s.ARCHIVE_FLAG = ""
        }
      })
      tHeader = ['中心编号', '物料名称', '样品规则', '委托编号', '样品检验', '委托检测项目', '送样人', '送样日期', '返还人', '返还日期']
      filterVal = ['SAMPLE_NO', 'MAT_NAME', 'MAT_SHAPE_NAME', 'CAR_NO', 'ARCHIVE_FLAG', 'ANLY_NAME', 'CREATE_MAN', 'CREATE_TIME','KEEP_MAN', 'KEEP_TIME']
      exportToExcel(list, tHeader, filterVal, '来样信息确认表', [], [], [])
    },
    //     doExport() {
    //         if (this.tableData1.length === 0) {
    //             this.$message.warning("表格无数据不能导出");
    //             return true;
    //         }
    //         this.tableExportLoading = true;
    //         setTimeout(() => {
    //           this.tableExportLoading = false;
    //           this.doExport2();
    //         }, 1);
    //     },
    // doExport2() {
    //         this.$nextTick(() => {
    //             let table = document.querySelector("#topTable .el-table");
    //             let header = document.querySelector(
    //             "#topTable .el-table .el-table__header"
    //             );
    //             let body = document.querySelector(
    //             "#topTable .el-table .el-table__body"
    //             );
    //             let oTd = document.querySelectorAll("#topTable .el-table td");
    //             let oTh = document.querySelectorAll("#topTable .el-table th");
    //             header.border = "1";
    //             body.border = "1";
    //             for (let item of Array.from(oTd)) {
    //             item.style.height = "27px";
    //             }
    //             for (let item of Array.from(oTh)) {
    //             item.style.height = "27px";
    //             }
    //             let ex = '<th class="gutter" align= "left" style="width: 10px; height: 27px;"></th>';
    //             let ex2 = '<th class="gutter" align= "left" style="width: 0px; height: 27px;"></th>';
    //             ex = new RegExp(ex, "g").test(table.outerHTML) ? ex : ex2;
    //             let bd = table.outerHTML.replace(new RegExp(ex, "g"), "");
    //             bd = bd.replace("正在整理数据", "");
    //             let html =
    //             '<html><head><meta charset="utf-8" /></head><body>' +
    //             bd +
    //             "</body></html>";
    //             let blob = new Blob([html], { type: "application/vnd.ms-excel" });
    //             let link = document.createElement("a");
    //             link.download = "来样信息确认表.xls";
    //             link.href = URL.createObjectURL(blob);
    //             link.click();
    //             header.border = "0";
    //             body.border = "0";
    //             for (let item of Array.from(oTd)) {
    //             item.style.height = "";
    //             }
    //             for (let item of Array.from(oTh)) {
    //             item.style.height = "";
    //             }
    //         });
    //     },
    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;
    },
    saveClickRow (row) {
      this.clickRow = row;
    },
    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;
    },
    //查询操作
    searchData () {
      this.pageIndex = 1;
      this.pageSize = 500;
      this.DataMain();
    },
    DataMain () {
      this.clickRow = {};
      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,
        },
        startTime: startTime,
        endTime: endTime,
        pageSize: this.pageSize,
        pageIndex: this.pageIndex,
      };
      this.tableLoading1 = true;
      this.axios.post("pass/testManagement/v1/limstestentrusts/queryComeToRegister", 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);
      });
    },
    handleSizeChangeMain (pageSize) {
      this.pageIndex = 1;
      this.pageSize = pageSize;
      this.DataMain();
    },
    handleCurrentChangeMain (pageIndex) {
      this.pageIndex = pageIndex;
      this.DataMain();
    },
    cellClassName({row, column, rowIndex, columnIndex}){
      if (columnIndex === 6) {
        if (row.ARCHIVE_FLAG === '1') {
          return 'error-green';
        }
        return '';
      }
    },
    tableHeaderCellStyle () {
      return 'color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;'
    },
  },
};
</script>
<style  scoped>
</style>
