<template>
  <div class="page-card">
    <h3>护理记录</h3>
    <el-form :model="form" label-width="120px">
      <el-form-item label="长者ID"><el-input v-model="form.elderId"/></el-form-item>
      <el-form-item label="护理内容"><el-input v-model="form.content" type="textarea"/></el-form-item>
      <el-form-item label="记录时间"><el-date-picker v-model="form.recordTime" type="datetime"/></el-form-item>
      <el-form-item label="附件">
        <el-upload action="" :http-request="upload" :show-file-list="false">
          <el-button size="mini">上传</el-button>
        </el-upload>
        <div v-if="form.attachmentUrl" style="margin-top: 6px;">
          已上传: {{ form.attachmentUrl }}
        </div>
      </el-form-item>
      <el-button type="primary" @click="create">保存</el-button>
    </el-form>

    <div style="margin-top: 16px;">
      <el-input v-model="filterElderId" placeholder="长者ID" style="width: 200px;" />
      <el-button @click="load" style="margin-left: 8px;">查询</el-button>
      <el-table :data="items" stripe style="margin-top: 12px;">
        <el-table-column prop="elderId" label="长者ID" width="100" />
        <el-table-column prop="content" label="护理内容" />
        <el-table-column prop="recordTime" label="记录时间" />
      </el-table>
    </div>
  </div>
</template>

<script>
import { nurseCareCreate, nurseCareList, uploadFile } from "../../api";
import { formatDateTime } from "../../utils/date";

export default {
  data() {
    return {
      form: { elderId: "", content: "", recordTime: "", attachmentUrl: "" },
      filterElderId: "",
      items: []
    };
  },
  methods: {
    async upload(option) {
      try {
        const res = await uploadFile(option.file);
        this.form.attachmentUrl = res.data.data.url;
      } catch (e) {
        this.$message.error(e.message || "上传失败");
      }
    },
    async create() {
      try {
        const payload = { ...this.form, recordTime: formatDateTime(this.form.recordTime) };
        await nurseCareCreate(payload);
        this.$message.success("保存成功");
      } catch (e) {
        this.$message.error(e.message || "保存失败");
      }
    },
    async load() {
      if (!this.filterElderId) return;
      try {
        const res = await nurseCareList(this.filterElderId);
        this.items = res.data.data;
      } catch (e) {
        this.$message.error(e.message || "加载失败");
      }
    }
  }
};
</script>


