<template>
  <!-- 项目! -->
  <div class="project">
    <!-- 以下为顶部搜索框 -->
    <div class="filter">
      <div class="select">
        <a-input
          style="width: 100%; height: 40px; border-radius: 8px"
          placeholder="请输入项目名称"
          allowClear
          showSearch
          v-model:value="projectName"
        >
        </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
          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-range-picker
          v-model:value="publishTime"
          type="date"
          valueFormat="YYYY-MM-DD"
          :placeholder="['发布开始时间','结束时间']"
          style="width: 100%; margin-right: 0px"
        />
      </div>
      <!-- <div class="select">
        <a-select
          style="width: 100%"
          placeholder="项目状态"
          allowClear
          :options="stateOptions"
          v-model:value="status"
        ></a-select>
      </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="exportAllClick">
        <div><img src="../../assets/images/coursewareManage/export1.png" alt="" /></div>
        <div class="btnzText">导出列表信息</div>
      </div>
      <div
        class="btnz btnz3"
        style="margin-left: 20px"
        @click="exportDetailClick"
      >
        <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,
        }"
      >
        <template #bodyCell="{ column, record }">
          <!-- 项目状态 -->
          <template v-if="column.key === 'projectStatus'">
            <span>{{
              record.reportProjectOverview.length > 0
                ? "-"
                : record.status == "0"
                ? "-"
                : record.status == "3"
                ? "已发布"
                : record.status == "4"
                ? "已发布"
                : record.status == "-1"
                ? "已结束"
                : ""
            }}</span>
          </template>
          <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 === 'proTime'">
            <a-tooltip placement="topRight">
              <template #title>
                <span
                  >{{
                    record.beginTime &&
                    dayjs(record.beginTime).format("YYYY-MM-DD HH:mm:ss")
                  }}~{{
                    record.endTime &&
                    dayjs(record.endTime).format("YYYY-MM-DD HH:mm:ss")
                  }}</span
                >
              </template>
              <span
                >{{
                  record.beginTime &&
                  dayjs(record.beginTime).format("YYYY-MM-DD HH:mm:ss")
                }}~{{
                  record.endTime &&
                  dayjs(record.endTime).format("YYYY-MM-DD HH:mm:ss")
                }}</span
              >
            </a-tooltip>
          </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";
import { useStore } from "vuex";
export default {
  name: "reportProject",
  setup() {
    const store = useStore();
    const state = reactive({
      tableLoading: false, // table加载图标
      tableDataTotal: 0, // 数据总条数
      pageSize: 10, // 每页条数
      pageNo: 1, //当前页码
      allowClear: false,
      option: [],
      orgId: null,
      resetOrgId: [],
      projectName: "",
      createName: "",
      publishTime: "", // 发布时间
      selectedRowKeys: [], // 选中的列
      status: null,
      stateOptions: [
        {
          label: "已发布",
          value: 2,
        },
        {
          label: "已结束",
          value: 3,
        },
      ],
    });
    const exportAllClick = async () => {
      if (state.selectedRowKeys.length > 0) {
        axios({
            method: "get",
            url: "/report/boeu/project/export",
            params: { ids: `${state.selectedRowKeys}` },
            responseType: "blob",
            headers: {
              token: Cookies.get("token"),
            },
          }).then((res) => {
            downLoad(res.data, "项目.xlsx");
          });
      } else {
        const params = {};
        params.status = state.status;
        params.orgId = state.orgId;
        params.createName = state.createName;
        params.projectName = state.projectName;
        // params.publishTime = state.publishTime;
        params.startTime = state.publishTime[0];
        params.endTime = state.publishTime[1];
        params.page = state.pageNo;
        params.size = state.pageSize;
        params.orgId = state.orgId ? state.orgId[state.orgId.length - 1] : null;
        axios({
          method: "post",
          url: "/report/boeu/project/exportAll",
          data:  params ,
          responseType: "blob",
          headers: {
            token: Cookies.get("token"),
          },
        }).then(
          (res) => {
            downLoad(res.data, "项目详情.xlsx");
          },
          (err) => {
            message.error(err);
          }
        );
      }
    };
    // 导出详情接口
    const exportDetailClick = async () => {
      if (state.selectedRowKeys.length > 0) {
        axios({
          method: "post",
          url: "/report/boeu/project/exportDetailAll",
          data: { ids: state.selectedRowKeys },
          responseType: "blob",
          headers: {
            token: Cookies.get("token"),
          },
        }).then(
          (res) => {
            downLoad(res.data, "项目详情.xlsx");
          },
          (err) => {
            message.error(err);
          }
        );
      } else {
        const params = {};
        params.status = state.status;
        params.orgId = state.orgId;
        params.createName = state.createName;
        params.projectName = state.projectName;
        // params.publishTime = state.publishTime;
        params.startTime = state.publishTime[0];
        params.endTime = state.publishTime[1];
        params.page = state.pageNo;
        params.size = state.pageSize;
        params.orgId = state.orgId ? state.orgId[state.orgId.length - 1] : null;
        axios({
          method: "post",
          url: "/report/boeu/project/exportDetailAll",
          data:  params ,
          responseType: "blob",
          headers: {
            token: Cookies.get("token"),
          },
        }).then(
          (res) => {
            downLoad(res.data, "项目详情.xlsx");
          },
          (err) => {
            message.error(err);
          }
        );
      }
    };
    // 表格数据
    let tableData = ref([]);
    // table选中
    const onSelectChange = (selectedRowKeys) => {
      state.selectedRowKeys = selectedRowKeys;
    };
    //请求组织接口
    const getOrgList = async () => {
      let params = {
        roleList: store.state.userInfo.roleList,
        userId: store.state.userInfo.userId
      }
      const res = await api.userGetUserOrg(params);
      if (res) {
        state.option = res.data?.result;
        state.orgId = [state.option[0]?.organizationId];
        state.resetOrgId = [state.option[0]?.organizationId];
        state.allowClear = true
        getTableData();
      }
    };
    const reset = async () => {
      state.tableLoading = true;
      state.orgId = state.resetOrgId;
      state.pageNo = 1;
      state.pageSize = 10;
      state.projectName = "";
      state.createName = "";
      state.publishTime = [];
      state.selectedRowKeys = [];
      state.status = null;
      getTableData();
    };
    const searchData=async()=>{
      state.pageNo=1
      state.pageSize=10
      getTableData()
    }
    // 获取table数据
    const getTableData = async () => {
      state.tableLoading = true;
      const params = {};
      params.status = state.status;
      params.createName = state.createName;
      params.projectName = state.projectName;
      // params.publishTime = state.publishTime;
      params.startTime = state.publishTime[0];
        params.endTime = state.publishTime[1];
      params.page = state.pageNo;
      params.size = state.pageSize;
      params.orgId = state.orgId ? state.orgId[state.orgId.length - 1] : null;
      const res = await api.boeuProjectPageList(params);
      if (res) {
        const fun = (arr) => {
          const list = arr.map((item) => {
            let children;
            if (item?.reportProjectOverview?.length) {
              children = fun(item.reportProjectOverview);
            }
            return {
              key: item.boeProjectInfoId,
              children: children,
              ...item,
            };
          });
          return list;
        };
        tableData.value = fun(res.data.result.rows);
        state.tableDataTotal = res.data.result.total;
        console.log("safgaga"+res.data.result.total)
        state.tableLoading = false;
      }
    };
    //table 分页事件
    const changePagination = (page) => {
      state.pageNo = page;
      getTableData();
    };
    // cloumns 表头
    const columns = ref([
      {
        title: "项目名称",
        dataIndex: "projectName",
        key: "projectName",
        width: 120,
        ellipsis: true,
        align: "left",
        fixed: "left",
      },
      {
        title: "归属组织",
        dataIndex: "sourceBelongName",
        ellipsis: true,
        key: "sourceBelongName",
        width: 120,
        align: "center",
        fixed: "left",
      },
      {
        title: "项目分类",
        dataIndex: "trainingType",
        ellipsis: true,
        key: "trainingType",
        width: 120,
        align: "center",
        fixed: "left",
      },
      {
        title: "阶段数",
        dataIndex: "stageTotal",
        ellipsis: true,
        key: "stageTotal",
        width: 120,
        align: "center",
      },
      {
        title: "任务数",
        dataIndex: "taskTotal",
        ellipsis: true,
        key: "taskTotal",
        width: 120,
        align: "center",
      },
      {
        title: "学习人数",
        dataIndex: "startLearnerTotal",
        ellipsis: true,
        key: "startLearnerTotal",
        width: 120,
        align: "center",
      },
      {
        title: "参与人数",
        dataIndex: "participantsTotal",
        ellipsis: true,
        key: "participantsTotal",
        width: 120,
        align: "center",
      },
      {
        title: "参与率",
        dataIndex: "participantsRate",
        ellipsis: true,
        key: "participantsRate",
        width: 120,
        align: "center",
      },
      {
        title: "完成人数",
        dataIndex: "peopleCompletedTotal",
        ellipsis: true,
        key: "peopleCompletedTotal",
        width: 120,
        align: "center",
      },
      {
        title: "完成率",
        dataIndex: "completedRate",
        ellipsis: true,
        key: "completedRate",
        width: 120,
        align: "center",
      },
      {
        title: "项目时间",
        dataIndex: "proTime",
        ellipsis: true,
        key: "proTime",
        width: 120,
        align: "center",
      },
      {
        title: "发布时间",
        dataIndex: "publishTime",
        ellipsis: true,
        key: "publishTime",
        width: 120,
        align: "center",
      },
      {
        title: "状态",
        dataIndex: "status",
        ellipsis: true,
        key: "projectStatus",
        width: 120,
        align: "center",
      },
      {
        title: "创建人",
        dataIndex: "createName",
        ellipsis: true,
        key: "manager",
        width: 120,
        align: "center",
      },
      {
        title: "操作",
        dataIndex: "operation",
        key: "operation",
        width: 150,
        align: "center",
        fixed: "right",
        customRender: (record) => {
          return (
            <a
              onClick={() => {
                exportOnly(record.record.boeProjectInfoId);
              }}
            >
              导出学习记录
            </a>
          );
        },
      },
    ]);
    const exportOnly = async (id) => {
      axios({
        method: "post",
        url: "/report/boeu/project/exportDetailAll",
        data: { ids: [id] },
        responseType: "blob",
        headers: {
          token: Cookies.get("token"),
        },
      }).then((res) => {
        downLoad(res.data, "项目详情.xlsx");
      });
    };
    onMounted(() => {
      getOrgList();
      state.tableLoading = true;
    });
    return {
      ...toRefs(state),
      getOrgList,
      tableData,
      dayjs,
      columns,
      changePagination,
      getTableData,
      reset,
      onSelectChange,
      exportDetailClick,
      exportAllClick,
      searchData,
    };
  },
};
</script>
<style lang="scss">
.project {
  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 {
      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>
