<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"
            @keyup.enter.native="searchData"
            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">
            <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="baseData.carNo"
                style="width: 100%"
                size="small"
              ></el-input>
            </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
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="contractNo"
          label="合同号"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="suppName"
          label="供应商"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="matName"
          label="物料"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="testTypeName"
          label="检验类型"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="matOrigin"
          label="产地"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="transportTypeName"
          label="运输类型"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="acceptName"
          label="收货单位"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="sendPlace"
          label="发站"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="packTypeName"
          label="包装方式"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="isCiq1"
          label="是否商检"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="isBatch1"
          label="是否配批"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="smpDeliName"
          label="取样频次"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="unloadPlace"
          label="卸货地点"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="planCompanyName"
          label="计划单位"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="companyName"
          label="检验单位"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="deptName"
          label="检验车间"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="sectionName"
          label="检验班组"
          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="batchAddLoading"
          @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: ["showFlag3", "Params"],
  data() {
    return {
      dialogTableVisible: false,
      tableTypeDsc: "新增检验委托信息",
      tableType: 0,
      companyNameType: [],
      sectionNameType: [],
      entrOrgNameType: [],
      tableData: [],
      height_top: 0,
      addLoading: false,
      batchAddLoading: false,
      entrustNum: 0,
      page: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0,
      },
      dataLoading: false,
      saveRow: [],
      icoreFilterFlag: true,
      baseData: { planDeptNo: "" },
    };
  },
  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;
      this.$emit("refresh", 2);
    },
    //根据选中的公司，查询部门名称数据
    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 = {
        object: {
          planCompanyNo: this.baseData.planCompanyNo,
          planDeptNo: this.baseData.planDeptNo,
          sampleTypeCode: "480101",
          matName: this.baseData.matName,
          validFlag: 1,
          planTypeCode: "",
        },
        list: [1],
        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, carNo: this.baseData.carNo },
          { 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;
      }
      this.planId = this.saveRow[0].planId;
      this.batchAddLoading = true;
      //isAutoBatchNo 参数：是否自动获取批次号  entrustNum 数据生成的次数
      this.axios
        .post(
          "pass/testManagement/v1/limstestentrusts/batchAddLimsTestEntrust",
          {
            entrustNum: this.entrustNum,
            limsTestEntrust: { planId: this.saveRow[0].planId },
            isAutoBatchNo: "0",
          },
          { individualType: "json" }
        )
        .then((res) => {
          if (res.code == 0 || res.code == 200) {
            this.batchAddLoading = false;
            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;
      this.$emit("refresh", 2);
    },
    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: {
    showFlag3(arg) {
      let _this = this;
      if (arg) {
        _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>
