<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="坐标系名称" prop="layerName">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入坐标系名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="EPSG代号" prop="layerName">
        <el-input
          v-model="queryParams.epsg"
          placeholder="请输入EPSG代号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @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"
          @click="handleAdd"
          v-hasPermi="['system:layers:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:layers:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:layers:remove']"
          >删除</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="layersList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column
        label="坐标系名称"
        align="center"
        prop="name"
        width="350"
      />
      <el-table-column label="JPG文件是否生成" align="center" prop="jpgUrl">
        <template #default="scope">
          <span>
           EPSG:{{scope.row.epsg}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="经度最小" align="center" prop="minLng" />
      <el-table-column label="经度最大" align="center" prop="maxLng" />
      <el-table-column label="中央经线" align="center" prop="centerLng" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >

          <el-button
            link
            type="danger"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:layers:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改部门对话框 -->

    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="epsgRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="坐标系名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="EPSG代号" prop="epsg">
              <el-input v-model="form.epsg" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="经度最小">
              <el-input v-model="form.minLng" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="经度最大">
              <el-input v-model="form.maxLng" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="中央经线">
              <el-input v-model="form.centerLng" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Layers">
import { list, del, add, update } from "@/api/system/coordinateSystem";
import { listUser } from "@/api/system/user";

const { proxy } = getCurrentInstance();
const data = reactive({
  // 按钮loading
  buttonLoading: false,
  // 遮罩层
  loading: true,
  // 选中数组
  ids: [],
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 显示搜索条件
  showSearch: true,
  // 总条数
  total: 0,
  // 坐标系管理表格数据
  layersList: [],
  // 弹出层标题
  title: "",
  // 是否显示弹出层
  open: false,
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    epsg: undefined,
    isAsc: "asc",
  },
  // 表单参数
  form: {},
  // 表单校验
  rules: {
    name: [{ required: true, message: "坐标系名称不能为空", trigger: "blur" }],
    epsg: [{ required: true, message: "EPSG代号不能为空", trigger: "blur" }],
  },
});

const {
  open,
  showSearch,
  queryParams,
  form,
  ids,
  rules,
  title,
  layersList,
  total,
  multiple,
  single,
  loading,
  buttonLoading,
} = toRefs(data);

// 表单重置
function reset() {
  form.value = {
    epsg: undefined,
    name: undefined,
    minLng: undefined,
    maxLng: undefined,
    centerLng: undefined,
  };
  proxy.resetForm("epsgRef");
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加坐标系";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["epsgRef"].validate((valid) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        update(form.value)
          .then((response) => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      } else {
        add(form.value)
          .then((response) => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      }
    }
  });
}
// 取消按钮
function cancel() {
  open.value = false;

  reset();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}
/** 查询坐标系管理列表 */
function getList() {
  //  loading.value = false;
  loading.value = true;
  list(queryParams.value).then((response) => {
    layersList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}

/** 删除按钮操作 */
function handleDelete(row) {
  const idsData = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除坐标系管理编号为"' + idsData + '"的数据项？')
    .then(() => {
      loading.value = true;
      return del(idsData);
    })
    .then(() => {
      loading.value = false;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {})
    .finally(() => {
      loading.value = false;
    });
}
function handleUpdate(row) {
  reset();

  form.value = JSON.parse(JSON.stringify(row));
  title.value = row.name;
  open.value = true;
}
getList();
</script>
<style lang="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: end;
}
</style>
