<script setup>
import { computed, reactive, ref, watch } from 'vue'
import ScreenModal from '@/components/ScreenModal.vue'
import { getCommonProsperityProjectTypeDetails } from '@/api/commonProsperity'
import { displayEmpty } from '@/utils/displayEmpty'
import { buildScreenYearOptions } from '@/utils/screenYearOptions'
import { useDialogStatYearSync } from '@/utils/useDialogStatYearSync'

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({
  statYear: undefined
})

const loading = ref(false)
const tableRows = ref([])
const total = ref(0)
const pageNum = ref(1)
const pageSize = ref(10)

const yearSelectOptions = buildScreenYearOptions()

const { syncDialogYearFromGlobal } = useDialogStatYearSync(query, {
  visible,
  onSynced: () => {
    pageNum.value = 1
    fetchList()
  }
})


async function fetchList() {
  loading.value = true
  try {
    const params = {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      statYear: query.statYear
    }
    const res = await getCommonProsperityProjectTypeDetails(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() {
  syncDialogYearFromGlobal()
  pageNum.value = 1
  fetchList()
}

function onOpen() {
  syncDialogYearFromGlobal()
  pageNum.value = 1
  fetchList()
}

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

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

<template>
  <ScreenModal
    v-model="visible"
    title="共富项目分类情况明细"
    width="1280px"
    height="720px"
    max-width="98vw"
    max-height="94vh"
    @open="onOpen"
    @close="onClose"
  >
    <el-form :inline="true" :model="query" class="ptd-form" @submit.prevent>
      <el-form-item label="年份">
        <el-select v-model="query.statYear" clearable placeholder="年份" style="width: 120px">
          <el-option v-for="y in yearSelectOptions" :key="y" :label="`${y}年`" :value="y" />
        </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="ptd-table"
      empty-text="暂无数据"
    >
      <el-table-column prop="statYear" label="所属年份" width="100" align="center">
        <template #default="{ row }">{{ displayEmpty(row.statYear) }}</template>
      </el-table-column>
      <el-table-column prop="totalCount" label="项目总数(个)" min-width="120" align="right">
        <template #default="{ row }">{{ displayEmpty(row.totalCount) }}</template>
      </el-table-column>
      <el-table-column prop="heMeiVillageCount" label="和美乡村建设项目(个)" min-width="160" align="right">
        <template #default="{ row }">{{ displayEmpty(row.heMeiVillageCount) }}</template>
      </el-table-column>
      <el-table-column
        prop="habitatImproveCount"
        label="人居环境整治提升项目(个)"
        min-width="180"
        align="right"
      >
        <template #default="{ row }">{{ displayEmpty(row.habitatImproveCount) }}</template>
      </el-table-column>
      <el-table-column prop="otherProjectCount" label="其他项目投入(个)" min-width="140" align="right">
        <template #default="{ row }">{{ displayEmpty(row.otherProjectCount) }}</template>
      </el-table-column>
    </el-table>

    <div class="ptd-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>
.ptd-form {
  flex-shrink: 0;
  margin-bottom: 8px;
}

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

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

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