<!--
 * @Date: 2023-10-11 15:34:12
 * @LastEditors: JinxChen
 * @LastEditTime: 2023-10-27 14:19:16
 * @FilePath: \TelpoH5FrontendWeb\src\views\gps-card-frontend\device-setting\index.vue
 * @description:
-->
<template>
    <div classs="device-setting">
        <div class="tree-select">
            <van-tree-select  :items="settingItems" :main-active-index.sync="active" :height="clientHeight"  @click-nav="onCkickNav">
                <template #content>
                    <div class="item" v-for="(item, index) in settingItems" :key="index" v-show="item.index === active">
                        <div class="item-setting" v-for="(child, index) in item.data" :key="index">
                            <customCell :title="child.name" :value="child.value" :click="child.router"/>
                        </div>
                    </div>
                    <div class="power" v-show="active === 11 || active === 12 || active === 13">
                            <van-button @click="onClick(active)">点击查看</van-button>
                        </div>
                </template>
            </van-tree-select>
        </div>
    </div>
</template>

<script>
import customCell from '@/components/custom-cell/index';
import { isNotNull} from "@/utils/index";
import APICore from "@/api/core";
import axios from 'axios';
export default {
  name:'',
  components: { customCell },
  data(){
   return {
       active: 0,
       settingItems: [
           { text: '设备状态', index: 0, data: [], className: '',},
           { text: '监护角色', index: 1, data: [],},
           { text: '场景模式', index: 2, data: []},
           { text: '加强省电模式', index: 3, data: []},
           { text: '定位监测', index: 4, data: []},
           { text: '危险区域监测', index: 5, data: [] },
           { text: '健康监测', index: 6, data: []},
           { text: '血压监测', index: 7, data: []},
           { text: '心理监测', index: 8, data: []},
           { text: '佩戴监测', index: 9, data: []},
           { text: '上报周期', index: 10, data: []},
           { text: '电量查看', index: 11, data: []},
           { text: '设备信号强度查看', index: 12, data: []},
           { text: '设备行为日志查看', index: 13, data: []},
       ],
       dateList: [], //时间列表
       clientHeight: '', //当前窗口可视页面高度
   }
  },
  computed: {
    imei() {
        return this.$store.getters.imei/* '861281060086216' */;
    },
  },
  created() {
    this.getAuth();
    this.getUrlQueryParams();
    this.loadRequest();
  },
  mounted() {
    /* this.$toast.loading({message: "加载中"});
    this.getWatchConfig();
    this.getIotCtlMode();
    this.getLocationConfig();
    this.getDrownConfig();
    this.getHealthConfig();
    this.getBloodConfig();
    this.getPsychAbilityConfig();
    this.getNowearConfig();
    this.getUploadConfig();
    this.clientHeight = document.documentElement.clientHeight;
    this.active = Number(this.$store.getters.active) || 0;
    this.$toast.success({message: "加载完成"}); */
  },
  methods: {
    // 加载接口
    async loadRequest() {
        this.$toast.loading({
            message: '数据加载中',
            duration: 0
        });
        this.clientHeight = document.documentElement.clientHeight;
        this.active = Number(this.$store.getters.active) || 0;
        const allPromise = [
            this.getDeviceStatus(),
            this.getWatchConfig(),
            this.getIotCtlMode(),
            this.getLocationConfig(),
            this.getDrownConfig(),
            this.getHealthConfig(),
            this.getBloodConfig(),
            this.getPsychAbilityConfig(),
            this.getNowearConfig(),
            this.getUploadConfig(),
        ];
        const promise = Promise.all(allPromise);
        try {
            const result = await promise;
            const isLoadAllSuccess = result.every(item => {
              return item;
            });
            if(isLoadAllSuccess) {
                this.$toast.success({
                    message: '数据加载完成',
                    duration: 2000
                });
            } else {
                this.$toast.clear();
                this.$dialog.comfirm({
                    title: '提示',
                    message: '请重新进入或者刷新页面'
                })
            }
        } catch(error){
            console.log('error', error);
            this.$toast.clear();
        }
    },
   // 获取b端接口的token
    getAuth() {
        let manufactorId = "5bf13062-a41e-4d00-ba14-1101aad12650";
        APICore.getAuth({ manufactorId: manufactorId }).then(res => {
            this.$store.commit("gatewayToken", res.data.data);
        });
    },
    getUrlQueryParams() {
        // 从url中获取参数并缓存
        let params = this.$route.query;
        if (isNotNull( params )) {
            // 首次进入且正确传参
            //this.imei = params.imei;
            if(params.accessToken) {this.$store.commit('accessToken', params.accessToken); }
            this.$store.commit('imei', params.imei);
        }
    },
    // 获取设备场景模式参数
    getWatchConfig() {
       /*  APICore.getWatchConfig({imei: this.imei}).then(res => {
            let data = res.data.data;
            if(data) {
                const modeId = data.modeId;
                const roleId = data.roleId;
                this.modeId = data.modeId;
                this.roleId = roleId;
                this.settingItems[0].text = `监护角色(${roleId === 1 ? '学生' : '老人'})`;
                this.settingItems[1].text = `场景模式(${this.getModeById(modeId)})`;
            } else {
                this.settingItems[0].text = `监护角色(无数据)`;
                this.settingItems[1].text = `场景模式(无数据)`;
            }
        }) */
        return new Promise((resolve, reject) => {
            APICore.getWatchConfig({imei: this.imei}).then(res => {
                let data = res.data.data;
                let roleViewIndex = 1;
                let modeViewIndex = 2;
                let roleIndex = this.settingItems.findIndex(item => {
                    return item.index === roleViewIndex
                });
                let modeIndex = this.settingItems.findIndex(item => {
                    return item.index === modeViewIndex
                });
                if(data) {
                    const modeId = data.modeId;
                    const roleId = data.roleId;
                    this.modeId = data.modeId;
                    this.roleId = data.roleId;
                    this.settingItems[roleIndex].text = `监护角色(${roleId === 1 ? '学生' : '老人'})`;
                    this.settingItems[modeIndex].text = `场景模式(${this.getModeById(modeId)})`;
                } else {
                    this.settingItems[roleIndex].text = `监护角色(无数据)`;
                    this.settingItems[modeIndex].text = `场景模式(无数据)`;
                    /* this.modeId = 1;
                    this.roleId = 1; */
                };
                resolve(true);
            })
        })
    },
     // 通过模式id获取场景模式
    getModeById(id) {
        if(id === 1) {
            return '标准'
        } else if (id === 2) {
            return '省电'
        } else {
            return '个性'
        }
    },
    // 组装选项数据
    concatTitle(title, status, power){
        return `${title}(${status}) (${power})`
    },
    // 获取加强省电模式参数配置
    getIotCtlMode() {
        /* APICore.getIotCtlMode(this.imei).then(res => {
            let data = res.data.data;
            if(data) {
                this.checked = data.mode === 1;
                this.settingItems[2].text = this.concatTitle('加强省电模式', data.mode === 1 ? '打开' : '关闭', data.mode === 1 ? '耗电高' : '耗电低');
                this.settingItems[2].data = [
                { name: '开启状态:', value: data.enabled === 1 ? '打开': '关闭' },
              ];
            } else {
                this.settingItems[2].text = this.concatTitle('加强省电模式','关闭', '耗电低');
            }
        }) */
        return new Promise((resolve) => {
            APICore.getIotCtlMode(this.imei).then(res => {
                let data = res.data.data;
                let index = 3;
                let findIndex = this.settingItems.findIndex(item => {
                    return item.index === index
                });
                if(data) {
                    this.checked = data.mode === 1;
                    this.settingItems[findIndex].text = this.concatTitle('加强省电模式', data.mode === 1 ? '打开' : '关闭', data.mode === 1 ? '耗电高' : '耗电低');
                    this.settingItems[findIndex].data = [
                    { name: '开启状态:', value: data.enabled === 1 ? '打开': '关闭' },
                ];
                } else {
                    this.settingItems[findIndex].text = this.concatTitle('加强省电模式','关闭', '耗电低');
                }
                resolve(true)
            });
            
        })
    },
    // 获取设备定位参数
    getLocationConfig() {
        /* APICore.getLocationConfig({imei: this.imei}).then(res => {
            let data = res.data.data;
            if(data) {
              this.settingItems[3].text = this.concatTitle('定位监测', data.enabled === 1 ? '打开': '关闭', data.enabled === 1 ? '耗电高' : '耗电低');
              this.settingItems[3].data = [
                { name: '开启状态:', value: data.enabled === 1 ? '打开': '关闭' },
                { name: '检测周期:', value: data.sampleinteval + '分钟' },
              ];
            } else {
                this.settingItems[3].text = this.concatTitle('定位监测','关闭', '耗电低');
            }
        }); */
        return new Promise(resolve => {
            APICore.getLocationConfig({imei: this.imei}).then(res => {
                let data = res.data.data;
                let index = 4;
                let findIndex = this.settingItems.findIndex(item => {
                    return item.index === index
                });
                if(data) {
                this.settingItems[findIndex].text = this.concatTitle('定位监测', data.enabled === 1 ? '打开': '关闭', data.enabled === 1 ? '耗电高' : '耗电低');
                this.settingItems[findIndex].data = [
                    { name: '开启状态:', value: data.enabled === 1 ? '打开': '关闭' },
                    { name: '检测周期:', value: data.sampleinteval + '分钟' },
                ];
                } else {
                    this.settingItems[findIndex].text = this.concatTitle('定位监测','关闭', '耗电低');
                }
                resolve(true)
            });
        })
    },
    // 获取免告警水域
    getDrownReportFilterQuery() {
        APICore.getDrownReportFilterQuery({imei: this.imei}).then(res => {
            let data = res.data.data;
            if(data) {
                let json = {};
                for(var i=0; i<data.length; i++){
                    json[i] = data[i];
                }
                this.drownWhite = JSON.stringify(json);
            }
        })
    },
    // 获取设备危险区域参数
    getDrownConfig() {
        /* APICore.getDrownConfig({imei: this.imei}).then(res => {
            let data = res.data.data;
            if(data) {
                this.settingItems[4].text = this.concatTitle('危险区域监测', data.config.enabled === 1 ? '打开': '关闭', data.config.enabled === 1 ? '耗电高' : '耗电低');
                this.settingItems[4].data = [
                    { name: '涉水监测:', value: data.config.enabled === 1 ? '打开': '关闭' },
                    { name: '检测周期:', value: data.config.intervallvl1 + data.config.intervallvl2 + '分钟' },
                    { name: '告警阈值:', value: data.config.warningdistance + '米' },
                    { name: '涉水停留告警间隔:', value: (data.config.distancelvl2 / 60) + '分钟' },
                    { name: '告警提示持续次数:', value: data.config.warningtimes + '次' },
                    { name: '首次告警逗留设置:', value: data.config.delaytimes + '次' },
                    { name: '时速免告警(≥):', value: data.config.ignorespeed + '公里' },
                    { name: '免告警水域:', value: '查看', router: 'drownWhiteList' },
                    { name: '告警方式:', value: `${data.config.vibrateenabled === 1 ? '震动' : '无'} , ${data.config.lcdenabled === 1 ? '亮屏' : '无'} , ${data.config.musicenabled === 1 ? '声音' : '无'}`},
                ];
            } else {
                this.settingItems[4].text = this.concatTitle('危险区域监测','关闭', '耗电低');
                this.settingItems[4].data = [];
            }
        }); */
        return new Promise(resolve => {
            APICore.getDrownConfig({imei: this.imei}).then(res => {
                let data = res.data.data;
                let index = 5;
                let findIndex = this.settingItems.findIndex(item => {
                    return item.index === index
                });
                if(data) {
                    this.settingItems[findIndex].text = this.concatTitle('危险区域监测', data.config.enabled === 1 ? '打开': '关闭', data.config.enabled === 1 ? '耗电高' : '耗电低');
                    this.settingItems[findIndex].data = [
                        { name: '涉水监测:', value: data.config.enabled === 1 ? '打开': '关闭' },
                        { name: '检测周期:', value: data.config.intervallvl1 + data.config.intervallvl2 + '分钟' },
                        { name: '告警阈值:', value: data.config.warningdistance + '米' },
                        { name: '涉水停留告警间隔:', value: (data.config.distancelvl2 / 60) + '分钟' },
                        { name: '告警提示持续次数:', value: data.config.warningtimes + '次' },
                        { name: '首次告警逗留设置:', value: data.config.delaytimes + '次' },
                        { name: '时速免告警(≥):', value: data.config.ignorespeed + '公里' },
                        { name: '免告警水域:', value: '查看', router: 'drownWhiteList' },
                        { name: '告警方式:', value: `${data.config.vibrateenabled === 1 ? '震动' : '无'} , ${data.config.lcdenabled === 1 ? '亮屏' : '无'} , ${data.config.musicenabled === 1 ? '声音' : '无'}`},
                    ];
                } else {
                    this.settingItems[findIndex].text = this.concatTitle('危险区域监测','关闭', '耗电低');
                    this.settingItems[findIndex].data = [];
                }
                resolve(true)
            });
        })
    },

    // 格式化某些特殊参数值的时间显示方式
    formatInteval(value) {
        return value >= 360 ? (value / 60) + '小时' : value + '分钟'
    },
    // 获取设备健康参数
    getHealthConfig() {
        /* APICore.getHealthConfig({imei: this.imei}).then(res => {
            let data = res.data.data;
            if(data) {
                let healthEnablelist = [
                    { enabled: data.config.heartrateenabled},
                    { enabled: data.config.spo2enabled},
                    { enabled: data.config.temperatureenabled},
                ];
                let config = data.config;
                // 获取健康设置打开的次数
                const healthSettingCount = healthEnablelist.reduce((acc, cur) => cur.enabled === 1 ? ++acc : acc, 0);
                this.settingItems[5].text = this.concatTitle('健康监测', healthSettingCount >= 3 ? '打开'
                : healthSettingCount >= 1 ?  '部分'
                : '关闭', healthSettingCount > 2 ? '耗电高': healthSettingCount === 2 ? '耗电中': '耗电低');
                this.settingItems[5].data = [
                    { name: '体温监测:', value: config.temperatureenabled === 1 ? '打开': '关闭' },
                    { name: '检测周期:', value: this.formatInteval(config.temperatureinteval) },
                    { name: '告警阈值-高温:', value: (config.temperaturemaxvalue / 10) + '度' },
                    { name: '告警方式:', value: `${config.temperaturevibrateenabled === 1 ? '震动' : '无'} , ${config.temperaturelcdenabled === 1 ? '亮屏' : '无'}`},

                    { name: '心率监测:', value: config.heartrateenabled === 1 ? '打开': '关闭' },
                     { name: '检测周期:', value: this.formatInteval(config.heartrateinteval) },
                    { name: '告警阈值-上限:', value: config.heartratemaxvalue + '次' },
                    { name: '告警阈值-下限:', value: config.heartrateminvalue + '次' },
                    { name: '告警方式:', value: `${config.heartratevibrateenabled === 1 ? '震动' : '无'} , ${config.heartratelcdenabled === 1 ? '亮屏' : '无'}`},

                    { name: '血氧监测:', value: config.spo2enabled === 1 ? '打开': '关闭' },
                     { name: '检测周期:', value: this.formatInteval(config.spo2inteval) },
                    { name: '告警阈值-低于:', value: config.spo2minvalue + '%' },
                    { name: '告警方式:', value: `${config.spo2vibrateenabled === 1 ? '震动' : '无'} , ${config.spo2lcdenabled === 1 ? '亮屏' : '无'}`},
                ]
            } else {
                this.settingItems[5].text = this.concatTitle('健康监测','关闭', '耗电低');
                this.settingItems[5].data = [];
            }
        }); */
        return new Promise(resolve => {
            APICore.getHealthConfig({imei: this.imei}).then(res => {
                let data = res.data.data;
                let index = 6;
                let findIndex = this.settingItems.findIndex(item => {
                    return item.index === index
                });
                if(data) {
                    let healthEnablelist = [
                        { enabled: data.config.heartrateenabled},
                        { enabled: data.config.spo2enabled},
                        { enabled: data.config.temperatureenabled},
                    ];
                    let config = data.config;
                    // 获取健康设置打开的次数
                    const healthSettingCount = healthEnablelist.reduce((acc, cur) => cur.enabled === 1 ? ++acc : acc, 0);
                    this.settingItems[findIndex].text = this.concatTitle('健康监测', healthSettingCount >= 3 ? '打开'
                    : healthSettingCount >= 1 ?  '部分'
                    : '关闭', healthSettingCount > 2 ? '耗电高': healthSettingCount === 2 ? '耗电中': '耗电低');
                    this.settingItems[findIndex].data = [
                        { name: '体温监测:', value: config.temperatureenabled === 1 ? '打开': '关闭' },
                        { name: '检测周期:', value: this.formatInteval(config.temperatureinteval) },
                        { name: '告警阈值-高温:', value: (config.temperaturemaxvalue / 10) + '度' },
                        /* { name: '告警阈值-低温:', value: config.temperatureminvalue + '度' }, */
                        { name: '告警方式:', value: `${config.temperaturevibrateenabled === 1 ? '震动' : '无'} , ${config.temperaturelcdenabled === 1 ? '亮屏' : '无'}`},

                        { name: '心率监测:', value: config.heartrateenabled === 1 ? '打开': '关闭' },
                        { name: '检测周期:', value: this.formatInteval(config.heartrateinteval) },
                        { name: '告警阈值-上限:', value: config.heartratemaxvalue + '次' },
                        { name: '告警阈值-下限:', value: config.heartrateminvalue + '次' },
                        { name: '告警方式:', value: `${config.heartratevibrateenabled === 1 ? '震动' : '无'} , ${config.heartratelcdenabled === 1 ? '亮屏' : '无'}`},

                        { name: '血氧监测:', value: config.spo2enabled === 1 ? '打开': '关闭' },
                        { name: '检测周期:', value: this.formatInteval(config.spo2inteval) },
                        { name: '告警阈值-低于:', value: config.spo2minvalue + '%' },
                        /* { name: '告警阈值-低温:', value: config.temperatureminvalue + '度' }, */
                        { name: '告警方式:', value: `${config.spo2vibrateenabled === 1 ? '震动' : '无'} , ${config.spo2lcdenabled === 1 ? '亮屏' : '无'}`},
                    ]
                } else {
                    this.settingItems[findIndex].text = this.concatTitle('健康监测','关闭', '耗电低');
                    this.settingItems[findIndex].data = [];
                }
                resolve(true)
            });
        })
    },
    // 获取血压监测参数设置
    getBloodConfig() {
        /* APICore.getBloodPressConfig({imei: this.imei}).then(res => {
            let data = res.data.data;
            if(data) {
                this.settingItems[6].text = this.concatTitle('血压监测', data.bloodPressenabled === 1 ? '打开': '关闭', data.bloodPressenabled === 1 ? '耗电高' : '耗电低');
                this.settingItems[6].data = [
                    { name: '血压监测:', value: data.bloodPressenabled === 1 ? '打开': '关闭' },
                    { name: '检测周期:', value: this.formatInteval(data.bloodPressinteval) },
                    { name: '告警阈值-收缩压:', value: String(data.systolicmaxvalue)},
                    { name: '告警方式:', value: `${data.bloodPressvibrateenabled === 1 ? '震动' : '无'} ,${data.bloodPresslcdenabled === 1 ? '亮屏' : '无'}`},
                ]
            } else {
                this.settingItems[6].text = this.concatTitle('血压监测','关闭', '耗电低');
                this.settingItems[6].data = [];
            }
        }); */
        return new Promise(resolve => {
            APICore.getBloodPressConfig({imei: this.imei}).then(res => {
                let data = res.data.data;
                let index = 7;
                let findIndex = this.settingItems.findIndex(item => {
                    return item.index === index
                });
                if(data) {
                    this.settingItems[findIndex].text = this.concatTitle('血压监测', data.bloodPressenabled === 1 ? '打开': '关闭', data.bloodPressenabled === 1 ? '耗电高' : '耗电低');
                    this.settingItems[findIndex].data = [
                        { name: '血压监测:', value: data.bloodPressenabled === 1 ? '打开': '关闭' },
                        { name: '检测周期:', value: this.formatInteval(data.bloodPressinteval) },
                        { name: '告警阈值-收缩压:', value: String(data.systolicmaxvalue)},
                        { name: '告警方式:', value: `${data.bloodPressvibrateenabled === 1 ? '震动' : '无'} ,${data.bloodPresslcdenabled === 1 ? '亮屏' : '无'}`},
                    ]
                } else {
                    this.settingItems[findIndex].text = this.concatTitle('血压监测','关闭', '耗电低');
                    this.settingItems[findIndex].data = [];
                }
                resolve(true)
            });
        })
    },
    // 格式化等级
    formatReminder(value) {
        let text = "";
        if (value == 1) {
            text = "轻度";
        } else if (value == 2) {
            text = "中度";
        } else if (value == 3) {
            text = "重度";
        }
        return text;
    },
    // 格式化监测时段
    formatTimeArea(timeArr) {
        let timeToString = timeArr.map(item => {
            return `{时段: ${item.time},时长:${item.duration};}`;
        });
        return String(timeToString);
    },
    // 获取心理监测参数设置
    getPsychAbilityConfig() {
        /* APICore.getPsychAbilityConfig({imei: this.imei}).then(res => {
            let data = res.data.data;
            if (data) {
                this.settingItems[7].text = this.concatTitle('心理监测', data.enabled === 1 ? '打开': '关闭', data.enabled === 1 ? '耗电高' : '耗电低');
                this.settingItems[7].data = [
                    { name: '心理监测:', value: data.enabled === 1 ? '打开': '关闭' },
                    { name: '设备显示:', value: data.device_display === 1 ? '打开': '关闭' },
                    { name: '监测时段:', value: this.formatTimeArea(data.time_area) },
                    { name: '提醒设置:', value: data.reminder_setting.enable === 1 ? '打开': '关闭' },
                    { name: '抑郁等级:', value: this.formatReminder(data.reminder_setting.setting.depressive) },
                    { name: '压力等级:', value: this.formatReminder(data.reminder_setting.setting.pressure) },
                    { name: '疲劳等级:', value: this.formatReminder(data.reminder_setting.setting.fatigue) },
                    { name: '告警方式:', value: `${data.vibrating_screen === 1 ? '震动' : '无'}, ${data.brightening_screen === 1 ? '亮屏' : '无'}`},
                ]
            } else {
                this.settingItems[7].text = this.concatTitle('心理监测', '关闭', '耗电低');
                this.settingItems[7].data = [];
            }
        }); */
        return new Promise(resolve => {
            APICore.getPsychAbilityConfig({imei: this.imei}).then(res => {
                let data = res.data.data;
                let index = 8;
                let findIndex = this.settingItems.findIndex(item => {
                    return item.index === index
                });
                if (data) {
                    this.settingItems[findIndex].text = this.concatTitle('心理监测', data.enabled === 1 ? '打开': '关闭', data.enabled === 1 ? '耗电高' : '耗电低');
                    this.settingItems[findIndex].data = [
                        { name: '心理监测:', value: data.enabled === 1 ? '打开': '关闭' },
                        { name: '设备显示:', value: data.device_display === 1 ? '打开': '关闭' },
                        { name: '监测时段:', value: this.formatTimeArea(data.time_area) },
                        { name: '提醒设置:', value: data.reminder_setting.enable === 1 ? '打开': '关闭' },
                        { name: '抑郁等级:', value: this.formatReminder(data.reminder_setting.setting.depressive) },
                        { name: '压力等级:', value: this.formatReminder(data.reminder_setting.setting.pressure) },
                        { name: '疲劳等级:', value: this.formatReminder(data.reminder_setting.setting.fatigue) },
                        { name: '告警方式:', value: `${data.vibrating_screen === 1 ? '震动' : '无'}, ${data.brightening_screen === 1 ? '亮屏' : '无'}`},
                    ]
                } else {
                    this.settingItems[findIndex].text = this.concatTitle('心理监测', '关闭', '耗电低');
                    this.settingItems[findIndex].data = [];
                }
                resolve(true)
            });
        })
    },
     // 转换时间格式
    shiftTime(time, model) {
        if(time) {
            let startTime = '{' + time.slice(0,2) + ":" + time.slice(2,4);
            let endTime = time.slice(4,6) + ":" + time.slice(6,8) + '}';
            let timeObj = {
                startTime: startTime,
                endTime: endTime,
            };
            this.dateList.push(timeObj);
        }
    },
    // 转化时间数组 :)
    fomatTimeArr(arr) {
        let timeString = arr.map(item => {
            return item.startTime + '-' + item.endTime
        });
        return String(timeString);
    },
    // 获取佩戴监测参数设置
    getNowearConfig() {
        /* APICore.getNowearConfig({ imei: this.imei }).then(res => {
            let data = res.data.data;
            if(data) {
                this.dateList = [];
                this.shiftTime(data.timearea1, 0);
                this.shiftTime(data.timearea2, 1);
                this.shiftTime(data.timearea3, 2);
                this.shiftTime(data.timearea4, 3);
                this.shiftTime(data.timearea5, 4);
                this.shiftTime(data.timearea6, 5);
                this.shiftTime(data.timearea7, 6);
                this.shiftTime(data.timearea8, 7);
                this.shiftTime(data.timearea9, 8);
                this.shiftTime(data.timearea10, 9);
                this.settingItems[8].text = this.concatTitle('佩戴监测', data.enabled === 1 ? '打开': '关闭', data.enabled === 1 ? '耗电高' : '耗电低');
                this.settingItems[8].data = [
                    { name: '佩戴监测:', value: data.enabled === 1 ? '打开': '关闭' },
                    { name: '检测周期:', value: (data.interval / 60) + '分钟' },
                    { name: '检测时段:', value: this.fomatTimeArr(this.dateList)},
                    { name: '未佩戴处理方式:', value: data.mode === 1 ? '不处理': '飞行模式' },
                    { name: '佩戴提醒:', value: data.warningtimes !== 0 ? '打开' : '关闭' },
                    { name: '提醒时间间隔:', value: (data.warninginterval / 60) + '分钟' },
                    { name: '提醒次数:', value: data.warningtimes + '次'},
                    { name: '告警方式:', value: `${data.vibrateenabled === 1 ? '震动' : '无'} ,${data.lcdenabled === 1 ? '亮屏' : '无'} ,${data.soundenabled === 1 ? '声音' : '无'}`},
                ]
            } else {
                this.settingItems[8].text = this.concatTitle('佩戴监测', '关闭', '耗电低');
                this.settingItems[8].data = [];
            }
        }); */
        return new Promise(resolve => {
            APICore.getNowearConfig({ imei: this.imei }).then(res => {
                let data = res.data.data;
                let index = 9;
                let findIndex = this.settingItems.findIndex(item => {
                    return item.index === index
                });
                if(data) {
                    this.dateList = [];
                    this.shiftTime(data.timearea1, 0);
                    this.shiftTime(data.timearea2, 1);
                    this.shiftTime(data.timearea3, 2);
                    this.shiftTime(data.timearea4, 3);
                    this.shiftTime(data.timearea5, 4);
                    this.shiftTime(data.timearea6, 5);
                    this.shiftTime(data.timearea7, 6);
                    this.shiftTime(data.timearea8, 7);
                    this.shiftTime(data.timearea9, 8);
                    this.shiftTime(data.timearea10, 9);
                    this.settingItems[findIndex].text = this.concatTitle('佩戴监测', data.enabled === 1 ? '打开': '关闭', data.enabled === 1 ? '耗电高' : '耗电低');
                    this.settingItems[findIndex].data = [
                        { name: '佩戴监测:', value: data.enabled === 1 ? '打开': '关闭' },
                        { name: '检测周期:', value: (data.interval / 60) + '分钟' },
                        { name: '检测时段:', value: this.fomatTimeArr(this.dateList)},
                        { name: '未佩戴处理方式:', value: data.mode === 1 ? '不处理': '飞行模式' },
                        { name: '佩戴提醒:', value: data.warningtimes !== 0 ? '打开' : '关闭' },
                        { name: '提醒时间间隔:', value: (data.warninginterval / 60) + '分钟' },
                        { name: '提醒次数:', value: data.warningtimes + '次'},
                        { name: '告警方式:', value: `${data.vibrateenabled === 1 ? '震动' : '无'} ,${data.lcdenabled === 1 ? '亮屏' : '无'} ,${data.soundenabled === 1 ? '声音' : '无'}`},
                    ]
                } else {
                    this.settingItems[findIndex].text = this.concatTitle('佩戴监测', '关闭', '耗电低');
                    this.settingItems[findIndex].data = [];
                }
                resolve(true)
            });
        })

    },
    // 获取设备上报参数
    getUploadConfig() {
       /*  APICore.getUploadConfig({imei: this.imei}).then(res => {
            const data = res.data.data;
            if(data) {
                this.settingItems[9].text = `上报周期(${data.uploadinteval}分钟)`;
            } else {
                this.settingItems[9].text = '上报周期(关闭)';
            }
        }); */
        return new Promise(resolve => {
            APICore.getUploadConfig({imei: this.imei}).then(res => {
                const data = res.data.data;
                let index = 10;
                let findIndex = this.settingItems.findIndex(item => {
                    return item.index === index
                });
                if(data) {
                    this.settingItems[findIndex].text = `上报周期(${data.uploadinteval}分钟)`;
                } else {
                    this.settingItems[findIndex].text = '上报周期(关闭)';
                }
                resolve(true)
            });
        })
    },
    // 获取设备状态
    getDeviceStatus() {
        let reqBody = {
            imei: /* '861281060086216' *//* '862838050029479' */this.$store.getters.imei
        };
        // 线上地址
       let baseUrl = process.env.VUE_APP_BASE_API;
       let reqUrl = `${baseUrl}/iotservice/queryDeviceDetail`;
        //本地调试地址    
       /* let reqUrl = `/api/id/queryDeviceDetail`; */
       return new Promise(resolve => {
            axios.post(`${reqUrl}`, reqBody).then(res => {
                let data = res.data;
                let index = 0;
                let findIndex = this.settingItems.findIndex(item => {
                    return item.index === index
                });
                if(data.code === 200) {
                    this.settingItems[findIndex].className = this.calcDeviceStatus(data.data.data.status, true);
                    this.settingItems[findIndex].text = `设备状态(${this.calcDeviceStatus(data.data.data.status)})`;
                } else {
                    this.settingItems[findIndex].text = `设备状态(无数据)`;
                }
                resolve(true)
            });
       })
    },
    // 计算设备状态
    calcDeviceStatus(status, isCallClass) {
        let result = ''
        let className = '';
        switch(status) {
            case 'ONLINE':
                result = '在线';
                className = 'device-status-online';
                break;
            case 'OFFLINE':
                result = '离线';
                className = 'device-status-other';
                break;
            case 'UNACTIVE':
                result = '未激活';
                className = 'device-status-other';
                break;
            case 'DISABLE':
                result = '已禁用';
                className = 'device-status-other';
                break;
            default: break;
        };
        return isCallClass ? className : result;
    },
    // 点击左边树形图
    onCkickNav(value) {
        this.active = value;
        this.$store.commit('active', value);
        switch(value) {
            /* case 10:
            case 11:
            case 12:
                this.$router.push({
                name: 'devicePower',
                query: {
                    title: `${value === 10 ? 'BatteryLevel' : value === 11 ? 'status': 'Offline'}`
                }
            });
                break; */
            case 0:
                this.getDeviceStatus();
                break;
            case 1:
                this.getWatchConfig();
                break;
            case 2:
                this.getWatchConfig();
                break;
            case 3:
                this.getIotCtlMode();
                break;
            case 4:
                this.getLocationConfig();
                break;
            case 5:
                this.getDrownConfig();
                break;
            case 6:
                this.getHealthConfig();
                break;
            case 7:
                this.getBloodConfig();
                break;
            case 8:
                this.getPsychAbilityConfig();
                break;
            case 9:
                this.getNowearConfig();
                break;
            case 10:
                this.getUploadConfig();
                break;
            default: break;

        }
    },
    // 点击页面查看按钮
    onClick(value) {
        if(value) {
            this.$router.push({
                name: 'devicePower',
                query: {
                    title: `${value === 11 ? 'BatteryLevel' : value === 12 ? 'status': 'Offline'}`
                }
            })
        }
    }
  }
}
</script>

<style lang="scss">
    .van-tree-select__nav-item {
        padding: 15px 5px !important;
    }
    .device-status-other {
        .van-sidebar-item__text {
            color: red !important;
        }
    }
    .device-status-online {
        .van-sidebar-item__text {
            color: green !important;
        }
    }
    .van-tree-select__content {
        padding: 5px;
    }
    .van-tree-select__nav{
        flex: 1 !important;
    }
    .van-tree-select__content {
        @include center();
    }
    
</style>
<style scoped lang="scss">
    @import "./index.scss";
</style>
