<template>
  <a-drawer
    :visible="ProjPvisible"
    class="drawerStyle ProjPowerList"
    placement="right"
    width="60%"
    @after-visible-change="afterVisibleChange"
  >
    <div class="drawerMain">
      <div class="header">
        <div class="headerTitle">授权名单</div>
        <img
          style="width: 29px; height: 29px; cursor: pointer"
          src="../../assets/images/basicinfo/close.png"
          @click="closeDrawer"
        />
      </div>
      <div class="main">
        <div class="search">
          <div class="leftchoose">
            <div class="namecon" style="margin-right: 30px">
              <div class="name">姓名：</div>
              <a-input
                v-model:value="name"
                style="width: 270px; height: 40px; border-radius: 8px"
                placeholder="请输入姓名"
              />
            </div>
          </div>

          <div class="btns">
            <div
              class="btn btn1"
              style="margin-right: 20px"
              @click="searchAuth"
            >
              <div class="img1"></div>
              <div class="wz">搜索</div>
            </div>
            <div class="btn btn2" @click="resetAuth">
              <div class="img2"></div>
              <div class="wz">重置</div>
            </div>
          </div>
        </div>

        <div class="tableBox" style="margin-top: 10px">
          <a-table
            style="border: 1px solid #f2f6fe"
            :columns="tableDataFunc()"
            :data-source="tableData"
            :loading="tableDataTotal === -1 ? true : false"
            expandRowByClick="true"
            @expand="expandTable"
            :scroll="{ x: 900 }"
            :pagination="false"
          />
        </div>
        <div class="tableBox">
          <div class="pa">
            <a-pagination
              v-if="tableDataTotal > 10"
              showSizeChanger="true"
              showQuickJumper="true"
              hideOnSinglePage="true"
              :pageSize="pageSize"
              :current="currentPage"
              :total="tableDataTotal"
              class="pagination"
              @change="changePagination"
            />
          </div>
        </div>

        <!-- <div class="tab" style="margin-top: 20px; margin-bottom: 100px">
          <a-table
            style="border: 1px solid #f2f6fe"
            :columns="tablecolumns"
            :data-source="tabledata"
            :loading="tableDataTotal === -1 ? true : false"
            expandRowByClick="true"
            :scroll="{ x: 900, y: 350 }"
            @expand="expandTable"
            :pagination="false"
            :row-selection="{
              columnWidth: 30,
              selectedRowKeys: selectedRowKeys,
              onChange: onSelectChange,
            }"
          />
        </div> -->
      </div>
    </div>
    <!-- 取消授权弹窗 -->
    <a-modal
      v-model:visible="cancelModal"
      :footer="null"
      :closable="closeCancel"
      wrapClassName="CopyModal"
      centered="true"
    >
      <div class="delete">
        <div class="del_header"></div>
        <div class="del_main">
          <div class="header">
            <div class="icon"></div>
            <span>提示</span>
            <div class="close_exit" @click="closeCancelModal"></div>
          </div>
          <div class="body">
            <span>您确定要取消该用户的授权吗</span>
          </div>
          <div class="del_btnbox">
            <div class="del_btn btn1" @click="closeCancelModal">
              <div class="btnText">取消</div>
            </div>
            <div class="del_btn btn2" @click="cancelAuth">
              <div class="btnText">确定</div>
            </div>
          </div>
        </div>
      </div>
    </a-modal>

    <div class="botm"></div>
    <div class="btnn">
      <button class="btn1" @click="closeDrawer">取消</button>
      <button class="btn2" @click="closeDrawer">确定</button>
    </div>
  </a-drawer>
</template>

<script>
import { toRefs, reactive } from "vue";
import * as api from "../../api/index1";
export default {
  name: "ProjPowerList",
  props: {
    ProjPvisible: {
      type: Boolean,
      default: false,
    },
    selectProjectId: {
      type: Number,
      default: null,
    },
    classify: {
      type: String,
      default: null,
    },
  },

  setup(props, ctx) {
    const state = reactive({
      name: null,
      showmodal: false, //勾选提示框
      closable: false, //modal右上角的关闭按钮
      pageSize: 10,
      currentPage: 1,
      tableDataTotal: -1,
      selectedRowKeys: [],
      cancelModal: false, //取消授权弹窗
      closeCancel: false, //取消授权弹窗关闭图标
      tableData: [
        // {
        //   key: 1,
        //   name: "张三",
        //   com: "产研部",
        //   gang: "产品经理",
        //   number: "20201234",
        //   state: "管理权",
        // },
      ],
      cancelAuthInfo: null, //取消授权id
    });

    const closeDrawer = () => {
      ctx.emit("update:ProjPvisible", false);
      state.currentPage = 1;
    };
    const afterVisibleChange = (bool) => {
      console.log("state", bool, props);
      if (bool) {
        optionAuthPerm();
      }
    };
    const onSelectChange = (selectedRowKeys) => {
      console.log("selectedRowKeys changed: ", selectedRowKeys);
      state.selectedRowKeys = selectedRowKeys;
    };
    const showCancelModal = (item) => {
      console.log("item", item);
      state.cancelModal = true;
      let obj = {
        id: item.id,
        name: item.name,
      };
      state.cancelAuthInfo = obj;
    };
    const closeCancelModal = () => {
      state.cancelModal = false;
    };

    const tableDataFunc = () => {
      const columns = [
        {
          title: "姓名",
          dataIndex: "name",
          // width: "30%",
          key: "name",
          width: 70,
          // align: "left",
          className: "classify",
          scopedSlots: { customRender: "action" }, //引入的插槽
          customRender: (text) => {
            // console.log(text.record.checked1);
            return (
              <div class="racona">
                <span> {text.record.name}</span>
              </div>
            );
          },
        },
        {
          title: "工号",
          dataIndex: "number",
          // width: "30%",
          key: "number",
          width: 100,
          align: "center",
          className: "h",
        },
        {
          title: "归属组织",
          dataIndex: "com",
          // width: "30%",
          key: "com",
          width: 100,
          align: "center",
          className: "h",
        },
        {
          title: "所在岗位",
          dataIndex: "gang",
          key: "gang",
          width: 100,
          align: "center",
          className: "h",
        },

        {
          title: "拥有权限",
          dataIndex: "state",
          key: "state",
          width: 100,
          align: "center",
          className: "h",
        },
        {
          title: "操作",
          className: "h",
          dataIndex: "opacation",
          key: "opacation",
          width: 100,
          align: "center",
          scopedSlots: { customRender: "action" }, //引入的插槽
          customRender: (text) => {
            return (
              <div
                class="opa"
                onClick={() => {
                  console.log("text.record.id", text.record.id);
                  showCancelModal(text.record);
                }}
              >
                取消授权
              </div>
            );
          },
        },
      ];
      return columns;
    };

    //‘获取权限名单
    const optionAuthPerm = () => {
      let obj = {
        keyWord: state.name ? state.name : "",
        type:
          props.classify === "learnPath"
            ? 1
            : props.classify === "project"
            ? 2
            : props.classify === "course"
            ? 3
            : null,
        tag: 1,
        opt: 1,
        refId: props.selectProjectId,
        pageNo: state.currentPage,
        pageSize: state.pageSize,
      };
      console.log("获取权限名单obj", obj);
      api
        .optionAuthPerm(obj)
        .then((res) => {
          // console.log("获取权限名单成功", res);
          if (res.data.code === 200) {
            console.log("获取权限名单成功", res.data.data.records);
            let arr = res.data.data.records;
            let array = [];
            arr.map((value) => {
              let obj = {
                key: value.memberId,
                id: value.memberId,

                name: value.memberName ? value.memberName : "-",

                com: value.orgName ? value.orgName : "-",
                gang: "-",
                number: value.memberNo ? value.memberNo : "-",
                state: value.tagName ? value.tagName : "-",
              };
              array.push(obj);
            });
            state.tableData = array;
            state.tableDataTotal = res.data.data.total;
          }
        })
        .catch((err) => {
          console.log("获取权限名单失败", err);
        });
    };

    //分页
    const changePagination = (page) => {
      state.currentPage = page;
      optionAuthPerm();
    };
    //查询
    const searchAuth = () => {
      state.currentPage = 1;
      optionAuthPerm();
    };
    //重置
    const resetAuth = () => {
      state.currentPage = 1;
      state.name = null;
      optionAuthPerm();
    };

    //取消授权
    const cancelAuth = () => {
      let obj = {
        keyWord: "",
        type:
          props.classify === "learnPath"
            ? 1
            : props.classify === "project"
            ? 2
            : props.classify === "course"
            ? 3
            : null,
        tag: 1,
        opt: 4,
        refId: props.selectProjectId,
        pageNo: 1,
        pageSize: 20,
        deptList: [],
        groupList: [],
        studentList: [state.cancelAuthInfo],
      };
      console.log("取消权限名单obj", obj);
      api
        .optionAuthPerm(obj)
        .then((res) => {
          console.log("取消权限名单", res);
          if (res.data.code === 200) {
            // console.log("取消授权成功", res.data.data);
            optionAuthPerm();
            closeCancelModal();
          }
        })
        .catch((err) => {
          console.log("取消授权失败", err);
        });
    };
    return {
      ...toRefs(state),
      closeDrawer,
      onSelectChange,
      tableDataFunc,
      showCancelModal,
      closeCancelModal,
      afterVisibleChange,
      changePagination,
      searchAuth,
      resetAuth,
      cancelAuth,
    };
  },
};
</script>

<style lang="scss">
.me {
  .ant-modal-body {
    padding: 0px;
  }
}

.ProjPowerList {
  // width: 80%;
  .ant-drawer-content-wrapper {
    // max-width: 1000px;
    .ant-drawer-header {
      display: none !important;
    }
    .ant-drawer-body {
      padding: 0;
    }
  }
  .drawerMain {
    min-width: 550px;
    margin: 0px 32px 0px 32px;
    // overflow-x: auto;
    display: flex;
    flex-direction: column;
    .header {
      height: 73px;
      border-bottom: 1px solid #e8e8e8;
      display: flex;
      justify-content: space-between;
      align-items: center;
      // background-color: red;
      margin-bottom: 20px;
      flex-shrink: 0;
      .headerTitle {
        font-size: 18px;
        font-weight: 600;
        color: #333333;
        line-height: 25px;
        // margin-left: 24px;
      }
    }
    .main {
      width: 100%;
      height: 100%;
      // background-color: #bfa;
      overflow-y: auto;
      .search {
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        margin-top: 10px;
        // justify-content: space-between;
        .leftchoose {
          display: flex;
          margin-right: 20px;
          .namecon {
            display: flex;
            flex-wrap: nowrap;
            margin-bottom: 10px;
            .name {
              margin-top: 8px;
              white-space: nowrap;
            }
            // .name {
            //   margin-top: 8px;

            //   color: rgba(0, 0, 0, 0.85);
            //   font-size: 14px;
            //   font-weight: 400;
            // }
          }
        }

        .btns {
          display: flex;
          flex-wrap: nowrap;

          .btn {
            cursor: pointer;
            width: 100px;
            height: 40px;
            border-radius: 8px;
            display: flex;
            justify-content: center;
            align-items: center;
            .img1 {
              width: 15px;
              height: 17px;
              background-image: url(../../assets/images/courseManage/search0.png);
              background-size: 100% 100%;
              margin-right: 7px;
            }
            .img2 {
              width: 16px;
              height: 18px;
              background-image: url(../../assets/images/courseManage/reset1.png);
              background-size: 100% 100%;
              margin-right: 7px;
            }
          }

          .btn1 {
            background: #388be1;

            color: #ffffff;
          }
          .btn2 {
            background: #ffffff;

            color: #388be1;
            border: 1px solid #388be1;
          }
        }
      }

      .tableBox {
        // margin-bottom: 80px;

        .classify {
          // margin-left: 11px !important;
          // padding-left: 9px !important;
          padding-left: 20px !important;
        }
        .ant-checkbox-wrapper {
          align-items: center;
          margin-top: -2px;
        }
        .ant-table-selection-column {
          padding: 0px !important;
          // padding-left: 45px !important;
        }
        .ant-table-thead > tr > th {
          background-color: rgba(239, 244, 252, 1);
        }
        th.h {
          background-color: #eff4fc !important;
        }

        .ant-table-tbody
          > tr:hover:not(.ant-table-expanded-row):not(.ant-table-row-selected)
          > td {
          background: #f6f9fd;
        }
        .opa {
          // background-color: #bfa;
          font-size: 14px;
          font-weight: 400;
          color: #388be1;
          cursor: pointer;
        }
      }
      .pa {
        // left: 0;
        margin-top: 25px;
        margin-bottom: 70px;
        width: 100%;
        // height: 20px;
        // background-color: red;
        display: flex;
        justify-content: center;
        // position: absolute;
        // bottom: 20px;
        .ant-pagination-item,
        .ant-pagination-prev,
        .ant-pagination-next,
        .ant-pagination-options {
          margin-bottom: 10px;
        }
      }
      // .tab {
      //   .ant-table-thead > tr > th {
      //     background-color: rgba(239, 244, 252, 1) !important;
      //   }

      //   th.h {
      //     background-color: #eff4fc !important;
      //   }

      //   .ant-table-tbody
      //     > tr:hover:not(.ant-table-expanded-row):not(.ant-table-row-selected)
      //     > td {
      //     background: #f6f9fd;
      //   }
      // }
    }
  }
  // .botm {
  //   width: 100%;
  //   height: 100%;
  //   background-color: red;
  //   // flex-shrink: 1;
  // }
  .btnn {
    height: 72px;
    width: 100%;
    position: absolute;
    background-color: #fff;
    bottom: 0;
    left: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0px 1px 35px 0px rgba(118, 136, 166, 0.16);
    .btn1 {
      width: 100px;
      height: 40px;
      border: 1px solid #4ea6ff;
      border-radius: 8px;
      color: #4ea6ff;
      background-color: #fff;
      cursor: pointer;
    }
    .btn2 {
      cursor: pointer;
      width: 100px;
      height: 40px;
      background: #4ea6ff;
      border-radius: 8px;
      border: 0;
      margin-left: 15px;
      color: #fff;
    }
  }
}
</style>
