<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { carePlanApi, careProfileApi } from '@/api/care.js'
import { userApi } from '@/api/system.js'

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

const searchForm = reactive({ care_profile_id: '', plan_date: '', status: '' })
const pagination = reactive({ page: 1, per_page: 20 })

const formRef = ref(null)
const form = reactive({ id: null, care_profile_id: '', plan_date: '', stage: '', items: [], nurse_id: '', status: 0 })
const isEdit = ref(false)

const statusMap = { 0: '待执行', 1: '执行中', 2: '已完成' }
const statusType = { 0: 'info', 1: 'warning', 2: 'success' }

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

async function fetchProfiles() { try { const res = await careProfileApi.getList({ per_page: 500 }); profiles.value = res.data?.list || res.data?.data || [] } catch {} }
async function fetchNurses() { try { const res = await userApi.getList({ per_page: 200 }); nurses.value = res.data?.list || res.data?.data || [] } catch {} }

function handleSearch() { pagination.page = 1; fetchList() }
function handleReset() { Object.assign(searchForm, { care_profile_id: '', plan_date: '', status: '' }); handleSearch() }
function handleAdd() { isEdit.value = false; Object.assign(form, { id: null, care_profile_id: '', plan_date: '', stage: '', items: [], nurse_id: '', status: 0 }); dialogVisible.value = true }
async function handleEdit(row) { isEdit.value = true; Object.assign(form, { id: row.id, care_profile_id: row.care_profile_id, plan_date: row.plan_date, stage: row.stage || '', items: row.items || [], nurse_id: row.nurse_id || '', status: row.status }); dialogVisible.value = true }

async function handleSubmit() {
  submitLoading.value = true
  try {
    if (isEdit.value) { await carePlanApi.update(form.id, form) } else { await carePlanApi.create(form) }
    ElMessage.success(isEdit.value ? '更新成功' : '创建成功'); dialogVisible.value = false; fetchList()
  } finally { submitLoading.value = false }
}

function handlePageChange(val) { pagination.page = val; fetchList() }
function handleSizeChange(val) { pagination.per_page = val; pagination.page = 1; fetchList() }
onMounted(() => { fetchList(); fetchProfiles(); fetchNurses() })
</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.care_profile_id" placeholder="护理档案" clearable filterable style="width:200px"><el-option v-for="p in profiles" :key="p.id" :label="`${p.customer?.name||''} - ${p.type===1?'妈妈':'宝宝'}`" :value="p.id" /></el-select>
      <el-date-picker v-model="searchForm.plan_date" type="date" value-format="YYYY-MM-DD" placeholder="计划日期" style="width:160px" />
      <el-select v-model="searchForm.status" placeholder="状态" clearable style="width:120px"><el-option v-for="(v,k) in statusMap" :key="k" :label="v" :value="Number(k)" /></el-select>
      <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"></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.profile?.customer?.name || '-' }}</template></el-table-column>
          <el-table-column prop="plan_date" label="计划日期" min-width="110" />
          <el-table-column prop="stage" label="阶段" min-width="100" />
          <el-table-column label="护士" min-width="100"><template #default="{ row }">{{ row.nurse?.name || '-' }}</template></el-table-column>
          <el-table-column label="状态" width="90" align="center"><template #default="{ row }"><el-tag :type="statusType[row.status]" size="small">{{ statusMap[row.status] }}</el-tag></template></el-table-column>
          <el-table-column label="操作" width="120" fixed="right" align="center">
            <template #default="{ row }"><el-button size="small" type="primary" text @click="handleEdit(row)">编辑</el-button></template>
          </el-table-column>
        </el-table>
      </div>
      <div class="px-5 py-3 border-t border-gray-50">
        <el-pagination v-model:current-page="pagination.page" v-model:page-size="pagination.per_page" :total="total" :page-sizes="[10,20,50]" layout="total,sizes,prev,pager,next,jumper" background @current-change="handlePageChange" @size-change="handleSizeChange" />
      </div>
    </div>
    <el-dialog v-model="dialogVisible" :title="isEdit?'编辑计划':'新增计划'" width="550px" destroy-on-close>
      <el-form ref="formRef" :model="form" label-width="80px">
        <el-form-item label="护理档案"><el-select v-model="form.care_profile_id" filterable style="width:100%"><el-option v-for="p in profiles" :key="p.id" :label="`${p.customer?.name||''} - ${p.type===1?'妈妈':'宝宝'}`" :value="p.id" /></el-select></el-form-item>
        <el-form-item label="计划日期"><el-date-picker v-model="form.plan_date" type="date" value-format="YYYY-MM-DD" style="width:100%" /></el-form-item>
        <el-form-item label="阶段"><el-input v-model="form.stage" placeholder="如：产后第1周" /></el-form-item>
        <el-form-item label="护士"><el-select v-model="form.nurse_id" filterable clearable style="width:100%"><el-option v-for="n in nurses" :key="n.id" :label="n.name" :value="n.id" /></el-select></el-form-item>
        <el-form-item v-if="isEdit" label="状态"><el-select v-model="form.status" style="width:100%"><el-option v-for="(v,k) in statusMap" :key="k" :label="v" :value="Number(k)" /></el-select></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>
