<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="区属" prop="areaDes">
        <el-input
          v-model="queryParams.areaDes"
          placeholder="请输入区属"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="行政区属" prop="areaAdDes">
        <el-input
          v-model="queryParams.areaAdDes"
          placeholder="请输入行政区属"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展序号" prop="exId">
        <el-input
          v-model="queryParams.exId"
          placeholder="请输入扩展序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设施名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入设施名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="路名" prop="roadName">
        <el-input
          v-model="queryParams.roadName"
          placeholder="请输入路名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性质(雨水、污水)" prop="nature">
        <el-input
          v-model="queryParams.nature"
          placeholder="请输入性质(雨水、污水)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设施起点" prop="addrFrom">
        <el-input
          v-model="queryParams.addrFrom"
          placeholder="请输入设施起点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设施终点" prop="addrEnd">
        <el-input
          v-model="queryParams.addrEnd"
          placeholder="请输入设施终点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展编号1" prop="exId1">
        <el-input
          v-model="queryParams.exId1"
          placeholder="请输入扩展编号1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:facilities:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:facilities:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:facilities:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:facilities:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="facilitiesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="区属" align="center" prop="areaDes" />
      <el-table-column label="行政区属" align="center" prop="areaAdDes" />
      <el-table-column label="扩展序号" align="center" prop="exId" />
      <el-table-column label="设施名称" align="center" prop="name" />
      <el-table-column label="路名" align="center" prop="roadName" />
      <el-table-column label="性质(雨水、污水)" align="center" prop="nature" />
      <el-table-column label="设施起点" align="center" prop="addrFrom" />
      <el-table-column label="设施终点" align="center" prop="addrEnd" />
      <el-table-column label="扩展编号1" align="center" prop="exId1" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:facilities:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:facilities:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设施对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="fform" :rules="rules" label-width="80px">
        <el-form-item label="区属" prop="areaDes">
          <el-input v-model="fform.areaDes" placeholder="请输入区属" />
        </el-form-item>
        <el-form-item label="行政区属" prop="areaAdDes">
          <el-input v-model="fform.areaAdDes" placeholder="请输入行政区属" />
        </el-form-item>
        <el-form-item label="扩展序号" prop="exId">
          <el-input v-model="fform.exId" placeholder="请输入扩展序号" />
        </el-form-item>
        <el-form-item label="设施名称" prop="name">
          <el-input v-model="fform.name" placeholder="请输入设施名称" />
        </el-form-item>
        <el-form-item label="路名" prop="roadName">
          <el-input v-model="fform.roadName" placeholder="请输入路名" />
        </el-form-item>
        <el-form-item label="性质(雨水、污水)" prop="nature">
          <el-input v-model="fform.nature" placeholder="请输入性质(雨水、污水)" />
        </el-form-item>
        <el-form-item label="设施起点" prop="addrFrom">
          <el-input v-model="fform.addrFrom" placeholder="请输入设施起点" />
        </el-form-item>
        <el-form-item label="设施终点" prop="addrEnd">
          <el-input v-model="fform.addrEnd" placeholder="请输入设施终点" />
        </el-form-item>
        <el-form-item label="扩展编号1" prop="exId1">
          <el-input v-model="fform.exId1" placeholder="请输入扩展编号1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { listFacilities, getFacilities, delFacilities, addFacilities, updateFacilities } from "@/api/system/facilities";
import { defineComponent, ref, onMounted, watch } from "vue";
const { proxy } = getCurrentInstance();

      // 遮罩层
const loading = ref(true);
      // 选中数组
const ids = ref([]);
      // 非单个禁用
const single = ref(true);
      // 非多个禁用
const multiple = ref(true);
      // 显示搜索条件
const showSearch = ref(true);
      // 总条数
const total = ref(0);
      // 设施表格数据
const facilitiesList = ref([]);
      // 弹出层标题
const title = ref("");
      // 是否显示弹出层
const open = ref(false);
      // 查询参数

const data = reactive({
  fform: {},
  queryParams: {
    pageNum: 1,
  pageSize: 10,
  areaDes: null,
  areaAdDes: null,
  exId: null,
  name: null,
  roadName: null,
  nature: null,
  addrFrom: null,
  addrEnd: null,
  exId1: null
  },
  rules: {
     }
});
const { queryParams, fform, rules } = toRefs(data);

onMounted(() => {
  getList();
})


const getList = () => {
  loading.value = true;
  listFacilities(queryParams.value).then(response => {
    facilitiesList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
};
    // 取消按钮
const cancel = () => {
  open.value = false;
  reset();
};
    // 表单重置
const reset = () => {
  fform.value = {
    id: null,
    areaDes: null,
    areaAdDes: null,
    exId: null,
    name: null,
    roadName: null,
    nature: null,
    addrFrom: null,
    addrEnd: null,
    exId1: null
  };
  proxy.resetForm("form");
};
    /** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
    /** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryForm");
  handleQuery();
};
    // 多选框选中数据
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
};
    /** 新增按钮操作 */
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "添加设施";
};
    /** 修改按钮操作 */
const handleUpdate = (row) => {
  reset();
  const id = row.id || ids.value;
  getFacilities(id).then(response => {
    fform.value = response.data;
    open.value = true;
    title.value = "修改设施";
  });
};
    /** 提交按钮 */
const submitForm = () => {
  proxy.$refs["form"].validate(valid => {
    if (valid) {
      if (fform.value.id != null) {
        updateFacilities(fform.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFacilities(fform.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
};
    /** 删除按钮操作 */
const handleDelete = (row) => {
  const ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除设施编号为"' + ids + '"的数据项？').then(function () {
    return delFacilities(ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
};
    /** 导出按钮操作 */
const handleExport = () => {
  proxy.download('system/facilities/export', {
    ...queryParams.value
  }, `facilities_${new Date().getTime()}.xlsx`)
};


</script>
