<template>
  <!-- 文章! -->
  <div class="article">
    <!-- 以下为顶部搜索框 -->
    <div class="filter">
      <div class="select">
        <a-input
          style="width: 100%; height: 40px; border-radius: 8px"
          placeholder="请输入文章名称"
          allowClear
          showSearch
          v-model:value="title"
        >
        </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-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="btnzx btnzx1" @click="resetClick">
          <div class="search"></div>
          <div class="btnzText">重置</div>
        </div>
      </div>
    </div>
    <!-- 以下为导出按钮 -->
    <div class="btnzs">
      <div class="btnz btnz3" @click="exportAll">
        <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 === 'publishTime'">
            <a-tooltip placement="topRight">
              <template #title>
                <span>{{
                  record.sysUpdateTime
                    ? dayjs(record.sysUpdateTime).format("YYYY-MM-DD HH:mm:ss")
                    : record.sysCreateTime
                    ? dayjs(record.sysCreateTime).format("YYYY-MM-DD HH:mm:ss")
                    : ""
                }}</span>
              </template>
              <span>{{
                  record.sysUpdateTime
                    ? dayjs(record.sysUpdateTime).format("YYYY-MM-DD HH:mm:ss")
                    : record.sysCreateTime
                    ? dayjs(record.sysCreateTime).format("YYYY-MM-DD HH:mm:ss")
                    : ""
                }}</span>
            </a-tooltip>
          </template>
          <template v-if="column.key === 'status'">
            <span>{{
              record.status == "1"
                ? "草稿"
                : record.status == "2"
                ? "已提交待审核"
                : record.status == "3"
                ? "审核不通过"
                : record.status == "9"
                ? "已发布"
                : ""
            }}</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: "reportArticle",
  setup() {
    const state = reactive({
      tableLoading: false, // table加载图标
      tableDataTotal: 0, // 数据总条数
      pageSize: 10, // 每页条数
      pageNo: 1, //当前页码
      createName: "", //创建者名称
      publishTime: "", // 发布时间
      title: "", // 文章名称
      selectedRowKeys: [], // 选中的列
    });
    //导出
    const exportAll = async () => {
      if (state.selectedRowKeys.length > 0) {
        axios({
          method: "post",
          url: "/report/boeu/article/exportAll",
          data: { ids: state.selectedRowKeys },
          responseType: "blob",
          headers: {
            token: Cookies.get("token"),
          },
        }).then(
          (res) => {
            downLoad(res.data, "文章.xlsx");
          },
          (err) => {
            message.error(err);
          }
        );
      } else {
        const params = { page: state.pageNo, size: state.pageSize };
        if (state.title) {
          params.title = state.title;
        }
        if (state.createName) {
          params.createName = state.createName;
        }
        if (state.publishTime.length>0) {
          // params.publishTime = state.publishTime;
          params.startTime = state.publishTime[0];
          params.endTime = state.publishTime[1];
        }
        axios({
          method: "post",
          url: "/report/boeu/article/exportAll",
          data: params,
          responseType: "blob",
          headers: {
            token: Cookies.get("token"),
          },
        }).then(
          (res) => {
            downLoad(res.data, "文章.xlsx");
          },
          (err) => {
            message.error(err);
          }
        );
      }
    };
    // 表格数据
    const searchData=async()=>{
      state.pageNo=1
      state.pageSize=10
      getTableData()
    }
    const getTableData = async () => {
      state.tableLoading = true;
      const params = {
        title: state.title,
        createName: state.createName,
        // publishTime: state.publishTime,
        startTime: state.publishTime[0],
        endTime: state.publishTime[1],
        page: state.pageNo,
        size: state.pageSize
      };
      if (state.title) {
        params.title = state.title;
      }
      if (state.createName) {
        params.createName = state.createName;
      }
      if (state.publishTime.length>0) {
        // params.publishTime = state.publishTime;
        params.startTime = state.publishTime[0];
        params.endTime = state.publishTime[1];
      }
      const res = await api.boeuArticlePageList(params);
      if (res) {
        state.tableLoading = false;
        state.tableDataTotal = res.data.result.total;
        const list = res.data.result.rows?.map((item) => {
          return {
            ...item,
            key: item.id,
          };
        });
        tableData.value = list;
      }
    };
    const resetClick = () => {
      state.selectedRowKeys = [];
      state.createName = "";
      state.publishTime = [];
      state.title = "";
      getTableData();
    };
    let tableData = ref([]);
    // cloumns 表头
    const columns = ref([
      {
        title: "文章名称",
        dataIndex: "title",
        ellipsis: true,
        key: "title",
        align: "center",
      },
      {
        title: "归属组织",
        dataIndex: "orgName",
        ellipsis: true,
        key: "orgName",
        align: "center",
      },
      {
        title: "浏览数",
        dataIndex: "views",
        ellipsis: true,
        key: "views",
        align: "center",
      },
      {
        title: "点赞数",
        dataIndex: "praises",
        ellipsis: true,
        key: "praises",
        align: "center",
      },
      {
        title: "评论数",
        dataIndex: "comments",
        ellipsis: true,
        key: "comments",
        align: "center",
      },
      {
        title: "分享数",
        dataIndex: "shares",
        ellipsis: true,
        key: "shares",
        align: "center",
      },
      {
        title: "收藏数",
        dataIndex: "favorites",
        ellipsis: true,
        key: "favorites",
        align: "center",
      },
      {
        title: "发布时间",
        dataIndex: "publishTime",
        ellipsis: true,
        key: "publishTime",
        align: "center",
      },
      // {
      //   title: "文章状态",
      //   dataIndex: "status",
      //   ellipsis: true,
      //   key: "status",
      //   align: "center",
      // },
      {
        title: "创建人",
        dataIndex: "sysCreateBy",
        ellipsis: true,
        key: "sysCreateBy",
        align: "center",
      },
    ]);
    // table选中
    const onSelectChange = (selectedRowKeys) => {
      state.selectedRowKeys = selectedRowKeys;
    };
    //table 分页事件
    const changePagination = (page) => {
      state.pageNo = page;
      getTableData();
    };
    onMounted(() => {
      getTableData();
      state.tableLoading = true;
    });
    return {
      ...toRefs(state),
      searchData,
      dayjs,
      onSelectChange,
      resetClick,
      tableData,
      columns,
      changePagination,
      getTableData,
      exportAll,
    };
  },
};
</script>
<style lang="scss">
.article {
  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>
