<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { scheduleApi } from '@/api/hr.js'

const tableData = ref([])
const total = ref(0)
const loading = ref(false)
const dialogVisible = ref(false)
const submitLoading = ref(false)

const searchForm = reactive({ user_id: '', start_date: '', end_date: '' })
const pagination = reactive({ page: 1, per_page: 31 })

const form = reactive({ user_id: '', schedule_date: '', shift_type: 1, start_time: '', end_time: '' })

const shiftMap = { 1: '早班', 2: '中班', 3: '晚班', 4: '夜班', 5: '休息' }
const shiftType = { 1: 'success', 2: 'primary', 3: 'warning', 4: 'danger', 5: 'info' }

async function fetchList() {
  loading.value = true
  try {
    const res = await scheduleApi.getList({ ...searchForm, ...pagination })
    tableData.value = res.data?.list || res.data?.data || []
    total.value = res.data?.total || 0
  } finally { loading.value = false }
}

function handleSearch() { pagination.page = 1; fetchList() }
function handleReset() { Object.assign(searchForm, { user_id: '', start_date: '', end_date: '' }); handleSearch() }

function handleAdd() {
  Object.assign(form, { user_id: '', schedule_date: '', shift_type: 1, start_time: '', end_time: '' })
  dialogVisible.value = true
}

async function handleSubmit() {
  if (!form.user_id || !form.schedule_date || !form.shift_type) { ElMessage.warning('请填写必要信息'); return }
  submitLoading.value = true
  try {
    await scheduleApi.create(form)
    ElMessage.success('排班成功'); dialogVisible.value = false; fetchList()
  } finally { submitLoading.value = false }
}

function handlePageChange(val) { pagination.page = val; fetchList() }

onMounted(() => fetchList())
</script>

<template>
  <div class="p-6 space-y-4">
    <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-4 flex items-center flex-wrap gap-3">
      <el-input v-model="searchForm.user_id" placeholder="员工ID" style="width:160px" clearable @keydown.enter="handleSearch" />
      <el-date-picker v-model="searchForm.start_date" type="date" value-format="YYYY-MM-DD" placeholder="开始日期" />
      <el-date-picker v-model="searchForm.end_date" type="date" value-format="YYYY-MM-DD" placeholder="结束日期" />
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button @click="handleReset">重置</el-button>
    </div>

    <div class="bg-white rounded-2xl border border-gray-100 shadow-sm">
      <div class="flex items-center justify-between px-5 py-4 border-b border-gray-50">
        <div class="flex items-center gap-3">
          <span class="font-medium text-gray-700">排班列表</span>
        </div>
        <el-button type="primary" @click="handleAdd">新增排班</el-button>
      </div>
      <div class="px-2">
        <el-table v-loading="loading" :data="tableData" style="width:100%">
          <el-table-column label="员工" min-width="100">
            <template #default="{ row }">{{ row.user?.name || '-' }}</template>
          </el-table-column>
          <el-table-column prop="schedule_date" label="排班日期" width="120" />
          <el-table-column label="班次" width="90" align="center">
            <template #default="{ row }">
              <el-tag :type="shiftType[row.shift_type]" size="small">{{ shiftMap[row.shift_type] }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="start_time" label="开始时间" width="100" />
          <el-table-column prop="end_time" label="结束时间" width="100" />
        </el-table>
      </div>
      <div class="px-5 py-3 border-t border-gray-50">
        <el-pagination v-model:current-page="pagination.page" :total="total" layout="total,prev,pager,next" background @current-change="handlePageChange" />
      </div>
    </div>

    <el-dialog v-model="dialogVisible" title="新增排班" width="500px" destroy-on-close>
      <el-form label-width="80px">
        <el-form-item label="员工ID"><el-input v-model="form.user_id" /></el-form-item>
        <el-form-item label="排班日期"><el-date-picker v-model="form.schedule_date" type="date" value-format="YYYY-MM-DD" style="width:100%" /></el-form-item>
        <el-form-item label="班次">
          <el-select v-model="form.shift_type" style="width:100%">
            <el-option v-for="(v,k) in shiftMap" :key="k" :label="v" :value="Number(k)" />
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间"><el-time-picker v-model="form.start_time" format="HH:mm" value-format="HH:mm" style="width:100%" /></el-form-item>
        <el-form-item label="结束时间"><el-time-picker v-model="form.end_time" format="HH:mm" value-format="HH:mm" style="width:100%" /></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="submitLoading" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
