<!-- 新增检验委托信息-成品 -->
<template>
  <div class="examination">
    <el-dialog
      @close="closeDialog"
      :visible.sync="dialogTableVisible"
      width="80%"
    >
      <div slot="title" class="titleBox">
        <i
          :class="tableType === 0 ? 'el-icon-document' : 'el-icon-edit-outline'"
          style="font-size: 20px"
        ></i>
        <!-- <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/more.png' /> -->
        <span style="margin-left: 10px; font-size: 18px">{{
          tableTypeDsc
        }}</span>
      </div>
      <el-form
        ref="baseData"
        label-width="80px"
        :inline="true"
        :model="baseData"
      >
        <el-form-item label="计划单位">
          <el-select
            v-model="baseData.planCompanyNo"
            filterable
            clearable
            style="width: 150px"
            collapse-tags
            @change="selectDepartment"
            size="small"
          >
            <el-option
              v-for="item in companyNameType"
              :key="item.orgCode"
              :label="item.orgName"
              :value="item.orgCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="计划车间">
          <el-select
            v-model="baseData.planDeptNo"
            filterable
            clearable
            style="width: 150px"
            collapse-tags
            size="small"
          >
            <el-option
              v-for="item in sectionNameType"
              :key="item.orgCode"
              :label="item.orgName"
              :value="item.orgCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="matNo" label="物料名称">
          <el-input
            v-model="baseData.matName"
            style="width: 150px"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item prop="matNo">
          <el-button
            icon="el-icon-search"
            type="goon"
            size="mini"
            @click="searchData"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="common-title-div">
        <div class="common-title-name" style="margin-right: 10px">
          <img
            style="width: 25px; height: 25px"
            src="../../../../assets/img/imgScreen/logo.png"
          />
          计划信息表
        </div>
        <div>
          <el-form label-width="80px" :inline="true">
            <!-- <el-form-item label="批号">
              <el-input
                v-model="baseData.rollNo"
                style="width: 100%"
                size="small"
              ></el-input>
            </el-form-item> -->
            <el-form-item label="生成数量">
              <el-input
                v-model="entrustNum"
                style="width: 120px"
                type="number"
                size="small"
              ></el-input>
            </el-form-item>
            <!-- <el-form-item label="自动获取批次号">
              <el-input
                v-model="isAutoBatchNo"
                style="width: 100%"
                type="number"
                size="small"
              ></el-input>
              <el-select v-model="isAutoBatchNo" size="mini" style="width: 120px">
                <el-option v-for="item in autoList" :key="item.id" :value="item.id" :label="item.val"></el-option>
              </el-select>
            </el-form-item> -->
          </el-form>
        </div>
      </div>
      <el-table
        id="excelTable"
        v-loading="dataLoading"
        ref="dataTable"
        border
        :height="height_top"
        highlight-current-row
        :data="tableData"
        :icore-filter-flag="icoreFilterFlag"
        :header-cell-style="tableHeaderCellStyle"
        @selection-change="searchFollow"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column
          type="index"
          label="NO"
          align="center"
        ></el-table-column>
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="100px"
          prop="id"
          label="编号"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="80px"
          prop="planYear"
          label="计划年份"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="140px"
          prop="planCompanyName"
          label="计划单位"
          align="center"
        ></el-table-column
        ><!-- companyName -->
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="140px"
          prop="planDeptName"
          label="计划车间"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="80px"
          prop="sampleTypeName"
          label="样品类型"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="120px"
          prop="matName"
          label="物料名称"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="120px"
          prop="gradeName"
          label="牌号"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="120px"
          prop="matShapeName"
          label="试样形状"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="120px"
          prop="smpDeliName"
          label="送样频次"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="120px"
          prop="companyName"
          label="检验单位"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="120px"
          prop="deptName"
          label="检验车间"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="140px"
          prop="sectionName"
          label="检验班组"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="120px"
          prop="smpCompanyName"
          label="采样单位"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="120px"
          prop="smpDeptName"
          label="采样车间"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="140px"
          prop="smpSectionName"
          label="采样班组"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="120px"
          prop="testTypeName"
          label="检验类型"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="80px"
          prop="planDate1"
          label="计划日期"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="100px"
          prop="testTime1"
          label="测试起始时间"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="100px"
          prop="durTime"
          label="间隔时间（h）"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="120px"
          prop="pointName"
          label="取样地点"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="100px"
          prop="testLimit"
          label="测试时限(h)"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="80px"
          prop="isSmp1"
          label="是否取样"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="80px"
          prop="memo"
          label="备注"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="95px"
          prop="createMan"
          label="创建人"
          align="center"
        ></el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          sortable
          min-width="160px"
          prop="createTime"
          label="创建时间"
          align="center"
        ></el-table-column>
      </el-table>
      <el-pagination
        @size-change="pageSizeChange"
        @current-change="pageCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[10, 20, 50, 100, 200]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.totalPage"
        background
      >
      </el-pagination>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="goon"
          size="mini"
          icon="el-icon-check"
          :loading="addLoading"
          @click="addData()"
          >新增</el-button
        >
        <el-button
          type="goon"
          size="mini"
          icon="el-icon-check"
          :loading="addLoading"
          @click="batchAddData()"
          >批量新增</el-button
        >
        <el-button
          size="mini"
          type="goon"
          icon="el-icon-remove-outline"
          @click="cancel"
          >取消</el-button
        >
      </span>
    </el-dialog>
  </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 {
  props: ["showFlag", "Params"],
  data() {
    return {
      dialogTableVisible: false,
      tableTypeDsc: "新增检验委托信息",
      tableType: 0,
      companyNameType: [],
      sectionNameType: [],
      entrOrgNameType: [],
      tableData: [],
      height_top: 0,
      addLoading: false,
      page: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0,
      },
      dataLoading: false,
      saveRow: [],
      icoreFilterFlag: true,
      baseData: { planDeptNo: "" },
      batchAddLoading: false,
      entrustNum: 0,
      isAutoBatchNo: "0",
      autoList: [
        {
          id: "1",
          val: "是",
        },
        {
          id: "0",
          val: "否",
        },
      ],
    };
  },
  created() {
    this.height_top =
      window.innerHeight / 2 - 130 < 249 ? 249 : window.innerHeight / 2 - 130;
  },
  mounted() {
    //获取公司
    this.axios
      .post(
        "pass/baseManagement/v1/sysorgs/querySysOrgCompanyAll",
        {},
        { individualType: "json" }
      )
      .then((response) => {
        this.entrOrgNameType = this.companyNameType = response.data;
      });
  },
  methods: {
    getCode(code, arr, codeKey, nameKey) {
      if (!code || !arr) {
        return "";
      }
      let targetRow = arr.find((item) => {
        return item[codeKey] === code;
      });
      // console.log(targetRow, 'targetRow');
      return targetRow ? targetRow[nameKey] : "";
    },
    searchFollow(row) {
      this.saveRow = row;
    },
    pageSizeChange(size) {
      this.page.pageSize = size;
      this.mainData();
    },
    pageCurrentChange(index) {
      this.page.pageNum = index;
      this.mainData();
    },
    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;
    },
    cancel() {
      this.baseData = { planDeptNo: "", matNo: "" };
      this.dialogTableVisible = false;
      this.addLoading = false;
    },
    //根据选中的公司，查询部门名称数据
    selectDepartment(val) {
      this.sectionNameType = [];
      this.companyNameType.find((item) => {
        if (val === item.orgCode) {
          this.sectionNameType = item.sysOrgList;
          this.baseData.clearFlag ? "" : (this.baseData.planDeptNo = "");
        }
      });
    },
    searchData() {
      this.page.pageNum = 1;
      this.page.pageSize = 50;
      this.mainData();
    },
    mainData() {
      this.saveRow = {};
      this.dataLoading = true;
      let obj = {
        list: [1],
        object: {
          planCompanyNo: this.baseData.planCompanyNo,
          planDeptNo: this.baseData.planDeptNo,
          sampleTypeCode: "480103",
          matName: this.baseData.matName,
          validFlag: 1,
          planTypeCode: "",
        },
        pageIndex: this.page.pageNum,
        pageSize: this.page.pageSize,
      };
      this.axios
        .post(
          "pass/testManagement/v1/limstestplans/queryLimsTestPlanPage",
          obj,
          { individualType: "json" }
        )
        .then((res) => {
          this.dataLoading = false;
          this.tableData = res.data.list;
          this.tableData.forEach((item) => {
            item.isSmp1 =
              item.isSmp === "0" ? "否" : item.isSmp === "1" ? "是" : "";
            item.planDate1 = formatDate(item.planDate, "MM-dd");
            item.testTime1 = formatDate(item.testTime, "HH:mm:ss");
          });
          this.page.totalPage = res.data.total;
        })
        .catch((e) => {
          this.dataLoading = false;
        });
    },
    addData() {
      if (this.saveRow.length != 1) {
        this.$message.warning("请先勾选一条数据");
        return;
      }
      this.planId = this.saveRow[0].planId;
      this.addLoading = true;
      this.axios
        .post(
          "pass/testManagement/v1/limstestentrusts/addLimsTestEntrust",
          { planId: this.planId, rollNo: this.baseData.rollNo },
          { individualType: "json" }
        )
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.dialogTableVisible = false;
            this.$message.success("添加成功");
            this.$emit("refresh");
          } else {
            this.$message.error(res.message);
          }
          this.addLoading = false;
        })
        .catch((e) => {
          this.addLoading = false;
          this.$message.error(e.message);
        });
    },
    // 批量新增
    batchAddData() {
      if (this.saveRow.length != 1) {
        this.$message.warning("请先勾选一条数据");
        return;
      }
      // console.log(this.saveRow, '要进行批量新增的数据模板')
      // console.log()
      this.planId = this.saveRow[0].planId;
      this.batchAddLoading = true;
      this.axios
        .post(
          "pass/testManagement/v1/limstestentrusts/batchAddLimsTestEntrust",
          {
            entrustNum: this.entrustNum,
            limsTestEntrust: { planId: this.saveRow[0].planId },
            isAutoBatchNo: this.isAutoBatchNo,
          },
          { individualType: "json" }
        )
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.dialogTableVisible = false;
            this.$message.success("批量新增成功");
            this.$emit("refresh");
          } else {
            this.$message.error(res.message);
          }
          this.batchAddLoading = false;
        })
        .catch((e) => {
          this.batchAddLoading = false;
          this.$message.error(e.message);
        });
    },
    closeDialog() {
      this.addLoading = false;
    },
    tableHeaderCellStyle() {
      return "background-color: rgb(249,249,249) !important;color: rgb(73, 72, 72);border-bottom: 1px solid #DCDFE7;border-top:3px solid #3ac1ea;";
    },
  },
  watch: {
    showFlag(arg) {
      let _this = this;
      _this.dialogTableVisible = true;
      if (_this.Params !== null) {
        _this.baseData = Object.assign({ clearFlag: true }, this.Params);
        this.selectDepartment(this.baseData.planCompanyNo);
        // this.selctMatName(this.baseData.planDeptNo);
        this.baseData.clearFlag = false;
        _this.tableType = 1;
        _this.tableTypeDsc = "修改检验委托信息";
      } else {
        _this.tableType = 0;
        _this.seenNo = false;
        _this.tableTypeDsc = "新增检验委托信息";
      }
    },
  },
};
</script>
<style lang="less">
.customWidth_syst {
  width: 600px !important;
}
</style>
