<template>
  <div>
    <!-- 搜索工作栏 -->
    <el-form :model="searchForm" inline label-width="0">
      <el-form-item>
        <el-date-picker
          v-model="searchForm.period"
          type="month"
          format="YYYY-MM"
          value-format="YYYY-MM"
          placeholder="年月"
          style="width: 100%"
          @change="handleSearch"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model.trim="searchForm.dept"
          placeholder="组织名称"
          clearable
          @keyup.enter="handleSearch"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model.trim="searchForm.examinedUserName"
          placeholder="员工姓名"
          clearable
          @keyup.enter="handleSearch"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="getList" v-hasPermi="['kpi:score:search']"> 搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableList"
      border
      stripe
      :default-expand-all="false"
      row-key="id"
      :tree-props="{ children: 'examineResultRespVOList', hasChildren: 'hasChildren' }"
      :span-method="spanMethod"
    >
      <el-table-column label="姓名" min-width="90px">
        <template #default="{ row }">
          <span v-if="row.examineResultRespVOList && row.examineResultRespVOList.length">
            <span>{{ row.period }}</span>
            <span class="ml-20px">考核人数：{{ row.examineNum }}人</span>
          </span>
          <span v-else>
            {{ row.examinedUserName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="period" label="年月" min-width="90px" />
      <el-table-column prop="dept" label="组织" min-width="90px" />
      <el-table-column label="考核总分" prop="score" align="center" />
      <el-table-column label="操作" fixed="right" width="200">
        <template #default="{ row }">
          <el-button
            type="danger"
            v-if="!row.examinedUser"
            :disabled="row.status == 1"
            style="padding: 0"
            text
            v-hasPermi="['home:false-dilegence:sealup']"
            @click="handleSealup(row)"
          >
            <span v-if="row.status == 0">封存</span>
            <span v-else-if="row.status == 1">已封存</span>
          </el-button>
          <el-button
            type="primary"
            text
            v-if="row.examinedUser"
            style="padding: 0"
            v-hasPermi="['kpi:record:detail']"
            @click="handleDetail(row)"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <Pagination
      :total="total"
      v-model:page="searchForm.pageNo"
      v-model:limit="searchForm.pageSize"
      @pagination="getList"
    />

    <DialogScoreDetail ref="detailDialogRef" />
  </div>
</template>

<script setup name="KpiRecord">
import { removeNullField } from '@/utils'
import * as KpiApi from '@/api/kpi/score.js'
import DialogScoreDetail from './DialogScoreDetail.vue'

const message = useMessage() // 消息弹窗

const searchForm = ref({
  examinedUserName: undefined,
  period: undefined,
  dept: undefined,
  pageNo: 1,
  pageSize: 20
})

onMounted(() => {
  handleSearch()
})

function handleSearch() {
  searchForm.value.pageNo = 1
  getList()
}

const loading = ref(false)
const tableList = ref([])
const total = ref(0)
async function getList() {
  loading.value = true
  try {
    let params = {
      ...searchForm.value
    }
    if (params.period) {
      params = {
        ...params,
        year: new Date(params.period).getFullYear(),
        month: new Date(params.period).getMonth() + 1
      }
    }
    const data = await KpiApi.getScoreRecord(removeNullField(params))
    if (searchForm.value.examinedUserName) {
      tableList.value = data.list.reduce((pre, cur) => {
        return pre.concat(cur.examineResultRespVOList)
      }, [])
    } else {
      tableList.value = data.list.map((it, index) => ({
        ...it,
        examineResultRespVOList: it.examineResultRespVOList,
        id: index + 1
      }))
    }
    total.value = data.total
  } catch (err) {
    console.log(err)
  } finally {
    loading.value = false
  }
}

function spanMethod({ row, columnIndex }) {
  if (row.examineResultRespVOList && row.examineResultRespVOList.length > 0) {
    if (columnIndex === 0) {
      return [1, 4]
    } else if (columnIndex == 4 && row.id) {
      return [1, 1]
    } else {
      return [0, 0]
    }
  }
}

const detailDialogRef = ref()
function handleDetail(row) {
  detailDialogRef.value.open(row.id)
}

async function handleSealup(row) {
  try {
    // 二次确认
    await message.confirm('确认要封存"' + row.period + '"考核记录吗?')
    // 发起修改状态
    await KpiApi.saveScoreRecord({
      period: row.period
    })
    message.success('封存成功！')
    // 刷新列表
    await getList()
  } catch (err) {
    console.log(err)
  }
}
</script>

<style lang="scss" scoped>
:deep(.el-table__indent) {
  display: none;
}

:deep(.el-table__placeholder) {
  display: none;
}
</style>
