<template>
    <!-- pages/smsRecord/smsRecord.wxml -->

    <view class="container">
        <view class="top_view flex-row flex-between">
            <view v-if="changeIndex == 0" class="flex-row">
                <view class="info_group">{{$t('设备编号') + '：'}}</view>
                <view class="info_group_mark">{{ macid }}</view>
            </view>
            <view @tap="bindChangeDevice" v-if="changeIndex == 0" class="changeDevice">{{$t('切换查看所有设备')}}</view>

            <view v-if="changeIndex == 1" class="flex-row">
                <view class="info_group">{{$t('所有设备')}}</view>
            </view>
            <view @tap="bindChangeDevice" v-if="changeIndex == 1" class="changeDevice">{{ $t('切换设备编号') + '：'+ macid }}</view>
        </view>
        <view v-for="(item, index) in alarmList" :key="index">
            <view class="info-group bg_view">
                <view class="flex-row flex-between">
                    <view class="flex-row">
                        <view class="info_group">{{$t('设备名称') + '：'}}</view>
                        <view class="info_group_mark">{{ item.gpsbms_name }}</view>
                    </view>
                    <view class="flex-row">
                        <view class="info_group">{{$t('设备编号') + '：'}}</view>
                        <view class="info_group_mark">{{ item.battery_sn }}</view>
                    </view>
                </view>
                <view class="flex-row">
                    <view class="info_group">{{$t('报警类型') + '：'}}</view>
                    <view class="info_red">{{ item.alarm_type }}</view>
                </view>
                <view class="flex-row">
                    <view class="info_group">{{$t('报警时间') + '：'}}</view>
                    <view class="info_group_mark">{{ tools.formatTime(item.alarm_time) }}</view>
                </view>
            </view>
        </view>
    </view>
</template>
<script module="tools" lang="wxs" src="@/pages/common/wxs/tools.wxs"></script>
<script>
// pages/smsRecord/smsRecord.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');
const LIMT_PAGE = 20;
export default {
    data() {
        return {
            isLoading: false,
            alarmList: [],
            start_page: 1,
            limit_page: LIMT_PAGE,
            changeIndex: 0,
            macid: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
		uni.setNavigationBarTitle({
			title: this.$t('短信报警记录')
		});
        this.setData({
            macid: options.macid || ''
        });
        this.refreshLoadSmsAlarmList();
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
		uni.setNavigationBarTitle({
			title: this.$t('短信报警记录')
		});
	},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.refreshLoadSmsAlarmList();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.isLoading) {
            return;
        }
        this.setData({
            isLoading: true
        });
        this.start_page++;
        this.loadWarnList();
    },
    methods: {
        bindChangeDevice: function (e) {
            if (this.changeIndex === 0) {
                this.setData({
                    changeIndex: 1
                });
            } else {
                this.setData({
                    changeIndex: 0
                });
            }
            this.refreshLoadSmsAlarmList();
        },

        refreshLoadSmsAlarmList: function () {
            if (this.isLoading) {
                return;
            }
            this.setData({
                alarmList: [],
                isLoading: true
            });
            common.loading(this);
            this.limit_page = LIMT_PAGE;
            this.start_page = 1;
            this.loadWarnList();
        },

        loadWarnList() {
            const me = this;
            var dataMacId = '';
            if (me.changeIndex === 0) {
                dataMacId = me.macid;
            }
            const pData = {
                size: me.limit_page,
                page: me.start_page,
                macid: dataMacId
            };
            common.loading(this);
            http.postRequest(config.API_RECORD_LIST, pData, function (resp) {
                uni.stopPullDownRefresh();
                uni.hideLoading();
                if (resp.data.code === 200) {
                    let alarmList = me.alarmList;
                    alarmList.push.apply(alarmList, resp.data.data.list);
                    me.setData({
                        alarmList: alarmList,
                        isLoading: false
                    });
                } else {
                    common.simpleToast(me,resp.data.msg);
                }
            });
        }
    }
};
</script>
<style>
@import './smsRecord.css';
</style>
