<template>
	<ServicePageScaffold title="我的" fallback-url="/pages/mall/delivery/home/index" :hide-header="true">
		<view class="page">
			<view class="hero">
				<view class="hero-row">
					<view class="avatar"><text class="avatar-text">护</text></view>
					<view class="hero-main">
						<text class="hero-name">{{ nameText }}</text>
						<text class="hero-role">服务人员 / 护工 / 上门服务人员</text>
						<text class="hero-org">{{ organizationText }}</text>
					</view>
					<view class="hero-setting" @click="goSettings"><text class="hero-setting-text">设置</text></view>
				</view>
			</view>

			<view class="card stats-card">
				<view class="stat-item"><text class="stat-value">{{ acceptedText }}</text><text class="stat-label">今日接单</text></view>
				<view class="stat-item"><text class="stat-value">{{ servingText }}</text><text class="stat-label">服务中</text></view>
				<view class="stat-item"><text class="stat-value">{{ completedText }}</text><text class="stat-label">今日完成</text></view>
			</view>

			<view class="card">
				<text class="section-title">账号信息</text>
				<text class="row-text">人员编号：{{ staffNoText }}</text>
				<text class="row-text">联系电话：{{ phoneText }}</text>
				<text class="row-text">服务区域：{{ serviceAreaText }}</text>
				<text class="row-text">资质状态：{{ certificateText }}</text>
				<text class="row-text">资质到期：{{ certificateExpireText }}</text>
			</view>

			<view class="card">
				<text class="section-title">工作状态</text>
				<view class="status-row">
					<view class="status-btn" :class="onlineStatusText == 'online' ? 'status-btn-active' : ''" @click="changeStatus('online')"><text class="status-btn-text">在线</text></view>
					<view class="status-btn" :class="onlineStatusText == 'resting' ? 'status-btn-active' : ''" @click="changeStatus('resting')"><text class="status-btn-text">离线</text></view>
					<view class="status-btn" :class="onlineStatusText == 'busy' ? 'status-btn-active' : ''" @click="changeStatus('busy')"><text class="status-btn-text">忙碌</text></view>
				</view>
			</view>

			<view class="card action-card">
				<view class="tool-item" @click="goOrders('today')"><text class="tool-title">今日订单</text><text class="tool-desc">查看待服务订单</text></view>
				<view class="tool-item" @click="goOrders('history')"><text class="tool-title">历史订单</text><text class="tool-desc">查看已完成和异常</text></view>
				<view class="tool-item" @click="goSettings"><text class="tool-title">设置</text><text class="tool-desc">退出登录和提醒设置</text></view>
			</view>
		</view>
	</ServicePageScaffold>
</template>

<script setup lang="uts">
import { computed, ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import ServicePageScaffold from '@/components/homeService/ServicePageScaffold.uvue'
import type { DeliveryInfoType } from '@/types/delivery.uts'
import { getDeliveryProfile, updateDeliveryOnlineStatus } from '@/services/deliveryService.uts'
import { requireDeliveryAuth } from '@/utils/deliveryAuth.uts'

const deliveryInfo = ref<DeliveryInfoType | null>(null)

const nameText = computed((): string => deliveryInfo.value != null ? deliveryInfo.value.name : '服务人员')
const organizationText = computed((): string => deliveryInfo.value != null ? deliveryInfo.value.organizationName : '机构待绑定')
const acceptedText = computed((): string => deliveryInfo.value != null ? String(deliveryInfo.value.todayAccepted) : '0')
const servingText = computed((): string => deliveryInfo.value != null ? String(deliveryInfo.value.todayServing) : '0')
const completedText = computed((): string => deliveryInfo.value != null ? String(deliveryInfo.value.todayCompleted) : '0')
const staffNoText = computed((): string => deliveryInfo.value != null ? deliveryInfo.value.staffNo : '待分配')
const phoneText = computed((): string => deliveryInfo.value != null ? deliveryInfo.value.phone : '待完善')
const serviceAreaText = computed((): string => deliveryInfo.value != null ? deliveryInfo.value.serviceArea : '待完善')
const certificateText = computed((): string => deliveryInfo.value != null ? deliveryInfo.value.certificateStatus : '待认证')
const certificateExpireText = computed((): string => deliveryInfo.value != null ? deliveryInfo.value.certificateExpireAt : '暂无')
const onlineStatusText = computed((): string => deliveryInfo.value != null ? deliveryInfo.value.onlineStatus : 'resting')

async function loadData() {
	const authResult = await requireDeliveryAuth({ redirectOnFail: true, toastOnFail: true })
	if (!authResult.ok) {
		return
	}
	deliveryInfo.value = await getDeliveryProfile()
}

async function changeStatus(status: string) {
	if (deliveryInfo.value == null) {
		return
	}
	deliveryInfo.value = await updateDeliveryOnlineStatus(status)
	uni.showToast({ title: '状态已更新', icon: 'success' })
}

function goOrders(tab: string) {
	uni.setStorageSync('delivery_order_tab', tab)
	uni.switchTab({ url: '/pages/mall/delivery/orders/index' })
}

function goSettings() {
	uni.navigateTo({ url: '/pages/mall/delivery/profile/settings' })
}

onShow(() => {
	loadData()
})
</script>

<style scoped>
.page {
	min-height: 100%;
	margin-left: -24rpx;
	margin-right: -24rpx;
	margin-top: -24rpx;
	padding: 0 24rpx 36rpx;
	background-color: #f3f8fb;
}

.hero,
.card {
	border-radius: 28rpx;
	background-color: #ffffff;
	box-shadow: 0 10rpx 28rpx rgba(15, 35, 55, 0.06);
}

.hero {
	padding: 64rpx 28rpx 28rpx;
	border-bottom-left-radius: 36rpx;
	border-bottom-right-radius: 36rpx;
	background: linear-gradient(180deg, #1f7db4 0%, #22a380 100%);
	box-shadow: none;
}

.hero-row,
.stats-card,
.status-row {
	display: flex;
	flex-direction: row;
	justify-content: space-between;
}

.hero-row {
	align-items: center;
}

.avatar {
	width: 96rpx;
	height: 96rpx;
	border-radius: 48rpx;
	background-color: rgba(255, 255, 255, 0.22);
	align-items: center;
	justify-content: center;
}

.avatar-text {
	font-size: 38rpx;
	font-weight: 700;
	color: #ffffff;
}

.hero-main {
	flex: 1;
	padding-left: 20rpx;
	padding-right: 20rpx;
}

.hero-name {
	font-size: 34rpx;
	font-weight: 700;
	color: #ffffff;
}

.hero-role,
.hero-org,
.stat-label,
.row-text,
.tool-desc,
.hero-setting-text,
.status-btn-text {
	font-size: 24rpx;
	line-height: 36rpx;
}

.hero-role,
.hero-org {
	margin-top: 10rpx;
	color: rgba(255, 255, 255, 0.88);
}

.hero-setting {
	padding: 14rpx 22rpx;
	border-radius: 999rpx;
	background-color: rgba(255, 255, 255, 0.2);
}

.hero-setting-text {
	color: #ffffff;
}

.card {
	margin-top: 22rpx;
	padding: 24rpx;
}

.stats-card {
	align-items: center;
}

.stat-item {
	width: 31%;
	padding: 22rpx 14rpx;
	border-radius: 20rpx;
	background-color: #f7fbfd;
	align-items: center;
}

.stat-value,
.section-title,
.tool-title {
	font-weight: 700;
}

.stat-value {
	font-size: 34rpx;
	color: #176e97;
}

.stat-label,
.tool-desc,
.row-text {
	color: #5e758c;
}

.section-title,
.tool-title {
	font-size: 30rpx;
	color: #16324f;
}

.row-text {
	display: block;
	margin-top: 10rpx;
}

.status-btn {
	width: 31%;
	padding: 18rpx 0;
	border-radius: 18rpx;
	background-color: #eef6fa;
	align-items: center;
	justify-content: center;
}

.status-btn-active {
	background-color: #d8eef9;
}

.status-btn-text {
	color: #176e97;
}

.tool-item {
	margin-top: 14rpx;
	padding: 18rpx;
	border-radius: 18rpx;
	background-color: #f7fbfd;
}

.tool-title {
	font-size: 28rpx;
	color: #16324f;
}

.tool-desc {
	margin-top: 10rpx;
}
</style>