<template>
  <div class="page-card">
    <h3>账单支付</h3>
    <div style="margin-bottom: 12px;">
      <el-select v-model="elderId" placeholder="选择亲人" @change="load">
        <el-option v-for="elder in elders" :key="elder.id" :label="elder.name" :value="elder.id" />
      </el-select>
    </div>
    <el-table :data="items" stripe>
      <el-table-column prop="month" label="月份" />
      <el-table-column prop="total" label="金额" />
      <el-table-column label="状态" width="120">
        <template slot-scope="scope">
          <el-tag :type="statusTag(scope.row.status)">
            {{ statusLabel(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="pay(scope.row)" :disabled="scope.row.status === 'PAID'">
            支付
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { familyElders, familyBills, familyPay } from "../../api";

export default {
  data() {
    return { elders: [], elderId: "", items: [] };
  },
  async created() {
    try {
      const res = await familyElders();
      this.elders = res.data.data;
    } catch (e) {
      this.$message.error(e.message || "加载失败");
    }
  },
  methods: {
    statusLabel(status) {
      if (status === "PAID") return "已支付";
      if (status === "UNPAID") return "未支付";
      return status || "-";
    },
    statusTag(status) {
      if (status === "PAID") return "success";
      if (status === "UNPAID") return "warning";
      return "info";
    },
    async load() {
      if (!this.elderId) return;
      try {
        const res = await familyBills(this.elderId);
        this.items = res.data.data;
      } catch (e) {
        this.$message.error(e.message || "加载失败");
      }
    },
    async pay(row) {
      try {
        await familyPay(row.id, { method: "ONLINE" });
        this.$message.success("支付成功");
        this.load();
      } catch (e) {
        this.$message.error(e.message || "支付失败");
      }
    }
  }
};
</script>

