<!-- 监督检查工程管理 -->
<script>
import dayjs from "dayjs";
import {
  delSuperviseWayItemData,
  getSuperviseWayTaskListData,
} from "@/api/dike";

import TopBackTitle from "@/components/TopBackTitle/index.vue";
import { calcTableHeight } from "@/mixins/calcTableHeight";

export default {
  name: "projectManage",
  components: { TopBackTitle },
  mixins: [calcTableHeight],
  data() {
    return {
      searchTaskName: "",
      searchManageName: "",
      searchProjectName: "",
      paramsData: {
        name: "",
        type: null,
        dikeName: "",
        selectLevel: "",
        selectType: "",
      },
      tableData: [], // 检查列表
      pageData: {
        pageNum: 1, // 当前页
        pageSize: 10, // 请求数量
        pageSizes: [10, 20, 50, 100],
        total: 0, // 总数量
      },
    };
  },
  methods: {
    handleCurrentPageChange(page) {
      this.pageData.pageNum = page;
      this.getTableData();
    },
    handlePageSizeChange(pageSize) {
      this.pageData.pageSize = pageSize;
      this.getTableData();
    },
    // 查看
    handleCheck(row) {
      this.$router.push({
        path: "supervisionProjectReport",
        query: {
          type: "preview",
          wayId: row.wayId,
          id: row.id,
        },
      });
    },
    // 跳转编辑页面
    handleEdit(row) {
      this.$router.push({
        path: "supervisionProjectReport",
        query: {
          type: "edit",
          wayId: row.wayId,
          id: row.id,
        },
      });
    },
    // 跳转报告页面
    handleCheckReport(row) {
      this.$router.push({
        path: "supervisionProjectReportPage",
        query: {
          wayId: row.wayId,
          id: row.id,
        },
      });
    },
    // 计算doneTime距离此刻剩余多少天
    calcDeadLine(time) {
      if (!time) return "-";
      let days = dayjs(time).diff(dayjs(), "day");
      let seconds = 0;
      if (days <= 0) {
        seconds = dayjs(time).diff(dayjs(), "second");
      }
      return days > 0
        ? days
        : seconds > 0
        ? 1
        : dayjs(time).diff(dayjs(), "day") - 1;
    },
    // 获取列表数据
    getTableData() {
      getSuperviseWayTaskListData({
        data: {
          name: this.searchTaskName || null,
          dikeName: this.searchProjectName || null,
          engineeringManagementUnit: this.searchManageName || null,
        },
        pageSize: this.pageData.pageSize,
        pageNum: this.pageData.pageNum,
      }).then((res) => {
        if (res) {
          this.tableData = res.records.map((item) => {
            if (item.state != "1") {
              item.deadTime = this.calcDeadLine(item.doneTime);
            }
            return item;
          });
          this.pageData.total = res.total;
        }
      });
    },
    // 重置搜索
    resetSearch() {
      this.searchTaskName = null;
      this.searchManageName = null;
      this.searchProjectName = null;
      this.getTableData();
    },
  },
  mounted() {
    // 获取列表数据
    this.getTableData();
  },
};
</script>

<template>
  <div class="body slider-right">
    <TopBackTitle></TopBackTitle>

    <div class="table-box slider-right-body" ref="tableBoxRef">
      <div class="pb-16" ref="topBoxRef">
        <span>任务名称：</span>
        <el-input
          class="search-input ml-10"
          v-model="searchTaskName"
          placeholder="请输入名称"
        />
        <span class="ml-10">管理单位：</span>
        <el-input
          class="search-input ml-10"
          v-model="searchManageName"
          placeholder="请输入"
        />
        <span class="ml-10">工程对象：</span>
        <el-input
          class="search-input ml-10"
          v-model="searchProjectName"
          placeholder="请输入"
        />
        <el-button class="search-btn" type="success" @click="getTableData()"
          >搜索</el-button
        >
        <el-button class="search-btn" @click="resetSearch">重置</el-button>
      </div>
      <el-table :height="tableHeight" :data="tableData" border>
        <el-table-column type="index" align="center" label="序号" width="60" />
        <el-table-column prop="name" align="center" label="检查任务" />
        <el-table-column
          prop="engineeringManagementUnit"
          align="center"
          label="管理单位"
        />
        <el-table-column prop="dikeName" align="center" label="工程对象">
        </el-table-column>
        <el-table-column
          prop="doneTime"
          align="center"
          sortable
          label="完成时间"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.state == '1'">完成</span>
            <span
              v-else
              :class="{
                'warning-time':
                  scope.row.deadTime != '-' && scope.row.deadTime <= 1,
              }"
              >{{ scope.row.deadTime }}天</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="operate" align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleCheck(scope.row)"
              v-hasPermi="['df:run:sup:proj:view']"
              >查看</el-button
            >
            <el-button
              type="text"
              size="small"
              v-if="scope.row.state != '1'"
              @click="handleEdit(scope.row)"
              v-hasPermi="['df:run:sup:proj:patrol']"
              >巡查</el-button
            >
            <el-button
              v-else
              type="text"
              size="small"
              @click="handleCheckReport(scope.row)"
              v-hasPermi="['df:run:sup:proj:report']"
              >报告</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        class="pagination"
        style="margin-top: 16px; margin-right: 16px; float: right"
        :current-page="pageData.pageNum"
        :page-sizes="pageData.pageSizes"
        layout="total, prev, pager, next, sizes, jumper"
        :total="pageData.total"
        @size-change="(e) => handlePageSizeChange(e)"
        @current-change="(e) => handleCurrentPageChange(e)"
      >
      </el-pagination>
    </div>
  </div>
</template>

<style scoped lang="less">
.body {
  width: 100%;
  padding-left: 24px;

  .top-title {
    height: 40px;
    background-color: white;
    display: flex;
    padding-left: 16px;
    align-items: center;
    font-weight: 600;
  }

  .table-box {
    width: 100%;
    margin-top: 24px;
    padding: 16px;
    background-color: white;

    .top-search {
      margin-bottom: 8px;
    }
  }
  .search-input {
    width: 202px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 10px;
  }

  .warning-time {
    color: red;
  }
}
</style>
