<template>
  <div class="app-container">
    <el-card shadow="never">
      <template #header>
        <div class="card-header"><span>用水单位管理</span><el-button type="primary" size="small" style="float:right" @click="handleAdd">新增单位</el-button></div>
      </template>
      <el-form :model="queryParams" :inline="true" size="small">
        <el-form-item label="单位名称"><el-input v-model="queryParams.name" placeholder="请输入" clearable @keyup.enter="handleQuery" /></el-form-item>
        <el-form-item label="所在城市"><el-input v-model="queryParams.city" placeholder="请输入" clearable @keyup.enter="handleQuery" /></el-form-item>
        <el-form-item label="过期筛选">
          <el-select v-model="queryParams.overdue" placeholder="全部" clearable @change="handleQuery">
            <el-option label="全部" value="" />
            <el-option label="已过期（超5年）" value="yes" />
            <el-option label="未过期" value="no" />
          </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-button type="danger" size="small" :disabled="!ids.length" style="margin-bottom:12px" @click="handleBatchDelete">批量删除</el-button>

      <el-table v-loading="loading" :data="list" stripe :row-class-name="rowClassName" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" />
        <el-table-column prop="id" label="ID" width="60" />
        <el-table-column prop="name" label="单位名称" min-width="160" />
        <el-table-column prop="province" label="省" width="80" />
        <el-table-column prop="city" label="市" width="80" />
        <el-table-column prop="contacts" label="联系人" width="100" />
        <el-table-column prop="telphone" label="联系电话" width="130" />
        <el-table-column prop="fax" label="传真" width="120" />
        <el-table-column prop="approvalDate" label="批准日期" width="120" />
        <el-table-column label="操作" width="240" fixed="right">
          <template #default="s">
            <el-button type="warning" link icon="Folder" @click="$router.push('/slgc/unit/file?id=' + s.row.id)">附件</el-button>
            <el-button type="primary" link @click="$router.push('/slgc/unit/record?id=' + s.row.id)">用水记录</el-button>
            <el-button type="primary" link icon="Edit" @click="handleEdit(s.row)">编辑</el-button>
            <el-button type="danger" link icon="Delete" @click="handleDelete(s.row)">删除</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-card>

    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form :model="form" label-width="100px">
        <el-row>
          <el-col :span="12"><el-form-item label="单位名称"><el-input v-model="form.name" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="联系人"><el-input v-model="form.contacts" /></el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="联系电话"><el-input v-model="form.telphone" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="传真"><el-input v-model="form.fax" /></el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="省"><el-input v-model="form.province" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="市"><el-input v-model="form.city" /></el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="24"><el-form-item label="地址"><el-input v-model="form.address" /></el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="经度"><el-input v-model="form.longitude" /></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="纬度"><el-input v-model="form.latitude" /></el-form-item></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><el-form-item label="批准日期"><el-date-picker v-model="form.approvalDate" type="date" value-format="YYYY-MM-DD" style="width:100%" /></el-form-item></el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Unit">
import { getUnitList, getUnit, saveUnit, delUnit } from '@/api/slgc/unit/index'
const { proxy } = getCurrentInstance()
const loading = ref(true); const total = ref(0); const open = ref(false); const title = ref('')
const list = ref([]); const ids = ref([])
const queryParams = reactive({ pageNum: 1, pageSize: 10, name: undefined, city: undefined, overdue: undefined })
const form = ref({})

function isOverdue(row) {
  if (!row.approvalDate) return false
  const fiveYearsAgo = new Date()
  fiveYearsAgo.setFullYear(fiveYearsAgo.getFullYear() - 5)
  return new Date(row.approvalDate) <= fiveYearsAgo
}

function rowClassName({ row }) {
  return isOverdue(row) ? 'overdue-row' : ''
}

function getList() {
  loading.value = true
  getUnitList(queryParams).then(res => {
    let rows = res.rows || []
    if (queryParams.overdue === 'yes') {
      rows = rows.filter(r => isOverdue(r))
    } else if (queryParams.overdue === 'no') {
      rows = rows.filter(r => !isOverdue(r))
    }
    list.value = rows
    total.value = res.total
    loading.value = false
  })
}

function handleQuery() { queryParams.pageNum = 1; getList() }
function resetQuery() { queryParams.name = undefined; queryParams.city = undefined; queryParams.overdue = undefined; handleQuery() }
function handleSelectionChange(selection) { ids.value = selection.map(s => s.id) }
function handleEdit(row) { getUnit(row.id).then(res => { form.value = res.data; title.value = '编辑单位'; open.value = true }) }
function handleAdd() { form.value = {}; title.value = '新增单位'; open.value = true }
function submitForm() { saveUnit(form.value).then(() => { proxy.$modal.msgSuccess('操作成功'); open.value = false; getList() }) }
function cancel() { open.value = false }
function handleDelete(row) {
  proxy.$modal.confirm('确认删除"' + row.name + '"？').then(() => {
    delUnit(row.id).then(() => { proxy.$modal.msgSuccess('删除成功'); getList() })
  })
}

function handleBatchDelete() {
  if (!ids.value.length) return
  proxy.$modal.confirm('确认删除选中的 ' + ids.value.length + ' 条记录？').then(() => {
    delUnit(ids.value.join(',')).then(() => { proxy.$modal.msgSuccess('删除成功'); getList() })
  })
}

onMounted(() => { getList() })
</script>

<style scoped>
.card-header { font-weight: 600; font-size: 16px; }
:deep(.overdue-row) { background-color: #fdf6ec !important; }
:deep(.overdue-row td) { background-color: #fdf6ec !important; }
</style>
