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

    <view class="container">
        <view class="item-container">
            <view class="command-item flex-row" v-for="(item, index) in commandList" :key="index">
                <view style="width: 100%">
                    <view class="flex-row flex-between" style="width: 100%; margin-bottom: 8rpx">
                        <view class="command-title">{{ commandLabel[item.cmd] ? commandLabel[item.cmd] : item.cmd }}</view>
                        <view class="command-status">{{ item.status == 0 ? '发送成功' : '发送失败' }}</view>
                    </view>
                    <view class="command-time">{{ tools.formatTime(item.ctime) }}</view>
                </view>
            </view>
        </view>
        <i-load-more :tip="isLoading ? '正在加载 ...' : '没有更多信息啦'" :loading="isLoading" />
    </view>
</template>
<script module="tools" lang="wxs" src="@/pages/common/wxs/tools.wxs"></script>
<script>
import noMore from '@/component/nomore/nomore';
import iLoadMore from '@/component/iview/load-more/index';
// pages/commandList/commandList.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');
const LIMT_PAGE = 20;
export default {
    components: {
        noMore,
        iLoadMore
    },
    data() {
        return {
            isLoading: false,
            macid: '',
            commandLabel: {
                CLOSERELAY: '断开油电',
                OPENRELAY: '恢复油电',
                SAFEON: '设备设防',
                SAFEOFF: '设备撤防',
                LOCATION: '立即定位',
                RESTART: '设备重启',
                RESET: '恢复出厂设置',
                UPTIME: '调整上报间隔',
                OPENBUZZER: '打开蜂鸣器',
                CLOSEBUZZER: '关闭蜂鸣器',
                SETLOWVOLTAGE: '设置休眠电压'
            },
            commandList: [],
            start_page: 1,
            limit_page: LIMT_PAGE
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (!options.macid) {
            uni.navigateBack({
                delta: 1
            });
        }
        this.setData({
            macid: options.macid
        });
        this.refreshLoadCommandList();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.refreshLoadCommandList();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.isLoading) {
            return;
        }
        this.setData({
            isLoading: true
        });
        this.start_page++;
        //common.loading()
        this.loadCommandList();
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        refreshLoadCommandList: function () {
            if (this.isLoading) {
                return;
            }
            this.setData({
                commandList: [],
                isLoading: true
            });
            common.loading();
            this.limit_page = LIMT_PAGE;
            this.start_page = 1;
            this.loadCommandList();
        },

        loadCommandList: function () {
            const pData = {
                size: this.limit_page,
                page: this.start_page,
                macid: this.macid
            };
            http.postApi(config.API_BATTERY_COMMAND_LIST, pData, (resp) => {
                uni.hideLoading();
                uni.stopPullDownRefresh();
                if (resp.data.code === 200) {
                    let commandList = this.commandList;
                    commandList.push.apply(commandList, resp.data.data.list);
                    this.setData({
                        commandList: commandList,
                        isLoading: false
                    });
                } else {
                    common.simpleToast(resp.data.msg);
                }
            });
        }
    }
};
</script>
<style>
@import './commandList.css';
</style>
