<template>
  <!-- 员工学习数据页面 -->
  <div class="employeelearning">
    <!-- 以下为顶部搜索框 -->
    <div class="filterShow">
      <div class="select">
        <!-- <a-select
          style="width: 100%"
          placeholder="请选择组织"
          v-model:value="orgId"
          :options="option"
          allowClear
        ></a-select> -->
        <a-cascader
          change-on-select
          :options="option"
          v-model:value="orgId"
          style="width: 100%"
          placeholder="请选择归属组织"
          :allowClear="allowClear"
          :fieldNames="{
            label: 'orgName',
            value: 'organizationId',
            children: 'childList',
          }"
        >
        </a-cascader>
      </div>
      <div class="select">
        <a-input
          style="width: 100%; height: 40px; border-radius: 8px"
          placeholder="请输入姓名"
          v-model:value="name"
          allowClear
          showSearch
        >
        </a-input>
      </div>
      <div class="select">
        <a-input
          style="width: 100%; height: 40px; border-radius: 8px"
          placeholder="请输入工号"
          v-model:value="userNo"
          allowClear
          showSearch
        >
        </a-input>
      </div>
      <div class="select">
        <a-input
          style="width: 100%; height: 40px; border-radius: 8px"
          placeholder="请输入Band"
          v-model:value="band"
          allowClear
          showSearch
        >
        </a-input>
      </div>
      <div style="display: flex; margin-bottom: 20px">
        <div class="btnzx btnzx1" @click="getTableData">
          <div class="search"></div>
          <div class="btnzText">搜索</div>
        </div>
        <div class="btnz btnzx2" @click="reset">
          <div class="search"></div>
          <div class="btnzText">重置</div>
        </div>
      </div>
    </div>
    <!-- 以下为导出按钮 -->
    <div class="btnzs">
      <div class="btnz btnz3" @click="exportAllbtnz" style="margin-right: 20px">
        <div>
          <img src="../../assets/images/coursewareManage/export1.png" alt="" />
        </div>
        <div class="btnzText">导出全部</div>
      </div>
      <div class="btnz btnz3" @click="exportbtnz">
        <div>
          <img src="../../assets/images/coursewareManage/export1.png" alt="" />
        </div>
        <div class="btnzText">导出</div>
      </div>
    </div>
    <!-- 以下为table表格 -->
    <div class="tableBox">
      <a-table
        :columns="columns"
        :data-source="tableData"
        :loading="tableLoading"
        :scroll="{ x: 700 }"
        :pagination="false"
        :row-selection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
        }"
      >
      </a-table>
      <div class="tableBox">
        <div class="pa">
          <a-pagination
            v-if="tableDataTotal > 10"
            :showSizeChanger="false"
            showQuickJumper="true"
            hideOnSinglePage="true"
            :pageSize="pageSize"
            v-model:current="pageNo"
            :total="tableDataTotal"
            class="pagination"
            @change="changePagination"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { ref, toRefs, reactive, onMounted } from "vue";
import * as api from "../../api/indexOvervoew";
import { message } from "ant-design-vue";
import Cookies from "vue-cookies";
import axios from "axios";
import downLoad from "../../utils/downLoad";
export default {
  name: "EmployeelearninG",
  setup() {
    const state = reactive({
      tableLoading: false, // table加载图标
      tableDataTotal: 0, // 数据总条数
      pageSize: 10, // 每页条数
      pageNo: 1, //当前页码
      orgId: null, // 状态值
      name: "", // 名称
      band: "", // band
      userNo: "", //工号
      option: [], //组织列表
      selectedRowKeys: [], // 选中的列
      ids: [],
      allowClear:true,
      resetOrgId:[]
    });
    // table选中
    const onSelectChange = (selectedRowKeys, record) => {
      state.selectedRowKeys = selectedRowKeys;
      state.ids = record?.map((item) => {
        return item.basicBoeUserId;
      });
    };
  //请求组织接口
    const getOrgList = async () => {
      const res = await api.userGetUserOrg({});
      if (res) {
        state.option = res.data?.result;
        state.orgId = [state.option[0]?.organizationId];
        state.resetOrgId = [state.option[0]?.organizationId];
        res.data?.result?.userType === 1
          ? (state.allowClear = true)
          : (state.allowClear = false);
        getTableData();
      }
    };
    //导出
    const exportbtnz = async () => {
      if (!state.selectedRowKeys?.length) {
        return message.warning("请至少选择一条数据进行导出");
      } else {
        axios({
          method: "get",
          url: "/report/boeu/studyData/export",
          params: { ids: `${state.ids}` },
          responseType: "blob",
          headers: {
            token: Cookies.get("token"),
          },
        }).then((res) => {
          downLoad(res.data, "学习员工数据.xlsx");
        });
      }
    };
    // 表格数据
    let tableData = ref([]);
    // cloumns 表头
    const columns = ref([
      {
        title: "工号",
        dataIndex: "userNo",
        key: "userNo",
        width: 120,
        ellipsis: true,
        align: "center",
      },
      {
        title: "姓名",
        dataIndex: "userName",
        ellipsis: true,
        key: "userName",
        width: 120,
        align: "center",
      },
      {
        title: "所属组织",
        dataIndex: "orgName",
        ellipsis: true,
        key: "orgName",
        width: 120,
        align: "center",
      },
      {
        title: "岗位",
        dataIndex: "positionName",
        ellipsis: true,
        key: "positionName",
        width: 120,
        align: "center",
      },
      {
        title: "Band",
        dataIndex: "bandCode",
        ellipsis: true,
        key: "bandCode",
        width: 120,
        align: "center",
      },
      {
        title: "授课次数",
        dataIndex: "teachingTimes",
        ellipsis: true,
        key: "teachingTotal",
        width: 120,
        align: "center",
      },
      {
        title: "授课时长",
        dataIndex: "teachingDuration",
        ellipsis: true,
        key: "teachingDuration",
        width: 120,
        align: "center",
      },
      {
        title: "案例创建",
        dataIndex: "cases",
        ellipsis: true,
        key: "cases",
        width: 120,
        align: "center",
      },
      {
        title: "课程学习",
        dataIndex: "courseStudy",
        ellipsis: true,
        key: "courseStudy",
        width: 120,
        align: "center",
      },
      {
        title: "项目学习",
        dataIndex: "projectStudy",
        ellipsis: true,
        key: "projectStudy",
        width: 120,
        align: "center",
      },
      {
        title: "路径图学习",
        dataIndex: "routerStudy",
        ellipsis: true,
        key: "routerStudy",
        width: 120,
        align: "center",
      },
      {
        title: "学习时长",
        dataIndex: "learningDuration",
        ellipsis: true,
        key: "learningDuration",
        width: 120,
        align: "center",
      },
      {
        title: "操作",
        dataIndex: "operation",
        key: "operation",
        width: 150,
        align: "center",
        fixed: "right",
        customRender: (record) => {
          return (
            <a
              key="export"
              onClick={() => {
                oneExport(record);
              }}
            >
              导出详细信息
            </a>
          );
        },
      },
    ]);
    // 行内单条下载
    const oneExport = (record) => {
      axios({
        method: "get",
        url: "/report/boeu/studyData/export",
        params: { ids: `${record.record.basicBoeUserId}` },
        responseType: "blob",
        headers: {
          token: Cookies.get("token"),
        },
      }).then((res) => {
        downLoad(res.data, "学习员工数据.xlsx");
      });
    };
    //table 分页事件
    const changePagination = (page) => {
      state.pageNo = page;
      getTableData();
    };
    // 获取数据
    const getTableData = async () => {
      state.tableLoading = true;
      const res = await api.boeuStudyDataPageList({
        page: state.pageNo,
        size: state.pageSize,
        userNo: state.userNo,
        name: state.name,
        departmentId: state.orgId ? state.orgId[state.orgId.length - 1] : null,
        bandCode: state.band,
      });
      if (res) {
        state.tableDataTotal = res.data.result.total;
        const list = res.data.result.rows?.map((item) => {
          return {
            key: item.id,
            ...item,
          };
        });
        tableData.value = list;
        state.tableLoading = false;
      }
    };
    // 重置按钮
    const reset = async () => {
      state.tableLoading = true;
      state.name = "";
      state.band = "";
      state.userNo = "";
      state.orgId = state.resetOrgId;
      getTableData();
    };

    // 导出全部按钮
    const exportAllbtnz = async () => {
      axios({
        method: "post",
        url: "/report/boeu/studyData/exportAll",
        data: {
          userNo: state.userNo,
          name: state.name,
          departmentId: state.orgId ? state.orgId[state.orgId.length - 1] : null,
          bandCode: state.band,
        },
        responseType: "blob",
        headers: {
          token: Cookies.get("token"),
        },
      }).then((res) => {
        downLoad(res.data, "学习员工数据.xlsx");
      });
    };
    onMounted(() => {
      getOrgList();
      state.tableLoading = true;
    });
    return {
      exportAllbtnz,
      onSelectChange,
      exportbtnz,
      reset,
      getTableData,
      ...toRefs(state),
      tableData,
      columns,
      changePagination,
    };
  },
};
</script>
<style lang="scss">
.employeelearning {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  .filterShow {
    margin-left: 38px;
    margin-right: 20px;
    margin-top: 30px;
    display: flex;
    flex-wrap: wrap;
    .select {
      margin-right: 20px;
      margin-bottom: 20px;
      width: calc((100% - 76px - 240px) / 4);
    }

    .btnz {
      padding: 0px 26px 0px 26px;
      height: 38px;
      background: rgba(64, 158, 255, 0);
      border-radius: 8px;
      border: 1px solid rgba(64, 158, 255, 1);
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 14px;
      flex-shrink: 0;
      cursor: pointer;

      .search {
        background-size: 100%;
      }

      .btnzText {
        font-size: 14px;
        font-weight: 400;
        color: rgba(64, 158, 255, 1);
        line-height: 36px;
        margin-left: 5px;
      }
    }

    .btnzx {
      padding: 0px 26px 0px 26px;
      height: 38px;
      background: #4ea6ff;
      border-radius: 8px;
      border: 1px solid rgba(64, 158, 255, 1);
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 14px;
      flex-shrink: 0;
      cursor: pointer;

      .search {
        background-size: 100%;
      }

      .btnzText {
        font-size: 14px;
        font-weight: 400;
        color: #ffffff;
        line-height: 36px;
        margin-left: 5px;
      }
    }

    .btnzx1 {
      .search {
        width: 15px;
        height: 17px;
        background-image: url("../../assets/images/courseManage/search0.png");
      }
    }

    .btnzx2 {
      background: #4ea6ff;
      .search {
        width: 16px;
        height: 18px;
        background-image: url("../../assets/images/courseManage/reset0.png");
      }
      .btnzText {
        color: #ffffff;
      }
    }

    .btnzx1:hover {
      background: rgba(64, 158, 255, 0.76);

      .search {
        background-image: url("../../assets/images/courseManage/search0.png");
      }

      .btnzText {
        color: #ffffff;
      }
    }

    .btnzx1:active {
      background: #0982ff;
    }

    .btnzx2:hover {
      background: rgba(64, 158, 255, 0.76);
    }

    .btnzx2:active {
      background: #0982ff;
    }
  }
  .tableBox {
    margin: 20px 38px 30px;

    .ant-table-thead > tr > th {
      background-color: #eff4fc;
    }
  }
  .btnzs {
    display: flex;
    margin-left: 38px;
    // flex-wrap: wrap;
    .btnz {
      padding: 0px 26px 0px 26px;
      height: 38px;
      background: white;
      border-radius: 8px;
      border: 1px solid rgba(64, 158, 255, 1);
      display: flex;
      align-items: center;
      cursor: pointer;
      justify-content: center;
      margin-right: 14px;
      flex-shrink: 0;

      .search {
        background-size: 100%;
      }

      .btnzText {
        font-size: 14px;
        font-weight: 400;
        color: #4ea6ff;
        line-height: 36px;
        margin-left: 5px;
      }
    }

    .btnz3 {
      margin-right: 0px;
      background: #4ea6ff;
      .search {
        width: 17px;
        height: 18px;
        background-image: url("../../assets/images/courseManage/add0.png");
      }
      .btnzText {
        color: #ffffff;
      }
    }

    .btnz3:hover {
      // background: rgba(64, 158, 255, 0.76);
      background: rgba(64, 158, 255, 0.76);
    }

    .btnz3:active {
      background: #0982ff;
    }
  }

  .tableBox {
    padding-bottom: 20px;

    .pa {
      // position: absolute;
      // bottom: 20px;
      // left: 0;
      width: 100%;
      // height: 20px;
      // background-color: red;
      display: flex;
      justify-content: center;
      // margin-bottom: 10px;
      // position: absolute;
      // bottom: -40px;
    }
  }
}
</style>
