<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: 140px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="河流" prop="rvnm">
        <el-select v-model="queryParams.rvnm" placeholder="全部" clearable style="width: 160px">
          <el-option v-for="item in rvnmOptions" :key="item" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="检测时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </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="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['yjjt:sl:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['yjjt:sl:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="dataList" @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="rvnm" width="120" />
      <el-table-column label="检测时间" prop="spt" width="170" align="center" />
      <el-table-column label="水位(m)" prop="z" width="100" align="right" />
      <el-table-column label="假定水位(m)" prop="zJ" width="120" align="right" />
      <el-table-column label="流量(m³/s)" prop="q" width="120" align="right" />
      <el-table-column label="流向" prop="liuxiang" width="80" align="center" />
      <el-table-column label="上传单位" prop="addvcd" width="100" />
      <el-table-column label="操作" width="100" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['yjjt:sl: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" />
  </div>
</template>

<script setup name="YjjtSlData">
import { listSlData, delSlData, getRvnmList } from "@/api/yjjt/sl";

const { proxy } = getCurrentInstance();

const dataList = ref([]);
const rvnmOptions = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const multiple = ref(true);
const total = ref(0);
const dateRange = ref([]);

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  stcd: undefined,
  rvnm: undefined,
});

function getList() {
  loading.value = true;
  const params = { ...queryParams.value };
  if (dateRange.value && dateRange.value.length === 2) {
    params.params = { beginTime: dateRange.value[0], endTime: dateRange.value[1] };
  }
  listSlData(params).then((response) => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function loadRvnmList() {
  getRvnmList().then((response) => {
    rvnmOptions.value = response.data || [];
  });
}

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

function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

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

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

function handleExport() {
  proxy.download("/yjjt/sl/data/export", { ...queryParams.value }, `yjjt_sl_${new Date().getTime()}.xlsx`);
}

loadRvnmList();
getList();
</script>
