export default {
    data() {
        return {
            formData: {
                mobile: '13612345678',
                code: '',
                name: '张三哥',
                gender: 1, // 1男 2女
                birthday: '2026-02-13',
                password: '',
                serviceType: ['宠物接送'],
                city: '',
                station: ''
            },
            showPwd: false,
            isAgreed: false,
            serviceTypes: ['宠物接送', '上门喂遛', '上门洗护'],

            // 日期选择器相关
            showPicker: false,
            years: [],
            months: [],
            days: [],
            pickerValue: [0, 0, 0], // 选中项索引
            tempYear: 0,
            tempMonth: 0,
            tempDay: 0,

            // 城市选择器相关
            showCityPicker: false,
            // 模拟级联数据
            cityData: [
                {
                    id: 11, name: '北京市', children: [
                        {
                            id: 1101, name: '北京市', children: [
                                { id: 110101, name: '东城区' },
                                { id: 110105, name: '朝阳区' },
                                { id: 110108, name: '海淀区' }
                            ]
                        }
                    ]
                },
                {
                    id: 44, name: '广东省', children: [
                        {
                            id: 4401, name: '广州市', children: [
                                { id: 440106, name: '天河区' },
                                { id: 440104, name: '越秀区' }
                            ]
                        },
                        {
                            id: 4403, name: '深圳市', children: [
                                { id: 440304, name: '福田区' },
                                { id: 440305, name: '南山区' },
                                { id: 440306, name: '宝安区' },
                                { id: 440309, name: '龙华区' }
                            ]
                        }
                    ]
                }
            ],
            selectStep: 0, // 0:省, 1:市, 2:区
            selectedPathway: [], // 已选节点 [{id, name}, ...]
            currentList: [], // 当前可选列表


            // 站点选择器相关
            showStationPicker: false,
            stationList: ['民治街道第一站', '民治街道第二站', '龙华中心站', '福田区分站'],

            // 协议弹窗
            showPrivacy: false,
            privacyTitle: '',
            privacyContent: ''
        }
    },
    created() {
        this.initDateData();
    },
    methods: {
        initDateData() {
            const now = new Date();
            const currentYear = now.getFullYear();

            // 初始化年份 (1980 - 2030)
            for (let i = 1980; i <= currentYear + 5; i++) {
                this.years.push(i);
            }
            // 初始化月份
            for (let i = 1; i <= 12; i++) {
                this.months.push(i);
            }
            // 初始化日期 (默认31天, 实际应动态计算, 这里简化处理或在change中联动)
            for (let i = 1; i <= 31; i++) {
                this.days.push(i);
            }
        },

        // 打开选择器
        openPicker() {
            // 解析当前选中日期或默认日期
            const dateStr = this.formData.birthday || '2000-01-01';
            const [y, m, d] = dateStr.split('-').map(Number);

            // 查找索引
            const yIndex = this.years.indexOf(y);
            const mIndex = this.months.indexOf(m);
            const dIndex = this.days.indexOf(d);

            this.pickerValue = [
                yIndex > -1 ? yIndex : 0,
                mIndex > -1 ? mIndex : 0,
                dIndex > -1 ? dIndex : 0
            ];

            this.tempYear = this.years[this.pickerValue[0]];
            this.tempMonth = this.months[this.pickerValue[1]];
            this.tempDay = this.days[this.pickerValue[2]];

            this.showPicker = true;
        },

        closePicker() {
            this.showPicker = false;
        },

        onPickerChange(e) {
            const val = e.detail.value;
            this.tempYear = this.years[val[0]];
            this.tempMonth = this.months[val[1]];
            this.tempDay = this.days[val[2]];
        },

        confirmPicker() {
            // 格式化日期
            const mStr = this.tempMonth < 10 ? '0' + this.tempMonth : this.tempMonth;
            const dStr = this.tempDay < 10 ? '0' + this.tempDay : this.tempDay;
            this.formData.birthday = `${this.tempYear}-${mStr}-${dStr}`;
            this.closePicker();
        },

        toggleService(item) {
            const idx = this.formData.serviceType.indexOf(item);
            if (idx > -1) {
                this.formData.serviceType.splice(idx, 1);
            } else {
                this.formData.serviceType.push(item);
            }
        },

        // 城市选择器 logic
        openCityPicker() {
            this.showCityPicker = true;
            // 如果未选择过，初始化第一级
            if (this.selectedPathway.length === 0) {
                this.resetCityPicker();
            }
        },
        resetCityPicker() {
            this.selectStep = 0;
            this.selectedPathway = [];
            this.currentList = this.cityData;
        },
        closeCityPicker() {
            this.showCityPicker = false;
        },
        // 点击列表项
        selectCityItem(item) {
            // 记录当前选择
            this.selectedPathway[this.selectStep] = item;

            // 尝试获取下一级
            if (item.children && item.children.length > 0) {
                this.selectStep++;
                this.currentList = item.children;
                // 清理后续旧数据（如果是重新选择的情况）
                this.selectedPathway = this.selectedPathway.slice(0, this.selectStep);
            } else {
                // 没有下一级了，自动确认
                this.confirmCity();
            }
        },
        // 点击“已选路径”回溯
        jumpToStep(step) {
            this.selectStep = step;
            // 重新计算该级的列表
            if (step === 0) {
                this.currentList = this.cityData;
            } else {
                // 列表是上一级选择的 item 的 children
                const parent = this.selectedPathway[step - 1];
                this.currentList = parent ? parent.children : [];
            }
        },
        confirmCity() {
            // 拼接完整名称
            const fullPath = this.selectedPathway.map(i => i.name).join(' ');
            this.formData.city = fullPath;
            this.closeCityPicker();
        },

        // 站点选择器
        openStationPicker() {
            this.showStationPicker = true;
        },
        closeStationPicker() {
            this.showStationPicker = false;
        },
        selectStation(item) {
            this.formData.station = item;
            this.closeStationPicker();
        },

        openPrivacy() {
            this.privacyTitle = '宠宝履约者说明';
            this.privacyContent = '1. 履约职责\n作为宠宝履约者，您需要按照平台标准完成宠物接送、喂遛或洗护服务，确保宠物安全与健康。\n\n2. 结算方式\n服务费用将根据订单类型和距离计算，定期结算至您的账户。具体结算周期请查看钱包说明。\n\n3. 行为规范\n请在这个过程中保持专业，穿着整洁，礼貌待人。严禁虐待宠物，违反者将承担法律责任。';
            this.showPrivacy = true;
        },

        goToAuth() {
            if (!this.isAgreed) {
                uni.showToast({ title: '请勾选协议', icon: 'none' });
                return;
            }
            const services = JSON.stringify(this.formData.serviceType);
            uni.navigateTo({
                url: `/pages/recruit/auth?services=${services}`
            });
        }
    }
}
