<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 label="记录日期">
        <el-date-picker v-model="dateRange" type="daterange" value-format="YYYY-MM-DD"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" clearable />
      </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:bannerLetter:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['ledger:bannerLetter:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['ledger:bannerLetter:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['ledger:bannerLetter: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="recordDate" width="110">
        <template #default="{ row }">{{ parseTime(row.recordDate, '{y}-{m}-{d}') }}</template>
      </el-table-column>
      <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="type">
        <template #default="{ row }">
          <el-tag :type="row.type === '1' ? 'warning' : 'success'">{{ row.type === '1' ? '锦旗' : '感谢信' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="赠予方" align="center" prop="giver" />
      <el-table-column label="内容描述" align="center" prop="contentDesc" show-overflow-tooltip />
      <el-table-column label="加分" align="center" prop="addScore">
        <template #default="{ row }"><span v-if="row.addScore" style="color:#67c23a">+{{ row.addScore }}</span></template>
      </el-table-column>
      <el-table-column label="附件" align="center" prop="evidenceFile">
        <template #default="{ row }">
          <el-link v-if="row.evidenceFile" :href="row.evidenceFile" target="_blank" type="primary">查看</el-link>
          <span v-else>-</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:bannerLetter:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(row)" v-hasPermi="['ledger:bannerLetter: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="recordDate">
          <el-date-picker v-model="form.recordDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择记录日期" style="width:100%" />
        </el-form-item>
        <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="type">
          <el-radio-group v-model="form.type">
            <el-radio value="1">锦旗</el-radio>
            <el-radio value="2">感谢信</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="赠予方" prop="giver">
          <el-input v-model="form.giver" placeholder="请输入赠予方" />
        </el-form-item>
        <el-form-item label="内容描述" prop="contentDesc">
          <el-input v-model="form.contentDesc" type="textarea" :rows="3" placeholder="请输入内容描述" />
        </el-form-item>
        <el-form-item label="加分" prop="addScore">
          <el-input-number v-model="form.addScore" :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 { listBannerLetter, addBannerLetter, updateBannerLetter, delBannerLetter, exportBannerLetter } from '@/api/ledger/index'
import { parseTime } from '@/utils/ruoyi'

defineOptions({ name: 'LedgerBannerLetter' })

const loading = ref(false), list = ref([]), total = ref(0), showSearch = ref(true)
const dateRange = ref([]), 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, recordDate: '', deptName: '', teamName: '', personName: '', type: '1', giver: '', contentDesc: '', addScore: 0, evidenceFile: '', remark: '' })
const rules = { personName: [{ required: true, message: '请输入姓名', trigger: 'blur' }], type: [{ required: true, message: '请选择类型', trigger: 'change' }] }

function getList() {
  loading.value = true
  const p = { ...queryParams }
  if (dateRange.value?.length === 2) { p['params[beginTime]'] = dateRange.value[0]; p['params[endTime]'] = dateRange.value[1] }
  listBannerLetter(p).then(r => { list.value = r.rows; total.value = r.total }).finally(() => loading.value = false)
}
function handleQuery() { queryParams.pageNum = 1; getList() }
function resetQuery() { dateRange.value = []; 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, recordDate: '', deptName: '', teamName: '', personName: '', type: '1', giver: '', contentDesc: '', addScore: 0, evidenceFile: '', 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 updateBannerLetter(form); ElMessage.success('修改成功') }
  else { await addBannerLetter(form); ElMessage.success('新增成功') }
  dialogVisible.value = false; getList()
}
function handleDelete(row) {
  const delIds = row?.id ? [row.id] : ids.value
  ElMessageBox.confirm('确认删除？', '提示', { type: 'warning' }).then(() => {
    delBannerLetter(delIds.join(',')).then(() => { ElMessage.success('删除成功'); getList() })
  })
}
function handleExport() {
  const p = { ...queryParams }
  if (dateRange.value?.length === 2) { p['params[beginTime]'] = dateRange.value[0]; p['params[endTime]'] = dateRange.value[1] }
  exportBannerLetter(p)
}
onMounted(getList)
</script>
