// pages/order/confirmOreder/confirmOreder.js
const app = getApp();
const addr = require('../../my/address/addSite/addSite.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentList: [{
            tit: '塑封包膜',
            num: "30",
            id: 1
        }, {
            tit: '绒面鞋补色',
            num: "20",
            id: 2
        }, {
            tit: '防水处理',
            num: "20",
            id: 3
        }, {
            tit: '防水处理',
            num: "10",
            id: 4
        }],
        mode: 1,
        checked: false,
        show1: false,
        show2: false,
        code: "",
        order_no: "",
        orderinfo: "",
        address: "",
        useraddr: "",
        remark: "",
        address_id:0,

    },
    onLoad(opticon) {
        let order_no = opticon.order_no;
        let currentList = this.data.currentList
        currentList.map(item => {
            item.active = false
        })
        this.detail(order_no);
        this.setData({
            currentList,
            order_no
        })

    },
    //详情
    detail(order) {
        var that = this;
        app.api.useApi(app.globalData.baseAppUrl + "api/order_detail", {
            order_no: order,
        }, "get").then(function (res) {
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.msg,
                })
                setTimeout(function () {
                    wx.hideLoading({
                        success: (res) => { },
                    })
                }, 2000);

            } else {
                that.setData({
                    orderinfo: res.message.data,
                    address: res.message.address.address,
                    code: res.message.address.phone,
                    name: res.message.address.username,
                    address_id:res.message.address.id,
                    useraddr: res.message.address,
                })
            }

        }).catch(function (err) {
            console.log(222);
        })
    },
    changeClose1(e) {
        this.setData({
            show1: !this.data.show1
        })
    },

    changeClose2(e) {
        this.setData({
            show2: !this.data.show2,
            remark: this.data.remark
        })

    },
    onChange(event) {
        console.log(event);
        this.setData({
            checked: event.detail,
        });
    },
    chios(e) {
        let { item } = e.currentTarget.dataset

        let currentList = this.data.currentList
        currentList.map((it, index) => {
            if (item.id == it.id) {
                it.active = !it.active
            }
        })
        this.setData({
            currentList
        })
    },
    changeMode(e) {
        var id = e.target.dataset.id
        console.log(id);
        this.setData({
            mode: id
        })
    },

    goto() {
        if(!this.data.checked){
            wx.showLoading({
                title: '请阅读并同意洗护协议',
            })
            setTimeout(function () {
                wx.hideLoading({
                    success: (res) => { },
                })
            }, 1000);
        }else{this.orderConfirm();}
        
    },
    getPhoneNumber(e) {
        app.login();
        console.log(e)

        var data = {
            'mds': wx.getStorageSync('seisson_key'),
            'encryptedData': e.detail.encryptedData,
            'errMsg': e.detail.errMsg,
            'iv': e.detail.iv,
            'appid': wx.getAppBaseInfo().host.appId,
            'token': wx.getStorageSync('token')
        }
        wx.cloud.callFunction({
            name: 'getPhoneNumber',
            data: {
                'message': data
            },
            success: res => {
                console.log(res)
                var par = res.result.original;
                if (par.code == 200) {
                    var phone = par.message.data.phone
                    if (phone) {
                        this.setData({
                            code: phone,
                        })
                    }
                }
            },
            fail: err => {
                console.log(err)
            }
        })

        // console.log("fetch cloudfunction success", cloudResult.result)




    },
    input1(e) {
        console.log(e.detail);
        this.setData({
            name: e.detail.value
        })
    },
    input2(e) {
        this.setData({
            code: e.detail.value
        })
    },
    input3(e) {
        this.setData({
            address: e.detail.value
        })
    },
    input4(e) {
        this.setData({
            remark: e.detail.value
        })

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

        }, "post").then(function (res) {
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.msg,
                })
                setTimeout(function () {
                    wx.hideLoading({
                        success: (res) => { },
                    })
                }, 1000);
            } else {
                that.changeClose1();
                that.setData({
                    address_id: res.message.data.message.id,
                })
            }
           
        }).catch(function (err) {
            console.log(222);
        })
    },
    updateaddr(e) {
        var that = this;
        app.api.useApi(app.globalData.baseAppUrl + "api/address/update", {
            username: that.data.name,
            phone: that.data.code,
            address: that.data.address,
            id: e.target.dataset.id,
        }, "post").then(function (res) {
            if (res.original.code != 200) {
                wx.showLoading({
                    title: res.original.message.msg,
                })
            } else {
                that.changeClose1();
                // wx.navigateTo({
                //     url: '/pages/my/address/address',
                // })
            }
            setTimeout(function () {
                wx.hideLoading({
                    success: (res) => { },
                })
            }, 2000);


        }).catch(function (err) {
            console.log(222);
        })
    },
    orderConfirm() {
        var that=this;
        app.api.useApi(app.globalData.baseAppUrl + "api/orders", {
            order_no: that.data.order_no,
            order_type: that.data.mode,
            user_remark: that.data.remark,
            address_id:that.data.address_id
        }, "post").then(function (res) {
            if (res.original.code != 200) {
                wx.showLoading({
                    title: res.original.message.msg,
                })
                setTimeout(function () {
                    wx.hideLoading({
                        success: (res) => { },
                    })
                }, 1000);

            } else {

                wx.navigateTo({
                    url: '/pages/my/cashier/cashier'
                })
            }


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



})