<template>
    <!-- pages/deviceCommand/deviceCommand.wxml -->
    <view class="container">
        <view class="item-container">
            <button v-if="type != 'YWWL'" class="cmd-item flex-row flex-between" @tap="bindUptimeDialogOpen">
                <view class="flex-row">
                    <image class="item-icon" src="/static/resource/images/shangbaojiange.png"></image>
                    <view class="item-text">上报间隔</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>
            <button class="cmd-item flex-row flex-between" @tap="bindSendCommand" data-cmd="SAFEON">
                <view class="flex-row">
                    <image class="item-icon" src="/static/resource/images/bufang.png"></image>
                    <view class="item-text">布防</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>
            <button class="cmd-item flex-row flex-between" @tap="bindSendCommand" data-cmd="SAFEOFF">
                <view class="flex-row">
                    <image class="item-icon" src="/static/resource/images/chefang.png"></image>
                    <view class="item-text">撤防</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>
            <button class="cmd-item flex-row flex-between" @tap="bindSendCommand" data-cmd="LOCATION">
                <view class="flex-row">
                    <image class="item-icon" src="/static/resource/images/lijidingwei.png"></image>
                    <view class="item-text">立即定位</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>

            <!-- <button wx:if="{{ type=='YWJT' }}" class="cmd-item flex-row flex-between" bindtap="bindPrioritySendCommand" data-cmd="SETLOWVOLTAGE">
      <view class="flex-row">
        <image class="item-icon" src="/resource/images/lijidingwei.png"></image>
        <view class="item-text">低电告警设置</view>
      </view>
      <image class="item-icon" src="/resource/images/youjiantou.png"></image>
    </button> -->

            <button v-if="type == 'YWWL'" class="cmd-item flex-row flex-between" @tap="bindPrioritySendCommand" data-cmd="LOCATIONPRIORITY">
                <view class="flex-row">
                    <image class="item-icon" src="/static/resource/images/lijidingwei.png"></image>
                    <view class="item-text">定位优先级设置</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>

            <button v-if="type == 'YWWL'" class="cmd-item flex-row flex-between" @tap="bindLocationModelSendCommand" data-cmd="LOCATIONMODEL">
                <view class="flex-row">
                    <image class="item-icon" src="/static/resource/images/lijidingwei.png"></image>
                    <view class="item-text">定位模式</view>
                </view>
                <view class="flex-row">
                    <view v-if="gpsData.yw_work_mode != null">
                        <view v-if="index == 0" class="item-text" style="margin-right: 20rpx">{{ columns[index] + ' ' + intelligentList[timeIndex].lable }}</view>
                        <view v-if="index == 1" class="item-text" style="margin-right: 20rpx">{{ columns[index] + ' ' + positionList[timeIndex].lable }}</view>
                        <view v-if="index == 2" class="item-text" style="margin-right: 20rpx">{{ columns[index] + ' ' + powersaveList[timeIndex].lable }}</view>
                    </view>
                    <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
                </view>
            </button>
        </view>
        <view class="item-container">
            <!--button class="cmd-item flex-row flex-between">
      <view class="flex-row">
        <image class="item-icon" src="/resource/images/weilan.png"></image>
        <view class="item-text">围栏</view>
      </view>
      <image class="item-icon" src="/resource/images/youjiantou.png"></image>
    </button-->
            <button v-if="type != 'YWWL'" class="cmd-item flex-row flex-between" @tap="bindSendCommand" data-cmd="CLOSERELAY">
                <view class="flex-row">
                    <image class="item-icon" src="/static/resource/images/duandian.png"></image>
                    <view class="item-text">控制断电</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>
            <button v-if="type != 'YWWL'" class="cmd-item flex-row flex-between" @tap="bindSendCommand" data-cmd="OPENRELAY">
                <view class="flex-row">
                    <image class="item-icon" src="/static/resource/images/tongdian.png"></image>
                    <view class="item-text">恢复通电</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>

            <button class="cmd-item flex-row flex-between" @tap="bindSendCommand" data-cmd="RESTART">
                <view class="flex-row">
                    <image class="item-icon" src="/static/resource/images/chongqi.png"></image>
                    <view class="item-text">设备重启</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>
            <button v-if="type != 'YWWL'" class="cmd-item flex-row flex-between" @tap="bindSendCommand" data-cmd="RESET">
                <view class="flex-row">
                    <image class="item-icon" src="/static/resource/images/huifuchuchang.png"></image>
                    <view class="item-text">恢复出厂设置</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>

            <!-- <button wx:if="{{ type !='YWWL' }}" class="cmd-item flex-row flex-between" bindtap="bindSendCommand" data-cmd="RESET">
      <view class="flex-row">
        <image class="item-icon" src="http://qiniu.bms16.com/yijian_report.png"></image>
        <view class="item-text">一键报失</view>
      </view>
      <image class="item-icon" src="/resource/images/youjiantou.png"></image>
    </button> -->

            <button v-if="type != 'YWJT' && type != 'YWWL'" class="cmd-item flex-row flex-between" @tap="bindSendCommand" data-cmd="OPENBUZZER">
                <view class="flex-row">
                    <image class="item-icon" src="/static/resource/images/fmqk.png"></image>
                    <view class="item-text">打开蜂鸣器</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>

            <button v-if="type != 'YWJT' && type != 'YWWL'" class="cmd-item flex-row flex-between" @tap="bindSendCommand" data-cmd="CLOSEBUZZER">
                <view class="flex-row">
                    <image class="item-icon" src="/static/resource/images/fmqg.png"></image>
                    <view class="item-text">关闭蜂鸣器</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>

            <button v-if="bt_type == 'AD3BTS'" class="cmd-item flex-row flex-between" @tap="bindSendCommand" data-cmd="BPS">
                <view class="flex-row">
                    <image class="item-icon" src="/static/resource/images/fmqk.png"></image>
                    <view class="item-text">设置波特率</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>
        </view>
        <view class="item-container">
            <button class="cmd-item flex-row flex-between" @tap="bindCommandList">
                <view class="flex-row">
                    <image class="item-icon" src="/static/resource/images/shijian1.png"></image>
                    <view class="item-text">指令记录</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>
        </view>
        <view class="item-container">
            <button class="cmd-item flex-row flex-between" @tap="bindUnbind">
                <view class="flex-row">
                    <view class="item-text">解绑设备</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>
        </view>
        <van-dialog
            use-slot
            title="上报间隔"
            :show="uptimeDialogShow"
            show-cancel-button
            confirm-button-text="确定"
            confirm-button-color="#06C689"
            @confirm="bindUptimeDialogConfirm"
        >
            <van-cell-group style="padding: 40rpx 32rpx">
                <van-field :value="uptime" left-icon="clock-o" @change="onUptimeChange" />
            </van-cell-group>
        </van-dialog>
        <van-dialog
            use-slot
            title="类型"
            :show="locationModelDialogShow"
            show-cancel-button
            confirm-button-text="确定"
            confirm-button-color="#06C689"
            @confirm="bindLocatonModelDialogConfirm"
        >
            <!-- <van-cell-group style="padding: 40rpx 32rpx;"> -->
            <!-- <van-picker value="{{ uptime }}" left-icon="clock-o" bind:change="onUptimeChange"/> -->
            <!-- <van-picker title="定位模式" columns="{{columns}}" bind:confirm="onConfirm" bind:cancel="onCancel" bind:change="onChange" /> -->
            <!-- </van-cell-group> -->
            <button class="cmd-item flex-row flex-between" @tap="clickSelectModelType">
                <view class="flex-row">
                    <view class="item-text">{{ columns[index] }}</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>

            <button class="cmd-item flex-row flex-between" @tap="clickSelectTimeModelType">
                <view v-if="index == 0" class="flex-row">
                    <view class="item-text">{{ intelligentList[timeIndex].lable }}</view>
                </view>
                <view v-if="index == 1" class="flex-row">
                    <view class="item-text">{{ positionList[timeIndex].lable }}</view>
                </view>
                <view v-if="index == 2" class="flex-row">
                    <view class="item-text">{{ powersaveList[timeIndex].lable }}</view>
                </view>
                <image class="item-icon" src="/static/resource/images/youjiantou.png"></image>
            </button>
            <view class="tag" v-if="index == 0">根据G-Sensor的判断，设备检测到震动时，按设置的时间间隔定时上报位置；静止超过三分钟，自动进入休眠仅维持心跳，显示在线</view>
            <view class="tag" v-if="index == 1">设备仅按设置的时间间隔定时上报位置，每次上报位置之后自动进入休眠仅维持心跳，显示在线</view>
            <view class="tag" v-if="index == 2">设备仅按设置的时间间隔定时上报位置，每次上报位置之后自动关闭设备网络进入深度休眠省电模式，显示离线，无法远程唤醒</view>

            <!-- <view wx:if="{{index == 0}}" class="flex-row">
        <view class="item-text">{{intelligentList[timeIndex].lable}}</view>
      </view>
      <view wx:if="{{index == 1}}" class="flex-row">
        <view class="item-text">{{positionList[timeIndex].lable}}</view>
      </view>
      <view wx:if="{{index == 2}}" class="flex-row">
        <view class="item-text">{{powersaveList[timeIndex].lable}}</view>
      </view> -->
        </van-dialog>
    </view>
</template>

<script>
// pages/deviceCommand/deviceCommand.js
var config = require('../../common/config.js');
var http = require('../../common/http.js');
var common = require('../../common/common.js');
var storage = require('../../common/storage.js');
var bluetooth = require('../../common/bluetooth.js');
export default {
    data() {
        return {
            macid: '',
            type: '',
            uptimeDialogShow: false,
            locationModelDialogShow: false,
            uptime: '',
            loading: false,
            param: [],
            cmdLabel: '',
            timeLabel: '',
            gpsData: null,
            columns: ['智能模式', '定位模式', '省电模式'],

            intelligentList: [
                {
                    lable: '30秒',
                    value: 30
                },
                {
                    lable: '60秒',
                    value: 60
                },
                {
                    lable: '180秒',
                    value: 180
                }
            ],

            positionList: [
                {
                    lable: '5分钟',
                    value: 300
                },
                {
                    lable: '10分钟',
                    value: 600
                },
                {
                    lable: '1小时',
                    value: 3600
                }
            ],

            powersaveList: [
                {
                    lable: '4小时',
                    value: 240
                },
                {
                    lable: '12小时',
                    value: 720
                },
                {
                    lable: '24小时',
                    value: 1440
                }
            ],

            index: 0,
            timeIndex: 0,
            bt_type: '',
            bt_status: 0,
            lable: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        if (!options.macid) {
            uni.navigateBack({
                delta: 1
            });
        }
        console.log(options);
        const battery_sn = options.macid;
        // if (bluetooth.isConnected(battery_sn)) {
        //   bluetooth.onCharacteristicStateChange(battery_sn, 'deviceCommand', (data) => {
        //     console.log("蓝牙读取的bms数据：")
        //     console.log(data)
        //     var btBmsInfo = bluetooth.bmsInfo(battery_sn)
        //     if (btBmsInfo) {
        //       btBmsInfo.time = parseInt(new Date().getTime() / 1000)
        //     }
        //     if (btBmsInfo.btsets && this.data.btBmsInfo && this.data.btBmsInfo.btsets) {
        //       btBmsInfo.btsets.forEach((item, index) => {
        //         item.vars.forEach((v, i) => {
        //           this.data.btBmsInfo.btsets.forEach(p => {
        //             var n = p.vars.find(u => u.name == v.name && u.set)
        //             if (n != undefined) {
        //               btBmsInfo.btsets[index].vars[i].set = n.set
        //             }
        //           })
        //         })
        //       })
        //     }
        //     console.log(btBmsInfo)
        //     this.setData({
        //       btBmsInfo: btBmsInfo
        //     })
        //   })
        //   bluetooth.stateUpdate(battery_sn)
        // }

        const device = storage.getSelectedDeviceInfo();
        console.log(device);
        this.setData({
            macid: options.macid,
            type: options.type ? options.type : '',
            bt_type: options.bt_type ? options.bt_type : ''
        });
        this.loadGPSInfo();
        console.log('type' + this.type);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        clickSelectTimeModelType() {
            var list = [];
            if (this.index == 0) {
                list = this.intelligentList;
            }
            if (this.index == 1) {
                list = this.positionList;
            }
            if (this.index == 2) {
                list = this.powersaveList;
            }
            var strList = [];
            for (var i = 0; list.length > i; i++) {
                strList.push(list[i].lable);
            }
            const me = this;
            uni.showActionSheet({
                itemList: strList,
                success(res) {
                    me.setData({
                        timeIndex: res.tapIndex
                    });
                }
            });
        },

        clickSelectModelType() {
            const me = this;
            uni.showActionSheet({
                itemList: ['智能模式', '定位模式', '省电模式'],
                success(res) {
                    me.setData({
                        index: res.tapIndex,
                        timeIndex: 0
                    });
                }
            });
        },

        loadGPSInfo() {
            const me = this;
            http.postApi(
                config.API_GPS_INFO,
                {
                    macid: this.macid
                },
                (resp) => {
                    if (resp.data.code === 200) {
                        var data = resp.data.data.info;
                        this.setData({
                            gpsData: data
                        });
                        if (data.yw_location_pri != null) {
                        }
                        if (data.yw_work_mode != null) {
                            var intervalTime = 0;
                            if (data.yw_work_mode.mode - 0 == 3) {
                                intervalTime = (data.yw_work_mode.inteval - 0) / 60;
                            } else {
                                intervalTime = data.yw_work_mode.inteval - 0;
                            }
                            if (data.yw_work_mode.mode - 0 == 2) {
                                this.setData({
                                    index: 0
                                });
                                for (var i = 0; this.intelligentList.length > i; i++) {
                                    if (intervalTime == this.intelligentList[i].value) {
                                        this.setData({
                                            timeIndex: i
                                        });
                                    }
                                }
                            }
                            if (data.yw_work_mode.mode - 0 == 1) {
                                this.setData({
                                    index: 1
                                });
                                for (var i = 0; this.positionList.length > i; i++) {
                                    if (intervalTime == this.positionList[i].value) {
                                        this.setData({
                                            timeIndex: i
                                        });
                                    }
                                }
                            }
                            if (data.yw_work_mode.mode - 0 == 3) {
                                this.setData({
                                    index: 2
                                });
                                for (var i = 0; this.powersaveList.length > i; i++) {
                                    if (intervalTime == this.powersaveList[i].value) {
                                        this.setData({
                                            timeIndex: i
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            );
        },

        bluetoothConnect: function (time = null) {
            if (this.loading && time == null) {
                return;
            }
            const device = storage.getSelectedDeviceInfo();
            if (device && bluetooth.acceptDevice(device)) {
                this.setData({
                    loading: true
                });
                if (time == null) {
                    time = new Date().getTime();
                }
                bluetooth.openBluetoothAdapter(
                    (res) => {
                        console.log(res);
                        bluetooth.connectDevice(
                            device,
                            () => {
                                common.simpleToast('蓝牙连接成功');
                                this.setData({
                                    bt_status: 1
                                });
                                bluetooth.onCharacteristicStateChange(device.mac_id, 'home', (data) => {
                                    if (bluetooth.bluetoothDeviceConfig && data != null) {
                                        common.reportBms(device.mac_id, data, function (success) {});
                                    }
                                });
                                bluetooth.onConnectionStateChange(device.mac_id, 'home', (res) => {
                                    if (!res.connected) {
                                        this.setData({
                                            bt_status: 2
                                        });
                                    } else {
                                        this.setData({
                                            bt_status: 1
                                        });
                                    }
                                });
                                setTimeout(() => {
                                    this.setData({
                                        loading: false
                                    });
                                }, 800 - ((new Date().getTime() - time) % 800));
                            },
                            (res) => {
                                console.log(res);
                                uni.showModal({
                                    content: '连接设备失败，请靠近设备重试',
                                    confirmText: '重试',
                                    success: (res) => {
                                        if (res.confirm) {
                                            this.bluetoothConnect(time);
                                        } else {
                                            setTimeout(() => {
                                                this.setData({
                                                    loading: false
                                                });
                                            }, 800 - ((new Date().getTime() - time) % 800));
                                        }
                                    },
                                    fail: (res) => {
                                        console.log(res);
                                        setTimeout(() => {
                                            this.setData({
                                                loading: false
                                            });
                                        }, 800 - ((new Date().getTime() - time) % 800));
                                    }
                                });
                            }
                        );
                    },
                    (res) => {
                        console.log(res);
                        uni.showModal({
                            content: '需使用手机蓝牙功能，请打开蓝牙',
                            confirmText: '重试',
                            success: (res) => {
                                if (res.confirm) {
                                    this.bluetoothConnect(time);
                                } else {
                                    setTimeout(() => {
                                        this.setData({
                                            loading: false
                                        });
                                    }, 800 - ((new Date().getTime() - time) % 800));
                                }
                            },
                            fail: (res) => {
                                console.log(res);
                                setTimeout(() => {
                                    this.setData({
                                        loading: false
                                    });
                                }, 800 - ((new Date().getTime() - time) % 800));
                            }
                        });
                    }
                );
            }
        },

        sendBtCommand: function (cmd) {
            const that = this;
            const device = storage.getSelectedDeviceInfo();
            if (bluetooth.isConnected(this.macid)) {
                if (cmd == 'CLOSERELAY') {
                    bluetooth.turnOff(
                        that.macid,
                        function () {
                            common.simpleToast('发送成功');
                            if (bluetooth.isUniversalBluetoothPlugin(device)) {
                                setTimeout(function () {
                                    bluetooth.stateUpdate(
                                        that.macid,
                                        (res) => {},
                                        (res) => {}
                                    );
                                }, 2000);
                            }
                        },
                        function () {
                            common.simpleToast('发送失败');
                        }
                    );
                } else if (cmd == 'OPENRELAY') {
                    bluetooth.turnOn(
                        that.macid,
                        function () {
                            if (bluetooth.isUniversalBluetoothPlugin(device)) {
                                setTimeout(function () {
                                    bluetooth.stateUpdate(
                                        that.macid,
                                        (res) => {},
                                        (res) => {}
                                    );
                                }, 2000);
                            }
                            common.simpleToast('发送成功');
                        },
                        function () {
                            common.simpleToast('发送失败');
                        }
                    );
                } else if (cmd == 'OPENBUZZER') {
                    bluetooth.turnOnBuzzer(
                        that.macid,
                        function () {
                            common.simpleToast('发送成功');
                        },
                        function () {
                            common.simpleToast('发送失败');
                        }
                    );
                } else if (cmd == 'CLOSEBUZZER') {
                    bluetooth.turnOffBuzzer(
                        that.macid,
                        function () {
                            common.simpleToast('发送成功');
                            if (bluetooth.isUniversalBluetoothPlugin(device)) {
                                setTimeout(function () {
                                    bluetooth.stateUpdate(
                                        that.macid,
                                        (res) => {},
                                        (res) => {}
                                    );
                                }, 2000);
                            }
                        },
                        function () {
                            common.simpleToast('发送失败');
                        }
                    );
                } else if (cmd == 'BPS') {
                    uni.showActionSheet({
                        itemList: ['0: 4800', '1: 9600', '2: 14400', '3: 38400', '4: 56000', '5: 57600', '6: 115200'],
                        success(res) {
                            bluetooth.setBaud(
                                that.macid,
                                res.tapIndex,
                                function () {
                                    common.simpleToast('发送成功');
                                },
                                function () {
                                    common.simpleToast('发送失败');
                                }
                            );
                        }
                    });
                }
            } else {
                this.bluetoothConnect();
            }
        },

        bindPrioritySendCommand: function (e) {
            const cmd = e.currentTarget.dataset.cmd;
            uni.showActionSheet({
                itemList: ['GLW', 'LGW'],
                success: (res) => {
                    var index = res.tapIndex + 1;
                    this.setData({
                        param: [index]
                    });
                    this.sendCommand(cmd);
                }
            });
        },

        bindLocationModelSendCommand() {
            this.setData({
                locationModelDialogShow: true
            });
        },

        bindLocatonModelDialogConfirm() {
            common.loading();

            // if(this.data.columns[this.data.index] == 0){

            // }
            var params1 = '';
            var params2 = '';
            if (this.index == 0) {
                params1 = 2;
                params2 = this.intelligentList[this.timeIndex].value;
            }
            if (this.index == 1) {
                params1 = 1;
                params2 = this.positionList[this.timeIndex].value;
            }
            if (this.index == 2) {
                params1 = 3;
                params2 = this.powersaveList[this.timeIndex].value;
            }
            console.log([params1, params2]);
            http.postApi(
                config.API_BATTERY_SEND_COMMAND,
                {
                    macid: this.macid,
                    cmd: 'LOCATIONMODEL',
                    param: [params1, params2]
                },
                function (resp) {
                    if (resp.data.code === 200) {
                        uni.hideLoading();
                        common.simpleToast('操作成功');
                    } else {
                        common.simpleToast(resp.data.msg);
                    }
                }
            );
        },

        bindSendCommand: function (e) {
            const device = storage.getSelectedDeviceInfo();
            const cmd = e.currentTarget.dataset.cmd;
            this.sendCommand(e.currentTarget.dataset.cmd);
            if (bluetooth.isSingleBT(device)) {
                if (cmd == 'CLOSERELAY' || cmd == 'OPENRELAY' || cmd == 'OPENBUZZER' || cmd == 'CLOSEBUZZER' || cmd == 'BPS') {
                    this.sendBtCommand(cmd);
                }
            }
        },

        bindkeyReportLoss() {},

        sendCommand: function (cmd) {
            uni.showModal({
                content: '确定发送指令吗？',
                showCancel: true,
                cancelText: '取消',
                confirmText: '确定',
                success: (res) => {
                    console.log(res);
                    if (res.confirm) {
                        common.loading();
                        http.postApi(
                            config.API_BATTERY_SEND_COMMAND,
                            {
                                macid: this.macid,
                                cmd: cmd,
                                param: this.param
                            },
                            function (resp) {
                                if (resp.data.code === 200) {
                                    uni.hideLoading();
                                    common.simpleToast('操作成功');
                                } else {
                                    common.simpleToast(resp.data.msg);
                                }
                            }
                        );
                    }
                },
                fail: function (res) {},
                complete: function (res) {}
            });
        },

        bindUptimeDialogOpen() {
            this.setData({
                uptimeDialogShow: true,
                uptime: ''
            });
        },

        onUptimeChange(event) {
            this.setData({
                uptime: event.detail
            });
        },

        bindUptimeDialogConfirm() {
            common.loading();
            http.postApi(
                config.API_BATTERY_SEND_COMMAND,
                {
                    macid: this.macid,
                    cmd: 'UPTIME',
                    param: [this.uptime]
                },
                function (resp) {
                    if (resp.data.code === 200) {
                        uni.hideLoading();
                        common.simpleToast('操作成功');
                    } else {
                        common.simpleToast(resp.data.msg);
                    }
                }
            );
        },

        bindCommandList() {
            uni.navigateTo({
                url: '/pages/commandList/commandList?macid=' + this.macid
            });
        },

        bindUnbind() {
            uni.showModal({
                content: '您确定要解绑当前设备吗？',
                showCancel: true,
                cancelText: '取消',
                confirmText: '确定',
                success: (res) => {
                    console.log(res);
                    if (res.confirm) {
                        common.loading();
                        http.postApi(
                            config.API_BATTERY_UNBIND,
                            {
                                macid: this.macid
                            },
                            function (resp) {
                                if (resp.data.code === 200) {
                                    uni.hideLoading();
                                    common.simpleToast('操作成功');
                                    storage.setRefreshDeviceoPage(true);
                                    uni.navigateBack({
                                        delta: 1
                                    });
                                } else {
                                    common.simpleToast(resp.data.msg);
                                }
                            }
                        );
                    }
                },
                fail: function (res) {},
                complete: function (res) {}
            });
        }
    }
};
</script>
<style>
@import './deviceCommand.css';
</style>
