<template>
  <div>
    <el-form :model="searchForm" ref="searchFormRef" inline @submit.prevent>
      <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="searchForm.orgName"
          placeholder="组织名称"
          clearable
          @keyup.enter="handleSearch"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="searchForm.name"
          placeholder="员工姓名"
          clearable
          @keyup.enter="handleSearch"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleSearch" v-hasPermi="['home:false-dilegence:search']">
          查询
        </el-button>
        <el-button @click="handleSync" v-hasPermi="['home:false-dilegence:sync']">
          同步钉钉假勤
        </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: 'userSalaryGrantRespVOList', hasChildren: 'hasChildren' }"
      :span-method="spanMethod"
    >
      <el-table-column label="姓名" min-width="90px">
        <template #default="{ row }">
          <span v-if="row.userSalaryGrantRespVOList && row.userSalaryGrantRespVOList.length">
            <span>{{ row.period }}</span>
            <span class="ml-20px">考勤人数：{{ row.grantNum }}人</span>
            <span class="ml-20px">不考勤人数：{{ row.allGrantSalaryTotal }}</span>
          </span>
          <span v-else>
            {{ row.name }}
          </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 prop="post" label="计薪天数" min-width="90px" />
      <el-table-column label="出勤天数" align="center">
        <template #default="{ row }">
          <el-input-number
            v-if="row.edit"
            v-model="row.baseSalary"
            :min="0"
            :controls="false"
            size="small"
            style="width: 65px"
          />
          <span v-else> {{ row.baseSalary }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缺勤天数" align="center">
        <template #default="{ row }">
          <el-input-number
            v-if="row.edit"
            v-model="row.baseSalary"
            :min="0"
            :controls="false"
            size="small"
            style="width: 65px"
          />
          <span v-else> {{ row.baseSalary }}</span>
        </template>
      </el-table-column>
      <el-table-column label="事假时长" align="center">
        <template #default="{ row }">
          <el-input-number
            v-if="row.edit"
            v-model="row.baseSalary"
            :min="0"
            :controls="false"
            size="small"
            style="width: 65px"
          />
          <span v-else> {{ row.baseSalary }}</span>
        </template>
      </el-table-column>
      <el-table-column label="婚假天数" align="center">
        <template #default="{ row }">
          <el-input-number
            v-if="row.edit"
            v-model="row.baseSalary"
            :min="0"
            :controls="false"
            size="small"
            style="width: 65px"
          />
          <span v-else> {{ row.baseSalary }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产假天数" align="center">
        <template #default="{ row }">
          <el-input-number
            v-if="row.edit"
            v-model="row.baseSalary"
            :min="0"
            :controls="false"
            size="small"
            style="width: 65px"
          />
          <span v-else> {{ row.baseSalary }}</span>
        </template>
      </el-table-column>
      <el-table-column label="丧假天数" align="center">
        <template #default="{ row }">
          <el-input-number
            v-if="row.edit"
            v-model="row.baseSalary"
            :min="0"
            :controls="false"
            size="small"
            style="width: 65px"
          />
          <span v-else> {{ row.baseSalary }}</span>
        </template>
      </el-table-column>
      <el-table-column label="旷工天数" align="center">
        <template #default="{ row }">
          <el-input-number
            v-if="row.edit"
            v-model="row.baseSalary"
            :min="0"
            :controls="false"
            size="small"
            style="width: 65px"
          />
          <span v-else> {{ row.baseSalary }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="200">
        <template #default="{ row }">
          <el-button
            type="primary"
            style="padding: 0"
            v-if="row.edit === '0'"
            text
            v-hasPermi="['home:false-dilegence:update']"
            @click="handleEdit(row)"
          >
            修改
          </el-button>
          <el-button
            type="primary"
            v-else-if="row.edit === '1'"
            text
            style="padding: 0"
            v-hasPermi="['home:false-dilegence:update']"
            @click="handleSave(row)"
          >
            保存
          </el-button>
          <el-button
            type="danger"
            v-if="row.id"
            :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>
        </template>
      </el-table-column>
    </el-table>

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

<script setup name="FalseDilegenceReport">
import { removeNullField } from '@/utils'
import { formatDate } from '@/utils/formatTime'
import * as SalaryApi from '@/api/home/salary.js'

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

const searchForm = ref({
  name: undefined,
  period: 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 SalaryApi.getSalaryPage(removeNullField(params))
    if (searchForm.value.name) {
      tableList.value = data.list.reduce((pre, cur) => {
        return pre.concat(cur.userSalaryGrantRespVOList)
      }, [])
    } else {
      tableList.value = data.list.map((it, index) => ({
        ...it,
        id: index + 1,
        edit: it.status == 1 ? '2' : '0'
      }))
    }
    total.value = data.total
  } catch (err) {
    console.log(err)
  } finally {
    loading.value = false
  }
}

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

async function handleSync() {
  const result = await message.prompt('请输入年月，如2024-01')
  message.success(`正在同步${formatDate(new Date(result.value), 'YYYY-MM')}假勤数据，请稍后...`)
}

function handleEdit(row) {
  row.edit = '1'
  row.userSalaryGrantRespVOList.forEach((it) => (it.edit = true))
}

async function handleSave(row) {
  row.edit = '0'
  row.userSalaryGrantRespVOList.forEach((it) => (it.edit = false))
  loading.value = true
  await SalaryApi.updateSalarySlip(row.userSalaryGrantRespVOList)
  message.success('保存成功！')
  getList()
}

async function handleSealup(row) {
  try {
    // 二次确认
    await message.confirm('确认要封存"' + row.period + '"假勤吗?')
    // 发起修改状态
    await SalaryApi.sealupSalarySlip({
      grantIdList: row.userSalaryGrantRespVOList.map((it) => it.grantId),
      period: row.period
    })
    // 刷新列表
    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>
