<template>
  <div class="app-container">
    <el-alert type="warning" :closable="false" show-icon style="margin-bottom: 10px">
      本页面可查看所有工资录入数据；点击“补录”后将该记录标记为已补录（isMakeup=1）。
    </el-alert>

    <el-form ref="queryRef" :model="queryParams" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="业务日期" prop="entryDate">
        <el-date-picker clearable v-model="queryParams.entryDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择业务日期" />
      </el-form-item>
      <el-form-item label="员工姓名" prop="empName">
        <el-input v-model="queryParams.empName" placeholder="请输入员工姓名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="查看范围" prop="makeupView">
        <el-select v-model="queryParams.makeupView" placeholder="请选择查看范围" style="width: 150px" @change="handleQuery">
          <el-option label="全部查看" value="all" />
          <el-option label="仅看补录" value="onlyMakeup" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button size="small" icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button size="small" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="业务日期" align="center" prop="entryDate" width="120" />
      <el-table-column label="员工姓名" align="center" prop="empName" width="120" />
      <el-table-column label="计费类型" align="center" prop="billingType" width="100">
        <template #default="scope">{{ formatBillingType(scope.row.billingType) }}</template>
      </el-table-column>
      <el-table-column label="工种" align="center" prop="workTypeName" min-width="140" />
      <el-table-column label="工作时间/加工数量" align="center" prop="workload" width="140" />
      <el-table-column label="单价" align="center" prop="unitPrice" width="110" />
      <el-table-column label="奖惩金额" align="center" prop="extraAmount" width="130" />
      <el-table-column label="奖惩原因" align="center" prop="extraReason" min-width="160" :show-overflow-tooltip="true" />
      <el-table-column label="补录状态" align="center" prop="isMakeup" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.isMakeup === '1' ? 'warning' : 'info'">{{ scope.row.isMakeup === '1' ? '已补录' : '未补录' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="补录责任人" align="center" prop="makeupResponsible" width="120" />
      <el-table-column label="补录原因" align="center" prop="makeupReason" min-width="180" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" width="220">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleMakeup(scope.row)">补录</el-button>
          <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" v-model="open" width="640px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="业务日期" prop="entryDate">
          <el-date-picker clearable v-model="form.entryDate" type="date" value-format="YYYY-MM-DD" style="width: 100%" />
        </el-form-item>
        <el-form-item label="员工姓名" prop="empName"><el-input v-model="form.empName" /></el-form-item>
        <el-form-item label="计费类型" prop="billingType">
          <el-select v-model="form.billingType" style="width: 100%">
            <el-option label="小时工" value="1" />
            <el-option label="计件工" value="2" />
            <el-option label="天工" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="工种" prop="workTypeName"><el-input v-model="form.workTypeName" /></el-form-item>
        <el-form-item :label="form.billingType === '2' ? '加工数量' : '工作时间'" prop="workload"><el-input v-model="form.workload" /></el-form-item>
        <el-form-item label="单价" prop="unitPrice" v-if="form.billingType === '2'"><el-input v-model="form.unitPrice" placeholder="元/件" /></el-form-item>
        <el-form-item label="奖惩金额" prop="extraAmount"><el-input v-model="form.extraAmount" placeholder="负数为惩罚金额" /></el-form-item>
        <el-form-item label="奖惩原因" prop="extraReason"><el-input v-model="form.extraReason" placeholder="请输入奖惩原因" /></el-form-item>
        <el-form-item label="补录原因" prop="makeupReason"><el-input type="textarea" v-model="form.makeupReason" /></el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WageMakeup">
import { listWageEntryDetail, updateWageEntryDetail, delWageEntryDetail } from '@/api/oa/wageEntryDetail'
import useUserStore from '@/store/modules/user'
import { onMounted } from 'vue'

const { proxy } = getCurrentInstance()
const userStore = useUserStore()
const list = ref([])
const loading = ref(true)
const total = ref(0)
const showSearch = ref(true)
const open = ref(false)
const title = ref('')
const buttonLoading = ref(false)

// 存储累计金额数据
const cumulativeAmounts = ref({})

// 从localStorage加载累计金额数据
function loadCumulativeAmounts() {
  const stored = localStorage.getItem('wageCumulativeAmounts')
  if (stored) {
    try {
      cumulativeAmounts.value = JSON.parse(stored)
    } catch (e) {
      console.error('Failed to parse cumulative amounts:', e)
      cumulativeAmounts.value = {}
    }
  }
}

// 保存累计金额数据到localStorage
function saveCumulativeAmounts() {
  localStorage.setItem('wageCumulativeAmounts', JSON.stringify(cumulativeAmounts.value))
}

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 50,
    entryDate: undefined,
    empName: undefined,
    makeupView: 'all'
  },
  rules: {
    entryDate: [{ required: true, message: '业务日期不能为空', trigger: 'change' }],
    empId: [{ required: true, message: '员工ID不能为空', trigger: 'blur' }],
    billingType: [{ required: true, message: '计费类型不能为空', trigger: 'change' }],
    rateId: [{ required: true, message: '费率ID不能为空', trigger: 'blur' }],
    workload: [{ required: true, message: '工作量不能为空', trigger: 'blur' }],
    makeupReason: [{ required: true, message: '补录原因不能为空', trigger: 'blur' }]
  }
})

const { queryParams, form, rules } = toRefs(data)

function formatBillingType(type) {
  if (type === '1') return '小时工'
  if (type === '2') return '计件工'
  if (type === '3') return '天工'
  return '-'
}

function buildQuery() {
  const q = {
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize,
    entryDate: queryParams.value.entryDate,
    empName: queryParams.value.empName
  }
  if (queryParams.value.makeupView === 'onlyMakeup') {
    q.isMakeup = '1'
  }
  return q
}

function getList() {
  loading.value = true
  listWageEntryDetail(buildQuery()).then(res => {
    list.value = res.rows || []
    total.value = res.total || 0
  }).finally(() => {
    loading.value = false
  })
}

function resetForm() {
  form.value = {
    detailId: null,
    sourceDetailId: null,
    entryDate: null,
    empId: null,
    empName: null,
    billingType: '1',
    rateId: null,
    workTypeName: null,
    workload: null,
    unitPrice: null,
    extraAmount: 0,
    extraReason: null,
    isMakeup: '1',
    makeupResponsibleId: userStore.id || null,
    makeupResponsible: userStore.nickName || null,
    makeupReason: null
  }
}

function handleMakeup(row) {
  form.value = {
    ...row,
    sourceDetailId: row.detailId,
    isMakeup: '1',
    makeupResponsibleId: userStore.id || row.makeupResponsibleId || null,
    makeupResponsible: userStore.nickName || row.makeupResponsible || null,
    makeupReason: row.makeupReason || null
  }
  title.value = '补录 补录负责人：'+form.value.makeupResponsible
  open.value = true
}

function submitForm() {
  proxy.$refs.formRef.validate(valid => {
    if (!valid) return
    buttonLoading.value = true
    const payload = {
      ...form.value,
      isMakeup: '1'
    }
    // 本页面补录语义：更新当前记录并标记为已补录
    updateWageEntryDetail(payload).then(() => {
      // 更新累计金额
      const workload = parseFloat(payload.workload) || 0
      const unitPrice = parseFloat(payload.unitPrice) || 0
      const extraAmount = parseFloat(payload.extraAmount) || 0
      const totalAmount = workload * unitPrice + extraAmount
      
      if (!cumulativeAmounts.value[payload.empName]) {
        cumulativeAmounts.value[payload.empName] = 0
      }
      cumulativeAmounts.value[payload.empName] += totalAmount
      saveCumulativeAmounts()
      
      proxy.$modal.msgSuccess('补录成功')
      open.value = false
      getList()
    }).finally(() => {
      buttonLoading.value = false
    })
  })
}

function handleDelete(row) {
  proxy.$modal.confirm('确认删除该记录吗？').then(() => delWageEntryDetail(row.detailId)).then(() => {
    proxy.$modal.msgSuccess('删除成功')
    getList()
  })
}

function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery() {
  proxy.resetForm('queryRef')
  queryParams.value.entryDate = undefined
  queryParams.value.makeupView = 'all'
  handleQuery()
}

function handleExport() {
  proxy.download('oa/wageEntryDetail/export', { ...buildQuery() }, `wage_makeup_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  loadCumulativeAmounts()
  getList()
})
</script>
