<script setup>
import { computed, reactive, ref, watch } from 'vue'
import ScreenModal from '@/components/ScreenModal.vue'
import { getPerCapitaIncomes } from '@/api/home'
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)

/** 弹框年份：2021～当年 */
const yearSelectOptions = buildScreenYearOptions()

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


function formatPercent(val) {
  if (val === null || val === undefined || val === '') {
    return displayEmpty(null)
  }
  const n = Number(val)
  if (Number.isNaN(n)) {
    return String(val)
  }
  return `${n}%`
}

async function fetchList() {
  loading.value = true
  try {
    const params = {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      statYear: query.statYear
    }
    const res = await getPerCapitaIncomes(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="1480px"
    height="780px"
    max-width="98vw"
    max-height="94vh"
    @open="onOpen"
    @close="onClose"
  >
    <el-form :inline="true" :model="query" class="pci-form" @submit.prevent>
      <el-form-item label="年份">
        <el-select v-model="query.statYear" clearable placeholder="年份" style="width: 110px">
          <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="pci-table"
      empty-text="暂无数据"
    >
      <el-table-column prop="statYear" label="年份" width="100" align="center" />
      <el-table-column
        label="农村居民人均可支配收入(元)"
        min-width="200"
        align="right"
        show-overflow-tooltip
      >
        <template #default="{ row }">{{ displayEmpty(row.ruralDisposableIncome) }}</template>
      </el-table-column>
      <el-table-column label="增幅" min-width="120" align="right" show-overflow-tooltip>
        <template #default="{ row }">{{ displayEmpty(row.incomeIncrease) }}</template>
      </el-table-column>
      <el-table-column label="增速" min-width="120" align="right" show-overflow-tooltip>
        <template #default="{ row }">{{ formatPercent(row.incomeGrowthRate) }}</template>
      </el-table-column>
      <el-table-column
        label="脱贫人口人均纯收入(元)"
        min-width="200"
        align="right"
        show-overflow-tooltip
      >
        <template #default="{ row }">{{ displayEmpty(row.povertyNetIncome) }}</template>
      </el-table-column>
    </el-table>

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

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

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

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