<template>
    <!-- pages/notice/notice.wxml -->
    <view class="container">
        <view class="item-container">
            <button class="alarm-item flex-row flex-between" @tap="bindAlarmChange" :data-index="index" v-for="(item, index) in alarm_list" :key="index">
                <view class="flex-row">
                    <view class="item-text">{{ item }}</view>
                </view>

                <image class="item-icon" :src="(alarm_push & (1 << index)) != 0 ? '/static/resource/images/kaiqi.png' : '/static/resource/images/guanbi.png'"></image>
            </button>
        </view>
    </view>
</template>

<script>
// pages/notice/notice.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');
export default {
    data() {
        return {
            alarm_list: ['出围栏报警', '入围栏报警', '断电报警', '低电报警', '震动报警', '位移报警', '点火报警', '侧翻报警', '拆卸报警'],
            alarm_push: 0
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.loadNotice();
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        loadNotice: function () {
            // uni.login({
            //     success: (res) => {
            //         http.postApi(
            //             config.API_XCX_NOTICE,
            //             {
            //                 code: res.code
            //             },
            //             (resp) => {
            //                 if (resp.data.code === 200) {
            //                     this.setData({
            //                         alarm_push: resp.data.data.alarm_push
            //                     });
            //                 } else {
            //                     common.simpleToast(resp.data.msg);
            //                     setTimeout(function () {
            //                         uni.navigateBack({
            //                             delta: 1
            //                         });
            //                     }, 1500);
            //                 }
            //             }
            //         );
            //     }
            // });
        },

        bindAlarmChange(e) {
            const index = e.currentTarget.dataset.index;
            common.loading();
            http.postApi(
                config.API_XCX_SET_NOTICE,
                {
                    alarm_push: this.alarm_push ^ (1 << index)
                },
                (resp) => {
                    uni.hideLoading();
                    if (resp.data.code === 200) {
                        this.setData({
                            alarm_push: this.alarm_push ^ (1 << index)
                        });
                    } else {
                        common.simpleToast(resp.data.msg);
                    }
                }
            );
        }
    }
};
</script>
<style>
@import './notice.css';
</style>
