<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="inspectorName">
        <el-input v-model="queryParams.inspectorName" placeholder="请输入巡查人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="责任人" prop="inspectedName">
        <el-input v-model="queryParams.inspectedName" 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="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['ledger:supervisionProblem:export']">导出</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="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="groupName" />
      <el-table-column label="巡查人" align="center" prop="inspectorName" />
      <el-table-column label="责任人" align="center" prop="inspectedName" />
      <el-table-column label="问题类型" align="center" prop="problemType" />
      <el-table-column label="问题描述" align="center" prop="problemDesc" show-overflow-tooltip />
      <el-table-column label="问题地点" align="center" prop="location" />
      <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" 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="scoreDimension" />
      <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>

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

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { listSupervisionProblem, exportSupervisionProblem } from '@/api/ledger/index'
import { parseTime } from '@/utils/ruoyi'

defineOptions({ name: 'LedgerSupervisionProblem' })

const loading = ref(false)
const list = ref([])
const total = ref(0)
const showSearch = ref(true)
const dateRange = ref([])
const queryRef = ref(null)

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  deptName: '',
  teamName: '',
  inspectorName: '',
  inspectedName: ''
})

function getList() {
  loading.value = true
  const params = { ...queryParams }
  if (dateRange.value && dateRange.value.length === 2) {
    params['params[beginTime]'] = dateRange.value[0]
    params['params[endTime]'] = dateRange.value[1]
  }
  listSupervisionProblem(params).then(res => {
    list.value = res.rows
    total.value = res.total
  }).finally(() => { loading.value = false })
}

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

function resetQuery() {
  dateRange.value = []
  queryRef.value?.resetFields()
  handleQuery()
}

function handleExport() {
  const params = { ...queryParams }
  if (dateRange.value && dateRange.value.length === 2) {
    params['params[beginTime]'] = dateRange.value[0]
    params['params[endTime]'] = dateRange.value[1]
  }
  exportSupervisionProblem(params)
}

onMounted(getList)
</script>
