/**
* v_doorlockUsers.js
* Version: 0.1
* User: wy
* Date: 2017-10-09
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 门锁用户页面
*/

import {iot, uPublic, uComponents} from '../../public/public.js';

iot.ready(init);
function init() {
    //通用 按钮、dialog、loading组件
    uPublic.componentsInit(['u-button','u-dialog','u-loading']);
    const app = new Vue({
        data:{ 
            userInfo: {
                image: '../../resources/image/gray_unbindHead_icon.png',
                role: 1,
                title: '我是个用户二号',
                subtitle: ''
            },
            bindShowFlag: true,  
            unbindShowFlag: false,
            stateShowFlag: false,
            guideShowFlag: false,   
            guideStep1ShowFlag: false, 
            guideStep2ShowFlag: false,  
            guideStep3ShowFlag: false,   
            guideStep4ShowFlag: false,
            //配置组件
            componentsConfig: initComponentsConfig(),
            extras: {}
        },
        mounted(){     
        	this.extras = iot.navigator.getExtras();
        	console.log(this.extras);
        	checkFirstAddUser(this);
        	getUserInfo(this);
        },
        methods:{
            getBindShowFlag(){
                return this.bindShowFlag;
            },
            setBindShowFlag(boolean){
                this.bindShowFlag = boolean;
            },
            getUnbindShowFlag(){
                return this.unbindShowFlag;
            },
            setUnbindShowFlag(boolean){
                this.unbindShowFlag = boolean;
            },
            getStateShowFlag(){
                return this.stateShowFlag;
            },
            setStateShowFlag(boolean){
                this.stateShowFlag = boolean;
            },
            getGuideShowFlag(){
                return this.guideShowFlag;
            },
            setGuideShowFlag(boolean){
                this.guideShowFlag = boolean;
            },
            getGuideStep1ShowFlag(){
                return this.guideStep1ShowFlag;
            },
            setGuideStep1ShowFlag(boolean){
                this.guideStep1ShowFlag = boolean;
            },
            getGuideStep2ShowFlag(){
                return this.guideStep2ShowFlag;
            },
            setGuideStep2ShowFlag(boolean){
                this.guideStep2ShowFlag = boolean;
            },
            getGuideStep3ShowFlag(){
                return this.guideStep3ShowFlag;
            },
            setGuideStep3ShowFlag(boolean){
                this.guideStep3ShowFlag = boolean;
            },
            getGuideStep4ShowFlag(){
                return this.guideStep4ShowFlag;
            },
            setGuideStep4ShowFlag(boolean){
                this.guideStep4ShowFlag = boolean;
            },
            //tap <
            onBackTap(){
                backTap();
            },
            onEditNameTap(){
                editNameTap();
            },
            //tap 邀请绑定
            onBindTap(){
                bindTap();
            },
            //tap 解绑
            onUnbindButtonTap(){
                unbindButtonTap(this);
            },
            //tap 绑定开锁信息
            onBindUnlockInfoTap(){
                bindUnlockInfoTap();
            },
            //tap 历史开门记录
            onOpendoorRecordTap(){
                opendoorRecordTap();
            },
            //tap 删除该用户
            onDeleteUserButtonTap(){
                deleteUserButtonTap(this);
            },
            onUnbindStateDialogTap(){
                unbindStateDialogTap(this);
            },
            //tap 下一步
            onNextButtonTap(){
                nextButtonTap(this);
            },
            //tap 我知道了
            onISeeButtonTap(){
                iSeeButtonTap(this);
            }                
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        //解绑 button 参数
        unbindButton: {   
            initParam: {
                class: 'custom-unbind-button',
                text: Vue.t('btn.unbind')
            }
        }, 
        //删除该用户 button 参数
        deleteUserButton: {   
            initParam: {
                class: 'custom-button',
                text: Vue.t('btn.deleteUser')
            }
        }, 
        //下一步 button 参数
        nextButton: {   
            initParam: {
                class: 'custom-dialog-button',
                text: Vue.t('btn.next')
            }
        },
        //我知道了 button 参数
        iSeeButton: {   
            initParam: {
                class: 'custom-dialog-button',
                text: Vue.t('btn.iSee')
            }
        },     
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }               
    }
}

//判断是否第一次添加用户
function checkFirstAddUser(){
	iot.storage.getMap('isFirstAddUser', (res) => {
        if (res) {
            
        } else {
            //显示引导动画
            showGuide();
        }
    }, () => {
		//显示 引导动画
		showGuide();
    });
}

//显示动画
async function showGuide(){
	try{
		let time =await sleep(1000);
		self.setGuideStep1ShowFlag(true); 

		time = await sleep(1000);
        self.setGuideStep2ShowFlag(true);
		
		time = await sleep(1000);
        self.setGuideStep3ShowFlag(true),   
		
		time = await sleep(1000);
		self.setGuideStep4ShowFlag(true);
	}
	catch (err){
		console.log(err);
	}
}

function sleep(time){
	return new Promise(function(resolve, reject){
		setTimeout(()=>{
			resolve(time);
		},time);
	})
}

function getUserInfo(self){
	iot.business.api.sendCustom('user/getUserInfo',{
		data: {
			id: self.extras.userId
		},
		success: (response) => {
			console.log(response);
			
		},
		error: (error) => {
			console.log(error);
		},
		complete: () => {}
	})
}

//tap <
function backTap(){
    iot.navigator.back();
}

function editNameTap(){
    iot.navigator.openWindow({
        url: './editName.html',
        id: 'editName'
    });
}

//tap 邀请绑定
function bindTap(){
    iot.navigator.openWindow({
        url: './inviteBind.html',
        id: 'inviteBind'
    });
}

//tap 解绑
function unbindButtonTap(self){
    uComponents.openConfirm(self, Vue.t('doorlockUsers.unbindDialogTip'),
       {text: Vue.t('dialog.cancel'), callback: function () {}},
       {text: Vue.t('dialog.unbind'), callback: function () {
            self.setStateShowFlag(true);
       }}
    );
}

function unbindStateDialogTap(self){
    self.setStateShowFlag(false);
    self.setBindShowFlag(true);
    self.setUnbindShowFlag(false);
}

//tap 绑定开锁信息
function bindUnlockInfoTap(){
    iot.navigator.openWindow({
        url: './unlockInfo.html',
        id: 'unlockInfo'
    });
}

//tap 历史开门记录
function opendoorRecordTap(){
    iot.navigator.openWindow({
        url: '../device/opendoorRecord.html',
        id: 'opendoorRecord'
    });
}

//tap 删除该用户
function deleteUserButtonTap(self){
    uComponents.openConfirm(self, Vue.t('doorlockUsers.deleteUserDialogTip'),
                           {text: Vue.t('dialog.cancel'), callback: function () {}},
                           {text: Vue.t('dialog.delete'), callback: function () {}}
    );
}

//tap 下一步
function nextButtonTap(self){
    self.setGuideStep1ShowFlag(false);
    self.setGuideStep2ShowFlag(false);
    self.setGuideStep3ShowFlag(true);
    self.setGuideStep4ShowFlag(true);
}

//tap 我知道了
function iSeeButtonTap(self){
    self.setGuideShowFlag(false);
}