<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="queryParams.deptName" placeholder="请输入部门名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="队室/班组" prop="teamName">
        <el-input v-model="queryParams.teamName" placeholder="请输入队室/班组" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="personName">
        <el-input v-model="queryParams.personName" placeholder="请输入姓名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['ledger:leaveSpecial:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['ledger:leaveSpecial:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['ledger:leaveSpecial:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['ledger:leaveSpecial:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column label="队室/班组" align="center" prop="teamName" />
      <el-table-column label="姓名" align="center" prop="personName" />
      <el-table-column label="假期类型" align="center" prop="leaveType" />
      <el-table-column label="开始日期" align="center" prop="startDate" width="170">
        <template #default="{ row }">{{ row.startDate }}</template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endDate" width="170">
        <template #default="{ row }">{{ row.endDate }}</template>
      </el-table-column>
      <el-table-column label="天数" align="center" prop="days" />
      <el-table-column label="扣分" align="center" prop="deductScore">
        <template #default="{ row }"><span v-if="row.deductScore" style="color:#f56c6c">-{{ row.deductScore }}</span></template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template #default="{ row }">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(row)" v-hasPermi="['ledger:leaveSpecial:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(row)" v-hasPermi="['ledger:leaveSpecial:remove']">删除</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="dialogTitle" v-model="dialogVisible" width="600px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="队室/班组" prop="teamName">
          <el-input v-model="form.teamName" placeholder="请输入队室/班组" />
        </el-form-item>
        <el-form-item label="姓名" prop="personName">
          <el-input v-model="form.personName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="假期类型" prop="leaveType">
          <el-input v-model="form.leaveType" placeholder="请输入假期类型" />
        </el-form-item>
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker v-model="form.startDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择开始日期" style="width:100%" />
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker v-model="form.endDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择结束日期" style="width:100%" />
        </el-form-item>
        <el-form-item label="天数" prop="days">
          <el-input-number v-model="form.days" :precision="1" :min="0" style="width:100%" />
        </el-form-item>
        <el-form-item label="扣分" prop="deductScore">
          <el-input-number v-model="form.deductScore" :precision="2" :min="0" style="width:100%" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { listLeaveSpecial, addLeaveSpecial, updateLeaveSpecial, delLeaveSpecial, exportLeaveSpecial } from '@/api/ledger/index'
import { parseTime } from '@/utils/ruoyi'

defineOptions({ name: 'LedgerLeaveSpecial' })

const loading = ref(false), list = ref([]), total = ref(0), showSearch = ref(true)
const queryRef = ref(null), formRef = ref(null)
const dialogVisible = ref(false), dialogTitle = ref('')
const single = ref(true), multiple = ref(true), ids = ref([])
const queryParams = reactive({ pageNum: 1, pageSize: 10, deptName: '', teamName: '', personName: '' })
const form = reactive({ id: null, deptName: '', teamName: '', personName: '', leaveType: '', startDate: '', endDate: '', days: 0, deductScore: 0, remark: '' })
const rules = { personName: [{ required: true, message: '请输入姓名', trigger: 'blur' }], leaveType: [{ required: true, message: '请输入假期类型', trigger: 'blur' }] }

function getList() {
  loading.value = true
  listLeaveSpecial({ ...queryParams }).then(r => { list.value = r.rows; total.value = r.total }).finally(() => loading.value = false)
}
function handleQuery() { queryParams.pageNum = 1; getList() }
function resetQuery() { queryRef.value?.resetFields(); handleQuery() }
function handleSelectionChange(sel) { ids.value = sel.map(s => s.id); single.value = sel.length !== 1; multiple.value = !sel.length }
function resetForm() { Object.assign(form, { id: null, deptName: '', teamName: '', personName: '', leaveType: '', startDate: '', endDate: '', days: 0, deductScore: 0, remark: '' }) }
function handleAdd() { resetForm(); dialogTitle.value = '新增请休假记录'; dialogVisible.value = true }
function handleUpdate(row) {
  resetForm()
  const record = row || list.value.find(r => r.id === ids.value[0])
  if (record) Object.assign(form, record)
  dialogTitle.value = '修改请休假记录'; dialogVisible.value = true
}
async function submitForm() {
  await formRef.value.validate()
  if (form.id) { await updateLeaveSpecial(form); ElMessage.success('修改成功') }
  else { await addLeaveSpecial(form); ElMessage.success('新增成功') }
  dialogVisible.value = false; getList()
}
function handleDelete(row) {
  const delIds = row?.id ? [row.id] : ids.value
  ElMessageBox.confirm('确认删除？', '提示', { type: 'warning' }).then(() => {
    delLeaveSpecial(delIds.join(',')).then(() => { ElMessage.success('删除成功'); getList() })
  })
}
function handleExport() { exportLeaveSpecial({ ...queryParams }) }
onMounted(getList)
</script>
