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

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

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

const form = reactive({ nanny_id: '', nanny_order_id: '', schedule_date: '', type: 1, remark: '' })

const typeMap = { 1: '上班', 2: '休息', 3: '请假' }
const typeType = { 1: 'success', 2: 'info', 3: 'warning' }

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

async function fetchNannies() { try { const res = await nannyApi.getList({ per_page: 200, status: 2 }); nannies.value = res.data?.list || res.data?.data || [] } catch {} }

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

function handleAdd() { Object.assign(form, { nanny_id: '', nanny_order_id: '', schedule_date: '', type: 1, remark: '' }); dialogVisible.value = true }

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

function handlePageChange(val) { pagination.page = val; fetchList() }
onMounted(() => { fetchList(); fetchNannies() })
</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-select v-model="searchForm.nanny_id" placeholder="选择月嫂" clearable style="width:160px"><el-option v-for="n in nannies" :key="n.id" :label="n.name" :value="n.id" /></el-select>
      <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.nanny?.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="typeType[row.type]" size="small">{{ typeMap[row.type] }}</el-tag></template></el-table-column>
          <el-table-column label="关联订单" min-width="120"><template #default="{ row }">{{ row.order?.order_no || '-' }}</template></el-table-column>
          <el-table-column prop="remark" label="备注" min-width="150" />
        </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="450px" destroy-on-close>
      <el-form label-width="80px">
        <el-form-item label="月嫂"><el-select v-model="form.nanny_id" style="width:100%"><el-option v-for="n in nannies" :key="n.id" :label="n.name" :value="n.id" /></el-select></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.type" style="width:100%"><el-option v-for="(v,k) in typeMap" :key="k" :label="v" :value="Number(k)" /></el-select></el-form-item>
        <el-form-item label="备注"><el-input v-model="form.remark" /></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>
