<script setup>
import { computed, reactive, ref, toRef, watch } from 'vue'
import ScreenModal from '@/components/ScreenModal.vue'
import { getLivestockVetCounts } from '@/api/livestockResources'
import { displayEmpty } from '@/utils/displayEmpty'
import { useScreenTownVillageOptions } from '@/utils/screenTownVillage'

const props = defineProps({
  modelValue: { type: Boolean, default: false }
})

const emit = defineEmits(['update:modelValue'])

const visible = computed({
  get: () => props.modelValue,
  set: (v) => emit('update:modelValue', v)
})

const query = reactive({
  townDeptId: undefined
})

const { townOptions, loadTownVillageOptions } = useScreenTownVillageOptions(
  toRef(query, 'townDeptId')
)
const loading = ref(false)
const tableRows = ref([])
const total = ref(0)
const pageNum = ref(1)
const pageSize = ref(10)

async function fetchList() {
  loading.value = true
  try {
    const params = {
      pageNum: pageNum.value,
      pageSize: pageSize.value
    }
    if (query.townDeptId != null && query.townDeptId !== '') {
      params.townDeptId = query.townDeptId
    }
    const res = await getLivestockVetCounts(params)
    tableRows.value = res.rows || []
    total.value = Number(res.total) || 0
  } catch {
    tableRows.value = []
    total.value = 0
  } finally {
    loading.value = false
  }
}

function handleSearch() {
  pageNum.value = 1
  fetchList()
}

function handleReset() {
  query.townDeptId = undefined
  pageNum.value = 1
  fetchList()
}

function onOpen() {
  query.townDeptId = undefined
  pageNum.value = 1
  loadTownVillageOptions().then(() => fetchList())
}

function onClose() {
  tableRows.value = []
  total.value = 0
}

watch([pageNum, pageSize], () => {
  if (visible.value) {
    fetchList()
  }
})
</script>

<template>
  <ScreenModal
    v-model="visible"
    title="兽医数量明细"
    width="980px"
    height="720px"
    max-width="98vw"
    max-height="94vh"
    @open="onOpen"
    @close="onClose"
  >
    <el-form :inline="true" :model="query" class="vc-form" @submit.prevent>
      <el-form-item label="乡镇">
        <el-select
          v-model="query.townDeptId"
          placeholder="全部乡镇"
          clearable
          filterable
          style="width: 200px"
        >
          <el-option
            v-for="t in townOptions"
            :key="t.deptId"
            :label="t.deptName"
            :value="t.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="tableRows"
      border
      stripe
      height="100%"
      class="vc-table"
      empty-text="暂无数据"
    >
      <el-table-column prop="townName" label="乡镇" min-width="160" show-overflow-tooltip>
        <template #default="{ row }">{{ displayEmpty(row.townName) }}</template>
      </el-table-column>
      <el-table-column prop="vetCount" label="数量(人)" min-width="120" align="right">
        <template #default="{ row }">{{ displayEmpty(row.vetCount) }}</template>
      </el-table-column>
    </el-table>

    <div class="vc-pager">
      <el-pagination
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        background
        layout="total, sizes, prev, pager, next"
        :total="total"
        :page-sizes="[10, 20, 50]"
      />
    </div>
  </ScreenModal>
</template>

<style scoped>
.vc-form {
  flex-shrink: 0;
  margin-bottom: 8px;
}

.vc-form :deep(.el-form-item__label) {
  color: #a8d4c8;
}

.vc-table {
  width: 100%;
  flex: 1;
  min-height: 0;
}

.vc-pager {
  flex-shrink: 0;
  display: flex;
  justify-content: flex-end;
  margin-top: 12px;
}
</style>
