<template>
  <div class="tableBox">
    <!-- 年度计划 -->
    <div class="planBox" v-if="showTable != 'planData'">
      <div class="title-box-bg">
        <span>年度计划</span>
      </div>
      <div v-if="pageData.annualPlan.tableData" class="planTable">
        <el-table
          :border="false"
          :data="pageData.annualPlan.tableData"
          :show-header="false"
          @row-click="columnEvent"
        >
          <el-table-column prop="title" show-overflow-tooltip align="left" />
          <el-table-column
            prop="publishTime"
            width="120"
            align="right"
            :formatter="filterData"
          />
        </el-table>
        <el-button
          plain
          class="moreBtn"
          type="primary"
          size="small"
          @click="showMore('annualPlan')"
          v-if="showTable != 'annualPlan'"
          >更多</el-button
        >
        <el-button
          plain
          class="moreBtn"
          type="info"
          size="small"
          @click="hideenMore('annualPlan')"
          v-if="showTable == 'annualPlan'"
          >收起</el-button
        >
      </div>

      <template v-else>
        <el-empty description="暂无数据" />
      </template>
    </div>
    <!-- 中长期计划 -->
    <div class="planBox" v-if="showTable != 'annualPlan'">
      <div class="title-box-bg">
        <span>中长期计划</span>
      </div>
      <div v-if="pageData.planData.tableData.length" class="planTable">
        <el-table
          :show-header="false"
          :data="pageData.planData.tableData"
          @row-click="columnEvent"
        >
          <el-table-column prop="title" show-overflow-tooltip align="left" />
          <el-table-column
            prop="publishTime"
            width="120"
            align="right"
            :formatter="filterData"
          />
        </el-table>
        <el-button
          plain
          class="moreBtn"
          type="primary"
          size="small"
          @click="showMore('planData')"
          v-if="showTable != 'planData'"
          >更多</el-button
        >
        <el-button
          plain
          class="moreBtn"
          type="info"
          size="small"
          @click="hideenMore('annualPlan')"
          v-if="showTable == 'planData'"
          >收起</el-button
        >
      </div>
      <template v-else>
        <el-empty description="暂无数据" />
      </template>
    </div>
    <div class="paginationBox" v-if="showTable != ''">
      <el-pagination
        :small="true"
        :total="pagination.total"
        :page-size="pagination.size"
        @current-change="handleCurrentChange"
        :currentPage="pagination.current"
        layout="total, prev, pager, next, jumper"
      />
    </div>
  </div>
</template>
<script setup>
import api from "@/api/zwgk.js";
import { useRouter } from "vue-router";
import { onMounted, reactive, ref, watch } from "vue";
const showTable = ref("");
const router = useRouter();
const pagination = reactive({
  total: 0,
  size: 7,
  current: 1,
});
const pageData = reactive({
  annualPlan: {
    tableData: [],
    search: {
      total: 0,
      size: 7,
      current: 1,
    },
  },
  planData: {
    tableData: [],
    search: {
      total: 0,
      size: 7,
      current: 1,
    },
  },
});
onMounted(() => {
  getTableList();
});

function getTableList() {
  if (showTable.value == "" || showTable.value == "annualPlan") {
    // 年度计划
    api.annualPlan(pageData.annualPlan.search).then((res) => {
      if (res.code == 200) {
        pageData.annualPlan.tableData = [];
        pageData.annualPlan.search.size = res.data.size;
        pageData.annualPlan.search.total = res.data.total;
        pageData.annualPlan.search.current = res.data.pages;
        res.data.records.map((item) => {
          pageData.annualPlan.tableData.push(item);
        });
        if (showTable.value == "annualPlan") {
          pagination.size = res.data.size;
          pagination.total = res.data.total;
          pagination.current = res.data.pages;
        }
      }
    });
  }

  if (showTable.value == "" || showTable.value == "planData") {
    // 年度计划
    api.plan(pageData.planData.search).then((res) => {
      if (res.code == 200) {
        pageData.planData.tableData = [];
        pageData.planData.search.size = res.data.size;
        pageData.planData.search.total = res.data.total;
        pageData.planData.search.current = res.data.pages;
        res.data.records.map((item) => {
          pageData.planData.tableData.push(item);
        });
        if (showTable.value == "planData") {
          pagination.size = res.data.size;
          pagination.total = res.data.total;
          pagination.current = res.data.pages;
        }
      }
    });
  }
}
// 切换页数
function handleCurrentChange(val) {
  if (showTable.value == "annualPlan") {
    pageData.annualPlan.search.current = val;
  } else {
    pageData.planData.search.current = val;
  }
  getTableList();
}

// 显示更多
function showMore(type) {
  showTable.value = type;
  pageData[type].search.size = 20;
  getTableList();
}
function hideenMore(type) {
  showTable.value = "";
  pageData[type].search.size = 10;
  getTableList();
}
// 过滤数据
function filterData(row, column) {
  if (row.publishTime) {
    return row.publishTime.substring(0, 10);
  }
}

// 分页
function paginationEvent(val) {
  pageData.search.current = val;
  getTableList();
}

function columnEvent(data) {
  //定义一个路径
  let href = router.resolve({
    path: "/articleDetails",
    query: { type: "ghjh", id: data.id },
  }).href;
  //打开新的页面 到 href 这个页面
  window.open(href, "_blank");
}
watch(
  () => router.currentRoute.value,
  (newValue) => {
    console.log(newValue.fullPath);
    // 此处写监听路由后要执行的事情
    // if (newValue.fullPath === '/manage/racking') {
    //   getList();
    // }
  },
  { immediate: true }
);
</script>
<style scoped lang="less">
.tableBox {
  position: relative;
  min-height: calc(100vh - 200px);
  .planTable {
    display: flex;
    flex-direction: column;
    justify-content: center;
    .moreBtn {
      margin: 30px auto;
    }
  }
  .paginationBox {
    bottom: 0;
    position: absolute;
    padding: 15px 10px;
  }
}
</style>
