var company = getApp().globalData.company;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        realname: '',
        // cardtype: '身份证', // 注释掉证件类型
        // idcard: '', // 注释掉身份证号码
        mobile: '',
        formctrl: {
            realname: '姓名',
            // cardtype: '证件类型', // 注释掉证件类型
            // idcard: '证件号码', // 注释掉身份证号码
            mobile: '手机号码' 
        },
        showTypePicker: false,
        columns: ['身份证', '护照', '港澳通行证', '台胞证'], // 注释掉证件类型选择
    },
    
    selectCardtype() {
        // this.setData({
        //     showTypePicker: true // 注释掉证件类型选择
        // })
    },
    
    typePickerEnter(event) {
        // const { picker, value, index } = event.detail;
        // console.log(picker, value, index);
        // console.log(`当前值：${value}, 当前索引：${index}`);
        // this.setData({
        //     cardtype: value,
        //     showTypePicker: false
        // })
    },
    
    typePickerCancel() {
        // console.log('取消');
        // this.setData({
        //     showTypePicker: false
        // })
    },
    
    onChange() {
        console.log(this.data.realname);
    },
    
    checkform() {
        let formctrl = this.data.formctrl;
        let keys = Object.keys(formctrl);
        for (let index = 0; index < keys.length; index++) {
            let key = keys[index];
            if (this.data[key].trim() == '') {
                console.log(key, this.data[key]);
                wx.showToast({
                    title: `请输入${formctrl[key]}`,
                    icon: 'error'
                });
                return false;
            }
        }
        
        // 只检查姓名和手机号码
        let checkName = /[\u4e00-\u9fa5]/.test(this.data.realname);
        if (!checkName) {
            wx.showToast({
                title: '请输入正确姓名',
                icon: 'none',
                duration: 2000
            });
            return false;
        }
        
        let checkMobile = /^1[34578]\d{9}$/.test(this.data.mobile);
        if (!checkMobile) {
            wx.showToast({
                title: '手机号码格式有误',
                icon: 'none',
                duration: 2000
            });
            return false;
        }

        return true;
    },
    
    async save() {
        let checked = this.checkform();
        if (!checked) {
            return false;
        }
    
        wx.showToast({
            title: '添加成功',
            icon: 'none',
            duration: 1000
        });
    
        let formdata = {
            realname: this.data.realname,
            // cardtype: this.data.cardtype, // 注释掉证件类型
            // idcard: this.data.idcard, // 注释掉身份证号码
            mobile: this.data.mobile
        };
    
        let customers = wx.getStorageSync("customers") || []; // 确保 customers 存在
        const existingCustomerIndex = customers.findIndex(customer => customer.realname === this.data.realname);
    
        if (existingCustomerIndex !== -1) {
            // 如果客户已经存在，则更新客户信息
            customers[existingCustomerIndex] = formdata;
        } else {
            // 如果客户不存在，则添加新的客户信息
            customers.push(formdata);
        }
    
        wx.setStorageSync("customers", customers);
        setTimeout(() => {
            wx.navigateBack({
                delta: 1,
            });
        }, 1000);
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        let { type, name } = options;
        if (type == 'edit' && name) {
            this.initCustomerInfo(name);
        }
    },
    
    initCustomerInfo(name) {
        let customers = wx.getStorageSync("customers");
        if (customers) {
            console.log(customers);
            customers.forEach((customer, index) => {
                if (customer.realname == name) {
                    this.setData({
                        realname: customer.realname,
                        // cardtype: customer.cardtype, // 注释掉证件类型
                        // idcard: customer.idcard, // 注释掉身份证号码
                        mobile: customer.mobile,
                    });
                }
            });
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {}
});