<template>
    <view class="container">
        <van-popup :show="isShowLeft" position="left" @close="handleCloseLeft">
            <view class="left-box">
                <image src="/static/resource/images/111.png" class="left-background"></image>
                <view class="head-left-box flex-row">
                    <image :src="userInfo.type == 'group' && userInfo.info.headimg ? userInfo.info.headimg : '/static/resource/images/logo.png'" class="head-left-img"></image>
                    <view style="text-align: left">
                        <view class="name-text">{{ userInfo.info.name }}</view>
                        <button class="quit-button" @tap="bindQuit">注销</button>
                        <view class="version-no">v{{ verson_no }}</view>
                    </view>
                </view>

                <view class="flex-row flex-between" @tap="bindSmsAlarm">
                    <view class="left-view-item flex-row">
                        <image src="http://qiniu.bms16.com/gps/money.png" class="left-img"></image>
                        <view class="left-text">短信报警</view>
                    </view>
                    <view class="gold-view">{{ coin / 100 }}币</view>
                </view>

                <button class="left-item flex-row" @tap="bindDevice">
                    <image src="/static/resource/images/guanli.png" class="left-img"></image>
                    <view class="left-text">设备管理</view>
                </button>
                <view v-if="notice" style="position: relative">
                    <button class="left-item flex-row" @tap="bindNotice">
                        <image src="/static/resource/images/xiaoxi.png" class="left-img"></image>
                        <view class="left-text">消息通知</view>
                    </button>
                    <image :src="noticeStatus ? '/static/resource/images/kaiqi.png' : '/static/resource/images/guanbi.png'" class="notice-img" @tap="bindSetNotice"></image>
                </view>
                <button v-if="macid" class="left-item flex-row" @tap="bindCredit">
                    <image src="/static/resource/images/chongzhi.png" class="left-img"></image>
                    <view class="left-text">续费充值</view>
                </button>
                <button class="left-item flex-row" @tap="bindWillExpire">
                    <image src="/static/resource/images/willExpire.png" class="left-img"></image>
                    <view class="left-text">即将到期</view>
                </button>
            </view>
        </van-popup>
        <view class="top-container">
            <view class="head-box flex-row">
                <image src="/static/resource/images/touxiang@3x.png" class="head-img" mode="widthFix" @tap="bindMy"></image>
                <view class="status-box flex-row">
                    <view v-if="deviceInfo.online == 1" class="status-item flex-row">
                        <image src="/static/resource/images/xianzhong@3x.png" class="status-img" mode="widthFix"></image>
                        <view class="status-text">设备在线</view>
                    </view>
                    <view v-else class="status-item flex-row">
                        <image src="/static/resource/images/weixianzhong@3x.png" class="status-img" mode="widthFix"></image>
                        <view class="status-text">设备离线</view>
                    </view>
                    <!--view class="status-item flex-row">
          <image src="/resource/images/xianzhong@3x.png" class="status-img" mode="widthFix"></image>
          <view class="status-text">
            手机在线
          </view>
        </view-->
                    <view v-if="bt_status == 1" class="status-item flex-row" @tap="bluetoothClose">
                        <image src="/static/resource/images/xianzhong@3x.png" class="status-img" mode="widthFix"></image>
                        <view class="status-text">蓝牙连接</view>
                    </view>
                    <view v-if="bt_status == 2" class="status-item flex-row" @tap="bluetoothConnect">
                        <image src="/static/resource/images/weixianzhong@3x.png" class="status-img" mode="widthFix"></image>
                        <view class="status-text">蓝牙断开</view>
                    </view>
                </view>
            </view>
            <view class="device-box flex-row flex-between">
                <view class="device-view" :data-gid="macid" @tap="bindDevice">
                    <view v-if="macid" class="device-text">设备 {{ macid }}</view>
                    <view v-else class="device-text">请选择设备</view>
                </view>
                <view>
                    <image
                        src="/static/resource/images/shuaxin.png"
                        class="load-img"
                        :style="loading ? 'animation:loading 0.8s linear infinite;' : ''"
                        @tap="loadDeviceInfo"
                    ></image>
                </view>
                <!-- <view>
        <image src="/resource/images/shuaxin.png" class="load-img"
          style="{{ loading ? 'animation:loading 0.8s linear infinite;' : '' }}" bindtap="refreshBluetooth"></image>
      </view> -->

                <!--view class="sky-view">
        <view class="sky-text" style="font-size:28rpx;">
          32℃
        </view>
        <view class="sky-text">
          多云转晴
        </view>
      </view-->
            </view>
            <view class="main-box">
                <view class="main-img">
                    <image src="/static/resource/images/daquan.png" class="main-img"></image>
                    <view class="main-text">
                        {{
                            btInfo.quantity
                                ? tools.toFixed(btInfo.quantity, 0)
                                : bmsInfo.state
                                ? tools.toFixed(bmsInfo.state.soc, 0)
                                : tools.toFixed(deviceInfo.quantity, 0)
                                ? tools.toFixed(deviceInfo.quantity, 0)
                                : '0'
                        }}%
                    </view>
                    <view
                        v-if="(bmsInfo.state && bmsInfo.chargeState >= 0 && bmsInfo.chargeState <= 2) || (btInfo && btInfo.chargeState >= 0 && btInfo.chargeState <= 2)"
                        class="main-status"
                    >
                        <image src="/static/resource/images/chongdianzhong.png" class="chongdian-img"></image>
                        <view class="main-status-text">
                            <block v-if="btInfo && btInfo.chargeState >= 0">
                                <text v-if="btInfo.chargeState == 0">静止</text>
                                <text v-else-if="btInfo.chargeState == 1">充电中</text>
                                <text v-else-if="btInfo.chargeState == 2">放电中</text>
                                <!-- <text wx:else>未知</text> -->
                            </block>
                            <block v-else-if="bmsInfo.state">
                                <text v-if="bmsInfo.state.chargeState == 0">静止</text>
                                <text v-else-if="bmsInfo.state.chargeState == 1">充电中</text>
                                <text v-else-if="bmsInfo.state.chargeState == 2">放电中</text>
                                <!-- <text wx:else>未知</text> -->
                            </block>

                            <!-- {{ btInfo.chargeState ? (btInfo.chargeState == 1 ? "充电中" : (btInfo.chargeState == 2 ? "放电中" : "静止")) : (bmsInfo.state ? (bmsInfo.state.chargeState == 1 ? "充电中" : (bmsInfo.state.chargeState == 2 ? "放电中" : "静止")) : (deviceInfo.chargeState == 1 ? "充电中" : (deviceInfo.chargeState == 0 ? "未充电" : "未知"))) }} -->
                        </view>
                    </view>
                </view>
            </view>
            <view v-if="bms" class="bms-box" @tap="bindToBMS">
                <view class="bms-text">BMS</view>
                <image src="/static/resource/images/fanhui.png" class="bms-img"></image>
            </view>
            <!-- BTJYWJT -->

            <view
                v-if="macid && isBtj && (deviceInfo.device_type == 'FMBMS' || deviceInfo.device_type == 'BTJYWJT' || deviceInfo.device_type == 'FMYWJT')"
                class="emergency-starting"
                @tap="bindEmergencyStarting"
            >
                <view class="alarm-text">应急启动</view>
            </view>

            <view v-if="macid" class="alarm-box" @tap="bindAlarm">
                <image src="/static/resource/images/baojin2.png" class="bms-img"></image>
                <view class="alarm-text">报警</view>
            </view>
        </view>
        <view class="status-container">
            <image src="/static/resource/images/card.png" class="stat-img"></image>
            <view class="stat-box">
                <view class="stat-item">
                    <view class="stat-name">总电压</view>
                    <view class="stat-value">
                        {{
                            btInfo.voltage
                                ? tools.toFixed(btInfo.voltage, 2)
                                : bmsInfo.state
                                ? tools.toFixed(bmsInfo.state.voltageAll, 1)
                                : deviceInfo.voltage
                                ? tools.toFixed(deviceInfo.voltage, 2)
                                : '0'
                        }}
                    </view>
                    <view class="stat-tip">(V)</view>
                </view>
                <view class="stat-item" @tap="bindMileage">
                    <view class="stat-name">当日里程</view>
                    <view class="stat-value">
                        {{ deviceInfo.day_mil ? tools.toFixed(deviceInfo.day_mil, 1) : '0' }}
                    </view>
                    <view class="stat-tip">(km)</view>
                </view>
                <view class="stat-item">
                    <view class="stat-name">停留时间</view>
                    <view class="stat-value">
                        {{ deviceInfo.heart_time && deviceInfo.heart_time - deviceInfo.gps_time > 0 ? tools.formatSeconds(deviceInfo.heart_time - deviceInfo.gps_time)[0] : '0' }}
                    </view>
                    <view class="stat-tip">
                        ({{
                            deviceInfo.heart_time && deviceInfo.heart_time - deviceInfo.gps_time > 0 ? tools.formatSeconds(deviceInfo.heart_time - deviceInfo.gps_time)[1] : '秒'
                        }})
                    </view>
                </view>
                <view v-if="bmsInfo.state" class="stat-item">
                    <view class="stat-name">电池温度</view>
                    <view v-if="bmsInfo.third_device_type != 'KYYWJT'" class="stat-value">
                        {{ btInfo.temp ? btInfo.temp : bmsInfo.state.temp[0] ? tools.toFixed(bmsInfo.state.temp[0], 1) : '未知' }}
                    </view>
                    <view class="stat-value" v-else>
                        {{ btInfo.temp ? btInfo.temp : tools.toFixed(bmsInfo.state.temp[0], 1) }}
                    </view>
                    <view class="stat-tip">(℃)</view>
                </view>
            </view>
        </view>
        <view class="map-container">
            <view class="map-box">
                <view class="map-top">
                    <view class="map-top-box">
                        <view class="map-top-text">GPS</view>
                        <view :class="gps > 0 ? 'map-top-i' : 'map-top-n'" />
                        <view :class="gps > 1 ? 'map-top-i' : 'map-top-n'" />
                        <view :class="gps > 2 ? 'map-top-i' : 'map-top-n'" />
                        <view :class="gps > 3 ? 'map-top-i' : 'map-top-n'" />
                        <view :class="gps > 4 ? 'map-top-i' : 'map-top-n'" />
                    </view>
                    <view class="map-top-box">
                        <view class="map-top-text">GSM</view>
                        <view :class="gsm > 0 ? 'map-top-i' : 'map-top-n'" />
                        <view :class="gsm > 1 ? 'map-top-i' : 'map-top-n'" />
                        <view :class="gsm > 2 ? 'map-top-i' : 'map-top-n'" />
                        <view :class="gsm > 3 ? 'map-top-i' : 'map-top-n'" />
                        <view :class="gsm > 4 ? 'map-top-i' : 'map-top-n'" />
                    </view>
                </view>
                <map
                    id="myMap"
                    style="width: 100%; height: 100%"
                    :enable-zoom="false"
                    :enable-scroll="false"
                    :longitude="deviceInfo.longitude"
                    :latitude="deviceInfo.latitude"
                    :markers="[{ id: 1, latitude: deviceInfo.latitude, longitude: deviceInfo.longitude, iconPath: '/static/resource/images/weizhi2.png', width: 45, height: 45 }]"
                    @tap="bindLocal"
                    @markertap="bindMarkertasp"
                >
                    <view v-if="addr" class="map-addr">
                        <image src="/static/resource/images/dingwei.png" class="addr-img"></image>
                        <view class="addr-text">
                            {{ addr }}
                        </view>
                    </view>
                </map>
            </view>
        </view>
        <view>
            <!-- #ifdef MP-WEIXIN -->
            <!-- [miniprogram-to-uniapp] 公众号关注组件 仅微信小程序支持 -->
            <official-account></official-account>
            <!-- #endif -->

            <!-- #ifndef MP-WEIXIN -->
            <!-- #endif -->
        </view>
    </view>
</template>
<script module="tools" lang="wxs" src="@/pages/common/wxs/tools.wxs"></script>
<script>
// pages/index/index.js
var config = require('../../common/config.js');
var helpConfig = require('../../common/help_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');
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
var joinPack = [];
export default {
    data() {
        return {
            macid: '',

            deviceInfo: {
                online: 0,
                device_type: '',
                voltage: false,
                day_mil: false,
                heart_time: '',
                gps_time: 0,
                longitude: '',
                latitude: ''
            },

            bmsInfo: {
                state: false,
                chargeState: 0,
                third_device_type: ''
            },

            bms: false,
            gps: 0,
            gsm: 0,
            addr: '',
            isShowLeft: false,

            userInfo: {
                type: '',

                info: {
                    headimg: '',
                    name: ''
                }
            },

            notice: false,
            noticeStatus: false,
            loading: false,
            bt_status: 0,

            btInfo: {
                quantity: false,
                chargeState: 0,
                voltage: false,
                temp: false
            },

            coin: 0,
            display_bms: true,
            verson_no: '',
            baud: '',
            isBtj: false,
            isShowModal: false
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        qqmapsdk = new QQMapWX({
            key: config.QQ_MAP_KEY
        });
        this.setData({
            verson_no: config.APP_VERSION
        });
        // const accountInfo = uni.getAccountInfoSync();
        // if (accountInfo.miniProgram.appId == 'wx092db752b69b47f5') {
        //     this.setData({
        //         isBtj: true
        //     });
        // }
        const userInfo = storage.getUserInfo();
        if (userInfo) {
            bluetooth.initBluetooth();
            common.bluetoothGetCtlData();
            this.loadUserInfo();
            this.loadIsWillExpireDevice();
            this.loadUpdateNotice();
        }
    },
    onReady: function () {},
    onUnload: function () {
        //this.bluetoothClose()
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        const userInfo = storage.getUserInfo();
        if (userInfo) {
            this.loadGetCoin();
            this.loadDeviceInfo();
            this.loadNotice();
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.loadDeviceInfo();
        this.loadNotice();
    },
    onReachBottom: function () {},
    methods: {
        loadUpdateNotice: function () {
            var current_version = 1;
            var get_version_current = storage.getNoticeVersion();
            if (get_version_current !== current_version) {
                uni.showModal({
                    title: '新增短信报警提醒功能',
                    content: '点击我的头像->短信报警',
                    showCancel: false,
                    success: function (res) {
                        if (res.confirm) {
                            //这里是点击了确定以后
                        } else {
                            //这里是点击了取消以后
                        }
                    }
                });
                storage.setNoticeVersion(current_version);
            }
        },

        bluetoothClose: function () {
            if (this.loading) {
                return;
            }
            const device = storage.getSelectedDeviceInfo();
            this.setData({
                loading: true
            });
            var time = new Date().getTime();
            bluetooth.closeDevice(
                device.mac_id,
                () => {
                    this.setData({
                        bt_status: 2
                    });
                    setTimeout(() => {
                        this.setData({
                            loading: false
                        });
                    }, 800 - ((new Date().getTime() - time) % 800));
                },
                () => {
                    setTimeout(() => {
                        this.setData({
                            loading: false
                        });
                    }, 800 - ((new Date().getTime() - time) % 800));
                }
            );
            bluetooth.offCharacteristicStateChange(device.mac_id, 'home');
            bluetooth.offConnectionStateChange(device.mac_id, 'home');
        },

        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,
                            () => {
                                this.setData({
                                    bt_status: 1
                                });
                                bluetooth.onCharacteristicStateChange(device.mac_id, 'home', (data) => {
                                    this.processBluetoothReceiveData(data, device);
                                });
                                bluetooth.onConnectionStateChange(device.mac_id, 'home', (res) => {
                                    if (!res.connected) {
                                        this.setData({
                                            bt_status: 2
                                        });
                                    } else {
                                        this.setData({
                                            bt_status: 1
                                        });
                                    }
                                });
                                common.bluetoothBaud(device, bluetooth, (res) => {
									console.log("波特率"+res);
                                    this.setData({
                                        baud: res
                                    });
                                });
                                bluetooth.stateUpdate(
                                    device.mac_id,
                                    (res) => {},
                                    (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));
                                    }
                                });
                            }
                        );
                    },
                    (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));
                            }
                        });
                    }
                );
            }
        },

        loadNotice: function () {
            // uni.login({
            //     success: (res) => {
            //         http.postApi(
            //             config.API_XCX_NOTICE,
            //             {
            //                 code: res.code
            //             },
            //             (resp) => {
            //                 if (resp.data.code === 200) {
            //                     this.setData({
            //                         notice: true,
            //                         noticeStatus: resp.data.data.status
            //                     });
            //                 }
            //             }
            //         );
            //     }
            // });
        },

        loadBmsData() {
            const deviceInfo = storage.getSelectedDeviceInfo();
            if (deviceInfo) {
            }
        },

        loadQuantityReponse(macid, voltage) {
            var postData = {
                macid: macid,
                voltage: voltage
            };
            http.postApi(config.API_ELE_QUANTITY, postData, (resp) => {
                if (resp.data.code === 200) {
                    var electric_quantity = resp.data.data.electric_quantity;
                    var btInfo = this.btInfo;
                    var deviceInfo = this.deviceInfo;
                    var bmsInfo = this.bmsInfo;
                    bmsInfo = {
                        state: {
                            soc: 0
                        }
                    };
                    bmsInfo.state.soc = electric_quantity;
                    btInfo.quantity = electric_quantity;
                    deviceInfo.quantity = electric_quantity;
                    this.setData({
                        btInfo: btInfo,
                        deviceInfo: deviceInfo,
                        bmsInfo: bmsInfo
                    });
                    console.log(this.btInfo);
                }
            });
        },

        refreshBluetooth() {
            const device = storage.getSelectedDeviceInfo();
            bluetooth.stateUpdate(
                device.mac_id,
                (res) => {},
                (res) => {}
            );
        },

        loadDeviceInfo: function () {
            const deviceInfo = storage.getSelectedDeviceInfo();
            if (deviceInfo) {
                this.setData({
                    loading: true
                });
                http.postApi(
                    config.API_GPS_INFO,
                    {
                        macid: deviceInfo.mac_id
                    },
                    (resp) => {
                        //wx.hideLoading()
                        if (resp.data.code === 200) {
                            this.setData({
                                macid: deviceInfo.mac_id,
                                deviceInfo: resp.data.data.info,
                                gps: resp.data.data.info.gpscount,
                                gsm: parseInt(resp.data.data.info.online) === 0 ? 0 : resp.data.data.info.gsmlevel <= 15 ? 3 : resp.data.data.info.gsmlevel > 18 ? 5 : 4
                            });
                            qqmapsdk.reverseGeocoder({
                                location: {
                                    latitude: resp.data.data.info.latitude,
                                    longitude: resp.data.data.info.longitude
                                },
                                success: (res) => {
                                    const formatted_addresses = res.result.formatted_addresses;
                                    const address = res.result.address; // + formatted_addresses.recommend
                                    this.setData({
                                        addr: address
                                    });
                                }
                            });
                        } else {
                            if (bluetooth.acceptDevice(deviceInfo)) {
                                this.setData({
                                    macid: deviceInfo.mac_id
                                });
                            } else {
                                common.simpleToast(resp.data.msg);
                                if (resp.data.code === 10044) {
                                    this.setData({
                                        macid: deviceInfo.mac_id
                                    });
                                } else {
                                    storage.setSelectedDeviceInfo(null);
                                    this.setData({
                                        macid: '',
                                        deviceInfo: {},
                                        gps: 0,
                                        gsm: 0
                                    });
                                }
                            }
                        }
                        this.loadBluetooth(deviceInfo);
                    }
                );
                this.loadBmsInfo();
            }
        },

        loadBluetooth(deviceInfo) {
            var time = new Date().getTime();
            if (bluetooth.acceptDevice(deviceInfo)) {
                if (this.bt_status != 1) {
                    this.setData({
                        bt_status: 2
                    });
                    bluetooth.openBluetoothAdapter(
                        (res) => {
                            bluetooth.connectDevice(
                                deviceInfo,
                                () => {
                                    this.setData({
                                        bt_status: 1
                                    });
                                    bluetooth.onCharacteristicStateChange(deviceInfo.mac_id, 'home', (data) => {
                                        this.processBluetoothReceiveData(data, deviceInfo);
                                    });
                                    bluetooth.onConnectionStateChange(deviceInfo.mac_id, 'home', (res) => {
                                        if (!res.connected) {
                                            this.setData({
                                                bt_status: 2
                                            });
                                        } else {
                                            this.setData({
                                                bt_status: 1
                                            });
                                        }
                                    });
                                    common.bluetoothBaud(deviceInfo, bluetooth, (res) => {
                                        this.setData({
                                            baud: res
                                        });
                                    });
                                    bluetooth.stateUpdate(
                                        deviceInfo.mac_id,
                                        (res) => {
                                            setTimeout(() => {
                                                this.setData({
                                                    loading: false
                                                });
                                            }, 800 - ((new Date().getTime() - time) % 800));
                                        },
                                        (res) => {
                                            setTimeout(() => {
                                                this.setData({
                                                    loading: false
                                                });
                                            }, 800 - ((new Date().getTime() - time) % 800));
                                        }
                                    );
                                },
                                (res) => {
                                    console.log(res);
                                    setTimeout(() => {
                                        this.setData({
                                            loading: false
                                        });
                                    }, 800 - ((new Date().getTime() - time) % 800));
                                }
                            );
                        },
                        (res) => {
                            console.log(res);
                            setTimeout(() => {
                                this.setData({
                                    loading: false
                                });
                            }, 800 - ((new Date().getTime() - time) % 800));
                        }
                    );
                } else {
                    bluetooth.stateUpdate(
                        deviceInfo.mac_id,
                        (res) => {
                            setTimeout(() => {
                                this.setData({
                                    loading: false
                                });
                            }, 800 - ((new Date().getTime() - time) % 800));
                        },
                        (res) => {
                            setTimeout(() => {
                                this.setData({
                                    loading: false
                                });
                            }, 800 - ((new Date().getTime() - time) % 800));
                        }
                    );
                }
            } else {
                setTimeout(() => {
                    this.setData({
                        loading: false
                    });
                }, 800 - ((new Date().getTime() - time) % 800));
            }
        },

        bindEmergencyStarting() {
            uni.showModal({
                content: '您确定要下发应急启动吗？',
                confirmText: '确定',
                success: (res) => {
                    if (res.confirm) {
                        const deviceInfo = storage.getSelectedDeviceInfo();
                        console.log(deviceInfo);
                        var setcommand = '';
                        if (deviceInfo.device_type == 'FMBMS' || deviceInfo.device_type == 'FMYWJT') {
                            setcommand = '0xCE';
                        } else {
                            setcommand = '32_29';
                        }
                        const pData = {
                            macid: deviceInfo.mac_id,
                            name: setcommand,
                            value: '1'
                        };
                        http.postApi(config.API_BMS_SET, pData, function (resp) {
                            uni.hideLoading();
                            if (resp.data.code === 200) {
                                common.simpleToast('下发成功');
                            } else {
                                common.simpleToast(resp.data.msg);
                            }
                        });
                    } else {
                        me.setData({
                            isShowModal: false
                        });
                    }
                },
                fail: (res) => {}
            });
        },

        processBluetoothReceiveData(data, deviceInfo) {
            if ('baud' in data) {
                common.bluetoothSetBaud(deviceInfo, data.baud, this.baud, bluetooth);
                return;
            }
            if (bluetooth.bluetoothDeviceConfig && data != null) {
                const me = this;
                common.reportBms(deviceInfo.mac_id, data, function (success) {
                    me.loadBmsInfo();
                });
            }
            if (!bluetooth.haveBMSForBT(deviceInfo) && data.voltage !== undefined && !data.hasOwnProperty('quantity')) {
                if (bluetooth.isVoltageToEle) {
                    common.getQVConfig(deviceInfo.mac_id, function (resp) {
                        var hex = resp.data.data.content;
                        if (hex != '') {
                            bluetooth.voltageToEle(
                                deviceInfo.mac_id,
                                hex,
                                (res) => {},
                                (res) => {}
                            );
                        }
                    });
                }
                this.loadQuantityReponse(deviceInfo.mac_id, data.voltage);
            }
            this.setData({
                btInfo: data
            });
        },

        loadUserInfo: function () {
            const userInfo = storage.getUserInfo();
            if (!userInfo) {
                http.postApi(config.API_USER_INFO, {}, (resp) => {
                    if (resp.data.code === 200) {
                        this.setData({
                            userInfo: resp.data.data
                        });
                        storage.setUserInfo(resp.data.data);
                    } else {
                        common.simpleToast(resp.data.msg);
                    }
                });
            } else {
                this.setData({
                    userInfo: userInfo
                });
            }
        },

        loadBmsInfo: function () {
            const deviceInfo = storage.getSelectedDeviceInfo();
            if (deviceInfo) {
                //common.loading();
                const me = this;
                http.postApi(
                    config.API_BMS_STATE,
                    {
                        macid: deviceInfo.mac_id
                    },
                    (resp) => {
                        //wx.hideLoading()
                        if (resp.data.code === 200) {
                            this.setData({
                                bms: true,
                                bmsInfo: resp.data.data
                            });
                            if (me.userInfo.type != 'group') {
                                if (me.bmsInfo.display_bms - 0 === 0) {
                                    me.setData({
                                        display_bms: false
                                    });
                                }
                            }
                        } else {
                            let havebms = bluetooth.haveBMSForBT(deviceInfo);
                            this.setData({
                                bms: havebms
                            });
                        }
                    }
                );
            }
        },

        bindDevice(event) {
            const userInfo = storage.getUserInfo();
            if (userInfo) {
                var $this = this;
                var battery_sn = event.currentTarget.dataset.gid;
                uni.navigateTo({
                    url: '/pages/device/device?macid=' + battery_sn
                });
            } else {
                uni.showModal({
                    title: '',
                    content: '您还未登录，请先登录',
                    showCancel: true,
                    cancelText: '取消',
                    confirmText: '确定',
                    success: function (res) {
                        if (res.confirm) {
                            uni.navigateTo({
                                url: '/pages/login/login'
                            });
                        }
                    }
                });
            }
        },

        bindMy() {
            const userInfo = storage.getUserInfo();
            if (userInfo) {
                this.setData({
                    isShowLeft: true
                });
            } else {
                uni.showModal({
                    title: '',
                    content: '您还未登录，请先登录',
                    showCancel: true,
                    cancelText: '取消',
                    confirmText: '确定',
                    success: function (res) {
                        if (res.confirm) {
                            uni.navigateTo({
                                url: '/pages/login/login'
                            });
                        }
                    }
                });
            }
        },

        handleCloseLeft() {
            this.setData({
                isShowLeft: false
            });
        },

        bindToBMS: function () {
            const deviceInfo = storage.getSelectedDeviceInfo();
            uni.navigateTo({
                url:
                    '/pages/bms/bms?macid=' +
                    deviceInfo.mac_id +
                    '&deviceType=' +
                    deviceInfo.device_type +
                    '&bt_type=' +
                    deviceInfo.bt_type +
                    '&bms=' +
                    deviceInfo.bms +
                    '&btid=' +
                    deviceInfo.btid +
                    '&btkey=' +
                    deviceInfo.btkey,
                success: function (res) {},
                fail: function (res) {},
                complete: function (res) {}
            });
        },

        bindLocal: function (e) {
            // const deviceInfo = storage.getSelectedDeviceInfo()
            console.log(deviceInfo);
            const deviceInfo = this.deviceInfo;
            if (deviceInfo) {
                if (
                    this.macid == '' ||
                    common.isEmpty(deviceInfo.longitude + '') ||
                    deviceInfo.longitude == '-' ||
                    common.isEmpty(deviceInfo.latitude + '') ||
                    deviceInfo.latitude == '-' ||
                    deviceInfo.longitude == 0 ||
                    deviceInfo.latitude == 0
                ) {
                    common.simpleToast('该设备暂无位置信息');
                    return;
                }
                uni.navigateTo({
                    url: '/pages/deviceLocal/deviceLocal'
                });
            }
        },

        bindMarkertasp: function (e) {
            if (e.markerId == 1) {
                this.bindLocal();
            }
        },

        bindAlarm: function () {
            uni.navigateTo({
                url: '/pages/warnList/warnList?macid=' + this.macid,
                success: function (res) {},
                fail: function (res) {},
                complete: function (res) {}
            });
        },

        bindQuit: function () {
            var that = this;
            uni.showModal({
                title: '',
                content: '确定要注销吗？',
                showCancel: true,
                cancelText: '取消',
                confirmText: '确定',
                success: function (res) {
                    if (res.confirm) {
                        that.bluetoothClose();
                        uni.clearStorage();
                        uni.reLaunch({
                            url: '/pages/login/login',
                            success: function (res) {},
                            fail: function (res) {},
                            complete: function (res) {}
                        });
                    }
                }
            });
        },

        bindCredit: function () {
            uni.navigateTo({
                url: '/pages/credit/credit?macid=' + this.macid,
                success: function (res) {},
                fail: function (res) {},
                complete: function (res) {}
            });
        },

        bindWillExpire: function () {
            uni.navigateTo({
                url: '/pages/willExpire/willExpireDevice',
                success: function (res) {},
                fail: function (res) {},
                complete: function (res) {}
            });
        },

        bindNotice: function () {
            uni.navigateTo({
                url: '/pages/notice/notice',
                success: function (res) {},
                fail: function (res) {},
                complete: function (res) {}
            });
        },

        bindSetNotice: function () {
            // common.loading();
            // uni.login({
            //     success: (res) => {
            //         http.postApi(
            //             config.API_XCX_SET_NOTICE,
            //             {
            //                 code: res.code,
            //                 status: !this.noticeStatus
            //             },
            //             (resp) => {
            //                 uni.hideLoading();
            //                 if (resp.data.code === 200) {
            //                     this.setData({
            //                         noticeStatus: !this.noticeStatus
            //                     });
            //                 } else {
            //                     common.simpleToast(resp.data.msg);
            //                 }
            //             }
            //         );
            //     }
            // });
        },

        loadIsWillExpireDevice: function () {
            const that = this;
            const pData = {
                size: 1,
                page: 1
            };
            http.postApi(config.API_RENEW_LIST, pData, (resp) => {
                if (resp.data.code === 200) {
                    let count = resp.data.data.count;
                    let cancelBtnVisiable = resp.data.data.cancelBtnVisiable;
                    if (count > 0) {
                        var dateBegin = new Date().getTime();
                        var getTime = storage.getPopTime();
                        var runTime = ((dateBegin - getTime) / 1000).toFixed(0);
                        if (runTime > resp.data.data.timer * 60 * 60) {
                            storage.setPopTime(dateBegin);
                            const me = this;
                            const msg = '您有' + count + '台设备需要续费';
                            uni.showModal({
                                title: '提示',
                                content: msg,
                                showCancel: cancelBtnVisiable,
                                cancelText: '取消',
                                confirmText: '前往处理',
                                success: function (res) {
                                    console.log(res);
                                    if (res.confirm) {
                                        uni.navigateTo({
                                            url: '/pages/willExpire/willExpireDevice',
                                            success: function (res) {},
                                            fail: function (res) {},
                                            complete: function (res) {}
                                        });
                                    }
                                },
                                fail: function (res) {},
                                complete: function (res) {}
                            });
                        }
                    }
                }
            });
        },

        bindMileage() {
            if (this.macid == '') {
                return;
            }
            uni.navigateTo({
                url: '/pages/mileage/mileage?macid=' + this.macid,
                success: function (res) {},
                fail: function (res) {},
                complete: function (res) {}
            });
        },

        bindSmsAlarm() {
            uni.navigateTo({
                url: '/pages/smsAlarm/smsAlarm',
                success: function (res) {},
                fail: function (res) {},
                complete: function (res) {}
            });
        },

        loadGetCoin() {
            const me = this;
            http.postApi(config.API_GET_COIN, {}, function (resp) {
                if (resp.data.code === 200) {
                    me.setData({
                        coin: resp.data.data.coin
                    });
                }
            });
        }
    }
};
</script>
<style>
@import './home.css';
</style>
