<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="日期" prop="salaryMonth">
        <el-date-picker
          v-model="queryParams.salaryMonth"
          type="month"
          value-format="YYYY-MM"
          placeholder="请选择日期"
          clearable
          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-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['erp:employee-salary:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['erp:employee-salary:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" @row-click="handleRowClick">
      <el-table-column label="日期" align="center" prop="salaryMonth" width="120" />
      <el-table-column label="应付职工薪酬" align="right" prop="payableSalary" width="130">
        <template #default="scope">
          {{ formatMoney(scope.row.payableSalary) }}
        </template>
      </el-table-column>
      <el-table-column label="工资" align="right" prop="basicSalary" width="120">
        <template #default="scope">
          {{ formatMoney(scope.row.basicSalary) }}
        </template>
      </el-table-column>
            <el-table-column label="工资部门" align="right" prop="salaryDept" width="120">
        <template #default="scope">
          {{ formatMoney(scope.row.salaryDept) }}
        </template>
      </el-table-column>
      <el-table-column label="职工福利" align="right" prop="employeeWelfare" width="120">
        <template #default="scope">
          {{ formatMoney(scope.row.employeeWelfare) }}
        </template>
      </el-table-column>
      <el-table-column label="社保保险费" align="right" prop="socialInsurance" width="120">
        <template #default="scope">
          {{ formatMoney(scope.row.socialInsurance) }}
        </template>
      </el-table-column>
      <el-table-column label="奖金" align="right" prop="bonus" width="120">
        <template #default="scope">
          {{ formatMoney(scope.row.bonus) }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="工会经费" align="right" prop="unionFund" width="120">
        <template #default="scope">
          {{ formatMoney(scope.row.unionFund) }}
        </template>
      </el-table-column>
      <el-table-column label="商业保险" align="right" prop="commercialInsurance" width="120">
        <template #default="scope">
          {{ formatMoney(scope.row.commercialInsurance) }}
        </template>
      </el-table-column>
      <el-table-column label="赔偿补偿" align="right" prop="compensation" width="120">
        <template #default="scope">
          {{ formatMoney(scope.row.compensation) }}
        </template>
      </el-table-column>
      <el-table-column label="安装人工费" align="right" prop="installationLabor" width="120">
        <template #default="scope">
          {{ formatMoney(scope.row.installationLabor) }}
        </template>formatMoney
      </el-table-column> -->
      <el-table-column label="个人所得税" align="right" prop="personalIncomeTax" width="120">
        <template #default="scope">
          {{ formatMoney(scope.row.personalIncomeTax) }}
        </template>
      </el-table-column>
            <el-table-column label="备注" align="right" prop="remark" width="120">
        <template #default="scope">
          {{ scope.row.remark }}
             <!-- {{ formatMoney(scope.row.remark) }} -->
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
        sortable
      />
      <el-table-column label="操作" align="center" fixed="right" width="150">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['erp:employee-salary:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['erp:employee-salary:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

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

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { EmployeeSalaryApi, EmployeeSalaryVO } from '@/api/erp/finance-alone/employeeSalary'
import EmployeeSalaryForm from './EmployeeSalaryForm.vue'

/** ERP 应付职工薪酬 列表 */
defineOptions({ name: 'ErpEmployeeSalary' })

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

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

/** 格式化金额 */
const formatMoney = (value: number | undefined) => {
  if (value === undefined || value === null) {
    return '0.00'
  }
  return value.toFixed(2)
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    console.log('+++++++++++++++++++++++++++++++++')
    const data = await EmployeeSalaryApi.getEmployeeSalaryPage(queryParams)
    console.log(data)
    // 兼容后端返回结构：{ list, total } 或 { code, data: { list, total } }
    const res = data && data.data ? data.data : data
    list.value = res?.list ?? []
    total.value = res?.total ?? res?.totalCount ?? 0
  } 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 EmployeeSalaryApi.deleteEmployeeSalary(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 行点击操作 */
const handleRowClick = (row: EmployeeSalaryVO, column: any, event: MouseEvent) => {
  // 检查是否点击了按钮、链接或其他交互元素
  const target = event.target as HTMLElement
  if (
    target.tagName === 'BUTTON' ||
    target.tagName === 'A' ||
    target.tagName === 'I' ||
    target.tagName === 'svg' ||
    target.closest('button') ||
    target.closest('a') ||
    target.closest('.el-button') ||
    target.closest('.el-checkbox')
  ) {
    return
  }
  openForm('update', row.id)
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await EmployeeSalaryApi.exportEmployeeSalary(queryParams)
    download.excel(data, '应付职工薪酬.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

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