<!-- 批量审核页面 -->
<template>
  <div class="examination">
    <div class="common-head-search">
      <el-form :inline="true" ref="searchForm" class="demo-form-inline sui-search-form" @submit.native.prevent>
        <el-form-item label="物料类型">
          <el-select v-model="search.matClassNo" @change="selctMatName" clearable style="width: 100%" filterable
            collapse-tags size="small">
            <el-option v-for="(item, index) in matClassNameType" :key="index" :label="item.matClassName"
              :value="item.matClassNo">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物料名称">
          <el-select v-model="search.matNo" clearable style="width: 100%" filterable collapse-tags size="small">
            <el-option v-for="(item, index) in matNameType" :key="index" :label="item.matName" :value="item.matNo">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分析项目">
          <el-select v-model="search.anlyItemNo" clearable style="width: 100%" filterable collapse-tags size="small">
            <el-option v-for="(item, index) in anlyType" :key="index" :label="item.anlyItemName"
              :value="item.anlyItemNo">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检验日期">
          <el-date-picker v-model="search.times[0]" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date"
            placeholder="开始日期" size="mini" style="width: 140px" :default-time="'00:00:00'">
          </el-date-picker>
          <span>至</span>
          <el-date-picker v-model="search.times[1]" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date"
            placeholder="结束日期" size="mini" style="width: 140px" :default-time="'23:59:59'">
          </el-date-picker>
          <!-- <el-date-picker
            v-model="search.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" :disable="disSearch"
            @click="searchData">查询</el-button>
          <el-button icon="el-icon-refresh" type="info" plain size="mini" :disable="disSearch"
            @click="resetData">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-title-div">
      <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="buttonLoading1"
          v-privilege="activeMenu + 'audit'" @click="audit">审批</el-button>
        <el-button icon="el-icon-remove-outline" type="danger" :loading="buttonLoading2" @click="cancel"
          v-privilege="activeMenu + 'cancel'" size="mini">取消</el-button>
        <el-button icon="el-icon-remove-outline" type="danger" :loading="buttonLoading3" size="mini"
          v-privilege="activeMenu + 'abnormal'" @click="abnormal">异常</el-button>
      </div>
      <div class="common-button-div"></div>
      <div></div>
    </div>
    <div class="common-table-div">
      <div id="table1">
        <el-table ref="tableMain" :height="theight1" v-loading="tableLoading1" :data="tableData1"
          @selection-change="searchFollow" highlight-current-row :cell-class-name="tableRowClassName"
          :summary-method="getSummaries" :header-cell-style="tableHeaderCellStyle" show-summary border
          style="width: 100%" :icorePanelShown="100" :icore-filter-flag="icoreFilterFlag">
          <el-table-column type="index" label="NO" align="center" width="50"></el-table-column>
          <el-table-column type="selection" width="55" align="center"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="80" prop="stateFlagName"
            label="状态"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="100" prop="sampleNo"
            label="检验日期"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="100" prop="matClassName"
            label="物料类型"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="100" prop="matName"
            label="物料名称"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="100" prop="receivedDate"
            label="收样日期"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="80" prop="batchNo"
            label="批次号"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="80" prop=""
            label="主品味1"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="80" prop=""
            label="主品味2"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="80" prop=""
            label="平均值"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="80" prop=""
            label="超值值"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="80" prop="auditMan"
            label="审核人"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="140" prop="auditTime"
            label="审核时间"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="80" prop="memo"
            label="备注"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="95px" prop="createMan"
            label="创建人"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="160px" prop="createTime"
            label="创建时间"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="95px" prop="updateMan"
            label="修改人"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" align="center" min-width="160px" prop="updateTime"
            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" size="mini"
          layout="total, sizes, prev, pager, next, jumper" :total="total_main" background></el-pagination>
      </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);
export default {
  data() {
    return {
      fromDaiban: null,
      search: {
        sampleTypeCode: "",
        matName: "",
        times: "",
        matClassNo: "",
        matNo: "",
        stateFlag: "",
      },
      sampleTypeNameType: [],
      secondDataLoading: false,
      secondData: [],
      classNo: [],
      matNameType: [],
      matClassNameType: [],
      anlyType: [],
      disSearch: false,
      theight1: 0,
      theight2: 0,
      oldTableLoading: false,
      dialogTableVisible: false,
      oldTableData: [],
      isFixed: "left",
      tableLoading1: false,
      tableLoading2: false,
      buttonLoading1: false,
      buttonLoading2: false,
      buttonLoading3: false,
      tableData1: [],
      click_main: [],
      tableData2: [],
      pageIndex: 1,
      pageSize: 100,
      total_main: 0,
      pageIndex_dep: 1,
      pageSize_dep: 100,
      total_dep: 0,
      icoreFilterFlag: true,
      saveAnlyData: [],
      activeMenu: "",
    };
  },
  created() {
    this.theight1 = window.innerHeight - 175;
    this.activeMenu = window.top.localStorage.getItem("activeMenu");
    // this.search.times = [this.getYNM(new Date()), this.getYNM(new Date())];

    this.fromDaiban = JSON.parse(sessionStorage.getItem("fromDaiban"));
    sessionStorage.removeItem("fromDaiban");

    if (this.fromDaiban) {
      this.search.times = [this.fromDaiban.startTime, this.fromDaiban.endTime];
      this.search.stateFlag = this.fromDaiban.status == 0 ? "5" : "6";
    } else {
      this.search.times = [
        this.getYNM(new Date()) + " 00:00:00",
        this.getYNM(new Date()) + " 23:59:59",
      ];
    }
    this.searchData();
  },
  mounted() {
    this.axios
      .post(
        "pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",
        { validFlag: 1, baseCode: 4801 },
        { individualType: "json" }
      )
      .then((res) => {
        this.sampleTypeNameType = res.data;
      });
    //物料类型
    this.axios
      .get("pass/baseManagement/v1/limsbasematerialsclasss/" + "?validFlag=1")
      .then((res) => {
        this.matClassNameType = res.data.list;
      });
    this.axios
      .post(
        "pass/baseManagement/v1/limsstdanlyitems/queryLimsStdAnlyItemPage",
        { object: { validFlag: 1 }, pageIndex: 1, pageSize: 9999 },
        { individualType: "json" }
      )
      .then((res) => {
        this.anlyType = res.data.list;
      });
    this.getCompany();
  },
  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;
    },
    selctMatName(val) {
      this.matNameType = [];
      this.baseData.clearFlag ? "" : (this.search.matNo = "");
      if (!val) {
        return;
      }
      //物料名称
      this.axios
        .post(
          "pass/baseManagement/v1/limsbasematerialss/queryLimsBaseMaterialsPage/",
          {
            object: { validFlag: 1, matClassNo: val },
            pageIndex: 1,
            pageSize: 99999,
          },
          { individualType: "json" }
        )
        .then((res) => {
          this.matNameType = res.data.list;
        });
    },
    //获取个人所属哪个公司
    getCompany() {
      this.axios
        .get("pass/baseManagement/v1/limslrcemps/" + userId)
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            if (!res.data) {
              this.searchData();
              return;
            }
            this.axios
              .post(
                "pass/baseManagement/v1/sysorgs/getSysOrgChildList?orgCode=" +
                res.data.sectionNo
              )
              .then((res) => {
                if (res.code == 0 || res.code == 200) {
                  this.classNo = res.data ? res.data : [];
                  this.searchData();
                } else {
                  this.disSearch = true;
                  this.$message.error(e.message);
                }
              })
              .catch((e) => {
                this.disSearch = true;
                this.$message.error(e.message);
              });
          } else {
            this.disSearch = true;
            this.$message.error(e.message);
          }
        })
        .catch((e) => {
          this.disSearch = true;
          this.$message.error(e.message);
        });
    },
    resetData() {
      this.search.matClassNo = "";
      this.search.matNo = "";
      this.search.anlyItemNo = "";
      this.search.list = [5, 6];
      this.search.stateFlag = "";
      this.search.times = [
        this.getYNM(new Date()) + " 00:00:00",
        this.getYNM(new Date()) + " 23:59:59",
      ];
      this.searchData();
    },
    //审核
    audit() {
      if (this.click_main.length < 1) {
        this.$message.warning("请先勾选中要审核的数据");
        return;
      }
      for (let index = 0; index < this.click_main.length; index++) {
        if (this.click_main[index].stateFlag == "6") {
          this.$message.warning("已审核的数据不可再次审核");
          return;
        }
      }
      this.buttonLoading1 = true;
      this.axios
        .post(
          "pass/testManagement/v1/limssamplereleases/approveLimsSampleRelease" +
          this.click_main,
          { 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.buttonLoading1 = false;
        })
        .catch((err) => {
          this.buttonLoading1 = false;
          this.$message.error(err.message);
        });
    },
    //取消
    cancel() {
      if (this.click_main.length < 1) {
        this.$message.warning("请先勾选中要审核的数据");
        return;
      }
      for (let index = 0; index < this.click_main.length; index++) {
        if (this.click_main[index].stateFlag == "6") {
          this.$message.warning("已审核的数据不可再次审核");
          return;
        }
      }
      this.buttonLoading2 = true;
      this.axios
        .post(
          "pass/testManagement/v1/limssamplereleases/cancelLimsSampleRelease?id=" +
          this.click_main.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.buttonLoading2 = false;
        })
        .catch((err) => {
          this.buttonLoading2 = false;
          this.$message.error(err.message);
        });
    },
    //异常
    abnormal() {
      // if (JSON.stringify(this.click_main)==="{}") {
      //   this.$message.warning("请先点击选中一条审核内容数据");
      //   return;
      // }
      // this.buttonLoading3 = true
      // this.axios.post('pass/testManagement/v1/limssampleresults/abnormityLimsSampleResult?id='+this.selectval.srId, {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.buttonLoading3 = false
      // }).catch(err =>{
      //   this.buttonLoading3 = false
      //   this.$message.error(err.message)
      // })
    },
    //查询
    searchData() {
      this.pageIndex = 1;
      this.pageSize = 100;
      this.dataMain();
    },
    dataMain() {
      this.tableData2 = [];
      let startTime = "";
      let endTime = "";
      if (!this.search.times) {
        startTime = "";
        endTime = "";
      } else {
        startTime = this.search.times[0];
        endTime = this.search.times[1];
      }
      let obj = {
        object: {
          matName: this.search.matName,
          sampleTypeCode: this.search.sampleTypeCode,
          stateFlag: this.search.stateFlag,
        },
        list: [5, 6],
        list2: this.classNo,
        startTime: startTime,
        endTime: endTime,
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
      };
      this.tableLoading1 = true;
      this.axios
        .post(
          "pass/testManagement/v1/limssamplereleases/queryLimsSampleReleasePage/",
          obj,
          { individualType: "json" }
        )
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.tableData1 = res.data.list;
            this.total_main = res.data.total;
            this.tableData1.forEach((item) => {
              item.stateFlagName =
                item.stateFlag == 5
                  ? "未审核"
                  : item.stateFlag == 6
                    ? "已审核"
                    : "";
            });
          } else {
            this.$message.error(res.message);
          }
          this.tableLoading1 = false;
        })
        .catch((err) => {
          this.tableLoading1 = false;
          this.$message.error(err.message);
        });
    },
    //点击审核内容，查询分析项目数据
    searchFollow(val) {
      this.click_main = val;
    },
    handleSizeChangeMain(pageSize) {
      this.pageIndex = 1;
      this.pageSize = pageSize;
      this.dataMain();
    },
    handleCurrentChangeMain(pageIndex) {
      this.pageIndex = pageIndex;
      this.dataMain();
    },
    tableRowClassName({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 2) {
        if (row.stateFlag === "6") {
          return "error-green";
        }
        return "";
      }
    },
    tableHeaderCellStyle() {
      return "color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;";
    },
  },
};
</script>
<style scoped></style>
