<!-- 采购计划信息页面 -->
<template>
  <div class="examination">
    <div class="common-head-search">
      <el-form :inline="true">
        <el-form-item label="物品类别">
          <el-select
            v-model="search.stuffType"
            clearable
            style="width: 100%"
            filterable
            collapse-tags
            size="small"
          >
            <el-option
              v-for="(item, index) in stuffTypes"
              :key="index"
              :label="item.stuffTypeName"
              :value="item.stuffTypeCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物品名称">
          <el-input
            v-model="search.stuffName"
            @keyup.enter.native="searchData()"
            size="small"
            placeholder="请输入物品名称"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="采购状态">
          <el-select
            v-model="search.stateFlag"
            style="width: 100%"
            filterable
            collapse-tags
            size="small"
          >
            <el-option
              v-for="(item, index) in stateFlagList"
              :key="index"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="计划日期">
          <el-date-picker
            v-model="search.time"
            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="reset"
            >重置</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"
          v-privilege="activeMenu + 'ADD'"
          @click="addData"
          >新增</el-button
        >
        <el-button
          icon="el-icon-check"
          type="goon"
          size="mini"
          :loading="updateLoading"
          v-privilege="activeMenu + 'PUT'"
          @click="updateData"
          >修改</el-button
        >
        <el-button
          icon="el-icon-check"
          type="danger"
          size="mini"
          :loading="stopLoading"
          v-privilege="activeMenu + 'STOP'"
          @click="stop"
          >停用</el-button
        >
        <el-button
          icon="el-icon-check"
          type="goon"
          size="mini"
          :loading="buttonLoading2"
          v-privilege="activeMenu + 'RECOVER'"
          @click="recoverData"
          >启用</el-button
        >
        <el-button
          icon="el-icon-check"
          type="goon"
          size="mini"
          :loading="aduitLoading1"
          v-privilege="activeMenu + 'auditTi'"
          @click="auditTi"
          >提交</el-button
        >
        <el-button
          icon="el-icon-check"
          type="danger"
          size="mini"
          :loading="cancelAduitLoading2"
          v-privilege="activeMenu + 'cancelAuditTi'"
          @click="cancelAuditTi"
          >取消提交</el-button
        >
        <el-button
          icon="el-icon-check"
          type="goon"
          size="mini"
          :loading="aduitLoading2"
          v-privilege="activeMenu + 'AUDIT'"
          @click="auditShenHe"
          >审核</el-button
        >
        <el-button
          icon="el-icon-check"
          type="danger"
          size="mini"
          :loading="cancelAduitLoading3"
          v-privilege="activeMenu + 'CANCELAUDIT'"
          @click="cancelAudit"
          >取消审核</el-button
        >
        <el-button
          icon="el-icon-check"
          type="goon"
          size="mini"
          :loading="aduitLoading3"
          v-privilege="activeMenu + 'auditQueRen'"
          @click="auditQueRen"
          >采购批准</el-button
        >
        <el-button
          icon="el-icon-check"
          type="danger"
          size="mini"
          :loading="aduitLoading3"
          v-privilege="activeMenu + 'auditQuXiao'"
          @click="cancelQueRen"
          >采购否决</el-button
        >
      </div>
    </div>
    <div class="common-table-div">
      <el-table
        v-loading="tableLoading1"
        ref="dataTable"
        border
        :height="tableTop"
        highlight-current-row
        @selection-change="handleSelectionChange"
        :data="tableData1"
        :row-class-name="tableRowClassName"
        :cell-class-name="tableCellClassName"
        :icore-filter-flag="icoreFilterFlag"
        :header-cell-style="tableHeaderCellStyle"
        :summary-method="getSummaries2"
         :exchangeFilterMap="{ 'purchaseState': { '0':'无效','1':'编辑','2':'已提交','3':'已审','4':'已确认' }}"
        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="80px"
          prop="purchaseState"
          label="采购状态"
          align="center"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.purchaseState === '0'">无效</span>
            <span v-else-if="scope.row.purchaseState === '1'">编辑</span>
            <span v-else-if="scope.row.purchaseState === '2'">已提交</span>
            <span v-else-if="scope.row.purchaseState === '3'">已审</span>
            <span v-else-if="scope.row.purchaseState === '4'">已确认</span>
          </template>
        </el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="100px"
          prop="ppId"
          label="计划编号"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="160px"
          prop="stuffName"
          label="物品名称"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="stuffSpec"
          label="规格说明"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="120px"
          prop="stuffTypeName"
          label="物品类别"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="80px"
          prop="stuffUnit"
          label="计量单位"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="planNum"
          label="计划数量"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="ppTypeName"
          label="采购类型"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="ppDesc"
          label="采购描述"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="120px"
          prop="ppDate"
          label="到货日期"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="80px"
          prop="ppDept"
          label="采购单位"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="80px"
          prop="unitPrice"
          label="标准单价"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="80px"
          prop="auditNum"
          label="审核数量"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="80px"
          prop="auditDesc"
          label="审核描述"
          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="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-column
          sortable
          :show-overflow-tooltip="true"
          min-width="95px"
          prop="memo2"
          label="提交人"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="160px"
          prop="memo3"
          label="提交时间"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="95px"
          prop="auditMan"
          label="审核人"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="160px"
          prop="auditTime"
          label="审核时间"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="95px"
          prop="apprMan"
          label="采购确认人"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="160px"
          prop="apprTime"
          label="确认时间"
          align="center"
        ></el-table-column>
      </el-table>
    </div>
    <!-- 分页一 -->
    <div class="common-foot-style">
      <el-pagination
        @size-change="pageSizeChange"
        @current-change="pageCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[200, 300, 500, 800, 1000]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </div>
    <el-dialog
      @close="cancelData"
      :visible.sync="dialogTableVisible"
      width="70%"
    >
      <div slot="title">
        <i class="el-icon-document" style="font-size: 20px"></i>
        <span style="margin-left: 10px; font-size: 18px">{{ tableDesc }}</span>
      </div>
      <el-form
        :model="formData"
        label-width="90px"
        :rules="monitorRules"
        ref="monitorForm"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item prop="stuffTypeCode" label="物品类别">
              <el-select
                v-model="formData.stuffTypeCode"
                @change="changeStuffType"
                filterable
                collapse-tags
                size="small"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in stuffTypes"
                  :key="index"
                  :label="item.stuffTypeName"
                  :value="item.stuffTypeCode"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="stuffNoUser" label="物品名称">
              <el-select
                v-model="formData.stuffNoUser"
                @change="changeStuff"
                filterable
                collapse-tags
                size="small"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in stuffList"
                  :key="index"
                  :label="item.stuffName"
                  :value="item.stuffNo"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="stuffSpec" label="规格说明">
              <el-input
                v-model="formData.stuffSpec"
                size="small"
                style="width: 100%"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item prop="ppTypeCode" label="采购类型">
              <el-select
                v-model="formData.ppTypeCode"
                filterable
                collapse-tags
                size="small"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in ppTypeList"
                  :key="index"
                  :label="item.baseName"
                  :value="item.baseCode"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="采购单位">
              <el-input
                v-model="formData.ppDept"
                size="small"
                placeholder="请输入"
                style="width: 100%"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="采购描述">
              <el-input
                v-model="formData.ppDesc"
                size="small"
                placeholder="请输入"
                style="width: 100%"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item prop="stuffUnit" label="计量单位">
              <el-input
                v-model="formData.stuffUnit"
                size="small"
                style="width: 100%"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="planNum" label="计划数量">
              <el-input
                v-model="formData.planNum"
                type="number"
                size="small"
                style="width: 100%"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="标准单价">
              <el-input
                v-model="formData.unitPrice"
                size="small"
                placeholder="请输入"
                style="width: 100%"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item prop="ppDate" label="到货日期">
              <el-date-picker
                v-model="formData.ppDate"
                type="datetime"
                size="small"
                placeholder="选择日期时间"
                align="right"
                style="width: 100%"
                value-format="yyyy-MM-dd HH:mm:ss"
                format="yyyy-MM-dd HH:mm:ss"
                :default-value="new Date()"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="memo" label="备注">
              <el-input
                v-model="formData.memo"
                size="small"
                placeholder="请输入"
                style="width: 100%"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="goon"
          size="mini"
          icon="el-icon-check"
          @click="tableType === '1' ? add() : update()"
          >确定</el-button
        >
        <el-button
          size="mini"
          type="goon"
          icon="el-icon-remove-outline"
          @click="cancelData"
          >取消</el-button
        >
      </div>
    </el-dialog>
    <div
      is="auditAlert"
      :showFlag="showFlag"
      :Params="Params"
      @refresh="auditRefresh"
    ></div>
  </div>
</template>
<script>
import { getCookie, formatDate, isStartEndDate } from "@/utils/util.js";
import { cookieUserId, cookieUserName } from "@/config/config.js";
import auditAlert from "./auditAlert";
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  components: { auditAlert },
  data() {
    return {
      fromDaiban: null,
      showFlag: false,
      Params: {},
      icoreFilterFlag: true,
      dialogTableVisible: false,
      tableDesc: "",
      tableType: "",
      ppTypeList: [],
      monitorRules: {
        stuffNoUser: [
          { required: true, message: "该项不能为空", trigger: "change" },
        ],
        stuffTypeCode: [
          { required: true, message: "该项不能为空", trigger: "change" },
        ],
        ppTypeCode: [
          { required: true, message: "该项不能为空", trigger: "change" },
        ],
      },
      formData: {
        stuffName: "",
        stuffNoUser: "",
        stuffSpec: "",
        stuffTypeCode: "",
        stuffTypeName: "",
        stuffUnit: "",
        auditNum: "",
        planNum: "",
        ppTypeCode: "",
        ppTypeName: "",
        ppDesc: "",
        ppDate: "",
        ppDept: "",
        unitPrice: "",
        memo: "",
        memo1: "",
        memo2: "",
        memo3: "",
      },
      activeName: "first",
      taskId: "",
      testItemNo: "",
      form: {
        empNo: "",
        empName: "",
      },
      jobType: [],
      allotIndex: 1,
      allotPage: 100,
      allotTotal: 0,
      classNo: "",
      deptNo: "",
      sectionNo: "",
      allotLoading: false,
      search: {
        mode: "",
        deptNo: "",
        stuffName: "",
        time: [],
        itemTypeName: "",
        stuffType: "",
        stateFlag: "",
      },
      sectionNameType: [],
      sectionClassType: [],
      stuffTypes: [],
      stuffList: [],
      stuffNames: [],
      stuffUnits: [],
      companyNameType: [],
      disSearch: false,
      allotData: [],
      clickAllot: {},
      tableData1: [],
      tableData2: [],
      tableData3: [],
      tableLoading1: false,
      tableLoading2: false,
      tableLoading3: false,
      recipientsLoading: false,
      buttonLoading1: false,
      buttonLoading2: false,
      updateLoading: false,
      stopLoading: false,
      aduitLoading2: false,
      aduitLoading1: false,
      aduitLoading3: false,
      cancelAduitLoading2: false,
      cancelAduitLoading3: false,
      buttonLoading3: false,
      buttonLoading4: false,
      itemType: [],
      selectMain: [],
      selectDep: [],
      tableTop: 0,
      tableBot: 0,
      pageIndex: 1,
      pageSize: 100,
      total: 0,
      pageIndex_dep: 1,
      pageSize_dep: 100,
      totalDep: 0,
      activeMenu: "",
      stateFlagList: [
        {
          value: "",
          label: "全部",
        },
        {
          value: "0",
          label: "无效",
        },
        {
          value: "1",
          label: "编辑",
        },
        {
          value: "2",
          label: "已提交",
        },
        {
          value: "3",
          label: "已审",
        },
        {
          value: "4",
          label: "已确认",
        },
      ],
    };
  },
  created() {
    this.tableTop = window.innerHeight - 230;
    this.tableBot = window.innerHeight - 230;

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

    if (this.fromDaiban) {
      this.search.time = [
        this.fromDaiban.startTime.split(" ")[0],
        this.fromDaiban.endTime.split(" ")[0],
      ];
      this.search.stateFlag = this.fromDaiban.status == 1 ? "3" : "2";
    } else {
      this.search.time = [this.getYNM(new Date()), this.getYNM(new Date())];
    }

    this.activeMenu = window.top.localStorage.getItem("activeMenu");
    this.axios
      .post(
        "pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",
        { validFlag: 1, baseCode: 4855 },
        { individualType: "json" }
      )
      .then((res) => {
        this.ppTypeList = res.data;
      });
    this.axios
      .post(
        "pass/testManagement/v1/limslrcstuffs/query",
        { id: 0 },
        { individualType: "json" }
      )
      .then((res) => {
        this.stuffNames = res.data;
        let set = new Set();
        this.stuffNames.forEach((item) => {
          if (!set.has(item.stuffTypeCode)) {
            this.stuffTypes.push({
              stuffTypeCode: item.stuffTypeCode,
              stuffTypeName: item.stuffTypeName,
            });
          }
          set.add(item.stuffTypeCode);
        });
      });
    this.searchData();
  },
  mounted() {
    // 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}`;
    },
    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;
    },
    selectDepartment(val) {
      this.sectionNameType = [];
      this.companyNameType.find((item) => {
        if (val === item.orgCode) {
          this.sectionNameType = item.sysOrgList;
        }
      });
    },
    clearSelect() {
      this.search.sectionNo = "";
      this.search.jobs = "";
      this.postType = "";
    },
    //根据选中的部门名称，查询班组名称
    selectClass(val) {
      this.sectionClassType = [];
      this.sectionNameType.find((item) => {
        if (val === item.orgCode) {
          this.sectionClassType = item.sysOrgList;
          this.search.sectionNo = "";
        }
      });
    },
    //根据选中的部门名称，查询班组名称
    selectClass2(val) {
      this.sectionClassType = [];
      this.sectionNameType.find((item) => {
        if (val === item.orgCode) {
          this.sectionClassType = item.sysOrgList;
        }
      });
    },
    selectPost(val) {
      this.postType = [];
      this.jobType.forEach((item) => {
        if (val === item.sectionNo) {
          this.postType.push(item);
        }
      });
    },
    selectSection(val) {
      this.search.jobs = "";
      this.selectPost(val);
    },
    changeStuff(val) {
      this.stuffList.forEach((item) => {
        if (item.stuffNo === val) {
          this.formData.stuffName = item.stuffName;
          this.formData.stuffSpec = item.stuffSpec;
          this.formData.stuffUnit = item.stuffUnit;
          this.formData.unitPrice = item.unitPrice;
        }
      });
    },
    changeStuffType(val) {
      this.stuffList = [];
      this.formData.stuffNoUser = "";
      this.formData.stuffName = "";
      this.stuffNames.forEach((item) => {
        if (item.stuffTypeCode === val) {
          this.stuffList.push(item);
          this.formData.stuffTypeName = item.stuffTypeName;
        }
      });
    },
    closeDialog() {
      this.formData = {
        stuffName: "",
        stuffNoUser: "",
        stuffSpec: "",
        stuffTypeCode: "",
        stuffTypeName: "",
        auditNum: "",
        stuffUnit: "",
        planNum: "",
        ppTypeCode: "",
        ppTypeName: "",
        ppDesc: "",
        ppDate: "",
        memo: "",
        memo1: "",
        memo2: "",
        memo3: "",
      };
      this.searchAllot();
    },
    cancelData() {
      this.formData = {
        stuffName: "",
        stuffNoUser: "",
        stuffSpec: "",
        stuffTypeCode: "",
        stuffTypeName: "",
        auditNum: "",
        stuffUnit: "",
        planNum: "",
        ppTypeCode: "",
        ppTypeName: "",
        ppDesc: "",
        ppDate: "",
        memo: "",
        memo1: "",
        memo2: "",
        memo3: "",
      };
      this.dataMain();
      this.dialogTableVisible = false;
    },
    auditRefresh() {
      this.dataMain();
    },
    searchAllot() {
      this.allotIndex = 1;
      this.allotPage = 100;
      this.dataAllot();
    },
    dataAllot() {
      this.clickAllot = {};
      this.allotLoading = true;
      let obj = {
        object: {
          empNo: this.form.empNo,
          empName: this.form.empName,
          validFlag: 1,
        },
        pageIndex: this.allotIndex,
        pageSize: this.allotPage,
      };
      this.axios
        .post("pass/baseManagement/v1/limslrcemps/queryEmpPagesDS", obj, {
          individualType: "json",
        })
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.allotData = res.data.list;
            this.allotTotal = res.data.total;
          } else {
            this.$message.error(res.message);
          }
          this.allotLoading = false;
        })
        .catch((err) => {
          this.allotLoading = false;
          this.$message.error(err.message);
        });
    },
    addData() {
      this.tableDesc = "新增采购计划";
      this.tableType = "1"; // 新增

      this.dialogTableVisible = true;
    },
    //获取个人所属哪个公司
    getCompany() {
      this.axios
        .get("pass/baseManagement/v1/limslrcemps/" + userId)
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.search.deptNo = res.data ? res.data.deptNo : "";
            this.search.sectionNo = res.data ? res.data.sectionNo : "";
            this.search.jobs = res.data ? res.data.postNo : "";
            // this.classNo = res.data ? res.data.postNo : ''
            // this.deptNo = res.data ? res.data.deptNo : ''
            // this.sectionNo = res.data ? res.data.sectionNo : ''
            this.selectClass2(this.search.deptNo);
            this.selectPost(this.search.sectionNo);
            this.searchData();
          } else {
            this.disSearch = true;
            this.$message.error(e.message);
          }
        })
        .catch((e) => {
          this.disSearch = true;
          this.$message.error(e.message);
        });
    },
    getNameByCode(code, list) {
      let baseName = "";
      list.forEach((item) => {
        if (item.baseCode === code) {
          baseName = item.baseName;
        }
      });
      return baseName;
    },
    //新增数据
    add() {
      this.$refs["monitorForm"].validate((valid) => {
        if (valid) {
          let model = JSON.parse(JSON.stringify(this.formData));
          model.ppTypeName = this.getNameByCode(
            model.ppTypeCode,
            this.ppTypeList
          );
          this.axios
            .post("pass/testManagement/v1/limslrcstuffpps/addModel", model, {
              individualType: "json",
            })
            .then((res) => {
              if (res.code == 0 || res.code == 200) {
                this.cancelData();
                return this.$message.success("新增成功!");
              } else {
                return this.$message.warning(res.message);
              }
            })
            .catch((e) => {
              return this.$message.error(e.message);
            });
        }
      });
    },
    // 修改
    update() {
      this.$refs["monitorForm"].validate((valid) => {
        if (valid) {
          let model = JSON.parse(JSON.stringify(this.formData));
          model.ppTypeName = this.getNameByCode(
            model.ppTypeCode,
            this.ppTypeList
          );
          this.axios
            .put(
              `pass/testManagement/v1/limslrcstuffpps/updateData/${model.id}`,
              model,
              { individualType: "json" }
            )
            .then((res) => {
              if (res.code == 0 || res.code == 200) {
                this.cancelData();
                return this.$message.success("修改成功!");
              } else {
                return this.$message.warning(res.message);
              }
            })
            .catch((e) => {
              return this.$message.error(e.message);
            });
        }
      });
    },
    //停用
    stop() {
      if (this.selectMain.length < 1) {
        this.$message.warning("请勾选您要停用的数据");
        return;
      }
      for (let item of this.selectMain) {
        if (item.validFlag === 0) {
          this.$message.warning("已停用的数据不可再次停用");
          return;
        }
      }
      this.stopLoading = true;
      this.axios
        .post(`pass/testManagement/v1/limslrcstuffpps/stop`, this.selectMain, {
          individualType: "json",
        })
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.$message.success(res.message);
            this.dataMain();
          } else {
            this.$message.error(res.message);
          }
          this.stopLoading = false;
        })
        .catch((err) => {
          this.stopLoading = false;
          this.$message.error(err.message);
        });
    },
    // 打开修改弹窗
    updateData() {
      if (this.selectMain.length != 1) {
        this.$message.warning("请勾选您要修改的1条数据!");
        return;
      }
      if (this.selectMain[0].validFlag === "0") {
        this.$message.warning("请启用无效数据后再进行修改!");
        return;
      }
      if (this.selectMain[0].stateFlag !== "1") {
        this.$message.warning("只能对编辑状态的数据进行修改!");
        return;
      }
      this.formData = JSON.parse(JSON.stringify(this.selectMain[0]));
      this.stuffList = [];
      this.stuffNames.forEach((item) => {
        if (item.stuffTypeCode === this.formData.stuffTypeCode) {
          this.stuffList.push(item);
        }
      });
      this.tableDesc = "修改采购计划";
      this.tableType = "2"; // 修改
      this.dialogTableVisible = true;
    },
    // 审核
    audit() {
      if (this.selectMain.length == 0) {
        this.$message.warning("请只勾选一条您要审核的数据~!");
        return;
      }
      if (this.selectMain[0].validFlag === "0") {
        this.$message.warning("无效数据无法审核!");
        return;
      }
      if (this.selectMain[0].stateFlag === "1") {
        this.$message.warning("未提交数据无法审核!");
        return;
      }
      if (this.selectMain[0].stateFlag === "3") {
        this.$message.warning("已经审核的数据无法再次审核!");
        return;
      }
      if (this.selectMain[0].stateFlag === "4") {
        this.$message.warning("已经采购确认数据无法再次审核!");
        return;
      }
      if (this.selectMain[0].memo2 === userName) {
        this.$message.warning("提交人与审核人一致，审核失败！！!");
        return;
      }
      this.showFlag = !this.showFlag;
      this.Params = this.selectMain[0];
    },
    //审核
    auditShenHe() {
      if (this.selectMain.length == 0) {
        this.$message.warning("请勾选至少一条您要提交的数据~!");
        return;
      }
      let bool = false;
      this.selectMain.forEach((s) => {
        if (s.stateFlag != "2") {
          bool = true;
        }
      });
      if (bool) {
        this.$message.warning("请勾选已提交数据！");
        return;
      }
      let arrList = false;
      this.selectMain.forEach((s) => {
        if (s.memo2 === userName) {
          arrList = true;
        }
      });
      if (arrList) {
        this.$message.warning("提交人与审核人一致，审核失败！！!");
        return;
      }
      this.selectMain.forEach((s) => {
        (s.apprMo = userId), (s.apprMan = userName), (s.stateFlag = "3");
      });
      this.auditLoading = true;
      this.axios
        .post(
          "pass/testManagement/v1/limslrcstuffpps/auditDataTi",
          this.selectMain,
          { individualType: "json" }
        )
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.auditLoading = false;
            this.dataMain();
            return this.$message.success("提交成功!");
          } else {
            return this.$message.warning(res.message);
          }
        })
        .catch((e) => {
          this.auditLoading = false;
          return this.$message.error(e.message);
        });
    },
    //采购确认
    auditQueRen() {
      if (this.selectMain.length == 0) {
        this.$message.warning("请勾选至少一条您要提交的数据~!");
        return;
      }
      let bool = false;
      this.selectMain.forEach((s) => {
        if (s.stateFlag != "3") {
          bool = true;
        }
      });
      if (bool) {
        this.$message.warning("请勾选已审核数据！");
        return;
      }
      let arrList = false;
      this.selectMain.forEach((s) => {
        if (s.auditMan === userName || s.memo2 === userName) {
          arrList = true;
        }
      });
      if (arrList) {
        this.$message.warning("提交人、审核人与采购确认人一致，审核失败！！!");
        return;
      }
      this.selectMain.forEach((s) => {
        (s.apprMo = userId), (s.apprMan = userName), (s.stateFlag = "4");
      });
      this.auditLoading = true;
      this.axios
        .post(
          "pass/testManagement/v1/limslrcstuffpps/auditDataTi",
          this.selectMain,
          { individualType: "json" }
        )
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.auditLoading = false;
            this.dataMain();
            return this.$message.success("提交成功!");
          } else {
            return this.$message.warning(res.message);
          }
        })
        .catch((e) => {
          this.auditLoading = false;
          return this.$message.error(e.message);
        });
    },
    //提交
    auditTi() {
      if (this.selectMain.length == 0) {
        this.$message.warning("请勾选至少一条您要提交的数据~!");
        return;
      }
      let bool = false;
      this.selectMain.forEach((s) => {
        if (s.stateFlag != "1") {
          bool = true;
        }
      });
      if (bool) {
        this.$message.warning("请勾选编辑数据！");
        return;
      }
      let arrList = [];
      this.selectMain.forEach((s) => {
        (s.memo1 = userId), (s.memo2 = userName), (s.stateFlag = "2");
      });
      this.auditLoading = true;
      this.axios
        .post(
          "pass/testManagement/v1/limslrcstuffpps/auditDataTi",
          this.selectMain,
          { individualType: "json" }
        )
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.auditLoading = false;
            this.dataMain();
            return this.$message.success("提交成功!");
          } else {
            return this.$message.warning(res.message);
          }
        })
        .catch((e) => {
          this.auditLoading = false;
          return this.$message.error(e.message);
        });
    },
    // 取消提交
    cancelAuditTi() {
      if (this.selectMain.length === 0) {
        this.$message.warning("请勾选一条您要取消提交的数据~!");
        return;
      }
      let bool = false;
      this.selectMain.forEach((s) => {
        if (s.validFlag === "0") {
          bool = true;
        }
      });
      if (bool) {
        this.$message.warning("无效数据无法操作，请启用后重新操作!");
        return;
      }
      let bool2 = false;
      this.selectMain.forEach((s) => {
        if (s.stateFlag !== "2") {
          bool = true;
        }
      });
      if (bool2) {
        this.$message.warning("非已提交的数据无法取消提交!");
        return;
      }
      this.cancelAduitLoading2 = true;
      this.axios
        .post(
          "pass/testManagement/v1/limslrcstuffpps/cancelAuditTi",
          this.selectMain,
          { individualType: "json" }
        )
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.dataMain();
            this.cancelAduitLoading2 = false;
            return this.$message.success("取消提交成功!");
          } else {
            this.cancelAduitLoading2 = false;
            return this.$message.warning(res.message);
          }
        })
        .catch((e) => {
          this.cancelAduitLoading2 = false;
          return this.$message.error(e.message);
        });
    },
    // 取消批准
    cancelQueRen() {
      if (this.selectMain.length === 0) {
        this.$message.warning("请勾选一条您要取消批准的数据~!");
        return;
      }
      let bool = false;
      this.selectMain.forEach((s) => {
        if (s.validFlag === "0") {
          bool = true;
        }
      });
      if (bool) {
        this.$message.warning("无效数据无法操作，请启用后重新操作!");
        return;
      }
      let bool2 = false;
      this.selectMain.forEach((s) => {
        if (s.stateFlag !== "4" && s.stateFlag !== "3" && s.stateFlag !== "2") {
          bool = true;
        }
      });
      if (bool2) {
        this.$message.warning("编辑状态数据无需取消批准!");
        return;
      }
      this.cancelAduitLoading2 = true;
      this.axios
        .post(
          "pass/testManagement/v1/limslrcstuffpps/cancelQueRen",
          this.selectMain,
          { individualType: "json" }
        )
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.dataMain();
            this.cancelAduitLoading2 = false;
            return this.$message.success("取消批准成功!");
          } else {
            this.cancelAduitLoading2 = false;
            return this.$message.warning(res.message);
          }
        })
        .catch((e) => {
          this.cancelAduitLoading2 = false;
          return this.$message.error(e.message);
        });
    },
    // 取消审核
    cancelAudit() {
      if (this.selectMain.length === 0) {
        this.$message.warning("请勾选一条您要取消审核的数据~!");
        return;
      }
      let bool = false;
      this.selectMain.forEach((s) => {
        if (s.validFlag === "0") {
          bool = true;
        }
      });
      if (bool) {
        this.$message.warning("无效数据无法操作，请启用后重新操作!");
        return;
      }
      let bool2 = false;
      this.selectMain.forEach((s) => {
        if (s.stateFlag !== "3" && s.stateFlag !== "2") {
          bool = true;
        }
      });
      if (bool2) {
        this.$message.warning("非已提交或已审数据无法取消审核!");
        return;
      }
      this.cancelAduitLoading2 = true;
      this.axios
        .post(
          "pass/testManagement/v1/limslrcstuffpps/cancelAudit",
          this.selectMain,
          { individualType: "json" }
        )
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.dataMain();
            this.cancelAduitLoading2 = false;
            return this.$message.success("取消审核成功!");
          } else {
            this.cancelAduitLoading2 = false;
            return this.$message.warning(res.message);
          }
        })
        .catch((e) => {
          this.cancelAduitLoading2 = false;
          return this.$message.error(e.message);
        });
    },
    // 恢复
    recoverData() {
      if (this.selectMain.length < 1) {
        this.$message.warning("请勾选您要启用的数据");
        return;
      }
      for (let i = 0; i < this.selectMain.length; i++) {
        if (this.selectMain[i].validFlag === "1") {
          return this.$message.warning(
            "勾选的数据中包含有效数据，请全部勾选无效数据进行启用!"
          );
        }
      }
      this.buttonLoading2 = true;
      this.axios
        .post(
          `pass/testManagement/v1/limslrcstuffpps/recover`,
          this.selectMain,
          { individualType: "json" }
        )
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.$message.success(res.message);
            this.dataMain();
          } else {
            this.$message.error(res.message);
          }
          this.buttonLoading2 = false;
        })
        .catch((err) => {
          this.buttonLoading2 = false;
          this.$message.error(err.message);
        });
    },
    // 查询
    searchData() {
      this.selectMain = [];
      this.pageIndex = 1;
      this.pageSize = 100;
      this.dataMain();
    },
    dataMain() {
      this.tableLoading1 = true;
      let startTime = "";
      let endTime = "";
      if (!this.search.time) {
        startTime = null;
        endTime = null;
      } else {
        startTime = this.search.time[0];
        endTime = this.search.time[1];
      }
      let obj = {
        stuffTypeCode: this.search.stuffType,
        stuffName: this.search.stuffName,
        startTime: startTime,
        endTime: endTime,
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        stateFlag: this.search.stateFlag || "",
      };
      this.axios
        .post(
          "pass/testManagement/v1/limslrcstuffpps/queryLrcStuffPpPage",
          obj,
          { individualType: "json" }
        )
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.tableData1 = res.data.list;
            this.total = res.data.total;
          } else {
            this.$message.error(res.message);
          }
          this.tableLoading1 = false;
        })
        .catch((e) => {
          this.tableLoading1 = false;
          this.$message.error(e.message);
        });
    },
    dataDep() {
      this.tableLoading2 = 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 tempobj = {
        object: {
          itemTypeName: this.search.itemTypeName,
          stateFlag: 1,
          sampleTypeCode: 480101,
          optNo: userId,
        },
        mode3: this.search.matName,
        mode4: this.search.deptNo ? this.search.deptNo : this.deptNo,
        list: this.search.sectionNo
          ? [this.search.sectionNo]
          : this.sectionNo
          ? [this.sectionNo]
          : [],
        mode: this.search.jobs ? this.search.jobs : this.classNo,
        pageIndex: this.pageIndex_dep,
        pageSize: this.pageSize_dep,
        startTime: startTime,
        endTime: endTime,
      };
      this.axios
        .post(
          "pass/testManagement/v1/limstasktests/queryLimsTaskTestPage",
          tempobj,
          { individualType: "json" }
        )
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.tableData2 = res.data.list;
            this.totalDep = res.data.total;
            this.tableData2.forEach((item) => {
              item.typeName =
                item.itemTypeCode == "480402" ? item.markNo : item.sampleNo;
              item.validFlagName =
                item.validFlag == 0
                  ? "无效"
                  : item.validFlag == 1
                  ? "有效"
                  : "";
              item.stateFlagName =
                item.stateFlag == 0
                  ? "未分配"
                  : item.stateFlag == 1
                  ? "已分配"
                  : "";
              item.isAutoName =
                item.isAuto == 0 ? "手动" : item.isAuto == 1 ? "自动" : "";
            });
          } else {
            this.$message.error(res.message);
          }
          this.tableLoading2 = false;
        })
        .catch((e) => {
          this.tableLoading2 = false;
          this.$message.error(e.message);
        });
    },
    // 重置
    reset() {
      this.search.stuffName = "";
      this.search.stuffType = "";
      this.search.stateFlag = "";
      this.search.time = [this.getYNM(new Date()), this.getYNM(new Date())];
      this.searchData();
    },
    // 取消分配
    cancel() {
      if (this.selectDep.length < 1) {
        this.$message.warning("请勾选需要取消分配的数据");
        return;
      }
      this.buttonLoading3 = true;
      this.axios
        .post("pass/testManagement/v1/limstasktests/unassign", this.selectDep, {
          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);
        });
    },
    pageSizeAllot(size) {
      this.allotIndex = 1;
      this.allotPage = size;
      this.dataAllot();
    },
    pageCurrentAllot(index) {
      this.allotIndex = index;
      this.dataAllot();
    },
    pageSizeChange(size) {
      this.pageIndex = 1;
      this.pageSize = size;
      this.dataMain();
    },
    pageCurrentChange(index) {
      this.pageIndex = index;
      this.dataMain();
    },
    pageSizeChange2(size) {
      this.pageIndex_dep = 1;
      this.pageSize_dep = size;
      this.dataDep();
    },
    pageCurrentChange2(index) {
      this.pageIndex_dep = index;
      this.dataDep();
    },
    //勾选主表数据
    handleSelectionChange(val) {
      this.selectMain = val;
    },
    //勾选人员表格数据
    handleAllot(val) {
      this.clickAllot = Object.assign({}, val);
    },
    handleSelectionChange2(val) {
      this.selectDep = val;
    },
    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 (row.validFlag !== "1") {
        return "error-row";
      }
    },
    tableCellClassName({ row, column, rowIndex, columnIndex }) {
      if (column.property === "stateFlag" && row.stateFlag === "3") {
        return "error-green";
      }
      if (column.property === "stateFlag" && row.stateFlag === "2") {
        return "error-blue";
      }
      if (column.property === "stateFlag" && row.stateFlag === "4") {
        return "error-yellow";
      }
    },
    tableRowClassName2({ row, column, rowIndex, columnIndex }) {
      if (row.validFlag !== "1") {
        return "error-row";
      }
      return "";
    },
    // 表格上横线
    tableHeaderCellStyle() {
      return "color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;";
    },
  },
};
</script>
<style scoped></style>
