<!-- 基础数据 - 物料信息配置 -->
<template>
  <div class="examination">
    <div class="common-head-search">
      <el-form :inline="true" @submit.native.prevent>
        <el-form-item label="物料名称">
          <el-input
            v-model="search.matName"
            @keyup.enter.native="getBaseDataList()"
            size="small"
            placeholder="请输入查询内容"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="goon"
            size="mini"
            @click="getBaseDataList"
            >查询</el-button
          >
          <el-button
            icon="el-icon-refresh"
            type="info"
            plain
            size="mini"
            @click="reset"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-row>
      <el-col :span="7">
        <div
          style="
            padding-left: 30px;
            padding-top: 20px;
            border-right: 1px solid rgb(233, 231, 231);
            border-bottom: 1px solid rgb(233, 231, 231);
            height: 710px;
            min-width: 300px;
          "
        >
          <div class="common-title-name">
            <img
              style="width: 25px; height: 25px"
              src="../../../../assets/img/imgScreen/logo.png"
            />
            导航树
          </div>
          <el-tree
            v-loading="dataLoading"
            ref="baseDataTree"
            :data="baseDataTree"
            highlight-current
            node-key="id"
            @node-click="nodeClick"
          >
          </el-tree>
        </div>
      </el-col>
      <el-col :span="17">
        <el-row>
          <div class="common-title-div">
            <div class="common-title-name">
              <img
                style="width: 25px; height: 25px"
                src="../../../../assets/img/imgScreen/logo.png"
              />
              物料信息
            </div>
          </div>
          <div>
            <el-table
              v-loading="dataLoading"
              ref="dataTable"
              border
              :height="theight1"
              highlight-current-row
              @selection-change="handleSelectionChange"
              :data="table.list"
              :icore-filter-flag="icoreFilterFlag"
              :header-cell-style="tableHeaderCellStyle"
              :row-class-name="tableRowClassName"
              :summary-method="getSummaries"
              show-summary
              @current-change="handleCurrentChange"
            >
              <el-table-column
                type="index"
                label="NO"
                fixed="left"
                align="center"
              >
              </el-table-column>
              <el-table-column
                type="selection"
                width="55"
                align="center"
                fixed="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="100px"
                prop="matNo"
                label="物料编号"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="140px"
                prop="matName"
                label="物料名称"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="120px"
                prop="matClassName"
                label="分类名称 "
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="100px"
                prop="matOrder"
                label="物料顺序"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="90px"
                prop="validFlag1"
                label="数据状态"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="90px"
                prop="matEngAbbr"
                label="查询标识"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="90px"
                prop="isQuotaName"
                label="是否控制"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="90px"
                prop="isMaterName"
                label="是否计量"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="90px"
                prop="isTestName"
                label="是否测控"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="95px"
                prop="memo"
                label="备注"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="95px"
                prop="createUsername"
                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="updateUsername"
                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>
          </div>
          <div class="common-foot-style">
            <el-pagination
              v-loading="dataLoading2"
              @size-change="pageSizeChange"
              @current-change="pageCurrentChange"
              :current-page.sync="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>
          </div>
        </el-row>
        <el-row>
          <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-circle-plus-outline"
                type="goon"
                size="mini"
                @click="initTemplate"
                >初始化模版</el-button
              >
              <el-button
                icon="el-icon-circle-plus-outline"
                type="goon"
                size="mini"
                @click="addData2"
                >新增</el-button
              >
              <el-button
                icon="el-icon-edit"
                type="goon"
                size="mini"
                @click="modifyData2"
                >修改</el-button
              >
              <el-button
                icon="el-icon-circle-close"
                type="danger"
                @click="deleteData2"
                size="mini"
                >删除</el-button
              >
            </div>
          </div>
          <div>
            <el-table
              v-loading="dataLoading"
              ref="dataTable2"
              border
              :height="theight2"
              highlight-current-row
              @selection-change="handleSelectionChange2"
              :data="table.list2"
              :icore-filter-flag="icoreFilterFlag"
              :header-cell-style="tableHeaderCellStyle"
              :row-class-name="tableRowClassName2"
              :summary-method="getSummaries"
              show-summary
            >
              <el-table-column
                type="index"
                label="NO"
                fixed="left"
                align="center"
              >
              </el-table-column>
              <el-table-column
                type="selection"
                width="55"
                align="center"
                fixed="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="160px"
                prop="fieldKeyName"
                label="字段名称"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="160px"
                prop="fieldValue"
                label="字段默认值"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="160px"
                prop="printType"
                label="字段类型"
                align="center"
              >
                <template slot-scope="scope">
                  <span>{{
                    scope.row.printType === 1 ? "自定义" : "系统带出"
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="160px"
                prop="isCheck"
                label="是否勾选"
                align="center"
              >
                <template slot-scope="scope">
                  <span>{{ scope.row.isCheck === 1 ? "是" : "否" }}</span>
                </template>
              </el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="95px"
                prop="createUsername"
                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="updateUsername"
                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>
          </div>
          <div class="common-foot-style">
            <el-pagination
              v-loading="dataLoading2"
              @size-change="pageSizeChange2"
              @current-change="pageCurrentChange2"
              :current-page="page2.pageNum"
              :page-sizes="[10, 20, 50, 100, 200]"
              :page-size="page2.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page2.totalPage"
              background
            >
            </el-pagination>
          </div>
        </el-row>
      </el-col>
    </el-row>
    <div
      is="alertComponets2"
      :showFlag2="showFlag2"
      :Params="Params"
      :Params2="Params2"
      @refresh="refresh2"
    ></div>
  </div>
</template>
<script>
import { getCookie } from "@/utils/util.js";
import { cookieUserId, cookieUserName } from "@/config/config.js";
import alertComponets2 from "./alertComponents2";
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  components: {
    alertComponets2,
  },
  data() {
    return {
      baseDataTree: [],
      table: {
        list: [],
        list2: [],
      },
      dataLoading: false,
      dataLoading2: false,
      icoreFilterFlag: true,
      page: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0,
      },
      page2: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0,
      },
      matNo: "",
      gradeNo: "",
      showFlag: false,
      Params: {},
      showFlag2: false,
      selectBaseCodeNo: "",
      Params2: {},
      treeItem: {},
      multipleSelection: [],
      multipleSelection2: [],
      search: {
        matName: "",
      },
      theight1: 0,
      theight2: 0,
    };
  },
  created() {
    this.theight1 =
      window.innerHeight / 2 - 130 < 249 ? 249 : window.innerHeight / 2 - 130;
    this.theight2 =
      window.innerHeight / 2 - 130 < 249 ? 249 : window.innerHeight / 2 - 130;
  },
  mounted() {
    this.getDataList2();
    this.getBaseDataList2();
    this.addScreen();
    this.addScreen2();
    this.treeItem = {};
  },
  methods: {
    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;
    },
    refresh() {
      this.selectBaseCodeNo = "";
      this.getBaseDataList2();
      this.getBaseDataList();
      this.getDataList2(this.matNo);
      this.treeItem = {};
    },
    refresh2() {
      this.getDataList2(this.matNo);
      // this.getBaseDataList();
    },
    addData2() {
      if (this.multipleSelection.length !== 1) {
        this.$message.warning("请选择一条物料信息数据！");
      } else {
        this.showFlag2 = !this.showFlag2;
        this.Params2 = null;
        this.Params = this.multipleSelection;
      }
    },
    modifyData2() {
      if (this.multipleSelection2.length !== 1) {
        this.$message.warning("请勾选您要修改的一条数据！");
      } else {
        this.showFlag2 = !this.showFlag2;
        this.Params2 = this.multipleSelection2;
        this.Params2[0].matName = this.matName;
        this.Params = null;
      }
    },
    initTemplate() {
      if (this.multipleSelection.length !== 1) {
        this.$message.warning("请选择一条物料信息数据！！");
        return;
      }
      this.$confirm("是否初始化模版?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        state: "warning",
      }).then(async () => {
        this.axios
          .request({
            url: `pass/testManagement/v1/limssampleprinttemplates/initData`,
            method: "get",
            params: {
              matClassNo: this.multipleSelection[0].matClassNo,
              matNo: this.multipleSelection[0].matNo,
            },
          })
          .then((res) => {
            if (res.succeed) {
              this.$message.success("操作成功");
              this.refresh2();
            } else {
              this.$message.error(res.message);
            }
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      });
    },
    deleteData2() {
      if (this.multipleSelection2.length < 1) {
        this.$message.warning("请选择您要删除的数据！");
      } else {
        this.$confirm("是否删除已选数据?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          state: "warning",
        }).then(async () => {
          const requestList = [];
          for (let i = 0; i < this.multipleSelection2.length; i++) {
            requestList.push(
              this.axios
                .delete(
                  `pass/testManagement/v1/limssampleprinttemplates/${this.multipleSelection2[i].id}`
                )
                .then((res) => {
                  if (res.succeed) {
                    this.$message.success("操作成功");
                  } else {
                    this.$message.error(res.message);
                  }
                })
                .catch((e) => {
                  this.$message.error(e.message);
                })
            );
          }
          Promise.all(requestList).finally(() => {
            this.getDataList2(this.matNo);
          });
        });
      }
    },
    reset() {
      this.search.matName = "";
      this.getBaseDataList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      if (val.length > 0) {
        this.matNo = val[val.length - 1].matNo;
        this.matName = val[val.length - 1].matName;
        this.getDataList2(this.matNo);
      }
    },
    handleSelectionChange2(val) {
      this.multipleSelection2 = val;
    },
    pageSizeChange(size) {
      this.page.pageSize = size;
      this.getBaseDataList();
    },
    pageCurrentChange(index) {
      this.page.pageNum = index;
      this.getBaseDataList();
    },
    pageSizeChange2(size) {
      this.page2.pageSize = size;
      this.getDataList2(this.matNo);
    },
    pageCurrentChange2(index) {
      this.page2.pageNum = index;
      this.getDataList2(this.matNo);
    },
    handleCurrentChange(val) {
      if (val === null || val.matNo === null) {
        this.matNo = "";
        this.matName = "";
      } else {
        this.matNo = val.matNo;
        this.matName = val.matName;
      }
      this.getDataList2(this.matNo);
    },
    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);
      });
    },
    getDataList2(matNo) {
      this.dataLoading2 = true;
      if (matNo === null || matNo === "") {
        this.table.list2 = [];
        this.dataLoading2 = false;
        return 0;
      } else {
        this.axios
          .get(
            "pass/testManagement/v1/limssampleprinttemplates/" +
              "?pageNum=" +
              this.page2.pageNum +
              "&pageSize=" +
              this.page2.pageSize +
              "&matNo=" +
              matNo
          )
          .then((res) => {
            this.dataLoading2 = false;
            this.table.list2 = res.data.list;
            for (let i = 0; i < this.table.list2.length; i++) {
              if (this.table.list2[i].validFlag === "0") {
                this.table.list2[i].validFlag1 = "无效";
              } else if (this.table.list2[i].validFlag === "1") {
                this.table.list2[i].validFlag1 = "有效";
              }
            }
            this.page2.totalPage = res.data.total;
          })
          .catch((e) => {
            this.dataLoading2 = false;
          });
      }
    },
    tableRowClassName({ row, column, rowIndex, columnIndex }) {
      if (row.validFlag !== "1") {
        return "error-row";
      }
      return "";
    },
    tableRowClassName2({ row, column, rowIndex, columnIndex }) {
      return "";
    },
    nodeClick(item) {
      this.selectBaseCodeNo = item.matClassNo ? item.matClassNo : "";
      this.treeItem = item;
      this.getBaseDataList();
    },
    getBaseDataList() {
      // 点击树的时候获得物料信息数据
      this.dataLoading = true;
      this.axios
        .get(
          "pass/baseManagement/v1/limsbasematerialss/" +
            "like/?pageNum=" +
            this.page.pageNum +
            "&pageSize=" +
            this.page.pageSize +
            "&matClassNo=" +
            this.selectBaseCodeNo +
            "&matName=" +
            encodeURI(this.search.matName)
        )
        .then((res) => {
          this.dataLoading = false;
          this.table.list = res.data.list;
          this.table.list.forEach((item) => {
            item.validFlag1 =
              item.validFlag === "0"
                ? "无效"
                : item.validFlag === "1"
                ? "有效"
                : "";
            item.isBatch1 =
              item.isBatch === "0" ? "否" : item.isBatch === "1" ? "是" : "";
            item.isQuotaName =
              item.isQuota === "0" ? "否" : item.isQuota === "1" ? "是" : "";
            item.isMaterName =
              item.isMater === "0" ? "否" : item.isMater === "1" ? "是" : "";
            item.isTestName =
              item.isTest === "0" ? "否" : item.isTest === "1" ? "是" : "";
          });
          this.page.totalPage = res.data.total;
        })
        .catch((e) => {
          this.dataLoading = false;
        });
    },
    getBaseDataList2() {
      // 获得树类数据
      this.axios
        .get("pass/baseManagement/v1/limsbasematerialsclasss/" + "?matClassNo=")
        .then((res) => {
          this.initTree(res.data.list);
        });
    },
    initTree(data) {
      var list = [];
      var parentList = [];
      for (var i = 0; i < data.length; i++) {
        data[i].children = [];
        data[i].label = data[i].matClassName;
        if (data[i].parentNo === "" || data[i].parentNo === null) {
          parentList.push(data[i]);
        } else {
          list.push(data[i]);
        }
      }
      this.getChildren(parentList, list);
      this.baseDataTree = parentList;
    },
    getChildren(parentList, list) {
      for (var i = 0; i < parentList.length; i++) {
        var childrenList = [];
        for (var j = 0; j < list.length; j++) {
          if (parentList[i].matClassNo === list[j].parentNo) {
            parentList[i].children.push(list[j]);
            childrenList.push(list[j]);
          }
        }
        if (childrenList.length > 0) {
          this.getChildren(childrenList, list);
        }
      }
    },
    tableHeaderCellStyle() {
      return "color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;";
    },
  },
};
</script>
<style lang="less">
.el-tree {
  overflow-x: hidden !important;
}

.el-tree-node {
  .el-tree-node__label {
    border-bottom: 0;
  }
}
</style>
