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

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        deviceList: [],
        deal: {},
        deviceInfo:{
            device_code:""
        }
    },
    choseJXS() {
        let that = this
        that.setData({
            show: true
        })
    },
    //扫码
    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 => {
                    let deviceList = that.data.deviceList
                    deviceList.push(res)
                    that.setData({
                        deviceList
                    })
                })

            },
            fail: (err) => {
                wx.showToast({
                    title: '扫码失败',
                    icon: 'none'
                });
            }
        });
    },

    iptDeviceCode(e){
        let that = this
        console.log(e);
        that.setData({
            "deviceInfo.device_code":e.detail.value
        })
        
    },

    search(){
        let that = this
        console.log(that.data.deviceInfo);
        util.postUrl4(apiArr.getDeviceByCode, {
            device_code: that.data.deviceInfo.device_code,
            install_id: that.data.id
        }, res => {
            let deviceList = that.data.deviceList
            deviceList.push(res)
            that.setData({
                deviceList
            })
        })
    },

    //经销商列表
    getDealerList() {
        let that = this
        util.postUrl4(apiArr.getPartnerList, {
            page_size: 50,
            page_num: 1
        }, res => {
            console.log(res);
            that.setData({
                columns: res.rows
            })
        })
    },

    deleteItem(e){
        console.log(e);
        let that = this
        let deviceList = that.data.deviceList
        deviceList.splice(e.currentTarget.dataset.idx,1)
        that.setData({
            deviceList
        })
    },

    onClose() {
        let that = this
        that.setData({
            show: false
        })
    },
    onConfirm(e) {
        console.log(e);
        let that = this
        that.setData({
            deal: e.detail.value,
            show: false
        })
    },

    allocation() {
        let that = this
        let device_id = []

        if (!that.data.deal.deal_id) {
            return wx.showToast({
                title: '请选择经销商',
                icon: "none"
            })
        }
        if (that.data.deviceList.length == 0) {
            return wx.showToast({
                title: '请添加设备',
                icon: "none"
            })
        }
        that.data.deviceList.forEach(item => {
            device_id.push(item.device_id)
        })
        util.postUrl4(apiArr.allocation, {
            dealer_id: that.data.deal.deal_id,
            device_id
        }, res => {
            wx.showToast({
                title: '分配成功！',
                icon: "none"
            })

            that.setData({
                deal:{},
                deviceList:[]
            })

        })
    },
    cancels(){
        let that= this
        that.setData({
            deviceList:[],
            deal:{}
        })
    },
    deleteItem(e) {
        console.log(e);
        let that = this
        let deviceList = that.data.deviceList
        deviceList.splice(e.currentTarget.dataset.idx, 1)
        that.setData({
            deviceList
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getDealerList()
    },

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

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

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

    }
})