<template>
  <!-- 考试! -->
  <div class="examination">
    <!-- 以下为顶部搜索框 -->
    <div class="filter">
      <div class="select">
        <a-input
          style="width: 100%; height: 40px; border-radius: 8px"
          placeholder="请输入考试名称"
          allowClear
          showSearch
          v-model:value="testName"
        >
        </a-input>
      </div>
      <div class="select">
        <a-input
          style="width: 100%; height: 40px; border-radius: 8px"
          placeholder="请输入创建者"
          allowClear
          showSearch
          v-model:value="createName"
        >
        </a-input>
      </div>
      <div class="select">
        <a-cascader
          :options="option"
          placeholder="请选择组织"
          v-model:value="orgId"
          :allowClear="allowClear"
          style="width: 100%"
          :fieldNames="{
            label: 'name',
            value: 'id',
            children: 'treeChildList',
          }"
        >
        </a-cascader>
      </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="btnzx btnzx1" @click="reset">
          <div class="search"></div>
          <div class="btnzText">重置</div>
        </div>
      </div>
    </div>
    <!-- 以下为导出按钮 -->
    <div class="btnzs">
      <div class="btnz btnz3" @click="exportList">
        <div>
          <img src="../../assets/images/coursewareManage/export1.png" alt="" />
        </div>
        <div class="btnzText">导出</div>
      </div>
      <div class="btnz btnz3" style="margin-left: 20px" @click="exportDetils">
        <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"
        rowKey="idStr"
        :row-selection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
        }"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'published'">
            <span>{{
              record.published == "0"
                ? "未发布"
                : record.published == "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 { ref, toRefs, reactive, onMounted } from "vue";
import * as api from "../../api/indexProject";
// import dayjs from "dayjs";
import axios from "axios";
import Cookies from "vue-cookies";
import downLoad from "../../utils/downLoad";
import { message } from "ant-design-vue";
export default {
  name: "reportExamination",
  setup() {
    const state = reactive({
      tableLoading: false, // table加载图标
      tableDataTotal: 0, // 数据总条数
      pageSize: 10, // 每页条数
      pageNo: 1, //当前页码
      testName: "",
      createName: "",
      orgId: null,
      allowClear: false,
      option: [],
      selectedRowKeys: [], // 选中的列
      resetOrgId: [],
    });
    const reset = async () => {
      state.createName = "";
      state.testName = "";
      state.selectedRowKeys = [];
      state.orgId = state.resetOrgId;
      getTableData();
    };
    // table选中
    const onSelectChange = (selectedRowKeys) => {
      state.selectedRowKeys = selectedRowKeys;
    };
    const exportList = async () => {
      if (state.selectedRowKeys.length > 0) {
        axios({
          method: "post",
          url: "/report/boeu/exam/export/list/v2",
          data: { ids: state.selectedRowKeys },
          responseType: "blob",
          headers: {
            token: Cookies.get("token"),
          },
        }).then(
          (res) => {
            downLoad(res.data, "考试.xlsx");
          },
          (err) => {
            message.error(err);
          }
        );
      } else {
        const params = {
          testName: state.testName,
          createName: state.createName,
          pageNo: state.pageNo,
          pageSize: state.pageSize,
          orgId: state.orgId ? state.orgId[state.orgId.length - 1] : null,
        };
        axios({
          method: "post",
          url: "/report/boeu/exam/export/list/v2",
          data: params,
          responseType: "blob",
          headers: {
            token: Cookies.get("token"),
          },
        }).then(
          (res) => {
            downLoad(res.data, "考试.xlsx");
          },
          (err) => {
            message.error(err);
          }
        );
      }
    };
    const exportDetail = async (id) => {
      axios({
        method: "post",
        url: "/report/boeu/exam/export/detail/v2",
        data: { ids: [id] },
        responseType: "blob",
        headers: {
          token: Cookies.get("token"),
        },
      }).then(
        (res) => {
          downLoad(res.data, "考试详细信息.xlsx");
        },
        (err) => {
          message.error(err);
        }
      );
    };
    // 表格数据
    let tableData = ref([]);
    // cloumns 表头
    const columns = ref([
      {
        title: "考试名称",
        dataIndex: "testName",
        ellipsis: true,
        key: "testName",
        align: "center",
        width: 120,
      },
      {
        title: "归属组织",
        dataIndex: "resOwner",
        ellipsis: true,
        key: "resOwner",
        align: "center",
        width: 120,
      },
      {
        title: "课程名称",
        dataIndex: "pertainCourse",
        ellipsis: true,
        key: "pertainCourse",
        align: "center",
        width: 120,
      },
      {
        title: "项目名称",
        dataIndex: "pertainProject",
        ellipsis: true,
        key: "pertainProject",
        align: "center",
        width: 120,
      },
      {
        title: "学习路径名称",
        dataIndex: "pertainRouter",
        ellipsis: true,
        key: "pertainRouter",
        align: "center",
        width: 120,
      },
      {
        title: "考试人数",
        dataIndex: "totalNumber",
        ellipsis: true,
        key: "totalNumber",
        align: "center",
        width: 120,
      },
      {
        title: "考试次数",
        dataIndex: "testTimes",
        ellipsis: true,
        key: "testTimes",
        align: "center",
        width: 120,
      },
      {
        title: "题量",
        dataIndex: "numOfQuest",
        ellipsis: true,
        key: "numOfQuest",
        align: "center",
        width: 120,
      },
      {
        title: "考试时长",
        dataIndex: "testDuration",
        ellipsis: true,
        key: "testDuration",
        align: "center",
        width: 120,
      },
      {
        title: "考试满分",
        dataIndex: "totalScore",
        ellipsis: true,
        key: "totalScore",
        align: "center",
        width: 120,
      },
      {
        title: "考试合格分",
        dataIndex: "passLine",
        ellipsis: true,
        key: "passLine",
        align: "center",
        width: 120,
      },
      {
        title: "平均答题时长",
        dataIndex: "averageAnswerTime",
        ellipsis: true,
        key: "averageAnswerTime",
        align: "center",
        width: 120,
      },
      {
        title: "平均得分",
        dataIndex: "averageScore",
        ellipsis: true,
        key: "averageScore",
        align: "center",
        width: 120,
      },
      {
        title: "考试通过率",
        dataIndex: "passRate",
        ellipsis: true,
        key: "passRate",
        align: "center",
        width: 120,
      },
      {
        title: "发布时间",
        dataIndex: "publishTime",
        ellipsis: true,
        key: "publishTime",
        align: "center",
        width: 120,
      },
      {
        title: "状态",
        dataIndex: "published",
        ellipsis: true,
        key: "published",
        align: "center",
        width: 120,
      },
      {
        title: "创建人",
        dataIndex: "creator",
        ellipsis: true,
        key: "creator",
        align: "center",
        width: 120,
      },
      {
        title: "操作",
        dataIndex: "operation",
        key: "operation",
        width: 150,
        align: "center",
        fixed: "right",
        customRender: (record) => {
          return (
            <a
              onClick={() => {
                exportDetail(record.record.idStr);
              }}
            >
              导出详细信息
            </a>
          );
        },
      },
    ]);
    const searchData=async()=>{
      state.pageNo=1
      state.pageSize=10
      getTableData()
    }
    const getTableData = async () => {
      state.tableLoading = true;
      const params = {
        testName: state.testName,
        createName: state.createName,
        pageNo: state.pageNo,
        pageSize: state.pageSize,
        orgId: state.orgId ? state.orgId[state.orgId.length - 1] : null,
      };
      const res = await api.boeuExamPageListV2(params);
      if (res) {
        tableData.value = res.data.rows;
        state.tableDataTotal = res.data.total;
        state.tableLoading = false;
      }
    };
    //table 分页事件
    const changePagination = (page) => {
      state.pageNo = page;
      getTableData();
    };
    const exportDetils = async () => {
      if (state.selectedRowKeys.length > 0) {
        axios({
          method: "post",
          url: "/report/boeu/exam/export/detail/v2",
          data: { ids: state.selectedRowKeys },
          responseType: "blob",
          headers: {
            token: Cookies.get("token"),
          },
        }).then(
          (res) => {
            downLoad(res.data, "考试详情.xlsx");
          },
          (err) => {
            message.error(err);
          }
        );
      } else {
        const params = {
          testName: state.testName,
          createName: state.createName,
          pageNo: state.pageNo,
          pageSize: state.pageSize,
          orgId: state.orgId ? state.orgId[state.orgId.length - 1] : null,
        };
        axios({
          method: "post",
          url: "/report/boeu/exam/export/detail/v2",
          data: params,
          responseType: "blob",
          headers: {
            token: Cookies.get("token"),
          },
        }).then(
          (res) => {
            downLoad(res.data, "考试详情.xlsx");
          },
          (err) => {
            message.error(err);
          }
        );
      }
    };
    //请求组织接口
    const getOrgList = async () => {
      const res = await api.userGetUserOrg({});
      if (res) {
        state.option = res.data?.result?.list;
        state.orgId = res.data?.result?.treeNodeList;
        state.resetOrgId = res.data?.result?.treeNodeList;
        res.data?.result?.userType === 1
          ? (state.allowClear = true)
          : (state.allowClear = false);
        getTableData();
      }
    };
    onMounted(() => {
      getOrgList();
      state.tableLoading = true;
    });
    return {
      reset,
      searchData,
      exportDetail,
      exportDetils,
      ...toRefs(state),
      tableData,
      columns,
      changePagination,
      getOrgList,
      getTableData,
      onSelectChange,
      exportList,
    };
  },
};
</script>
<style lang="scss">
.examination {
  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 - 220px) / 3);
    }

    .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 {
      width: 100%;
      display: flex;
      justify-content: center;
    }
  }
}
</style>