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

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

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

const form = reactive({ id: null, care_profile_id: '', care_plan_id: '', type: 1, items: [], remark: '', images: [], recorded_at: '' })
const isEdit = ref(false)
const typeMap = { 1: '妈妈护理', 2: '宝宝护理' }

async function fetchList() {
  loading.value = true
  try { const res = await careRecordApi.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 {} }

function handleSearch() { pagination.page = 1; fetchList() }
function handleReset() { Object.assign(searchForm, { care_profile_id: '', type: '' }); handleSearch() }
function handleAdd() { isEdit.value = false; Object.assign(form, { id: null, care_profile_id: '', care_plan_id: '', type: 1, items: [], remark: '', images: [], recorded_at: '' }); dialogVisible.value = true }
async function handleEdit(row) { isEdit.value = true; Object.assign(form, { id: row.id, care_profile_id: row.care_profile_id, care_plan_id: row.care_plan_id || '', type: row.type, items: row.items || [], remark: row.remark || '', images: row.images || [], recorded_at: row.recorded_at || '' }); dialogVisible.value = true }

async function handleSubmit() {
  submitLoading.value = true
  try {
    if (isEdit.value) { await careRecordApi.update(form.id, form) } else { await careRecordApi.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() })
</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-select v-model="searchForm.type" placeholder="类型" clearable style="width:140px"><el-option v-for="(v,k) in typeMap" :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 label="类型" width="100" align="center"><template #default="{ row }">{{ typeMap[row.type] || '-' }}</template></el-table-column>
          <el-table-column label="护士" min-width="100"><template #default="{ row }">{{ row.nurse?.name || '-' }}</template></el-table-column>
          <el-table-column prop="remark" label="备注" min-width="200" show-overflow-tooltip />
          <el-table-column prop="recorded_at" label="记录时间" min-width="160" />
          <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 :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-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" type="textarea" :rows="3" /></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>
