<template>
  <!-- 学习路径图! -->
  <div class="learningpathmap">
    <!-- 以下为顶部搜索框 -->
    <div class="filter">
      <div class="select">
        <a-cascader
          change-on-select
          :options="option"
          v-model:value="orgId"
          style="width: 100%"
          placeholder="请选择归属组织"
          :allowClear = false
          :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="routerName"
          allowClear
          showSearch
        >
        </a-input>
      </div>
      <div class="select">
        <a-input
          style="width: 100%; height: 40px; border-radius: 8px"
          placeholder="请输入创建人"
          allowClear
          v-model:value="createName"
          showSearch
        >
        </a-input>
      </div>
      <div class="select">
        <a-range-picker
          v-model:value="publishTime"
          type="date"
          valueFormat="YYYY-MM-DD"
          :placeholder="['发布开始时间','结束时间']"
          style="width: 100%; margin-right: 0px"
        />
      </div>
      <div style="display: flex; margin-bottom: 20px">
        <div class="btnzx btnzx1" @click="searchData">
          <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">
        <div>
          <img src="../../assets/images/coursewareManage/export1.png" alt="" />
        </div>
        <div class="btnzText">导出列表信息</div>
      </div>
      <div class="btnz btnz3" @click="exportbtnz" style="margin-left: 20px">
        <div>
          <img src="../../assets/images/coursewareManage/export1.png" alt="" />
        </div>
        <div class="btnzText">导出详细信息</div>
      </div>
    </div>
    <!-- 以下为table表格 -->
    <div class="tableBox">
      <a-table
        rowKey="boeRouterInfoId"
        :columns="columns"
        :data-source="tableData"
        :loading="tableLoading"
        :scroll="{ x: 700 }"
        :pagination="false"
        :row-selection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
        }"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'publishTime'">
            <a-tooltip placement="topRight">
              <template #title>
                <span>{{
                  record.publishTime &&
                  dayjs(record.publishTime).format("YYYY-MM-DD HH:mm:ss")
                }}</span>
              </template>
              <span>{{
                record.publishTime &&
                dayjs(record.publishTime).format("YYYY-MM-DD HH:mm:ss")
              }}</span>
            </a-tooltip>
          </template>
          <template v-if="column.key === 'status'">
            <span>{{
              record.status == "0"
                ? "草稿"
                : record.status == "1"
                ? "已发布"
                : record.status == "-1"
                ? "停用"
                : ""
            }}</span>
          </template>
        </template>
      </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 { message } from "ant-design-vue";
import * as api from "../../api/indexOvervoew";
// import * as api from "../../api/indexWay";
import { ref, toRefs, reactive, onMounted } from "vue";
import downLoad from "../../utils/downLoad";
import Cookies from "vue-cookies";
import axios from "axios";
import dayjs from "dayjs";
import { useStore } from "vuex";
export default {
  name: "LearningPathMap",
  setup() {
    const store = useStore();
    const state = reactive({
      tableLoading: false, // table加载图标
      tableDataTotal: 0, // 数据总条数
      pageSize: 10, // 每页条数
      pageNo: 1, //当前页码
      name: "", // 名称
      orgId: null, // 状态值
      routerName: "", // 路径图名称
      createName: "", // 创建者名称
      startTime: "", // 开始时间
      endTime: "", // 结束时间
      publishTime: "", // 创建时间
      option: [], // 组织列表
      boeRouterInfoName: "", // 路径名称
      selectedRowKeys: [], // 选中的列
      routerId: [],
      allowClear: true,
      resetOrgId: [],
      routerIdList: [],
    });
    // 表格数据
    let tableData = ref([]);
    // table选中
    const onSelectChange = (selectedRowKeys, record) => {
      state.selectedRowKeys = selectedRowKeys;
      state.routerId = record?.map((item) => {
        return item.boeRouterInfoId;
      });
    };
    const searchData=async()=>{
      state.pageNo=1
      state.pageSize=10
      getTableData()
    }
    //请求组织接口
    const getOrgList = async () => {
      //todo 获取用户角色列表，判断里面是否有system-admin
      // let roleList = store.state.userInfo.roleList;
      var manageFlag = false;
      for(let i=0;i<store.state.userInfo.roleList.length;i++){
        if(store.state.userInfo.roleList[i].roleCode=="system-admin"){
          manageFlag = true;
          break;
        }
      }
      const res = await api.userGetUserOrg();
      if (res) {
        // state.option = res.data?.result;
        // state.orgId = [state.option[0]?.organizationId];
        // state.resetOrgId = [state.option[0]?.organizationId];
        if(manageFlag){
          state.option = [{
            orgName:"全部",
            organizationId: null,
            childList:res.data?.result.orgTreeList
          }];
          state.orgId = [null,...res.data?.result.treeNodeList];
          state.resetOrgId = [null,...res.data?.result.treeNodeList];
        }else{
          if(res.data?.result.orgTreeList!=null){
            state.option = res.data?.result.orgTreeList;
            state.orgId = res.data?.result.treeNodeList;
            state.resetOrgId = res.data?.result.treeNodeList;
          }else{
            state.option = [{
              orgName:"无权限",
              organizationId: "1",
              childList:[],
            }];
            state.orgId = ["1"];
            state.resetOrgId = ["1"];
          }
        }
        state.allowClear = true;
        getTableData();
      }
    };

    // cloumns 表头
    const columns = ref([
      // {
      //   title: "编号",
      //   dataIndex: "num",
      //   key: "num",
      //   ellipsis: true,
      //   align: "center",
      //   width: 120,
      // },
      {
        title: "路径图名称",
        dataIndex: "boeRouterInfoName",
        ellipsis: true,
        key: "boeRouterInfoName",
        align: "center",
        width: 120,
      },
      {
        title: "归属组织",
        dataIndex: "organizationName",
        ellipsis: true,
        key: "organizationName",
        align: "center",
        width: 120,
      },
      {
        title: "关卡数",
        dataIndex: "levelTotal",
        ellipsis: true,
        key: "levelTotal",
        align: "center",
        width: 120,
      },
      {
        title: "任务数",
        dataIndex: "taskTotal",
        ellipsis: true,
        key: "taskTotal",
        align: "center",
        width: 120,
      },
      {
        title: "学习人数",
        dataIndex: "learnerNumber",
        ellipsis: true,
        key: "learnerNumber",
        align: "center",
        width: 120,
      },
      {
        title: "参与人数",
        dataIndex: "participantsTotal",
        ellipsis: true,
        key: "participantsTotal",
        align: "center",
        width: 120,
      },
      {
        title: "参与率",
        dataIndex: "participantsRate",
        ellipsis: true,
        key: "participantsRate",
        align: "center",
        width: 120,
      },
      {
        title: "完成人数",
        dataIndex: "completionNumber",
        ellipsis: true,
        key: "completionNumber",
        align: "center",
        width: 120,
      },
      {
        title: "完成率",
        dataIndex: "completedRate",
        ellipsis: true,
        key: "completedRate",
        align: "center",
        width: 120,
      },
      {
        title: "发布时间",
        dataIndex: "publishTime",
        ellipsis: true,
        key: "publishTime",
        align: "center",
        width: 120,
      },
      {
        title: "路径状态",
        dataIndex: "status",
        ellipsis: true,
        key: "status",
        align: "center",
        width: 120,
      },
      {
        title: "创建人",
        dataIndex: "createName",
        ellipsis: true,
        key: "createName",
        align: "center",
        width: 120,
      },
      {
        title: "操作",
        dataIndex: "operation",
        key: "operation",
        width: 200,
        align: "center",
        fixed: "right",
        customRender: (record) => {
          return (
            <div>
              <a
                key="export"
                onClick={() => {
                  oneExport(record);
                }}
              >
                导出历史记录
              </a>
              &nbsp;&nbsp;
              <a
                key="export"
                onClick={() => {
                  oneCurrentExport(record);
                }}
              >
                导出当前数据
              </a>
            </div>
          );
        },
      },
    ]);
    // 导出历史记录
    const oneExport = (record) => {
      axios({
        method: "post",
        url: "/report/boeu/router/exportHistoryRecord",
        data: { routerId: `${record.record.boeRouterInfoId}` },
        responseType: "blob",
        headers: {
          token: Cookies.get("token"),
        },
      }).then((res) => {
        downLoad(res.data, "路径图历史学习数据.xlsx");
      });
    };
    // 导出当前数据
    const oneCurrentExport = (record) => {
      axios({
        method: "post",
        url: "/report/boeu/router/exportCurrentRecord",
        data: { routerId: `${record.record.boeRouterInfoId}` },
        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.boeuRouterPlatePageList({
        page: state.pageNo,
        size: state.pageSize,
        orgId: state.orgId ? state.orgId[state.orgId.length - 1] : null,
        routerName: state.routerName,
        createName: state.createName,
        startTime: typeof state.publishTime[0] === 'undefined' ? null : state.publishTime[0] + " 00:00:01",
        endTime: typeof state.publishTime[1] == 'undefined' ? null : state.publishTime[1] + " 23:59:59",
        // publishTime: state.publishTime,
        name: state.name,
      });
      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.createName = "";
      state.publishTime = [];
      state.routerName = "";
      state.orgId = state.resetOrgId;
      state.selectedRowKeys = [];
      getTableData();
    };
    //导出详细信息
    const exportbtnz = async () => {
      if (state.selectedRowKeys?.length > 0) {
        axios({
          method: "post",
          url: "/report/boeu/router/exportCurrentRecord",
          data: { routerIdList: state.selectedRowKeys },
          responseType: "blob",
          headers: {
            token: Cookies.get("token"),
          },
        }).then((res) => {
          downLoad(res.data, "学习路径图详细信息.xlsx");
        }),
          (err) => {
            message.error(err);
          };
      } else {
        const params = {
          createName: state.createName,
          // publishTime: state.publishTime,
        startTime: typeof state.publishTime[0] === 'undefined' ? null : state.publishTime[0] + " 00:00:01",
        endTime: typeof state.publishTime[1] == 'undefined' ? null : state.publishTime[1] + " 23:59:59",
          routerName: state.routerName,
          orgId: state.orgId ? state.orgId[state.orgId.length - 1] : null,
        };
        axios({
          method: "post",
          url: "/report/boeu/router/exportCurrentRecord",
          data: params,
          responseType: "blob",
          headers: {
            token: Cookies.get("token"),
          },
        }).then((res) => {
          downLoad(res.data, "学习路径图详细信息.xlsx");
        }),
          (err) => {
            message.error(err);
          };
      }
    };
    // 导出列表信息
    const exportAllbtnz = async () => {
      if (state.selectedRowKeys?.length > 0) {
        axios({
          method: "post",
          url: "/report/boeu/router/exportList",
          data: { routerIdList: state.selectedRowKeys },
          responseType: "blob",
          headers: {
            token: Cookies.get("token"),
          },
        }).then((res) => {
          downLoad(res.data, "学习路径图列表信息.xlsx");
        }),
          (err) => {
            message.error(err);
          };
      } else {
        const params = {
          createName: state.createName,
          // publishTime: state.publishTime,
          startTime: typeof state.publishTime[0] === 'undefined' ? null : state.publishTime[0] + " 00:00:01",
          endTime: typeof state.publishTime[1] == 'undefined' ? null : state.publishTime[1] + " 23:59:59",
          routerName: state.routerName,
          orgId: state.orgId ? state.orgId[state.orgId.length - 1] : null,
        };
        axios({
          method: "post",
          url: "/report/boeu/router/exportList",
          data: params,
          responseType: "blob",
          headers: {
            token: Cookies.get("token"),
          },
        }).then((res) => {
          downLoad(res.data, "学习路径图信息.xlsx");
        }),
          (err) => {
            message.error(err);
          };
      }
    };
    onMounted(() => {
      getOrgList();
      state.tableLoading = true;
    });
    return {
      exportAllbtnz,
      onSelectChange,
      exportbtnz,
      reset,
      getTableData,
      ...toRefs(state),
      tableData,
      columns,
      changePagination,
      dayjs,
      searchData,
    };
  },
};
</script>
<style lang="scss">
.learningpathmap {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  .filter {
    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 {
      margin-right: 0px !important;
      background: #4ea6ff;
      .search {
        width: 16px;
        height: 18px;

        background-image: url("../../assets/images/courseManage/reset0.png");
      }
      .btnzText {
        color: white;
      }
    }

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

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

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

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

      .btnzText {
        color: #ffffff;
      }
    }
    .btnzx1:active,
    .btnzx2:active {
      background: #0982ff;
    }
  }
  .tabbtnz {
    width: 100%;
    height: 40px;
    display: flex;
    justify-content: space-between;
    .tab {
      margin-left: 38px;
      display: flex;
      background: #ecf5ff;
      height: 40px;
      line-height: 40px;
      border-radius: 8px;
      .tabItem {
        box-sizing: border-box;
        padding: 0 18px;
        cursor: pointer;
      }
      .tabActive {
        box-sizing: border-box;
        padding: 0 18px;
        background: #387df7;
        color: white;
        border-radius: 8px;
        cursor: pointer;
      }
    }
  }
  .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 {
    margin: 20px 38px 30px;

    .ant-table-thead > tr > th {
      background-color: #eff4fc;
    }
  }
  .tableBox {
    padding-bottom: 20px;

    .pa {
      width: 100%;
      display: flex;
      justify-content: center;
    }
  }
}
</style>
  