<!-- 核查任务管理 -->
<script>
import { delDangerTaskData, getDangerTaskListData } from "@/api/dike";
import { codeToText } from "element-china-area-data";

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

export default {
  name: "InspectionItems",
  components: { TopBackTitle },
  mixins: [calcTableHeight],
  data() {
    return {
      areasOptionProps: {
        emitPath: false,
        checkStrictly: true, //选择任意一级
        expandTrigger: "hover",
        lazy: true,
        lazyLoad: cascaderLazyLoad,
      },
      areasOptions: [],
      paramsData: {
        adcdStart: null,
        dikeName: null,
        dikeType: null,
        dikePatt: null,
        engineeringManagementUnit: null,
        mainBuildGrad: null,
      },
      tableData: [], // 检查列表
      pageData: {
        pageNum: 1, // 当前页
        pageSize: 10, // 请求数量
        pageSizes: [10, 20, 50, 100],
        total: 0, // 总数量
      },
      dikeFormOptions: [],
      buildingLevelOptions: [],
      projectTypeOptions: [],
    };
  },
  methods: {
    handleCurrentPageChange(page) {
      this.pageData.pageNum = page;
      this.getTableData();
    },
    handlePageSizeChange(pageSize) {
      this.pageData.pageSize = pageSize;
      this.getTableData();
    },
    // 格式化行政区划-获取字典值
    formatAdcd(adcd) {
      if (adcd) {
        let provinceCode = adcd.slice(0, 2);
        let cityCode = adcd.slice(2, 4);
        let areaCode = adcd.slice(4, 6);
        if (areaCode != "00") {
          return (
            codeToText[provinceCode] +
            "-" +
            codeToText[provinceCode + cityCode] +
            "-" +
            codeToText[provinceCode + cityCode + areaCode]
          );
        } else if (cityCode != "00") {
          return (
            codeToText[provinceCode] + "-" + codeToText[provinceCode + cityCode]
          );
        } else {
          return codeToText[provinceCode];
        }
      }
    },
    // 跳转任务新增页面
    handleGoDetails() {
      this.$router.push({
        path: "inspectTaskDetails",
        query: {
          type: "add",
        },
      });
    },
    // 跳转任务查看
    handlePreview(row) {
      this.$router.push({
        path: "inspectTaskDetails",
        query: {
          type: "preview",
          id: row.id,
        },
      });
    },
    // 跳转任务编辑页面
    handleEdit(row) {
      this.$router.push({
        path: "inspectTaskDetails",
        query: {
          type: "edit",
          id: row.id,
        },
      });
    },
    // 获取列表数据
    getTableData() {
      getDangerTaskListData({
        data: {
          timeView: {
            timeField: "create_time",
          },
          adcdStart: this.paramsData.adcdStart || null,
          dikePatt: this.paramsData.dikePatt || null,
          engineeringManagementUnit:
            this.paramsData.engineeringManagementUnit || null,
          mainBuildGrad: this.paramsData.mainBuildGrad || null,
          dikeName: this.paramsData.dikeName || null,
          dikeType: this.paramsData.dikeType || null,
        },
        params: {
          orderBy: "create_time",
          sort: "desc",
        },
        pageSize: this.pageData.pageSize,
        pageNum: this.pageData.pageNum,
      }).then((res) => {
        if (res) {
          this.tableData = res.records || [];
          this.pageData.total = res.total;
        }
      });
    },
    // 重置搜索
    resetSearch() {
      this.paramsData.adcdStart = null;
      this.paramsData.dikePatt = null;
      this.paramsData.engineeringManagementUnit = null;
      this.paramsData.mainBuildGrad = null;
      this.paramsData.dikeName = null;
      this.paramsData.dikeType = null;

      this.getTableData();
    },
    // 删除
    handleDel(row) {
      this.$confirm("是否删除该条数据？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delDangerTaskData(row.id).then((res) => {
            if (res) {
              this.$message.success("删除成功");
              this.getTableData();
            }
          });
        })
        .catch(() => {});
    },
  },
  created() {
    this.getDicts("embankment_form").then((response) => {
      this.dikeFormOptions = response.data;
    });
    this.getDicts("embankment_type").then((response) => {
      this.projectTypeOptions = response.data;
    });
    this.getDicts("embankment_level").then((response) => {
      this.buildingLevelOptions = response.data;
    });
  },
  mounted() {
    // 获取列表数据
    this.getTableData();
  },
};
</script>

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

    <div class="table-box slider-right-body" ref="tableBoxRef">
      <div class="pb-16" ref="topBoxRef">
        <div class="flex items-start">
          <div class="">
            <div>
              <span>堤防名称</span>
              <el-input
                class="search-input ml-10"
                v-model="paramsData.dikeName"
                placeholder="请输入名称"
              />
              <span class="ml-10">堤防级别</span>
              <el-select
                size=""
                class="search-input ml-10"
                v-model="paramsData.mainBuildGrad"
                filterable
                placeholder="请选择"
              >
                <el-option label="全部" value=""> </el-option>
                <el-option
                  v-for="item in buildingLevelOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                >
                </el-option>
              </el-select>
              <span class="ml-10">堤防型式</span>
              <el-select
                size=""
                class="search-input ml-10"
                v-model="paramsData.dikePatt"
                filterable
                placeholder="请选择"
              >
                <el-option label="全部" value=""> </el-option>
                <el-option
                  v-for="item in dikeFormOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                >
                </el-option>
              </el-select>
            </div>
            <div class="mt-10">
              <span class="">主管单位</span>
              <el-input
                class="search-input ml-10"
                v-model="paramsData.engineeringManagementUnit"
                placeholder="请输入"
              />
              <span class="ml-10">行政区划</span>
              <el-cascader
                class="ml-10 search-input"
                v-model="paramsData.adcdStart"
                :props="areasOptionProps"
                placeholder="请选择行政区划"
                clearable
                size="small"
              >
              </el-cascader>
              <span class="ml-10">堤防类型</span>
              <el-select
                size=""
                class="search-input ml-10"
                v-model="paramsData.dikeType"
                filterable
                placeholder="请选择"
              >
                <el-option label="全部" value=""> </el-option>
                <el-option
                  v-for="item in projectTypeOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                >
                </el-option>
              </el-select>
            </div>
          </div>
          <el-button class="search-btn" type="success" @click="getTableData()"
            >搜索</el-button
          >
          <el-button class="search-btn" @click="resetSearch">重置</el-button>
          <el-button
            class="search-btn"
            style="margin-left: auto"
            type="success"
            v-hasPermi="['df:run:dangerProj:task:add']"
            @click="handleGoDetails()"
            >新增</el-button
          >
        </div>
      </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="dikeName" align="center" label="堤防名称" />
        <el-table-column
          prop="engineeringManagementUnit"
          align="center"
          label="管理单位"
        />
        <el-table-column prop="adcdStart" align="center" label="所在区域">
          <template slot-scope="scope">
            {{ formatAdcd(scope.row.adcdStart) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          align="center"
          sortable
          label="创建时间"
        >
        </el-table-column>
        <el-table-column
          prop="updateTime"
          align="center"
          sortable
          label="更新时间"
        />
        <el-table-column prop="operate" align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handlePreview(scope.row)"
              v-hasPermi="['df:run:dangerProj:task:view']"
              >查看</el-button
            >
            <el-button
              v-hasPermi="['df:run:dangerProj:task:edit']"
              type="text"
              size="small"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              v-hasPermi="['df:run:dangerProj:task:delete']"
              type="text"
              size="small"
              @click="handleDel(scope.row)"
              >删除</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;
  }
}
</style>
