<template>
    <!-- pages/smsConfig/smsConfig.wxml -->
    <view class="container">
        <view>
            <view v-if="type == 0" class="device_number_view">配置设备编号：{{ macid }}</view>
            <view v-if="type == 1" class="device_number_view">配置所有设备</view>
        </view>
        <view>
            <view class="main_view">
                <view @tap="bindAlarmMain" :data-index="index" class="main_mark flex-row flex-row" v-for="(item, index) in alarmTypeList" :key="index">
                    <view :class="typeIndex == index ? 'line_view' : 'line_view_no'"></view>

                    <view :class="typeIndex == index ? 'select_main type_view' : 'type_view'">{{ item.name }}</view>
                </view>
            </view>
            <view class="sub_view">
                <view v-for="(item, index) in alarmList" :key="index">
                    <view @tap="bindAlarmSub" :data-index="index" class="alarm_view flex-row flex-between">
                        <view>{{ item.alarmName }}</view>
                        <image v-if="item.isSelect == '1'" class="select_done" src="http://qiniu.bms16.com/gps/select_true.png"></image>
                        <image v-if="item.isSelect != '1'" class="select_done" src="http://qiniu.bms16.com/gps/select_false.png"></image>
                    </view>
                </view>
            </view>
        </view>

        <view class="bottom-save">
            <!-- <view class="top_view">
      <view class="flex-row">
        <rich-text nodes="{{seriousHtml}}"></rich-text>
      </view>
      <view style="margin-top: 10rpx;" class="flex-row">
        <rich-text nodes="{{warnHtml}}"></rich-text>
      </view>
    </view> -->
            <!-- <view class="remind_view">友情提醒：短信严重报警每1分钟会发送一次，警告报警每30分钟会发送一次</view> -->
            <view @tap="saveAlarmType" class="save-btn">保存</view>
        </view>
    </view>
</template>

<script>
// pages/smsConfig/smsConfig.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 {
            seriousHtml: '',
            warnHtml: '',
            alarmTypeList: [],
            alarmList: [],
            alarmList1: [],
            alarmList2: [],
            typeIndex: 0,
            macid: '',
            type: 0
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        this.setData({
            macid: options.macid || ''
        });
        this.setData({
            type: options.type
        });
        this.loadAlarmType();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        loadAlarmType() {
            const pData = {
                macid: this.macid
            };
            const me = this;
            http.postApi(config.API_SMS_GET_DEVICE_SET, pData, function (resp) {
                if (resp.data.code === 200) {
                    if (resp.data.data.length > 1) {
                        me.setData({
                            alarmTypeList: resp.data.data,
                            alarmList: resp.data.data[0].list,
                            alarmList1: resp.data.data[0].list,
                            alarmList2: resp.data.data[1].list
                        });
                        me.currentSetAlarmType();
                    }
                }
            });
        },

        currentSetAlarmType() {
            var importAlarmList = [];
            var warnAlarmList = [];
            for (var i = 0; this.alarmList1.length > i; i++) {
                if (this.alarmList1[i].isSelect === '1') {
                    importAlarmList.push(this.alarmList1[i].alarmName);
                }
            }
            for (var i = 0; this.alarmList2.length > i; i++) {
                if (this.alarmList2[i].isSelect === '1') {
                    warnAlarmList.push(this.alarmList2[i].alarmName);
                }
            }
            // this.setData({
            //   seriousHtml:"<span style='font-size: 13px;'>严重报警：</span><span style='color: rgb(51, 51, 51,0.38);font-size: 13px;'>" +  importAlarmList.toString() + "</span>"
            // })
            // this.setData({
            //   warnHtml:"<span style='font-size: 13px;'>警告报警：</span><span style='color: rgb(51, 51, 51,0.38);font-size: 13px;'>" +  warnAlarmList.toString() + "</span>"
            // })
        },

        saveAlarmType() {
            var alarmList = [];
            var params = {};
            for (var i = 0; this.alarmList1.length > i; i++) {
                params[this.alarmList1[i].type] = this.alarmList1[i].isSelect;
            }
            for (var i = 0; this.alarmList2.length > i; i++) {
                params[this.alarmList2[i].type] = this.alarmList2[i].isSelect;
            }
            params['macid'] = this.macid;
            const me = this;
            http.postApi(config.API_SMS_SAVE_SET, params, function (resp) {
                if (resp.data.code === 200) {
                    common.simpleToast('保存成功');
                    setTimeout(function () {
                        uni.navigateBack({
                            delta: 1
                        });
                    }, 1000);
                    return;
                }
            });
        },

        bindAlarmMain: function (e) {
            const index = e.currentTarget.dataset.index;
            this.setData({
                typeIndex: index,
                alarmList: index === 0 ? this.alarmList1 : this.alarmList2
            });
        },

        bindAlarmSub: function (e) {
            const index = e.currentTarget.dataset.index;
            var alarmList1 = [];
            var alarmList2 = [];
            alarmList1 = this.alarmList1;
            alarmList2 = this.alarmList2;
            if (this.typeIndex === 0) {
                alarmList1[index].isSelect = alarmList1[index].isSelect === '1' ? '0' : '1';
                this.setData({
                    alarmList1: alarmList1,
                    alarmList: alarmList1
                });
            }
            if (this.typeIndex === 1) {
                alarmList2[index].isSelect = alarmList2[index].isSelect === '1' ? '0' : '1';
                this.setData({
                    alarmList: alarmList2,
                    alarmList2: alarmList2
                });
            }
            this.currentSetAlarmType();
        }
    }
};
</script>
<style>
@import './smsConfig.css';
</style>
