<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <!-- 搜索表单 -->
      <search-form 
        v-show="showSearch" 
        :model="search" 
        ref="searchFormRef"
        @keyup-enter="handleFilter"
      >
        <template #default="{ visible }">
          <template v-if="visible">
            <el-form-item label="姓名" prop="realName">
              <el-input
                v-model="search.realName"
                placeholder="请输入姓名"
                clearable
              />
            </el-form-item>

            <el-form-item label="工号" prop="teacherNo">
              <el-input
                v-model="search.teacherNo"
                placeholder="请输入工号"
                clearable
              />
            </el-form-item>

            <el-form-item label="年份" prop="year">
              <el-date-picker
                v-model="search.year"
                type="year"
                format="YYYY"
                value-format="YYYY"
                placeholder="请选择年份"
                clearable
              />
            </el-form-item>
          </template>
        </template>

        <!-- 查询和重置按钮 -->
        <template #actions>
          <el-form-item>
            <el-button type="primary" @click="handleFilter" icon="Search">查询</el-button>
            <el-button @click="resetQuery" icon="Refresh">重置</el-button>
          </el-form-item>
        </template>
      </search-form>

      <!-- 操作按钮 -->
      <el-row>
        <div class="mb15">
          <el-button
            v-if="hasAuth('teacher_award_import')"
            type="primary"
            plain
            icon="UploadFilled"
            @click="handleImportBaseSalary">绩效导入
          </el-button>
        </div>
      </el-row>

      <!-- 表格 -->
      <el-table
        ref="tableRef"
        :data="state.dataList"
        v-loading="state.loading"
        border
        :row-key="(row: any) => row.id ?? String(row.teacherNo) + row.year"
        :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle"
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        
        <el-table-column label="姓名/工号" min-width="150" align="center">
          <template #default="scope">
            <TeacherNameNo :name="scope.row.realName" :no="scope.row.teacherNo" />
          </template>
        </el-table-column>
        
        <el-table-column prop="startDate" label="开始日期" width="120" align="center" />
        
        <el-table-column prop="endDate" label="结束日期" width="120" align="center" />
        
        <el-table-column prop="year" label="年份" width="100" align="center" />
        
        <el-table-column prop="income" label="累计应纳税所得额" min-width="150" align="center" show-overflow-tooltip />
        
        <el-table-column prop="taxRate" label="税率" width="100" align="center" />
        
        <el-table-column prop="deductMoney" label="速算扣除" min-width="120" align="center" show-overflow-tooltip />
        
        <el-table-column prop="realMoney" label="应补(退)税额" min-width="130" align="center" show-overflow-tooltip />
      </el-table>

      <!-- 分页 -->
      <pagination
        v-bind="state.pagination"
        @current-change="currentChangeHandle"
        @size-change="sizeChangeHandle"
      />

      <!-- 导入组件 -->
      <import-award-tax ref="importAwardTaxRef" @refreshData="handleFilter" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { BasicTableProps, useTable } from '/@/hooks/table'
import { useAuth } from '/@/hooks/auth'
import { fetchList } from '/@/api/professional/salaries/teacherawardtax'
const ImportAwardTax = defineAsyncComponent(() => import('./importAwardTax.vue'))
const { hasAuth } = useAuth()

// 表格引用
const tableRef = ref()
const searchFormRef = ref()
const importAwardTaxRef = ref()

// 搜索表单显示状态
const showSearch = ref(true)

// 搜索表单数据
const search = reactive({
  realName: '',
  teacherNo: '',
  year: ''
})

// 配置 useTable
const state: BasicTableProps = reactive<BasicTableProps>({
  pageList: async (params: any) => {
    const response = await fetchList(params)
    return {
      data: {
        records: response.data.records || [],
        total: response.data.total || 0
      }
    }
  },
  queryForm: search
})

const { getDataList, currentChangeHandle, sizeChangeHandle, tableStyle } = useTable(state)

// 查询
const handleFilter = () => {
  getDataList() // 查询后跳转到第一页
}

// 重置
const resetQuery = () => {
  searchFormRef.value?.formRef?.resetFields()
  Object.assign(search, {
    realName: '',
    teacherNo: '',
    year: ''
  })
  handleFilter()
}

// 导入
const handleImportBaseSalary = () => {
  importAwardTaxRef.value?.init()
}
</script>

<style lang="scss" scoped>
</style>
