let util = require('../../../../utils/util')
let apiArr = require('../../../../api/partner')
// packages/partner/pages/chooseDevice/chooseDevice.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        deviceCode: "",
        deviceInfo: "",
        deviceList: [],
        flag: false,
        page_num: 1,
        page_size: 10,
    },
    scanQrcode() {
        let that = this
        wx.scanCode({
            success: (res) => {
                // 处理扫码结果
                this.setData({
                    deviceCode: res.result
                });
                util.postUrl4(apiArr.getDeviceByCode, {
                    device_code: res.result,
                    install_id: that.data.id
                }, res => {
                    that.setData({
                        deviceInfo: res
                    })
                })

            },
            fail: (err) => {
                wx.showToast({
                    title: '扫码失败',
                    icon: 'none'
                });
            }
        });
    },
    getDeviceList() {
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        let that = this
        let admin = wx.getStorageSync('admin')
        let param = {
            page_size: that.data.page_size,
            page_num: that.data.page_num,
            search: that.data.searchText,
            dealer_id: admin.shop_id,
        };


        util.postUrl4(apiArr.deviceList, param, res => {
            wx.hideLoading()
            let flag = false
            if (res.list && res.list.length == that.data.page_size) {
                console.log(123);
                flag = true
            } else {
                console.log(456);
                flag = false
            }
            that.setData({
                deviceList: that.data.deviceList.concat(res.list || []),
                total: res.total,
                flag,
                page_num: that.data.page_num + 1
            })
            console.log(that.data.flag);
        })
    },
    deviceDesc(e) {
        console.log(e);
        const pages = getCurrentPages();
        const prevPage = pages[pages.length - 2]; // 获取上一页实例
        // 传值给上一页
        prevPage.setData({
            deviceInfo: e.currentTarget.dataset.item // 可以是对象、数组等
        });
        // 调用上一页的方法
        // prevPage.yourRefreshMethod(); // 你定义的刷新方法
        // 返回上一页
        wx.navigateBack();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getDeviceList()
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log(123);
        let that = this
        if(that.data.flag){
            that.getDeviceList()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})