

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

    /**
     * 页面的初始数据
     */
    data: {
        orderList: [
        ],
        logisticsStatus: "",
        show: false,
        result: false,
        result2: false,
        countDownNum: 15,
        spot_id: 0,
        device_no: "",
        order_num: "",
        is_deposit: 1,
        netwrok_id: 0,
        order_type: 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: '等待取出',
            })
        }
    },
    onClose(e) {
        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
        wx.nextTick(() => {
            this.setData({
                show: !this.data.show
            })
        })
        if (this.data.is_deposit != 1) {
            if (this.data.spot_id == 0) {
                this.setData({
                    spot_id: spout_device_id,
                    device_no: device_id
                });
            }
            this.opentask1(no, type, this.data.device_no, this.data.spot_id)
        } else {
            this.opentask(no, type);
        }

    },
    countDown: function () {
        let that = this;
        let countDownNum = that.data.countDownNum;//获取倒计时初始值
        //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
        that.setData({
            timer: setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
                //每隔一秒countDownNum就减一，实现同步
                countDownNum--;
                //然后把countDownNum存进data，好让用户知道时间在倒计着
                that.setData({
                    countDownNum: countDownNum
                })
                //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
                if (countDownNum == 0) {
                    //这里特别要注意，计时器是始终一直在走的，如果你的时间为0，那么就要关掉定时器！不然相当耗性能
                    //因为timer是存在data里面的，所以在关掉时，也要在data里取出后再关闭
                    clearInterval(that.data.timer);
                    //关闭定时器之后，可作其他处理codes go here
                }
            }, 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: nid,
            order_type: 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);

        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 = "") {
        var that = this;
        if (no && type) {
            this.setData({
                order_no: no,
                type: type,
                order_num: that.data.order_num

            })
        } else {
            no = this.data.order_no;
            type = this.data.order_type;
        }
        app.api.useApi(app.globalData.baseAppUrl + "api/save", {
            // status: status,
            order_no: no,
            order_num: that.data.device_id,
        }, "post").then(function (res) {
            console.log(res);
            if (res.code != 200) {

                that.countDown();
                that.setData({
                    device_no: res.message.data.device_no,
                })
                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.setData({
                    show: false,
                    spot_id: res.message.data.spot_id,
                    device_no: res.message.data.device_id
                })
                
                if (type == 1) {
                    that.storeClothes()
                } else {
                    that.takeClothes()
                }

            }

            // wx.hideLoading();

        }).catch(function (err) {
            console.log(err.message)
        })
    },
    goto() {
        var order_no = this.data.order_no;

        this.setData({
            result: false,
            result2: false,
            show: false
        })

        if (this.data.is_deposit != 1) {
            app.api.useApi(app.globalData.baseAppUrl + "api/UserConfirm", {
                // status: status,
                device_id: this.data.device_no,
                spout_device_id: this.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: !this.data.show
                    })
                }

                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: this.data.spot_id,
            }, "post").then(function (res) {
                if (res.code != 200) {
                    wx.showLoading({
                        title: res.message.msg,
                    })
                    wx.setTimeout(() => {
                        wx.hideLoading();
                    }, 1000);

                } else {
                    that.getorderList(that.data.order_type, that.data.netwrok_id);
                    console.log(res);
                    if (type == 1) {
                        that.storeClothes()
                    } else {

                        that.takeClothes()
                    }

                    that.setData({
                        show: !this.data.show
                    })
                }

                wx.hideLoading();

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



    },


    /**
     * @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: device_id,
            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 {
                console.log(res);
                if (that.is_deposit == 1) {
                    that.storeClothes()
                } else {
                    that.takeClothes()
                }

            }

            wx.hideLoading();

        }).catch(function (err) {
            console.log(err.message)
        })
    },

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