<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="规则名称" prop="ruleName">
        <el-input
          v-model="queryParams.ruleName"
          placeholder="请输入规则名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
    >
    <el-table-column type="selection" width="55" />
      <el-table-column label="规则名称" align="center" prop="ruleName" />
      <el-table-column label="个税类型" align="center" prop="ruleName" />
      <el-table-column label="计税周期类型" align="center" prop="cycleType">
        <template #default="scope">
          <span>{{ scope.row.cycleType===1?'上年12月到今年11月（对应的工资发放方式为次月发上月工资）' : scope.row.cycleType===2 ?'今年1月到12月（对应的工资发放方式为当月发当月工资）':'' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否计税" align="center" prop="isTax" width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_YES_OR_NO" :value="scope.row.isTax" />
        </template>
      </el-table-column>
      <el-table-column label="起征点" align="center" prop="markingPoint" />
      <el-table-column label="起征点" align="center" prop="markingPoint">
        <template #default="scope">
          <span>{{ scope.row.markingPoint+'元/月'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="个税结果(保留小数点)" align="center" prop="decimalPoint" />
<!--      <el-table-column label="操作" align="center" min-width="120px">-->
<!--        <template #default="scope">-->
<!--          <el-button-->
<!--            link-->
<!--            type="primary"-->
<!--            @click="openForm('update', scope.row.id)"-->
<!--            v-hasPermi="['hr:salary-tax-rule:update']"-->
<!--          >-->
<!--            编辑-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SalaryTaxRuleForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { SalaryTaxRuleApi, SalaryTaxRule } from '@/api/hr/salarytaxrule'
import SalaryTaxRuleForm from './SalaryTaxRuleForm.vue'
import {DICT_TYPE} from "@/utils/dict";

/** 计税规则 列表 */
defineOptions({ name: 'HrSalaryTaxRule' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SalaryTaxRule[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  ruleName: undefined,
  taxType: undefined,
  isTax: undefined,
  markingPoint: undefined,
  decimalPoint: undefined,
  cycleType: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SalaryTaxRuleApi.getSalaryTaxRulePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SalaryTaxRuleApi.deleteSalaryTaxRule(id)
    message.success(t('common.delSuccess'))
    currentRow.value = {}
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除计税规则 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await SalaryTaxRuleApi.deleteSalaryTaxRuleList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: SalaryTaxRule[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SalaryTaxRuleApi.exportSalaryTaxRule(queryParams)
    download.excel(data, '计税规则.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
