// nova-tourism/pages/homestay/customer/select-customer/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        customers: []
    },
    navigate(){
        wx.navigateTo({
            url: `/nova-tourism/pages/homestay/customer/customer-info/index`
        })  
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

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

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let customers = wx.getStorageSync('customers');
        if(customers.length){
            customers = customers.map(customer =>  {
                 customer['idcard'] = this.hiddenMiddleText(customer['idcard'])
                 customer['checked'] = false
                 return customer;
            })
        }
        this.setData({
            customers
        })
        console.log(this.data.customers);
    },
    radioChange(event){
        console.log(111,event);
        let index = event.currentTarget.dataset.index;
        this.setData({
            [`customers[${index}].checked`] : !this.data.customers[index].checked
        })
        console.log(this.data.customers[index].checked);
    },
    toEdit(event){
        console.log(111,event);
        let name = event.currentTarget.dataset.name;
        wx.navigateTo({
            url: `/nova-tourism/pages/homestay/customer/customer-info/index?type=edit&name=${name}`
        })  
    },
    hiddenMiddleText(text){
        return text.replace(/(.{5}).*(.{3})/, "$1********$2")
    },
    save(){
        let customers = this.checkedCustomer();
        if(!customers.length || customers.length == 0) return;
        // 入住人数据保存到上一页面，再进行跳转
        var pages = getCurrentPages();var prevPage = pages[pages.length - 2];
        console.log(pages,prevPage,customers);
        prevPage.setData({
            customers
        })
          wx.navigateBack({
          delta: 1,
        })
        
        
    },
    checkedCustomer(){
        let customers = []
        for (let index = 0; index < this.data.customers.length; index++) {
            let customer = this.data.customers[index];
            if(customer.checked == true){
                customers.push(customer)
            }
        }
        return customers
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

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

    },

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

    },

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

    },

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

    }
})