<template>
  <div class="gyq_container">
    <div class="gyq_content">
      <div class="row">
        <el-form :model="queryForm" inline>
          <el-form-item>
            <el-date-picker style="width: 300px" v-model="times" type="daterange" range-separator="至"
              start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD" :disabled-date="disabledDate"
              @change="selectTimeChange"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-input v-model="queryForm.name" :maxlength="20" placeholder="请输入姓名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" :loading="tableData.loading" @click="searchHandle">搜索</el-button>
            <el-button icon="Refresh" :loading="tableData.loading" @click="resetHandle">重置</el-button>
            <el-button plain type="primary" @click="settingDialogRef.show()">应用配置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="row">
        <el-table :data="tableData.list" v-loading="tableData.loading" stripe border>
          <el-table-column label="姓名" prop="name"></el-table-column>
          <el-table-column label="出勤天数" prop="attendDays"></el-table-column>
          <el-table-column label="休息天数" prop="restDays"></el-table-column>
          <el-table-column label="工作时长（分钟）" prop="workDuration"></el-table-column>
          <el-table-column label="迟到次数" prop="lateCount"></el-table-column>
          <el-table-column label="迟到时长（分钟）" prop="lateDuration"></el-table-column>
          <el-table-column label="旷工天数" prop="absenceDays"></el-table-column>
          <!-- <el-table-column label="备注" prop="name"></el-table-column> -->
          <el-table-column label="操作" width="120">
            <template v-slot="scope">
              <el-button link type="primary" @click="detailDialogRef.show(scope.row)">查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- <div class="row" style="margin-top: 14px;">
        <el-pagination v-model:current-page="tableData.page" v-model:page-size="tableData.size"
          :page-sizes="[10, 20, 50, 100, 500]" background layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div> -->
    </div>
    <settingDialog ref="settingDialogRef" @success="searchHandle" />
    <detailDialog :startTime="queryForm.startTime" :endTime="queryForm.endTime" ref="detailDialogRef" />
  </div>
</template>

<script setup>
import dayjs from 'dayjs'
import { ref, reactive, onMounted } from 'vue'
import { attendanceList } from '@/api/coupon/index'
import settingDialog from './components/settingDialog.vue'
import detailDialog from './components/detailDialog.vue'

const settingDialogRef = ref(null)
const detailDialogRef = ref(null)

const queryForm = reactive({
  startTime: '',
  endTime: '',
  name: ''
})

// 选择日期
const times = ref([])

// 禁止选择最近一个月之外的日期（只能选择最近一个月内的日期）
function disabledDate(date) {
  const d = dayjs(date)
  const start = dayjs().subtract(1, 'month').startOf('day')
  const end = dayjs().endOf('day')
  return d.isBefore(start) || d.isAfter(end)
}

function selectTimeChange(e) {
  console.log(e);

  if (e && e.length === 2) {
    // 确保范围在允许区间内
    const start = dayjs().subtract(1, 'month').startOf('day')
    const end = dayjs().endOf('day')
    const s = dayjs(e[0])
    const ed = dayjs(e[1])
    const finalStart = s.isBefore(start) ? start : s
    const finalEnd = ed.isAfter(end) ? end : ed
    queryForm.startTime = finalStart.format('YYYY-MM-DD 00:00:00')
    queryForm.endTime = finalEnd.format('YYYY-MM-DD 23:59:59')
  } else {
    queryForm.startTime = ''
    queryForm.endTime = ''
  }
}

function searchHandle() {
  tableData.page = 1
  getTableData()
}

function resetHandle() {
  times.value = []
  queryForm.startTime = ''
  queryForm.endTime = ''
  queryForm.name = ''

  searchHandle()
}

const tableData = reactive({
  loading: false,
  page: 1,
  size: 10,
  total: 0,
  list: [],
})

// 分页大小发生变化
function handleSizeChange(e) {
  tableData.size = e;
  getTableData();
}

// 分页发生变化
function handleCurrentChange(e) {
  tableData.page = e;
  getTableData();
}


// 活动考勤数据
async function getTableData() {
  try {
    tableData.loading = true
    const res = await attendanceList(queryForm)
    tableData.list = res
  } catch (error) {
    console.log(error);
  }
  setTimeout(() => {
    tableData.loading = false
  }, 500);
}

onMounted(() => {
  getTableData()
})
</script>

<style scoped lang="scss">
.gyq_container {
  padding: 14px;

  .gyq_content {
    padding: 14px;
    background-color: #fff;
    border-radius: 8px;
  }
}
</style>