<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单位" prop="unitCode">
        <el-select v-model="queryParams.unitCode" placeholder="全部" clearable style="width: 200px" @change="handleQueryUnitChange">
          <el-option v-for="item in unitList" :key="item.unitCode" :label="item.unitName" :value="item.unitCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="业务域" prop="bizCode">
        <el-select v-model="queryParams.bizCode" placeholder="全部" clearable style="width: 200px">
          <el-option v-for="item in queryBizOptions" :key="item.bizCode" :label="item.bizName" :value="item.bizCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="站码" prop="stcd">
        <el-input v-model="queryParams.stcd" placeholder="请输入站码" clearable style="width: 160px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="站名" prop="stnm">
        <el-input v-model="queryParams.stnm" placeholder="请输入站名" clearable style="width: 160px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="全部" clearable style="width: 100px">
          <el-option label="正常" value="0" />
          <el-option label="停用" value="1" />
        </el-select>
      </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="['gx:station:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="Upload" @click="handleImport" v-hasPermi="['gx:station:add']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['gx:station:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['gx:station:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="stationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单位名称" prop="unitName" width="250" :show-overflow-tooltip="true" />
      <el-table-column label="业务域" prop="bizName" width="160" :show-overflow-tooltip="true" />
<!--      <el-table-column label="站码" prop="stcd" width="120" />-->
      <el-table-column label="站名" prop="stnm" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="河流" prop="rvnm" width="120" />
      <el-table-column label="湖泊" prop="hnnm" width="100" />
      <el-table-column label="经度" prop="lgtd" width="100" align="center" />
      <el-table-column label="纬度" prop="lttd" width="100" align="center" />
      <el-table-column label="站址" prop="stlc" :show-overflow-tooltip="true" />
      <el-table-column label="状态" prop="state" width="80" align="center">
        <template #default="scope">
          <el-tag :type="scope.row.state === '0' ? 'success' : 'danger'" size="small">
            {{ scope.row.state === '0' ? '正常' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['gx:station:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['gx:station: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="700px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="单位" prop="unitCode">
              <el-select v-model="form.unitCode" placeholder="请选择" style="width: 100%" @change="handleUnitChange">
                <el-option v-for="item in unitList" :key="item.unitCode" :label="item.unitName" :value="item.unitCode" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务域" prop="bizCode">
              <el-select v-model="form.bizCode" placeholder="请选择" style="width: 100%">
                <el-option v-for="item in bizOptions" :key="item.bizCode" :label="item.bizName" :value="item.bizCode" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="站码" prop="stcd">
              <el-input v-model="form.stcd" placeholder="请输入站码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="站名" prop="stnm">
              <el-input v-model="form.stnm" placeholder="请输入站名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="站址" prop="stlc">
              <el-input v-model="form.stlc" placeholder="请输入站址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经度" prop="lgtd">
              <el-input-number v-model="form.lgtd" :precision="6" :step="0.001" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="纬度" prop="lttd">
              <el-input-number v-model="form.lttd" :precision="6" :step="0.001" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="河流" prop="rvnm">
              <el-input v-model="form.rvnm" placeholder="请输入河流名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="湖泊" prop="hnnm">
              <el-input v-model="form.hnnm" placeholder="请输入湖泊名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="流域" prop="bsnm">
              <el-input v-model="form.bsnm" placeholder="请输入流域名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="状态" prop="state">
              <el-radio-group v-model="form.state">
                <el-radio value="0">正常</el-radio>
                <el-radio value="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </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>

    <!-- 站点导入对话框 -->
    <excel-import-dialog ref="importStationRef" title="站点导入" action="/gx/station/importData" template-action="/gx/station/importTemplate" template-file-name="station_template" update-support-label="是否更新已经存在的站点数据" @success="getList" />
  </div>
</template>

<script setup name="GxStation">
import { listStation, getStation, addStation, updateStation, delStation } from "@/api/gx/station";
import { listBiz } from "@/api/gx/biz";
import { listUnit } from "@/api/gx/unit";
import ExcelImportDialog from "@/components/ExcelImportDialog";
import { computed } from "vue";

const { proxy } = getCurrentInstance();

const stationList = ref([]);
const bizOptions = ref([]);
const allBizOptions = ref([]);
const unitList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const open = ref(false);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    bizCode: undefined,
    stcd: undefined,
    stnm: undefined,
    state: undefined,
  },
  rules: {
    bizCode: [{ required: true, message: "业务域不能为空", trigger: "change" }],
    stcd: [{ required: true, message: "站码不能为空", trigger: "blur" }],
    stnm: [{ required: true, message: "站名不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

const queryBizOptions = computed(() => {
  if (queryParams.value.unitCode) {
    return allBizOptions.value.filter((item) => item.bizUnit === queryParams.value.unitCode);
  }
  return allBizOptions.value;
});

function getList() {
  loading.value = true;
  listStation(queryParams.value).then((response) => {
    stationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function getBizOptions() {
  listBiz({ pageNum: 1, pageSize: 999 }).then((response) => {
    allBizOptions.value = response.rows;
    bizOptions.value = response.rows;
  });
}

function getUnitList() {
  listUnit({}).then((response) => {
    unitList.value = response.rows;
  });
}

function handleUnitChange(unitCode) {
  if (unitCode) {
    bizOptions.value = allBizOptions.value.filter((item) => item.bizUnit === unitCode);
  } else {
    bizOptions.value = allBizOptions.value;
  }
}

function handleQueryUnitChange(unitCode) {
  queryParams.value.bizCode = undefined;
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

function reset() {
  form.value = {
    id: undefined,
    unitCode: undefined,
    bizCode: undefined,
    stcd: undefined,
    stnm: undefined,
    dbcd: undefined,
    stlc: undefined,
    lgtd: undefined,
    lttd: undefined,
    rvnm: undefined,
    hnnm: undefined,
    bsnm: undefined,
    state: "0",
    flag1h3h: undefined,
    note: undefined,
  };
  proxy.resetForm("formRef");
}

function cancel() {
  open.value = false;
}

function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增站点";
}

function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getStation(id).then((response) => {
    form.value = response.data;
    if (form.value.unitCode) {
      handleUnitChange(form.value.unitCode);
    }
    open.value = true;
    title.value = "修改站点";
  });
}

function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateStation(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addStation(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function handleDelete(row) {
  const deleteIds = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除站点编号为"' + deleteIds + '"的数据项？').then(() => {
    return delStation(deleteIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

function handleExport() {
  proxy.download("/gx/station/export", { ...queryParams.value }, `station_${new Date().getTime()}.xlsx`);
}

/** 导入按钮操作 */
function handleImport() {
  proxy.$refs["importStationRef"].open();
}

getBizOptions();
getUnitList();
getList();
</script>
