<!-- 样品权限配置 -->
<template>
  <div class="examination materialConfiguration-alertComponents3">
    <el-dialog
      :visible.sync="dialogTableVisible"
      customClass="customWidth_mate"
    >
      <div slot="title" class="titleBox">
        <i
          :class="tableType === 0 ? 'el-icon-document' : 'el-icon-edit-outline'"
          style="font-size: 20px"
        ></i>
        <span style="margin-left: 10px; font-size: 18px">{{
          tableTypeDsc
        }}</span>
      </div>
      <el-table
        ref="table2"
        :data="userList"
        highlight-current-row
        style="width: 100%"
        border
        :height="theight2"
        :header-cell-style="tableHeaderCellStyle"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="60" align="center">
        </el-table-column>
        <el-table-column
          property="userCode"
          label="账户"
          align="center"
        ></el-table-column>
        <el-table-column
          property="userName"
          label="姓名"
          align="center"
        ></el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="goon"
          size="mini"
          icon="el-icon-check"
          @click="tableType === 0 ? addData() : modifyData()"
          >确定</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", "addParams", "editParams"],
  data() {
    return {
      dialogTableVisible: false,
      tableTypeDsc: "",
      tableType: 0,
      theight1: 0,
      theight2: 0,
      list: [],
      userList: [],
      multipleSelection: [],
      currentRow: {},
    };
  },
  created() {
    this.theight1 =
      window.innerHeight - 185 < 249 ? 249 : window.innerHeight - 230;
    this.theight2 =
      window.innerHeight - 185 < 249 ? 249 : window.innerHeight - 400;
  },
  methods: {
    cancel() {
      this.dialogTableVisible = false;
    },
    renderHeader(h) {
      return (
        <div
          style="cursor: pointer ;line-height:20px"
          onClick={() => this.addRow()}
        >
          <i
            class="el-icon-plus"
            style="font-size:20px; color:rgb(18, 52, 243)"
          ></i>
        </div>
      );
    },
    tableHeaderCellStyle() {
      return "background-color: rgb(249,249,249) !important;color: rgb(73, 72, 72);border-bottom: 1px solid #DCDFE7;border-top:3px solid #3ac1ea;";
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    async getUserList() {
      this.userList = [];
      const res = await this.axios.get(
        "pass/v1/sysusers/getUsersByOrgCode/?pageNum=1&pageSize=200&companyId=713710108567277568",
        { individualType: "json" }
      );
      console.log(res);
      this.userList = res.data.list.filter((item) => {
        // 未停用的
        return item.status === "1";
      });
      if (this.tableType === 1) {
        console.log("this.userList");
        console.log(this.userList);
        this.$nextTick(() => {
          for (let i = 0; i < this.userList.length; i++) {
            if (this.userList[i].userCode === this.editParams.userNo) {
              this.$refs.table2.toggleRowSelection(this.userList[i], true);
              break;
            }
          }
        });
      }
    },
    addData() {
      console.log(this.multipleSelection);
      if (this.multipleSelection.length < 1) {
        this.$message.warning("请至少勾选您要修改的一条数据！");
        return;
      }
      if (this.multipleSelection.length === 1) {
        const formData = new FormData();
        formData.append("createMan", userName);
        formData.append("createNo", userId);
        formData.append(
          "createTime",
          formatDate(Date.now(), "yyyy-MM-dd" + " " + "HH:mm:ss")
        );
        formData.append("matName", this.addParams.matName);
        formData.append("matNo", this.addParams.matNo);
        formData.append("userNo", this.multipleSelection[0].userCode);
        formData.append("username", this.multipleSelection[0].userName);
        this.axios
          .post("pass/testManagement/v1/limsbasematerialsusers/", formData, {
            headers: {
              "Content-Type": "multipart/form-data",
            },
          })
          .then((res) => {
            this.$message.success("添加成功");
            this.$emit("refresh");
            this.dialogTableVisible = false;
          });
      } else if (this.multipleSelection.length > 1) {
        const data = [];
        this.multipleSelection.forEach((item) => {
          data.push({
            createMan: userName,
            createNo: userId,
            createTime: formatDate(Date.now(), "yyyy-MM-dd" + " " + "HH:mm:ss"),
            matName: this.addParams.matName,
            matNo: this.addParams.matNo,
            userNo: item.userCode,
            username: item.userName,
          });
        });
        this.axios
          .post("pass/testManagement/v1/limsbasematerialsusers/batch/", data, {
            individualType: "json",
          })
          .then((res) => {
            this.$message.success("添加成功");
            this.$emit("refresh");
            this.dialogTableVisible = false;
          });
      }
    },
    modifyData() {
      console.log(this.multipleSelection);
      console.log(this.editParams);
      if (this.multipleSelection.length !== 1) {
        this.$message.warning("请勾选您要修改的一条数据！");
        return;
      }
      this.axios
        .put(
          "pass/testManagement/v1/limsbasematerialsusers/" + this.editParams.id,
          {
            createMan: userName,
            createNo: userId,
            createTime: formatDate(Date.now(), "yyyy-MM-dd" + " " + "HH:mm:ss"),
            id: this.editParams.id,
            matName: this.editParams.matName,
            matNo: this.editParams.matNo,
            userNo: this.multipleSelection[0].userCode,
            username: this.multipleSelection[0].userName,
          }
        )
        .then((res) => {
          this.$message.success("修改成功");
          this.$emit("refresh");
          this.dialogTableVisible = false;
        });
    },
  },
  watch: {
    showFlag(arg) {
      this.dialogTableVisible = true;
      console.log("this.editParams");
      console.log(this.editParams);
      if (this.editParams !== null) {
        this.tableType = 1;
        this.tableTypeDsc = "修改样品权限配置";
      } else {
        this.tableType = 0;
        this.tableTypeDsc = "新增样品权限配置";
      }
      this.getUserList();
    },
  },
};
</script>
<style lang="less">
.customWidth_mate {
  width: 550px;
}
.materialConfiguration-alertComponents3 {
  .el-dialog {
    width: 35%;
  }
}
</style>
