<template>
  <div class="hospital-service">
    <el-card shadow="never">
      <el-form :inline="true" :model="filters" class="filter-form">
        <el-form-item :label="$t('label.type')">
          <el-select v-model="filters.type" :placeholder="$t('common.all')" clearable style="width:160px">
            <el-option label="巡检服务" value="巡检服务" />
            <el-option label="技术支持" value="技术支持" />
            <el-option label="回访服务" value="回访服务" />
            <el-option label="培训服务" value="培训服务" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('table.manager')">
          <el-input v-model="filters.owner" :placeholder="$t('table.manager')" clearable style="width:160px" />
        </el-form-item>
        <el-form-item :label="$t('label.status')">
          <el-select v-model="filters.status" :placeholder="$t('common.all')" clearable style="width:140px">
            <el-option label="待执行" value="待执行" />
            <el-option label="进行中" value="进行中" />
            <el-option label="已完成" value="已完成" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">{{ $t('btn.query') }}</el-button>
          <el-button @click="onReset">{{ $t('btn.reset') }}</el-button>
          <el-button type="success" @click="onAdd">{{ $t('btn.add') }}</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="list" v-loading="loading" border stripe>
        <el-table-column prop="serviceNo" label="No." width="160" />
        <el-table-column :label="$t('table.hospital')" prop="hospitalName" min-width="160" show-overflow-tooltip />
        <el-table-column :label="$t('label.type')" prop="type" width="120" />
        <el-table-column :label="$t('table.manager')" prop="owner" width="120" />
        <el-table-column :label="$t('common.time')" prop="planDate" width="120" />
        <el-table-column :label="$t('label.status')" width="100">
          <template #default="{ row }">
            <el-tag :type="statusType(row.status)" size="small">{{ row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column :label="$t('common.remark')" prop="remark" min-width="200" show-overflow-tooltip />
        <el-table-column :label="$t('table.action')" width="160" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="onView(row)">{{ $t('btn.view') }}</el-button>
            <el-button link type="success" @click="onFinish(row)" v-if="row.status !== '已完成'">{{ $t('status.completed') }}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="page"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="loadList"
          @current-change="loadList"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { useI18n } from "vue-i18n";

const { t: $t } = useI18n();

const MOCK_LIST = [
  { id: 1, serviceNo: "SV-2025-0001", hospitalName: "上海第一人民医院", type: "巡检服务", owner: "张三", planDate: "2025-05-10", status: "已完成", remark: "季度例行巡检" },
  { id: 2, serviceNo: "SV-2025-0002", hospitalName: "北京协和医院", type: "技术支持", owner: "李四", planDate: "2025-05-12", status: "进行中", remark: "系统升级支持" },
  { id: 3, serviceNo: "SV-2025-0003", hospitalName: "广州中山医院", type: "回访服务", owner: "王五", planDate: "2025-05-15", status: "待执行", remark: "客户满意度回访" },
  { id: 4, serviceNo: "SV-2025-0004", hospitalName: "深圳人民医院", type: "培训服务", owner: "赵六", planDate: "2025-05-18", status: "待执行", remark: "操作人员培训" },
];

const list = ref([]);
const total = ref(0);
const page = ref(1);
const pageSize = ref(10);
const loading = ref(false);
const filters = reactive({ type: "", owner: "", status: "" });

const statusType = (s) => ({ 待执行: "info", 进行中: "warning", 已完成: "success" })[s] || "info";

const loadList = async () => {
  loading.value = true;
  try {
    await new Promise((r) => setTimeout(r, 200));
    list.value = MOCK_LIST;
    total.value = MOCK_LIST.length;
  } finally {
    loading.value = false;
  }
};

const onSearch = () => { page.value = 1; loadList(); };
const onReset = () => { filters.type = ""; filters.owner = ""; filters.status = ""; onSearch(); };
const onAdd = () => ElMessage.success($t('btn.add'));
const onView = (row) => ElMessage.info(`${$t('btn.view')}：${row.serviceNo}`);
const onFinish = (row) => {
  row.status = "已完成";
  ElMessage.success($t('msg.editSuccess'));
};

onMounted(loadList);
</script>

<style scoped lang="scss">
.filter-form {
  margin-bottom: 12px;
}
.pagination {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}
</style>
