<template>
    <view class="container">
        <!-- 自定义导航栏标题 -->
        <view class="custom-nav-bar" :style="{ backgroundColor: scrollTop > 20 ? '#fff' : 'transparent' }">
            <text class="nav-title">任务中心</text>
        </view>
        <!-- 顶部自定义导航栏背景 -->
        <!-- 顶部自定义导航栏背景 -->
        <view class="nav-bg">
            <view class="bg-circle-left"></view>
            <view class="bg-circle-right"></view>
        </view>

        <!-- 头部用户信息与统计 -->
        <view class="header-section">
            <view class="user-info">
                <image class="avatar" :src="profile?.avatarUrl || '/static/touxiang.png'" mode="aspectFill"></image>
                <view class="info-content">
                    <view class="top-row">
                        <text class="name">{{ profile?.name || '未登录' }}</text>
                        <view class="status-pill" :class="{ 'resting': workStatus !== 'busy' }"
                            @click="goToWorkStatus">
                            <view class="status-dot-bg" :class="{ 'busy': workStatus === 'busy', 'disabled': workStatus === 'disabled' }">
                                <text class="check-mark" v-if="workStatus === 'busy'">✔</text>
                                <text class="check-mark" v-else style="font-size: 16rpx; line-height: 20rpx;">✕</text>
                            </view>
                            <text class="status-text">{{ workStatus === 'busy' ? '接单中' : (workStatus === 'resting' ? '正在休息' : '已禁用') }}</text>
                            <text class="arrow-down">▼</text>
                        </view>
                    </view>
                    <view class="bottom-row">
                        <text class="city-label">接单城市：{{ profile?.cityName || '暂无' }}</text>
                        <text class="city-arrow">></text>
                    </view>
                </view>
               <!-- <view class="notification-box">
                    <image class="bell-img" src="/static/icons/bell.svg"></image>
                    <view class="badge-count">2</view>
                </view> -->
            </view>

            <!-- 统计卡片 -->
            <view class="stats-card">
                <view class="stat-item">
                    <text class="num">{{ orderStats.total }}</text>
                    <text class="label">全部订单</text>
                </view>
                <view class="divider"></view>
                <view class="stat-item">
                    <text class="num">{{ orderStats.reject }}</text>
                    <text class="label">拒接订单</text>
                </view>
                <view class="divider"></view>
                <view class="stat-item">
                    <text class="num">{{ orderStats.completed }}</text>
                    <text class="label">完成订单</text>
                </view>
                <view class="divider"></view>
                <view class="stat-item">
                    <text class="num">{{ (orderStats.price / 100).toFixed(2) }}</text>
                    <text class="label">服务总得</text>
                </view>
            </view>
        </view>


        <!-- 任务大厅标题栏 (Sticky Container) -->
        <view class="task-header">
            <!-- 标题栏内容 (Inner) -->
            <view class="header-inner"
                :style="{ backgroundColor: (scrollTop > 300 || isFilterShow) ? '#fff' : 'transparent' }">
                <view class="left-title">
                    <view class="orange-bar"></view>
                    <text class="title">任务大厅</text>
                    <text class="count">- ({{ taskList.length }}单)</text>
                </view>
                <view class="filter-options">
                    <view class="dropdown" @click="showFilterDropdown">
                        <text>筛选条件</text>
                        <text class="arrow-down">﹀</text>
                    </view>
                </view>
            </view>

            <!-- 筛选遮罩 (Absolute Child: Starts below header) -->
            <view class="filter-mask" v-if="isFilterShow" @click="closeFilter"></view>

            <!-- 筛选下拉面板 (Absolute Child) -->
            <view class="filter-panel" :class="{ show: isFilterShow }">
                <view class="filter-section">
                    <text class="section-title">服务类型</text>
                    <view class="options-grid">
                        <view class="option-btn" :class="{ active: tempFilter.service === null }"
                            @click="selectService(null)">全部</view>
                        <view class="option-btn" v-for="item in serviceList" :key="item.id"
                            :class="{ active: tempFilter.service === item.id }" @click="selectService(item.id)">{{
                                item.name }}</view>
                    </view>
                </view>


                <view class="filter-section">
                    <text class="section-title">金额</text>
                    <view class="options-grid">
                        <view class="option-btn" :class="{ active: tempFilter.amount === '全部' }"
                            @click="selectAmount('全部')">全部</view>
                        <view class="option-btn" :class="{ active: tempFilter.amount === '100以下' }"
                            @click="selectAmount('100以下')">100以下</view>
                        <view class="option-btn" :class="{ active: tempFilter.amount === '100-200' }"
                            @click="selectAmount('100-200')">100-200</view>
                        <view class="option-btn" :class="{ active: tempFilter.amount === '200-500' }"
                            @click="selectAmount('200-500')">200-500</view>
                        <view class="option-btn" :class="{ active: tempFilter.amount === '500以上' }"
                            @click="selectAmount('500以上')">500以上</view>
                    </view>
                </view>

                <view class="filter-actions">
                    <button class="action-btn reset" @click="resetFilter">重置</button>
                    <button class="action-btn confirm" @click="confirmFilter">确认</button>
                </view>
            </view>
        </view>

        <!-- 列表容器 -->
        <view class="task-list-container">
            <!-- 任务列表 -->
            <view class="task-list">
                <view class="task-card" v-for="item in taskList" :key="item.id" @click="goToDetail(item)">
                    <view class="card-header">
                        <view class="type-badge">
                            <image class="type-icon" :src="item.typeIcon"></image>
                            <text class="type-text">{{ item.typeText }}</text>
                        </view>
                        <text class="price">¥{{ item.price }}</text>
                    </view>
                    <view class="card-body">
                        <view class="time-row">
                            <text class="label">{{ item.timeLabel }}：</text>
                            <text class="value">{{ item.time }}</text>
                        </view>
                        <view class="pet-card">
                            <image class="pet-avatar" :src="item.petAvatarUrl || item.petAvatar" mode="aspectFill"></image>
                            <view class="pet-info">
                                <text class="pet-name">{{ item.petName }}</text>
                                <text class="pet-breed">品种: {{ item.petBreed }}</text>
                            </view>
                        </view>

                        <view class="route-info">
                            <template v-if="item.type === 1">
                                <view class="route-item" @click.stop="openNavigation(item, 'start')">
                                    <view class="icon-circle start">起</view>
                                    <view class="route-line-vertical"></view>
                                    <view class="address-box">
                                        <text class="addr-title">{{ item.startLocation }}</text>
                                        <text class="addr-desc">{{ item.startAddress }}</text>
                                    </view>
                                    <image class="nav-arrow" src="/static/icons/nav_arrow.svg" style="flex-shrink: 0; align-self: center;"></image>
                                </view>

                                <view class="route-item" @click.stop="openNavigation(item, 'end')">
                                    <view class="icon-circle end">终</view>
                                    <view class="address-box">
                                        <text class="addr-title">{{ item.endLocation }}</text>
                                        <text class="addr-desc">{{ item.endAddress }}</text>
                                    </view>
                                    <image class="nav-arrow" src="/static/icons/nav_arrow.svg" style="flex-shrink: 0; align-self: center;"></image>
                                </view>
                            </template>
                            <template v-else>
                                <view class="route-item" @click.stop="openNavigation(item, 'end')">
                                    <view class="icon-circle service">服</view>
                                    <view class="address-box">
                                        <text class="addr-title">{{ item.endLocation }}</text>
                                        <text class="addr-desc">{{ item.endAddress }}</text>
                                    </view>
                                    <image class="nav-arrow" src="/static/icons/nav_arrow.svg" style="flex-shrink: 0; align-self: center;"></image>
                                </view>
                                <view class="service-content" v-if="item.serviceContent">
                                    <text class="content-label">服务内容:</text>
                                    <text>{{ item.serviceContent }}</text>
                                </view>
                            </template>
                        </view>

                        <view class="remark-box" v-if="item.remark">
                            <text>备注：{{ item.remark }}</text>
                        </view>
                    </view>
                    <!-- action-btns class is in style.css but template used card-footer. style.css: .action-btns, index.vue: .card-footer -->
                    <!-- Wait, style.css has .action-btns { display: flex; ... } and .btn.reject/accept -->
                    <!-- I should use .action-btns instead of .card-footer -->
                    <view class="action-btns">
                        <button class="btn reject" @click.stop="openRejectModal(item)">拒绝</button>
                        <button class="btn accept" @click.stop="openAcceptModal(item)">接单</button>
                    </view>
                </view>
                <!-- Padding for safe area/tabbar -->
                <view style="height: 120rpx;"></view>
            </view>
        </view>
        <!-- 自定义确认弹窗 -->
        <view class="modal-mask" v-if="showConfirmModal">
            <view class="custom-modal">
                <text class="modal-title">提示</text>
                <text class="modal-content">是否确定结束休息，开始接单?</text>
                <view class="modal-btns">
                    <button class="modal-btn cancel" @click="closeConfirmModal">取消</button>
                    <button class="modal-btn confirm" @click="confirmStartWork">确定</button>
                </view>
            </view>
        </view>
        <!-- 宠物档案弹窗 -->
        <view class="pet-modal-mask" v-if="showPetModal" @click="closePetProfile">
            <view class="pet-modal-content" @click.stop>
                <view class="pet-modal-header">
                    <text class="pet-modal-title">宠物档案</text>
                    <view class="close-icon-btn" @click="closePetProfile">×</view>
                </view>
                <scroll-view scroll-y class="pet-modal-scroll">
                    <!-- Basic Info -->
                    <view class="pet-base-info">
                        <image class="pm-avatar" :src="currentPetInfo.petAvatar" mode="aspectFill"></image>
                        <view class="pm-info-text">
                            <view class="pm-name-row">
                                <text class="pm-name">{{ currentPetInfo.petName }}</text>
                                <!-- Gender badge -->
                                <view class="pm-gender" v-if="currentPetInfo.petGender === 'M'">
                                    <text class="gender-icon">♂</text>
                                    <text>公</text>
                                </view>
                                <view class="pm-gender female" v-else-if="currentPetInfo.petGender === 'F'">
                                    <text class="gender-icon">♀</text>
                                    <text>母</text>
                                </view>
                            </view>
                            <text class="pm-breed">品种：{{ currentPetInfo.petBreed }}</text>
                        </view>
                    </view>
                    <!-- Details Grid -->
                    <view class="pm-detail-grid">
                        <view class="pm-grid-item half">
                            <text class="pm-label">年龄</text>
                            <text class="pm-val">{{ currentPetInfo.petAge || '未知' }}</text>
                        </view>
                        <view class="pm-grid-item half">
                            <text class="pm-label">体重</text>
                            <text class="pm-val">{{ currentPetInfo.petWeight || '未知' }}</text>
                        </view>
                        <view class="pm-grid-item full">
                            <text class="pm-label">性格</text>
                            <text class="pm-val">{{ currentPetInfo.petPersonality || '无' }}</text>
                        </view>
                        <view class="pm-grid-item full">
                            <text class="pm-label">爱好</text>
                            <text class="pm-val">{{ currentPetInfo.petHobby || '无' }}</text>
                        </view>
                        <view class="pm-grid-item full">
                            <text class="pm-label">备注</text>
                            <text class="pm-val">{{ currentPetInfo.petRemark || '无特殊过敏史' }}</text>
                        </view>
                    </view>
                    <!-- Tags -->
                    <view class="pm-tags" v-if="currentPetInfo.petTags && currentPetInfo.petTags.length > 0">
                        <view class="pm-tag" v-for="(tag, index) in currentPetInfo.petTags" :key="index">{{ tag }}
                        </view>
                    </view>

                    <!-- Log Section -->
                    <view class="pm-section-title">
                        <view class="orange-bar"></view>
                        <text>备注日志</text>
                    </view>
                    <view class="pm-log-list">
                        <view class="pm-log-item" v-for="(log, lIndex) in currentPetInfo.petLogs" :key="lIndex">
                            <text class="pm-log-date">{{ log.date }}</text>
                            <text class="pm-log-text">{{ log.content }}</text>
                            <text class="pm-log-recorder">{{ log.recorder === '系统记录' ? '' : '记录人: ' }}{{ log.recorder
                            }}</text>
                        </view>
                    </view>
                    <view style="height: 40rpx;"></view>
                    <button class="pm-bottom-close" @click="closePetProfile">关闭</button>
                    <view style="height: 20rpx;"></view>
                </scroll-view>
            </view>
        </view>
    </view>

    <!-- 拒绝接单弹窗 -->
    <view class="modal-mask" v-if="showRejectModal">
        <view class="custom-modal">
            <text class="modal-title">拒绝接单</text>
            <textarea class="reject-textarea" v-model="rejectReason" placeholder="请输入拒绝理由（必填）"
                maxlength="100"></textarea>
            <view class="modal-btns mt-30">
                <button class="modal-btn cancel" @click="closeRejectModal">取消</button>
                <button class="modal-btn confirm" @click="confirmReject">提交</button>
            </view>
        </view>
    </view>

    <!-- 确认接单弹窗 -->
    <view class="modal-mask" v-if="showAcceptConfirmModal">
        <view class="custom-modal">
            <text class="modal-title">接单确认</text>
            <view class="modal-content-box">
                <text class="modal-content-main">请确认是否接收此订单？</text>
                <text class="modal-content-sub">接单后请尽快通过电话联系用户</text>
            </view>
            <view class="modal-btns mt-30">
                <button class="modal-btn cancel" @click="closeAcceptModal">再想想</button>
                <button class="modal-btn confirm" @click="confirmAccept">确认接单</button>
            </view>
        </view>
    </view>

    <!-- 选择地图导航弹窗 -->
    <view class="nav-modal-mask" v-if="showNavModal" @click="closeNavModal">
        <view class="nav-action-sheet" @click.stop>
            <view class="nav-sheet-title">选择地图进行导航</view>
            <view class="nav-sheet-item" @click="chooseMap('高德')">高德地图</view>
            <view class="nav-sheet-item" @click="chooseMap('腾讯')">腾讯地图</view>
            <view class="nav-sheet-item" @click="chooseMap('百度')">百度地图</view>
            <view class="nav-sheet-gap"></view>
            <view class="nav-sheet-item cancel" @click="closeNavModal">取消</view>
        </view>
    </view>
    <custom-tabbar currentPath="pages/home/index"></custom-tabbar>
</template>

<script>
import logic from './logic.js';
export default {
    ...logic
}
</script>

<style>
@import './style.css';
</style>
