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

    /**
     * 页面的初始数据
     */
    data: {
        orders:"",
        network_id:0,
        orde_num:0,
        iswhite: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options.q);
        var network_id=options.network_id;
        if(options.q){
            var url=decodeURIComponent(options.q)
            var order_num=app.getQueryVariable(url,"orde_num");
            var network_id=wx.getStorageSync('nid');
        }
        this.setData({
            network_id:network_id,
            orde_num:order_num?order_num:0
        })
      
        
    },
    scan(){
    wx.scanCode({
        onlyFromCamera: true,
        success: (result) => {
            var url=result.result;
            var network_id=this.data.network_id;
            var order_num=app.getQueryVariable(url,"orde_num");
            wx.navigateTo({
              url: '/pages/order/deposit/deposit?network_id='+network_id+"&device_id="+order_num+"&order_num="+order_num+"&is_deposit=1"
            })
            console.log(result)
        },
        fail: (res) => {},
        complete: (res) => {},
    })
    },


    /**
     * 取件
     */
    receive(){
        wx.scanCode({
            onlyFromCamera: true,
            success: (result) => {
                var url=result.result;
                var network_id=this.data.network_id?this.data.network_id:app.getQueryVariable(url,"network_id");
                var order_num=app.getQueryVariable(url,"orde_num");
                wx.navigateTo({
                  url: '/pages/order/deposit/deposit?network_id='+network_id+"&order_num="+order_num+"&is_deposit=0",
                })
                console.log(result)
            },
            fail: (res) => {},
            complete: (res) => {},
        })
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that=this;
        var token =wx.getStorageSync('token');
        that.setData({iswhite: true})
        if(!token){
            app.login().then(res => {
                console.log(res)
                that.setData({
                    token: res.message.token,
                })
                that.getDeviceinfo(that.data.orde_num);
            })
        
        }else{
            that.getDeviceinfo(that.data.orde_num);
        }
  

        
    },

    /**
     * @name 获取当前网点存取单
     * @param int network_id 网点id
     */
    getDeviceinfo:function(order_num){
        var that=this;
        wx.showLoading({
          title: '加载中...',
        })
        app.api.useApi(app.globalData.baseAppUrl+"api/order/getDeviceinfo",{
            order_num:order_num,
            network_id:this.data.network_id
        }).then((res)=>{
            if(res.code!=200){
                setTimeout(() => {
                    that.getDeviceinfo(order_num)
                }, 1000);
                // wx.showToast({
                //     title: res.message.msg,
                //     icon:"none",
                //     success:function(){
                //        setTimeout(() => {
                //            wx.hideLoading({
                //              success: (res) => {},
                //            })
                //        }, 2000);
                //     }
                //   })
            }else{
                wx.hideLoading()
                var orders=res.message.data;
                that.setData({
                    orders:orders
                })
                
                if(orders.send_count<1 && orders.receive_count <1 && orders.network.id>0){
                    wx.navigateTo({
                      url: "/pages/order/placeOrder/placeOrder?network_id="+orders.network.id
                    })
                }else{
                    that.setData({iswhite: false})
                }
            }
        }).catch((err)=>{
           console.log(err);
        })
    }

})