<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <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="sttp">
        <el-select v-model="queryParams.sttp" placeholder="全部" clearable style="width: 120px">
          <el-option label="水质" value="sz" />
          <el-option label="水量" value="sl" />
          <el-option label="常规" value="cg" />
          <el-option label="水文" value="sw" />
          <el-option label="巡测" value="xc" />
        </el-select>
      </el-form-item>
      <el-form-item label="河流" prop="rvnm">
        <el-input v-model="queryParams.rvnm" placeholder="请输入河流" clearable style="width: 140px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="年份" prop="year">
        <el-input v-model="queryParams.year" placeholder="请输入年份" clearable style="width: 120px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="省份" prop="prov">
        <el-select v-model="queryParams.prov" placeholder="全部" clearable style="width: 120px">
          <el-option label="江苏" value="江苏" />
          <el-option label="浙江" value="浙江" />
          <el-option label="上海" value="上海" />
        </el-select>
      </el-form-item>
      <el-form-item label="监测项目" prop="jcxm">
        <el-select v-model="queryParams.jcxm" placeholder="全部" clearable style="width: 120px">
          <el-option label="水质" value="sz" />
          <el-option label="水量" value="sl" />
          <el-option label="水量+水质" value="slsz" />
        </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="['yjjt:station:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['yjjt:station:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['yjjt: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="stcd" width="120" />
      <el-table-column label="站名" prop="stnm" width="140" :show-overflow-tooltip="true" />
      <el-table-column label="站类别" prop="sttp" width="80" align="center">
        <template #default="scope">
          <el-tag size="small">{{ sttpText(scope.row.sttp) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="河流" prop="rvnm" width="120" />
      <el-table-column label="站址" prop="stlc" width="160" :show-overflow-tooltip="true" />
      <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="admunit" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="监测单位" prop="jcdw" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="监测项目" prop="jcxm" width="100" align="center">
        <template #default="scope">
          <span>{{ jcxmText(scope.row.jcxm) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年份" prop="year" width="100" />
      <el-table-column label="省份" prop="prov" width="80" />
      <el-table-column label="创建时间" prop="createTime" width="160" align="center" />
      <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="['yjjt:station:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['yjjt: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="800px" 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="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="sttp">
              <el-select v-model="form.sttp" placeholder="请选择" style="width: 100%">
                <el-option label="水质" value="sz" />
                <el-option label="水量" value="sl" />
                <el-option label="常规" value="cg" />
                <el-option label="水文" value="sw" />
                <el-option label="巡测" value="xc" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="监测项目" prop="jcxm">
              <el-select v-model="form.jcxm" placeholder="请选择" style="width: 100%">
                <el-option label="水质" value="sz" />
                <el-option label="水量" value="sl" />
                <el-option label="水量+水质" value="slsz" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="河流" prop="rvnm">
              <el-input v-model="form.rvnm" placeholder="请输入河流名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="站址" prop="stlc">
              <el-input v-model="form.stlc" placeholder="请输入站址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="经度" prop="lgtd">
              <el-input v-model="form.lgtd" placeholder="请输入经度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="lttd">
              <el-input v-model="form.lttd" placeholder="请输入纬度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="管理单位" prop="admunit">
              <el-input v-model="form.admunit" placeholder="请输入管理单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="监测单位" prop="jcdw">
              <el-input v-model="form.jcdw" placeholder="请输入监测单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="监测频次" prop="jcpc">
              <el-input v-model="form.jcpc" placeholder="请输入监测频次" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年份" prop="year">
              <el-input v-model="form.year" placeholder="请输入年份" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="省份" prop="prov">
              <el-select v-model="form.prov" placeholder="请选择" style="width: 100%">
                <el-option label="江苏" value="江苏" />
                <el-option label="浙江" value="浙江" />
                <el-option label="上海" value="上海" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行政区划" prop="addvcd">
              <el-input v-model="form.addvcd" placeholder="请输入行政区划编码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" 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>
  </div>
</template>

<script setup name="YjjtStation">
import { listStation, getStation, addStation, updateStation, delStation } from "@/api/yjjt/station";

const { proxy } = getCurrentInstance();

const stationList = 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 sttpOptions = { sz: '水质', sl: '水量', cg: '常规', sw: '水文', xc: '巡测' };
const jcxmOptions = { sz: '水质', sl: '水量', slsz: '水量+水质' };

function sttpText(val) { return sttpOptions[val] || val; }
function jcxmText(val) { return jcxmOptions[val] || val; }

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    stcd: undefined,
    stnm: undefined,
    sttp: undefined,
    rvnm: undefined,
    year: undefined,
    prov: undefined,
    jcxm: undefined,
  },
  rules: {
    stcd: [{ required: true, message: "站码不能为空", trigger: "blur" }],
    stnm: [{ required: true, message: "站名不能为空", trigger: "blur" }],
  },
});

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

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

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,
    stcd: undefined,
    stnm: undefined,
    sttp: undefined,
    rvnm: undefined,
    stlc: undefined,
    lgtd: undefined,
    lttd: undefined,
    addvcd: undefined,
    admunit: undefined,
    jcdw: undefined,
    jcpc: undefined,
    jcxm: undefined,
    year: undefined,
    prov: undefined,
    remark: undefined,
  };
  proxy.resetForm("formRef");
}

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;
    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("/yjjt/station/export", { ...queryParams.value }, `yjjt_station_${new Date().getTime()}.xlsx`);
}

getList();
</script>
