export default {
    data() {
        return {
            currentTab: 0,
            tabs: ['待接送/服务', '配送/服务中', '已完成', '已拒绝'],
            // Filter dropdown states
            typeFilterOptions: ['全部类型', '接送', '喂遛', '洗护'],
            currentTypeFilterIdx: 0,
            activeDropdown: 0, // 0 = none, 1 = type filter, 2 = time filter
            hasTimeFilter: false,

            // Custom Calendar State (Mock Feb 2026)
            currentMonth: '2026年2月',
            weekDays: ['日', '一', '二', '三', '四', '五', '六'],
            calendarDays: [], // Generated in onLoad or created
            selectedDateRange: [], // stores [start, end]

            allOrderList: [
                // === Tab 1: 待接送/服务 (Status: 接单) ===
                { type: 1, typeText: '接送', typeIcon: '/static/icons/car.svg', statusText: '接单', price: '20.00', timeLabel: '取货时间', time: '2026/02/10 10:00', petAvatar: '/static/dog.png', petName: '哈士奇宝宝', petBreed: '哈士奇', startLocation: '武汉大学宠物店', startAddress: '武汉市洪山区珞喻路458号', startDistance: '0.5km', endLocation: '张** 189****8451', endAddress: '武汉市武昌区新区大道凤凰广场25号', endDistance: '2.5km', remark: '尽快接单。' },
                { type: 2, typeText: '喂遛', typeIcon: '/static/icons/walk.svg', statusText: '接单', price: '30.00', timeLabel: '服务时间', time: '2026/02/10 14:00', petAvatar: '/static/dog.png', petName: '边牧', petBreed: '边境牧羊犬', endLocation: '李** 138****0001', endAddress: '武汉市洪山区保利花园', endDistance: '1.5km', serviceContent: '需自带牵引绳。', remark: '狗狗很活泼。' },
                { type: 3, typeText: '洗护', typeIcon: '/static/icons/wash.svg', statusText: '接单', price: '60.00', timeLabel: '服务时间', time: '2026/02/10 16:00', petAvatar: '/static/dog.png', petName: '布偶', petBreed: '布偶猫', endLocation: '王** 139****0002', endAddress: '武汉市汉阳区钟家村', endDistance: '3.5km', serviceContent: '上门洗护、全身检查', remark: '猫咪怕水。' },

                // === Tab 2: 配送/服务中 (Status: 出发/开始/到达等) ===
                { type: 1, typeText: '接送', typeIcon: '/static/icons/car.svg', statusText: '出发', price: '25.00', timeLabel: '取货时间', time: '2026/02/11 09:00', petAvatar: '/static/dog.png', petName: '柯基', petBreed: '柯基犬', startLocation: '南湖宠物店', startAddress: '武汉市洪山区南湖大道', startDistance: '1.0km', endLocation: '陈** 158****0003', endAddress: '武汉市江夏区藏龙岛', endDistance: '6.5km', remark: '已接到狗狗，送往目的地。' },
                { type: 2, typeText: '喂遛', typeIcon: '/static/icons/walk.svg', statusText: '开始', price: '35.00', timeLabel: '服务时间', time: '2026/02/11 15:00', petAvatar: '/static/dog.png', petName: '金毛', petBreed: '金毛寻回犬', endLocation: '赵** 159****0004', endAddress: '武汉市江汉区泛海国际', endDistance: '4.5km', serviceContent: '遛弯一小时', remark: '注意避开大型犬。' },
                { type: 3, typeText: '洗护', typeIcon: '/static/icons/wash.svg', statusText: '开始', price: '80.00', timeLabel: '服务时间', time: '2026/02/11 18:00', petAvatar: '/static/dog.png', petName: '英短', petBreed: '英国短毛猫', endLocation: '钱** 177****0005', endAddress: '武汉市武昌区楚河汉街', endDistance: '2.0km', serviceContent: '深度去污、洗护', remark: '客户要求修剪指甲。' },

                // === Tab 3: 已完成 (Status: 已完成) ===
                { type: 1, typeText: '接送', typeIcon: '/static/icons/car.svg', statusText: '已完成', price: '40.00', timeLabel: '取货时间', time: '2026/02/12 10:00', petAvatar: '/static/dog.png', petName: '柴犬', petBreed: '柴犬', startLocation: '汉口宠物医院', startAddress: '武汉市江岸区解放大道', startDistance: '2.0km', endLocation: '孙** 186****0006', endAddress: '武汉市东西湖区金银湖', endDistance: '10.5km', remark: '安全抵达。' },
                { type: 2, typeText: '喂遛', typeIcon: '/static/icons/walk.svg', statusText: '已完成', price: '28.00', timeLabel: '服务时间', time: '2026/02/12 16:00', petAvatar: '/static/dog.png', petName: '萨摩耶', petBreed: '萨摩耶犬', endLocation: '周** 188****0007', endAddress: '武汉市洪山区软件园', endDistance: '0.8km', serviceContent: '遛狗', remark: '遛得很开心。' },
                { type: 3, typeText: '洗护', typeIcon: '/static/icons/wash.svg', statusText: '已完成', price: '120.00', timeLabel: '服务时间', time: '2026/02/12 19:00', petAvatar: '/static/dog.png', petName: '加菲', petBreed: '加菲猫', endLocation: '吴** 189****0008', endAddress: '武汉市青山区别区', endDistance: '5.0km', serviceContent: '全套美容洗护', remark: '客户非常满意。' },

                // === Tab 4: 已拒绝 (Status: 已拒绝) ===
                { type: 1, typeText: '接送', typeIcon: '/static/icons/car.svg', statusText: '已拒绝', price: '15.00', timeLabel: '取货时间', time: '2026/02/13 09:00', petAvatar: '/static/dog.png', petName: '吉娃娃', petBreed: '吉娃娃', startLocation: '宠物乐园', startAddress: '武汉市黄陂区盘龙城', startDistance: '3.0km', endLocation: '郑** 133****0009', endAddress: '武汉市汉南区', endDistance: '20.5km', remark: '路程太远。' },
                { type: 2, typeText: '喂遛', typeIcon: '/static/icons/walk.svg', statusText: '已拒绝', price: '22.00', timeLabel: '服务时间', time: '2026/02/13 14:00', petAvatar: '/static/dog.png', petName: '博美', petBreed: '博美犬', endLocation: '刘** 134****0010', endAddress: '武汉市蔡甸区', endDistance: '15.0km', serviceContent: '公园遛弯', remark: '天气原因取消。' },
                { type: 3, typeText: '洗护', typeIcon: '/static/icons/wash.svg', statusText: '已拒绝', price: '45.00', timeLabel: '服务时间', time: '2026/02/13 17:00', petAvatar: '/static/dog.png', petName: '暹罗', petBreed: '暹罗猫', endLocation: '张** 135****0011', endAddress: '武汉市新洲区', endDistance: '18.0km', serviceContent: '洗澡', remark: '时间冲突。' }
            ],
            showPetModal: false,
            currentPetInfo: {},
            showNavModal: false,
            navTargetItem: null,
            navTargetPointType: '',

            // Popover control
            activeCallItem: null,
            // 备注输入弹窗
            showRemarkInput: false,
            remarkText: ''
        }
    },
    created() {
        this.initCalendar();
    },
    computed: {
        orderList() {
            return this.allOrderList.filter(item => {
                // 1. Tab Status Filter
                // 待接送/服务: [接送]的接单, [喂遛/洗护]的接单
                // 配送/服务中: [接送]的到达、出发、送达; [喂遛/洗护]的到达、开始、结束
                // 已完成: 完成
                let matchTab = false;
                if (this.currentTab === 0) {
                    matchTab = item.statusText === '接单';
                } else if (this.currentTab === 1) {
                    let pickingTypes = ['到达', '出发', '送达'];
                    let serviceTypes = ['到达', '开始', '结束'];
                    if (item.type === 1) {
                        matchTab = pickingTypes.includes(item.statusText);
                    } else {
                        matchTab = serviceTypes.includes(item.statusText);
                    }
                } else if (this.currentTab === 2) {
                    matchTab = item.statusText === '已完成';
                } else if (this.currentTab === 3) {
                    matchTab = item.statusText === '已拒绝';
                }

                // 2. Type Filter
                let matchType = true;
                if (this.currentTypeFilterIdx > 0) {
                    matchType = item.type === parseInt(this.currentTypeFilterIdx);
                }

                // 3. Time Filter (Mocked)
                let matchTime = true; // For now treat all equally unless parsed

                return matchTab && matchType && matchTime;
            });
        }
    },
    methods: {
        getDisplayStatus(item) {
            if (item.statusText === '已完成') return '已完成';
            if (item.statusText === '已拒绝') return '已拒绝';
            if (item.statusText === '接单') {
                return item.type === 1 ? '待接送' : '待服务';
            }
            // For other active states (出发, 到达, 送达, 开始, 结束)
            return item.type === 1 ? '配送中' : '服务中';
        },
        getStatusClass(item) {
            let display = this.getDisplayStatus(item);
            if (display === '已完成') return 'finish';
            if (display === '已拒绝') return 'reject';
            if (display === '配送中' || display === '服务中') return 'processing';
            return 'highlight';
        },
        goToDetail(item) {
            // 将在 detail 页面实现
            uni.navigateTo({
                url: `/pages/orders/detail?type=${item.type}`
            });
        },
        showPetProfile(item) {
            // 补全 mock 数据
            this.currentPetInfo = {
                ...item,
                petGender: 'M',
                petAge: '2岁',
                petWeight: '15kg',
                petPersonality: '活泼亲人，精力旺盛',
                petHobby: '喜欢追飞盘，爱吃肉干',
                petRemark: '肠胃较弱，不能乱喂零食；出门易爆冲，请拉紧牵引绳。',
                petTags: ['拉响警报', '不能吃鸡肉', '精力旺盛'],
                petLogs: [
                    { date: '2026-02-09 14:00', content: '今天遛弯拉了两次粑粑，精神状态很好。', recorder: '王阿姨' },
                    { date: '2026-02-08 10:30', content: '有些挑食，剩了小半碗狗粮。', recorder: '李师傅' },
                    { date: '2026-02-05 09:00', content: '建档。', recorder: '系统记录' }
                ]
            };
            this.showPetModal = true;
        },
        closePetProfile() {
            this.showPetModal = false;
        },
        openNavigation(item, pointType) {
            this.navTargetItem = item;
            this.navTargetPointType = pointType;
            this.showNavModal = true;
        },
        closeNavModal() {
            this.showNavModal = false;
        },
        chooseMap(mapType) {
            let item = this.navTargetItem;
            let pointType = this.navTargetPointType;
            let name = pointType === 'start' ? item.startLocation : item.endLocation;
            let address = pointType === 'start' ? item.startAddress : item.endAddress;

            this.showNavModal = false;

            uni.openLocation({
                latitude: 30.52, // Mock lat
                longitude: 114.31, // Mock lng
                name: name || '目的地',
                address: address || '默认地址',
                success: function () {
                    console.log('打开导航成功: ' + mapType);
                }
            });
        },
        toggleCallMenu(item) {
            if (this.activeCallItem === item) {
                this.activeCallItem = null;
            } else {
                this.activeCallItem = item;
            }
        },
        closeCallMenu() {
            this.activeCallItem = null;
        },
        doCall(type) {
            let phoneNum = ''
            if (type === 'merchant') {
                phoneNum = '18900008451' // Mock merchant
            } else if (type === 'customer') {
                phoneNum = '13800000001' // Mock customer
            }
            if (phoneNum) {
                uni.makePhoneCall({ phoneNumber: phoneNum })
            }
            this.activeCallItem = null;
        },
        reportAbnormal(item) {
            uni.showToast({ title: '上报功能待开发', icon: 'none' });
        },
        toggleDropdown(idx) {
            // If clicking the currently open one, close it
            if (this.activeDropdown === idx) {
                this.activeDropdown = 0;
            } else {
                this.activeDropdown = idx;
            }
        },
        closeDropdown() {
            this.activeDropdown = 0;
        },
        selectType(index) {
            this.currentTypeFilterIdx = index;
            this.closeDropdown();
        },
        initCalendar() {
            // Mock Feb 2026 (1 is Sunday, 28 is Saturday)
            let days = [];
            for (let i = 1; i <= 28; i++) {
                days.push(i);
            }
            this.calendarDays = days;
            // Pre-select 2, 3, 4 for the demo in Figure 2
            this.selectedDateRange = [2, 4];
        },
        prevMonth() { uni.showToast({ title: '上个月', icon: 'none' }); },
        nextMonth() { uni.showToast({ title: '下个月', icon: 'none' }); },
        selectDateItem(day) {
            // Simple range toggle logic
            if (this.selectedDateRange.length === 2) {
                this.selectedDateRange = [day]; // Reset to new start
            } else if (this.selectedDateRange.length === 1) {
                let start = this.selectedDateRange[0];
                if (day > start) {
                    this.selectedDateRange = [start, day];
                } else if (day < start) {
                    this.selectedDateRange = [day, start];
                } else {
                    this.selectedDateRange = []; // clear if same clicked
                }
            } else {
                this.selectedDateRange = [day];
            }
        },
        getDateClass(day) {
            if (this.selectedDateRange.length === 0) return '';
            if (this.selectedDateRange.length === 1) {
                return day === this.selectedDateRange[0] ? 'is-start' : '';
            }
            let start = this.selectedDateRange[0];
            let end = this.selectedDateRange[1];
            if (day === start) return 'is-start';
            if (day === end) return 'is-end';
            if (day > start && day < end) return 'is-between';
            return '';
        },
        resetTimeFilter() {
            this.hasTimeFilter = false;
            this.selectedDateRange = [];
            uni.showToast({ title: '已重置服务时间', icon: 'none' });
            this.closeDropdown();
        },
        confirmTimeFilter() {
            if (this.selectedDateRange.length === 0) {
                uni.showToast({ title: '请先选择日期', icon: 'none' });
                return;
            }
            let text = this.selectedDateRange.length === 2 ?
                `${this.selectedDateRange[0]}日-${this.selectedDateRange[1]}日` :
                `${this.selectedDateRange[0]}日`;
            uni.showToast({ title: '已应用: ' + text, icon: 'none' });
            this.hasTimeFilter = true;
            this.closeDropdown();
        },
        getMainActionText(item) {
            if (item.statusText === '接单') {
                return '到达打卡'; // Next step
            } else if (item.statusText === '到达' && item.type === 1) {
                return '确认出发';
            } else if (item.statusText === '到达' && item.type !== 1) {
                return '开始服务';
            } else if (item.statusText === '出发') {
                return '送达打卡';
            } else if (item.statusText === '开始') {
                return '结束服务';
            } else if (item.statusText === '送达' || item.statusText === '结束') {
                return '服务完成';
            }
            return '查看详情';
        },
        mainAction(item) {
            // Simply navigate to detail page, where the real action button lives
            uni.navigateTo({
                url: `/pages/orders/detail?type=${item.type}`
            });
        },
        reportAbnormal(item) {
            uni.navigateTo({
                url: '/pages/orders/anomaly?orderId=' + (item.orderNo || '')
            });
        },
        // 打开备注输入弹窗
        openRemarkInput() {
            this.remarkText = '';
            this.showRemarkInput = true;
        },
        // 关闭备注输入弹窗
        closeRemarkInput() {
            this.showRemarkInput = false;
            this.remarkText = '';
        },
        // 提交备注，追加到宠物档案日志列表
        submitRemark() {
            const text = this.remarkText.trim();
            if (!text) {
                uni.showToast({ title: '备注内容不能为空', icon: 'none' });
                return;
            }
            // 获取当前时间
            const now = new Date();
            const dateStr = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`;
            // 追加到当前宠物的日志列表头部
            if (!this.currentPetInfo.petLogs) {
                this.$set(this.currentPetInfo, 'petLogs', []);
            }
            this.currentPetInfo.petLogs.unshift({
                date: dateStr,
                content: text,
                recorder: '我'
            });
            uni.showToast({ title: '备注已添加', icon: 'success' });
            this.closeRemarkInput();
        }
    }
}
