﻿<template>
  <div class="page-card">
    <h3>Health Records</h3>
    <el-form :model="form" label-width="140px">
      <el-form-item label="Elder ID"><el-input v-model="form.elderId"/></el-form-item>
      <el-form-item label="Temperature"><el-input v-model="form.temperature"/></el-form-item>
      <el-form-item label="Blood Pressure">
        <div style="display:flex; gap:8px;">
          <el-input v-model="form.bpSystolic" placeholder="Systolic" />
          <el-input v-model="form.bpDiastolic" placeholder="Diastolic" />
        </div>
      </el-form-item>
      <el-form-item label="Heart Rate"><el-input v-model="form.heartRate"/></el-form-item>
      <el-form-item label="Note"><el-input v-model="form.note"/></el-form-item>
      <el-form-item label="Record Time"><el-date-picker v-model="form.recordTime" type="datetime"/></el-form-item>
      <el-button type="primary" @click="create">Save</el-button>
    </el-form>

    <div style="margin-top: 16px;">
      <el-input v-model="filterElderId" placeholder="Elder ID" style="width: 200px;" />
      <el-button @click="load" style="margin-left: 8px;">Search</el-button>
      <el-table :data="items" stripe style="margin-top: 12px;">
        <el-table-column prop="elderId" label="Elder ID" width="100" />
        <el-table-column prop="temperature" label="Temp" />
        <el-table-column prop="bpSystolic" label="BP S" />
        <el-table-column prop="bpDiastolic" label="BP D" />
        <el-table-column prop="heartRate" label="HR" />
        <el-table-column prop="recordTime" label="Record Time" />
      </el-table>
    </div>
  </div>
</template>

<script>
import { nurseHealthCreate, nurseHealthList } from "../../api";
import { formatDateTime } from "../../utils/date";

export default {
  data() {
    return {
      form: { elderId: "", temperature: "", bpSystolic: "", bpDiastolic: "", heartRate: "", note: "", recordTime: "" },
      filterElderId: "",
      items: []
    };
  },
  methods: {
    async create() {
      try {
        const payload = { ...this.form, recordTime: formatDateTime(this.form.recordTime) };
        await nurseHealthCreate(payload);
        this.$message.success("saved");
      } catch (e) {
        this.$message.error(e.message || "save failed");
      }
    },
    async load() {
      if (!this.filterElderId) return;
      try {
        const res = await nurseHealthList(this.filterElderId);
        this.items = res.data.data;
      } catch (e) {
        this.$message.error(e.message || "load failed");
      }
    }
  }
};
</script>
