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

export default {
  name: "InspectionItems",
  data() {
    return {
      areasOptionProps: {
        emitPath: false,
        checkStrictly: true, //选择任意一级
      },
      areasOptions: [],
      paramsData: {
        wagaName: null,
        wagaType: null,
        engScal: null,
        engineeringManagementUnit: null,
        mainBuildGrad: null,
      },
      tableData: [], // 检查列表
      pageData: {
        pageNum: 1, // 当前页
        pageSize: 10, // 请求数量
        pageSizes: [10, 20, 50, 100],
        total: 0, // 总数量
      },
      wagaLocOptions: [],
      engScalOptions: [],
      buildingLevelOptions: [],
      projectTypeOptions: [],
    };
  },
  methods: {
    handleCurrentPageChange(page) {
      this.pageData.pageNum = page;
      this.getTableData();
    },
    handlePageSizeChange(pageSize) {
      this.pageData.pageSize = pageSize;
      this.getTableData();
    },
    getTreeData() {
      getAreasData().then((items) => {
        // console.log("getAreasData", items.data);
        let res = [];
        let getChildren = (res, pid) => {
          for (const i of items.data) {
            if (i.parentid === pid) {
              const newItem = {
                label: i.name,
                value: i.id,
              };
              if (i.layer != 3) newItem.children = [];
              res.push(newItem);
              getChildren(newItem.children, newItem.value);
            }
          }
        };
        getChildren(res, items.data[0].parentid);
        this.areasOptions = res;
        // return res;
      });
    },
    // 格式化行政区划-获取字典值
    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",
          },
          adcd: this.paramsData.adcd || null,
          engScal: this.paramsData.engScal || null,
          engineeringManagementUnit:
            this.paramsData.engineeringManagementUnit || null,
          mainBuildGrad: this.paramsData.riverLocation || null,
          wagaName: this.paramsData.wagaName || null,
          wagaType: this.paramsData.wagaType || 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.adcd = null;
      this.paramsData.engScal = null;
      this.paramsData.engineeringManagementUnit = null;
      this.paramsData.mainBuildGrad = null;
      this.paramsData.wagaName = null;
      this.paramsData.wagaType = 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("location").then((response) => {
      this.wagaLocOptions = response.data;
    });
    this.getDicts("engineering_scale").then((response) => {
      this.engScalOptions = response.data;
    });
    this.getDicts("sluice_type").then((response) => {
      this.projectTypeOptions = response.data;
    });
    this.getDicts("building_level").then((response) => {
      this.buildingLevelOptions = response.data;
    });
    this.getTreeData();
  },
  mounted() {
    // 获取列表数据
    this.getTableData();
  },
};
</script>

<template>
  <div class="body slider-right">
    <div class="top-title">基础信息管理</div>
    <div class="table-box">
      <div class="flex items-start">
        <div class="">
          <div>
            <span>水闸名称</span>
            <el-input
              class="search-input ml-10"
              v-model="paramsData.wagaName"
              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.engScal"
              filterable
              placeholder="请选择"
            >
              <el-option label="全部" value=""> </el-option>
              <el-option
                v-for="item in engScalOptions"
                :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"
              :options="areasOptions"
              v-model="paramsData.adcd"
              :props="areasOptionProps"
              placeholder="请选择行政区划"
              clearable
              size="small"
            >
            </el-cascader>
            <span class="ml-10">水闸类型</span>
            <el-select
              size=""
              class="search-input ml-10"
              v-model="paramsData.wagaType"
              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="['sz:run:jc:task:add']"
          @click="handleGoDetails()"
          >添加</el-button
        >
      </div>
      <el-table class="table mt-16" height="610" :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="wagaName" align="center" label="水闸名称" />
        <el-table-column
          prop="engineeringManagementUnit"
          align="center"
          label="管理单位"
        />
        <el-table-column prop="adcd" align="center" label="所在区域">
          <template slot-scope="scope">
            {{ formatAdcd(scope.row.adcd) }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" align="center" label="创建时间">
        </el-table-column>
        <el-table-column prop="updateTime" align="center" label="更新时间" />
        <el-table-column prop="operate" align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handlePreview(scope.row)"
              >查看</el-button
            >
            <el-button type="text" size="small" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button 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%;
  min-height: calc(100vh - 76px);
  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%;
    min-height: calc(100vh - 56px - 64px);
    margin-top: 24px;
    padding: 16px;
    background-color: white;

    .top-search {
      margin-bottom: 8px;
    }

    .table {
      height: calc(680px - 34px);
    }
  }
  .search-input {
    width: 202px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 10px;
  }
}
</style>
