<template>
  <div class="order-list">
    <el-card shadow="never">
      <!-- 筛选区 -->
      <el-form :inline="true" :model="filters" class="filter-form">
        <el-form-item :label="$t('table.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-option label="已取消" value="已取消" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('table.priority')">
          <el-select v-model="filters.priority" :placeholder="$t('common.all')" clearable style="width:120px">
            <el-option label="高" value="高" />
            <el-option label="中" value="中" />
            <el-option label="低" value="低" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('placeholder.keyword')">
          <el-input v-model="filters.keyword" :placeholder="$t('placeholder.keyword')" clearable style="width:200px" />
        </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="goQuick">{{ $t('btn.quickSubmit') }}</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table :data="list" v-loading="loading" border stripe>
        <el-table-column :label="$t('table.id')" prop="orderNo" width="160" />
        <el-table-column :label="$t('table.title')" prop="title" min-width="200" show-overflow-tooltip />
        <el-table-column :label="$t('table.type')" prop="serviceType" width="120" />
        <el-table-column :label="$t('table.priority')" width="90">
          <template #default="{ row }">
            <el-tag :type="priorityType(row.priority)" size="small">{{ row.priority }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column :label="$t('table.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('table.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="goDetail(row.id)">{{ $t('btn.detail') }}</el-button>
            <el-button link type="danger" v-if="row.status === '待处理'" @click="cancel(row)">{{ $t('btn.cancel') }}</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 { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { useI18n } from "vue-i18n";
import { getHospitalOrders, cancelHospitalOrder } from "@/service/modular/hospital";

const { t: $t } = useI18n();
const router = useRouter();
const list = ref([]);
const total = ref(0);
const page = ref(1);
const pageSize = ref(10);
const loading = ref(false);
const filters = reactive({ status: "", priority: "", keyword: "" });

const priorityType = (p) => (p === "高" ? "danger" : p === "中" ? "warning" : "info");
const statusType = (s) =>
  ({
    待处理: "warning",
    处理中: "primary",
    已完成: "success",
    已取消: "info",
  })[s] || "info";

const loadList = async () => {
  loading.value = true;
  try {
    const res = await getHospitalOrders({
      page: page.value,
      pageSize: pageSize.value,
      ...filters,
    });
    list.value = res?.list || [];
    total.value = res?.total || 0;
  } finally {
    loading.value = false;
  }
};

const onSearch = () => {
  page.value = 1;
  loadList();
};
const onReset = () => {
  filters.status = "";
  filters.priority = "";
  filters.keyword = "";
  onSearch();
};

const goDetail = (id) => router.push(`/hospital/orders/detail/${id}`);
const goQuick = () => router.push("/hospital/orders/quick");

const cancel = async (row) => {
  try {
    await ElMessageBox.confirm(`${$t('msg.confirmCancelOrder').replace('{orderId}', row.title)}`, $t('common.confirm'), {
      type: "warning",
      confirmButtonText: $t('common.confirm'),
      cancelButtonText: $t('common.cancel'),
    });
    await cancelHospitalOrder(row.id);
    ElMessage.success($t('msg.cancelSuccess'));
    loadList();
  } catch (_) {
    /* canceled */
  }
};

onMounted(loadList);
</script>

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