<template>
  <view class="workbench">
    <view class="stats-row">
      <view class="stat-item"><text class="stat-num">{{ stats.todayTotal }}</text><text>今日工单</text></view>
      <view class="stat-item"><text class="stat-num">{{ stats.completedToday }}</text><text>已完成</text></view>
      <view class="stat-item warn"><text class="stat-num">{{ stats.exceptionCount }}</text><text>异常</text></view>
    </view>
    <view class="quick-actions">
      <view class="action-card" @click="navTo('/pages/delivery/orders/orders?status=ORDER_ASSIGNED')">
        <text class="action-num">{{ stats.pendingAccept }}</text><text>待接单</text>
      </view>
      <view class="action-card" @click="navTo('/pages/delivery/orders/orders?status=ORDER_ACCEPTED')">
        <text class="action-num">{{ stats.pendingCheckin }}</text><text>待签到</text>
      </view>
      <view class="action-card" @click="navTo('/pages/delivery/orders/orders?status=ORDER_IN_SERVICE')">
        <text class="action-num">{{ stats.inService }}</text><text>服务中</text>
      </view>
      <view class="action-card" @click="navTo('/pages/delivery/offline-sync/offline-sync')">
        <text>离线补传</text>
      </view>
    </view>
    <view class="order-list">
      <text class="section-title">今日任务</text>
      <view v-for="o in todayOrders" :key="o.id" class="order-item" @click="navTo('/pages/delivery/order-detail/order-detail?id='+o.id)">
        <text class="patient-name">{{ o.patient_name }}</text>
        <text class="time">{{ o.time_window_start }} - {{ o.time_window_end }}</text>
        <text :class="'status status-'+o.status">{{ statusText(o.status) }}</text>
      </view>
      <view v-if="todayOrders.length === 0" class="empty">暂无今日任务</view>
    </view>
  </view>
</template>

<script>
import { apiGet } from '@/common/api.js';
export default {
  data() {
    return {
      stats: { todayTotal: 0, completedToday: 0, exceptionCount: 0, pendingAccept: 0, pendingCheckin: 0, inService: 0 },
      todayOrders: []
    };
  },
  onShow() { this.loadData(); },
  methods: {
    async loadData() {
      try {
        const [wbRes, ordersRes] = await Promise.all([
          apiGet('/delivery/workbench'),
          apiGet('/delivery/work-orders/today')
        ]);
        if (wbRes.data.code === 200) this.stats = wbRes.data.data;
        if (ordersRes.data.code === 200) this.todayOrders = ordersRes.data.data;
      } catch (e) {}
    },
    navTo(url) { uni.navigateTo({ url }); },
    statusText(s) {
      const m = { ORDER_ASSIGNED: '待接单', ORDER_ACCEPTED: '待签到', ORDER_CHECKED_IN: '待服务', ORDER_IN_SERVICE: '服务中', ORDER_COMPLETED: '已完成', ORDER_EXCEPTION: '异常' };
      return m[s] || s;
    }
  }
};
</script>

<style>
.workbench { padding: 20rpx; }
.stats-row { display: flex; justify-content: space-around; padding: 30rpx; background: #fff; border-radius: 16rpx; margin-bottom: 20rpx; }
.stat-item { text-align: center; }
.stat-num { font-size: 48rpx; font-weight: bold; display: block; }
.stat-item.warn .stat-num { color: #ff4d4f; }
.quick-actions { display: flex; flex-wrap: wrap; gap: 20rpx; margin-bottom: 20rpx; }
.action-card { flex: 1; min-width: 150rpx; background: #fff; padding: 30rpx; border-radius: 16rpx; text-align: center; }
.action-num { font-size: 40rpx; font-weight: bold; display: block; color: #1677ff; }
.section-title { font-size: 32rpx; font-weight: bold; margin: 20rpx 0; display: block; }
.order-item { background: #fff; padding: 24rpx; border-radius: 12rpx; margin-bottom: 12rpx; display: flex; justify-content: space-between; }
.empty { text-align: center; color: #999; padding: 60rpx; }
</style>
