<template>
  <div class="order-report">
    <el-card shadow="never">
      <el-form :inline="true" :model="filters" class="filter-form">
        <el-form-item :label="$t('report.period')">
          <el-select v-model="filters.period" :placeholder="$t('common.pleaseSelect')" style="width:140px">
            <el-option label="本月" value="month" />
            <el-option label="本季度" value="quarter" />
            <el-option label="本年" value="year" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('label.hospitalName')">
          <el-input v-model="filters.hospital" :placeholder="$t('placeholder.hospital')" clearable style="width:180px" />
        </el-form-item>
        <el-form-item :label="$t('table.time')">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            range-separator="-"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
            value-format="YYYY-MM-DD"
            style="width:240px"
          />
        </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="onExport">{{ $t('btn.export') }}</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="list" v-loading="loading" border stripe>
        <el-table-column prop="reportNo" label="No." width="160" />
        <el-table-column :label="$t('table.hospital')" prop="hospitalName" min-width="160" show-overflow-tooltip />
        <el-table-column :label="$t('report.period')" prop="period" width="120" />
        <el-table-column :label="$t('stat.totalOrders')" prop="totalOrders" width="100" />
        <el-table-column :label="$t('stat.completedOrders')" prop="completed" width="100" />
        <el-table-column :label="$t('stat.pendingOrders')" prop="pending" width="100" />
        <el-table-column :label="$t('stat.completed')" width="100">
          <template #default="{ row }">
            <el-rate v-model="row.satisfaction" disabled show-score :colors="['#99A9BF', '#F7BA2A', '#FF9900']" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('common.time')" prop="createdAt" width="170" />
        <el-table-column :label="$t('table.action')" width="120" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="onView(row)">{{ $t('btn.view') }}</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>

    <el-empty v-if="!loading && list.length === 0" :description="$t('report.list')" />
  </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, reportNo: "RP-2025-001", hospitalName: "上海第一人民医院", period: "2025-Q1", totalOrders: 28, completed: 25, pending: 3, satisfaction: 5, createdAt: "2025-04-05 10:30" },
  { id: 2, reportNo: "RP-2025-002", hospitalName: "北京协和医院", period: "2025-Q1", totalOrders: 35, completed: 33, pending: 2, satisfaction: 4, createdAt: "2025-04-06 14:12" },
  { id: 3, reportNo: "RP-2025-003", hospitalName: "广州中山医院", period: "2025-03", totalOrders: 12, completed: 12, pending: 0, satisfaction: 5, createdAt: "2025-04-01 09:00" },
  { id: 4, reportNo: "RP-2025-004", hospitalName: "深圳人民医院", period: "2025-03", totalOrders: 19, completed: 16, pending: 3, satisfaction: 3, createdAt: "2025-04-02 16:45" },
  { id: 5, reportNo: "RP-2025-005", hospitalName: "杭州邵逸夫医院", period: "2025-03", totalOrders: 22, completed: 20, pending: 2, satisfaction: 4, createdAt: "2025-04-03 11:20" },
];

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

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.period = "";
  filters.hospital = "";
  dateRange.value = [];
  onSearch();
};
const onExport = () => ElMessage.success($t('btn.export'));
const onView = (row) => ElMessage.info(`${$t('btn.view')}：${row.reportNo}`);

onMounted(loadList);
</script>

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