

// pages/order/deposit/deposit.js
const app = getApp();
let timer;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderList: [
        ],
        logisticsStatus: "",
        show: false,
        result: false,
        result2: false,
        countDownNum: 0,
        spot_id: 0,
        device_no: "",
        order_num: "",
        is_deposit: 1,
        netwrok_id: 0,
        order_type: 0,
        external_cabinet:0,

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var order_type = options.order_type ? options.order_type : 2
        var netwrok_id = options.network_id;
        var order_num = options.order_num;
        var is_deposit = options.is_deposit;
        var device_id = options.device_id?options.device_id:0;

        console.log(options);
        this.setData({
            logisticsStatus: app.globalData.logisticsStatus,
            order_num: order_num,
            netwrok_id: netwrok_id,
            is_deposit: is_deposit,
            device_id:device_id,
            order_type: order_type
        })


        this.getorderList(order_type, netwrok_id, device_id);


        if (this.data.is_deposit != 1) {
            wx.setNavigationBarTitle({
                title: '等待取出',
            })
        }
    },
    againtakeclothes(){
        let that = this
        // console.log('type:'+e.currentTarget.dataset.type)
        app.api.useApi(app.globalData.baseAppUrl + "api/again_save", {
            // status: status,
            spot_id: that.data.spot_id,
        }, "post").then(function (res) {
            console.log(res);
            if (res.code != 200) {
                that.countDown();
                that.setData({
                    device_no: res.message.data.device_no,
                    spot_id:res.message.data.spot_id,
                })
                wx.showToast({
                    title: res.message.msg,
                })
                console.log(res.message.msg)
                setTimeout(function () {
                     wx.hideLoading();
                    that.setData({
                        show: !that.data.show
                    })
                }, 1000)

            } else {
                console.log(res);
                wx.showToast({
                    title: res.message.msg,
                })
                that.countDown();
                that.setData({
                    spot_id: res.message.data.spot_id,
                })
                // that.takeClothes()
                // that.goto()
            }
             wx.hideLoading();
        }).catch(function (err) {
            console.log(err.message)
        })
    },
    onClose(e) {
        let that = this
        console.log('type:'+e.currentTarget.dataset.type)
        if(that.data.countDownNum<=0){
            var no = e.currentTarget.dataset.no
            var type = e.currentTarget.dataset.type
            var device_id = e.currentTarget.dataset.device_id
            var spout_device_id = e.currentTarget.dataset.spout_device_id
            var external_cabinet = e.currentTarget.dataset.external_cabinet
            var metoh = e.currentTarget.dataset.metoh
            wx.nextTick(() => {
                that.setData({
                    show: !that.data.show
                })
            })
            if (that.data.is_deposit != 1) {
                console.log('1')
                // 注释掉
                // if (that.data.spot_id == 0) {
                    that.setData({
                        nonum: no,
                        spot_id: spout_device_id,
                        device_no: device_id,
                        external_cabinet:external_cabinet
                    });
                // }
                that.opentask1(no, type, that.data.device_no, that.data.spot_id)
            } else {
                console.log('2')
                that.opentask(no, type,metoh);
            }
        }else{
            wx.showToast({
                title: that.data.countDownNum+'秒后可重新开门',
                icon: 'none',
                duration: 1000
              })
        }

    },
    countDown: function () {
        let that = this;
        that.setData({countDownNum: 15})
        timer = setInterval(function () {
            that.setData({
                countDownNum: that.data.countDownNum - 1
            })
            if (that.data.countDownNum <= 0) {
                clearInterval(timer);
            }
        }, 1000)
    },



    takeClothes() {
        wx.nextTick(() => {
            this.setData({
                result: !this.data.result
            })
        })
    },
    storeClothes() {
        wx.nextTick(() => {
            this.setData({
                result2: !this.data.result2
            })
        })
    },

    getorderList(order_type, network_id, device_id = "") {
        var that = this;
        // var nid = network_id
        var data = {
            network_id: that.data.netwrok_id,
            order_type: that.data.order_type,
            order_num: this.data.order_num,
            logistics_status: that.data.is_deposit == 1 ? 1 : 6,
            is_deposit: that.data.is_deposit
        }
        console.log(data);
        // api/order/index
        // api/UserTakeList
        app.api.useApi(app.globalData.baseAppUrl + "api/order/index", data, "get").then(function (res) {
            if (res.code != 200) {
                console.log(res)
                wx.showLoading({
                    title: '订单加载失败',
                })
            } else {
                that.setData({
                    orderList: res.message.data.data,
                    network: res.message.network,
                    is_something: res.message.is_something
                })
            }

            wx.hideLoading();

        }).catch(function (err) {
            console.log(err);
        })
    },
    opentask(no = "", type = "",metoh="") {
        var that = this;
        console.log(type)
        if(no){
            this.setData({
                order_no: no,
                order_num: that.data.order_num
            })
        }else{
            no = this.data.order_no;
        }
        if(type){
            this.setData({
                type: type,
            })
        }else{
            type = this.data.order_type;
        }
        console.log(metoh);
        app.api.useApi(app.globalData.baseAppUrl + "api/"+metoh, {
            // status: status,
            order_no: no,
            order_num: that.data.device_id,
            spot_id:that.data.spot_id,
            external_cabinet:that.data.external_cabinet
        }, "post").then(function (res) {
            console.log(res);
            if (res.code != 200) {
                that.countDown();
                that.setData({
                    device_no: res.message.data.device_no,
                    spot_id:res.message.data.spot_id,
                })
                wx.showToast({
                    title: res.message.msg,
                })
                console.log(res.message.msg)
                // wx.setTimeout(() => {
                //     wx.hideLoading();
                // }, 1000);
                setTimeout(function () {
                     wx.hideLoading();
                    that.setData({
                        show: !that.data.show
                    })
                }, 1000)

            } else {
                console.log(res);
                that.countDown();
                that.setData({
                    show: false,
                    spot_id: res.message.data.spot_id,
                    device_no: res.message.data.device_id,
                    external_cabinet:res.message.data.external_cabinet
                })
                if (type == 1) {
                    console.log('1')
                    that.storeClothes()
                } else {
                    console.log('2')
                    that.takeClothes()
                }
                that.goto()
            }
             wx.hideLoading();
        }).catch(function (err) {
            console.log(err.message)
        })
    },
    goto() {
        var order_no = this.data.order_no;
        let that = this
        that.setData({
            result: false,
            result2: false,
            show: false
        })
        if (that.data.is_deposit != 1) {
            app.api.useApi(app.globalData.baseAppUrl + "api/UserConfirm", {
                // status: status,
                device_id: that.data.device_no,
                spout_device_id: that.data.spot_id,
            }, "post").then(function (res) {
                if (res.code != 200) {
                    wx.showLoading({
                        title: res.message.msg,
                    })
                    wx.setTimeout(() => {
                        wx.hideLoading();
                    }, 1000);

                } else {
                    let p = getCurrentPages().pop().options
                    console.log(p);
                    that.getorderList(p.order_type, p.network_id);
                    console.log(res);
                    if (is_deposit == 1) {
                        that.storeClothes()
                    } else {
                        that.takeClothes()
                    }
                    that.setData({
                        show: !that.data.show
                    })
                    that.getorderList()
                }
                wx.hideLoading();

            }).catch(function (err) {
                console.log(222);
            })
        } else {
            app.api.useApi(app.globalData.baseAppUrl + "api/saved", {
                // status: status,
                order_no: order_no,
                spot_id: that.data.spot_id,
            }, "post").then(function (res) {
                if (res.code != 200) {
                    wx.showLoading({
                        title: res.message.msg,
                    })
                    wx.setTimeout(() => {
                        wx.hideLoading();
                    }, 1000);

                } else {
                    wx.showToast({
                        title: res.message.msg,
                        icon: 'none',
                        duration: 1000
                      })
                      console.log('-------------j'+that.data.order_type, that.data.netwrok_id)
                    that.getorderList(that.data.order_type, that.data.netwrok_id);
                    console.log(res);
                    if (type == 1) {
                        that.storeClothes()
                    } else {
                        that.takeClothes()
                    }
                    that.setData({
                        show: !that.data.show
                    })
                    that.getorderList()
                }
                wx.hideLoading();
            }).catch(function (err) {
                console.log(222);
                that.getorderList()
            })
        }



    },


    /**
     * @name 开柜取出
     * @param {*} no 
     * @param {*} type 
     */
    opentask1(no = "", type = "", device_id, spout_device_id) {
        var that = this;
        if (no && type) {
            this.setData({
                order_no: no,
                type: type,

            })
        } else {
            no = this.data.order_no;
            type = this.data.order_type;
        }

        app.api.useApi(app.globalData.baseAppUrl + "api/UserOpenDevices", {
            // status: status,
            device_id: that.data.device_no,
            spout_device_id: spout_device_id
        }, "post").then(function (res) {
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.msg,
                })
                that.countDown();
                that.setData({
                    device_no: res.message.data.device_no,
                })
                // wx.setTimeout(() => {
                //     wx.hideLoading();
                // }, 1000);


                setTimeout(function () {
                    wx.hideLoading();
                    // that.setData({
                    //     show: !that.data.show
                    // })
                }, 3000)

            } else {
                wx.showToast({
                    title: res.message.msg,
                })
                that.countDown();
                console.log(res);
                if (that.is_deposit == 1) {
                    that.storeClothes()
                } else {
                    that.takeClothes()
                }

                that.goto()
            }

            wx.hideLoading();

        }).catch(function (err) {
            console.log(err.message)
        })
    },
    nogoto(){
        let that = this
        app.api.useApi(app.globalData.baseAppUrl + "api/not_saved", {
            // status: status,
            order_no: that.data.order_no,
            spot_id: that.data.spot_id,
        }, "post").then(function (res) {
            if (res.code != 200) {
                wx.showToast({
                    title: res.message.msg,
                    icon: 'none',
                    duration: 1000
                })
                wx.setTimeout(() => {
                    wx.hideLoading();
                }, 1000);

            } else {
                wx.showToast({
                    title: res.message.msg,
                    icon: 'none',
                    duration: 1000
                })
                console.log(res);
                setTimeout(() => {
                    that.getorderList()
                    that.storeClothes()
                }, 500);
            }
        }).catch(function (err) {
            console.log(222);
        })
    },
    nofun(){

    },
    onPullDownRefresh: function () {
        let p = getCurrentPages().pop().options
        console.log(p);
        this.getorderList(p.order_type, p.network_id);
    }
})