<template>
    <view class="container">
        <view class="list-group">
            <view @tap="bindAlarmMap" :data-index="index" v-for="(item, index) in alarmList" :key="index">
                <button class="list-item flex-row flex-start no-btn">
                    <view class="warn-info">
                        <view class="dev-info flex-row flex-between">
                            <view class="warn-desc">{{ item.macid }}</view>
                            <view class="warn-item">
                                <text class="warn-text">{{ item.alarm_desc ? item.alarm_desc : '' }}</text>
                            </view>
                        </view>
                        <view class="warn-content">
                            <view class="warn-desc">{{ tools.formatTime(item.send_time) }}</view>
                        </view>
                    </view>
                </button>
            </view>
        </view>

        <view class="no-more">没有更多信息</view>
    </view>
</template>
<script module="tools" lang="wxs" src="@/pages/common/wxs/tools.wxs"></script>
<script>
// pages/warnList/warnList.js
var appConfig = require('../../common/appConfig.js');
const config = require('../../common/config.js');
const http = require('../../common/http.js');
const common = require('../../common/common.js');
var storage = require('../../common/storage.js');
export default {
    data() {
        return {
            macid: '',
            alarmList: [],
            alarmStatus: false,
            // false 0: 未关闭  true 1: 关闭
            haveAlarmStatus: false,
            style_deg: 0
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        this.macid = options.macid || '';
        console.log(options);
        common.loading();
        if (this.macid != '') {
            this.loadWarnList();
        }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        if (this.macid != '') {
            this.loadWarnList();
        }
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    methods: {
        loadWarnList() {
            const me = this;
            common.loading();
            http.postRequest(
                config.API_GPS_ALARM,
                {
                    macid: this.macid
                },
                function (resp) {
                    uni.hideLoading();
                    uni.stopPullDownRefresh();
                    if (resp.data.code === 200) {
                        me.setData({
                            alarmList: resp.data.data.list
                        });
                    } else {
                        common.simpleToast(resp.data.msg);
                    }
                }
            );
        },

        navToPage(e) {
            const page = e.target.dataset.url;
            uni.navigateTo({
                url: page,
                success: function (res) {},
                fail: function (res) {},
                complete: function (res) {}
            });
        },

        bindChangeSetAlarm(e) {
            console.log(e);
            const status = e.detail.value ? 0 : 1;
            const pData = {
                macid: this.macid,
                status: status
            };
            http.postApi(config.API_BATTERY_SET_ALARM_PUSH, pData, function (resp) {
                if (resp.data.code === 200) {
                    common.simpleToast('设置成功');
                }
            });
        },

        getAlarmStatus() {
            const pData = {
                macid: this.macid
            };
            const me = this;
            http.postApi(config.API_BATTERY_GET_ALARM_STATUS, pData, function (resp) {
                if (resp.data.code === 200) {
                    me.setData({
                        alarmStatus: resp.data.data.status == 0 ? false : true
                    });
                }
            });
        },

        bindAlarmMap(e) {
            const alarmInfo = this.alarmList[e.currentTarget.dataset.index];
            storage.setAlarmInfo(alarmInfo);
            console.log(alarmInfo);
            uni.navigateTo({
                url: '/pages/warnMap/warnMap',
                success: function (res) {},
                fail: function (res) {},
                complete: function (res) {}
            });
        }
    }
};
</script>
<style>
@import './warnList.css';
</style>
