export default {
    data() {
        return {
            orderType: 1, // 1:接送, 2:喂遛, 3:洗护

            // Define steps dynamically based on type
            stepsPickup: ['接单', '到达', '出发', '送达', '完成'],
            stepsWalkWash: ['接单', '到达', '开始', '结束', '完成'],

            currentStep: 1, // 0-indexed (1 means 到达)

            // Mock Data
            orderDetail: {
                type: 1,
                price: '20.00',
                timeLabel: '取货时间',
                time: '2026/02/10 10:00',
                petAvatar: '/static/dog.png',
                petName: '哈士奇宝宝',
                petBreed: '哈士奇',
                serviceTag: '宠物接送',
                startLocation: '武汉大学宠物店',
                startAddress: '武汉市洪山区珞喻路458号',
                endLocation: '张** 189****8451',
                endAddress: '武汉市武昌区新区大道凤凰广场A座一楼25号',
                serviceContent: '这里是订单服务内容。',
                remark: '这里是订单备注信息。',
                orderNo: 'T1001',
                createTime: '2026/02/10 09:30',
                progressLogs: [
                    { status: '您已接单', time: '2026-02-10 10:00' }
                ]
            },

            // Modal states
            showPetModal: false,
            currentPetInfo: {},
            showNavModal: false,
            navTargetPointType: '',

            // Upload Modal State
            showUploadModal: false,
            modalMediaList: [],
            modalRemark: '',

            // 宠护小结弹窗状态
            showSumModal: false,
            sumContent: '',
            sumDate: '',
            sumSigner: '张*哥',

            // 宠物备注弹窗状态
            showPetRemarkInput: false,
            petRemarkText: ''
        }
    },
    computed: {
        steps() {
            return this.orderType === 1 ? this.stepsPickup : this.stepsWalkWash;
        },
        displayStatusText() {
            let status = this.steps[this.currentStep];
            if (status === '已完成' || status === '完成') return '已完成';
            if (status === '已拒绝') return '已拒绝';
            if (status === '接单') {
                return this.orderType === 1 ? '待接送' : '待服务';
            }
            // For other active states (出发, 到达, 送达, 开始, 结束)
            return this.orderType === 1 ? '配送中' : '服务中';
        },
        currentStatusText() {
            return this.steps[this.currentStep];
        },
        currentTaskTitle() {
            let action = this.steps[this.currentStep];
            if (action === '到达') return '到达打卡';
            if (action === '开始') return '开始服务';
            if (action === '出发') return '确认出发';
            if (action === '送达' || action === '结束') return '服务完成';
            return action;
        },
        currentTaskDesc() {
            let action = this.steps[this.currentStep];
            if (action === '到达') {
                return '打卡穿着工装消毒站门口的照片或视频';
            }
            if (this.orderType === 1) { // 送货
                if (action === '出发') return '拍摄宠物上车/出发时的状态照片或视频';
                if (action === '送达') return '打卡确认送达的照片或视频';
            } else if (this.orderType === 2) { // 喂遛
                if (action === '开始') return '开始服务并拍摄照片或视频';
                if (action === '结束') return '服务完成拍摄照片或视频';
            } else if (this.orderType === 3) { // 洗护
                if (action === '开始') return '开始服务并拍摄照片或视频';
                if (action === '结束') return '服务完成拍摄照片或视频';
            }
            return '请按要求提交照片或视频及备注';
        }
    },
    onLoad(options) {
        if (options.type) {
            this.orderType = parseInt(options.type);
            this.orderDetail.type = this.orderType;
            if (this.orderType === 2 || this.orderType === 3) {
                this.orderDetail.serviceTag = this.orderType === 2 ? '上门喂遛' : '上门洗护';
                this.orderDetail.orderNo = this.orderType === 2 ? 'W1002' : 'X1003';
                this.currentStep = 1;
            }
        }
    },
    methods: {
        showPetProfile() {
            // Use orderDetail basic info and mock the rest
            this.currentPetInfo = {
                ...this.orderDetail,
                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;
        },
        openPetRemarkInput() {
            this.petRemarkText = '';
            this.showPetRemarkInput = true;
        },
        closePetRemarkInput() {
            this.showPetRemarkInput = false;
        },
        submitPetRemark() {
            if (!this.petRemarkText.trim()) {
                uni.showToast({ title: '备注内容不能为空', icon: 'none' });
                return;
            }
            const now = new Date();
            const date = `${now.getFullYear()}/${String(now.getMonth() + 1).padStart(2, '0')}/${String(now.getDate()).padStart(2, '0')}`;
            if (!this.currentPetInfo.petLogs) {
                this.currentPetInfo.petLogs = [];
            }
            this.currentPetInfo.petLogs.unshift({
                date: date,
                content: this.petRemarkText,
                recorder: '张*哥'
            });
            this.closePetRemarkInput();
            uni.showToast({ title: '备注已添加', icon: 'success' });
        },
        goToAnomaly() {
            uni.navigateTo({
                url: '/pages/orders/anomaly?orderId=' + (this.orderDetail.orderNo || '')
            });
        },
        callPhone() {
            uni.makePhoneCall({ phoneNumber: '18900008451' });
        },
        openNavigation(type) {
            this.navTargetPointType = type;
            this.showNavModal = true;
        },
        closeNavModal() {
            this.showNavModal = false;
        },
        chooseMap(mapType) {
            let pointType = this.navTargetPointType;
            let name = pointType === 'start' ? this.orderDetail.startLocation : this.orderDetail.endLocation;
            let address = pointType === 'start' ? this.orderDetail.startAddress : this.orderDetail.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);
                }
            });
        },
        openUploadModal() {
            this.modalMediaList = [];
            this.modalRemark = '';
            this.showUploadModal = true;
        },
        closeUploadModal() {
            this.showUploadModal = false;
        },
        chooseModalMedia() {
            uni.chooseImage({
                count: 5 - this.modalMediaList.length,
                success: (res) => {
                    this.modalMediaList = this.modalMediaList.concat(res.tempFilePaths);
                    uni.showToast({ title: '添加成功', icon: 'none' });
                }
            });
        },
        removeModalMedia(index) {
            this.modalMediaList.splice(index, 1);
        },
        getCurrentTime() {
            const now = new Date();
            const y = now.getFullYear();
            const m = String(now.getMonth() + 1).padStart(2, '0');
            const d = String(now.getDate()).padStart(2, '0');
            const h = String(now.getHours()).padStart(2, '0');
            const min = String(now.getMinutes()).padStart(2, '0');
            return `${y}/${m}/${d} ${h}:${min}`;
        },
        confirmUploadModal() {
            if (this.modalMediaList.length === 0) {
                uni.showToast({ title: '请上传至少一张图片或视频', icon: 'none' });
                return;
            }

            // Append to timeline
            this.orderDetail.progressLogs.unshift({
                status: this.currentTaskTitle,
                time: this.getCurrentTime(),
                medias: [...this.modalMediaList],
                remark: this.modalRemark
            });

            // Advance step
            if (this.currentStep < this.steps.length - 1) {
                this.currentStep++;
            }

            this.closeUploadModal();
            uni.showToast({ title: '打卡成功', icon: 'success' });

            // Check if it's finished now
            if (this.currentStep === this.steps.length - 1) {
                setTimeout(() => {
                    uni.showToast({ title: '订单已完成', icon: 'none' });
                }, 1500);
            }
        },
        copyOrderNo() {
            uni.setClipboardData({
                data: this.orderDetail.orderNo,
                success: () => {
                    uni.showToast({ title: '复制成功', icon: 'none' });
                }
            });
        },
        openSumModal() {
            // 初始化日期
            const now = new Date();
            const y = now.getFullYear();
            const m = String(now.getMonth() + 1).padStart(2, '0');
            const d = String(now.getDate()).padStart(2, '0');
            this.sumDate = `${y}/${m}/${d}`;

            // 预设服务内容模板
            if (!this.sumContent) {
                this.sumContent =
                    '1. 精神/身体状态：\n' +
                    '2. 进食/饮水：\n' +
                    '3. 排泤情况：\n' +
                    '4. 卫生情况：\n' +
                    '5. 互动情况：\n' +
                    '6. 特殊情况/备注：';
            }
            this.showSumModal = true;
        },
        closeSumModal() {
            this.showSumModal = false;
        },
        submitSumModal() {
            if (!this.sumContent.trim()) {
                uni.showToast({ title: '请填写服务内容', icon: 'none' });
                return;
            }
            this.closeSumModal();
            uni.showToast({ title: '小结已提交', icon: 'success' });
        }
    }
}
