// pages/my/address/addSite/addSite.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: false,
        resultInfo: {
            name: '',
            phone: '',
            address: '',
            checked: false
        },
        name: '',
        phone: '',
        address: '',
        checked4: false,
        id:""

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var type = options.parmise == 'add' ? true : false;
        console.log(options.parmise);
        if(!type){
        var parmise = JSON.parse(options.parmise)
       
        this.setData({
            type: type,
            resultInfo: {
                name: parmise.username,
                phone: parmise.phone,
                address: parmise.address,
                checked4: parmise.is_default == 1 ? true : false
            },
            name: parmise.username,
            phone: parmise.phone,
            address: parmise.address,
            checked4: parmise.is_default == 1 ? true : false,
            id:parmise.id,
        })
    }else{
        this.setData({type: type})
    }
        if (this.data.type) {
            wx.setNavigationBarTitle({
                title: '添加新地址',
            })
        } else {
            wx.setNavigationBarTitle({
                title: '编辑地址',
            })
        }
    },
    onChange(e) {
        console.log(e);
        this.setData({
            checked4: e.detail
        })
        this.data.resultInfo.checked = this.data.checked4
    },
    input1(e) {
        console.log(this.data.resultInfo.name);
        this.setData({
            name: e.detail
        })
        this.data.resultInfo.name = this.data.name
        if (this.data.resultInfo.name != '') {
            this.setData({
                checked1: true
            })
        } else {
            this.setData({
                checked1: false
            })
        }
    },
    input2(e) {
        this.setData({
            phone: e.detail
        })
        this.data.resultInfo.phone = this.data.phone
        if (this.data.resultInfo.phone != '') {
            this.setData({
                checked2: true
            })
        } else {
            this.setData({
                checked2: false
            })
        }
    },
    input3(e) {
        this.setData({
            address: e.detail
        })
        this.data.resultInfo.address = this.data.address
        if (this.data.resultInfo.address != '') {
            this.setData({
                checked3: true
            })
        } else {
            this.setData({
                checked3: false
            })
        }

        console.log(this.data.checked1, this.data.checked2, this.data.checked3);
    },

    addAddress() {
        var that = this;
        app.api.useApi(app.globalData.baseAppUrl + "api/address/create", {
            username: that.data.resultInfo.name,
            phone: that.data.resultInfo.phone,
            address: that.data.resultInfo.address,
            is_default: that.data.resultInfo.checked

        }, "post").then(function (res) {
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.msg,
                })
            } else {
                wx.showLoading({
                    title: res.message.msg,
                })
                setTimeout(() => {
                    wx.navigateBack()
                }, 1000);
            }
            setTimeout(function () {
                wx.hideLoading({
                    success: (res) => { },
                })
            }, 2000);


        }).catch(function (err) {
            console.log(222);
        })
    },
    del(e) {
        console.log(e);
        app.api.useApi(app.globalData.baseAppUrl + "api/address/del", {
            id:e.target.dataset.id,
        }, "post").then(function (res) {
            console.log(res)
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.msg,
                })
            } else {
                wx.showLoading({
                    title: res.message.msg,
                })
                setTimeout(() => {
                    wx.navigateBack()
                }, 1000);
            }
            setTimeout(function () {
                wx.hideLoading({
                    success: (res) => { },
                })
            }, 2000);


        }).catch(function (err) {
            console.log(222);
        })
    },
    updateaddr(e){
        var that = this;
        app.api.useApi(app.globalData.baseAppUrl + "api/address/update", {
            username: that.data.resultInfo.name,
            phone: that.data.resultInfo.phone,
            address: that.data.resultInfo.address,
            is_default: that.data.resultInfo.checked,
            id:e.target.dataset.id,

        }, "post").then(function (res) {
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.msg,
                })
            } else {
                wx.showLoading({
                    title: res.message.msg,
                })
                setTimeout(() => {
                    wx.navigateBack()
                }, 1000);
            }
            setTimeout(function () {
                wx.hideLoading({
                    success: (res) => { },
                })
            }, 2000);


        }).catch(function (err) {
            console.log(222);
        })
    }
})