/**
* v_addUnlockInfo.js
* Version: 0.1
* User: wy
* Date: 2017-10-09
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 绑定开锁信息页面
*/

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

iot.ready(init);
function init() {
    //通用 select、按钮、dialog、loading组件
    uPublic.componentsInit(['u-select','u-button','u-dialog','u-loading']);
    const app = new Vue({
        data:{     
            recordSelectIndex: 0,
            openId: null,
            recordSelectList: [],   
            waysOpenDoor: [],
            selectedMode: 1,
            //配置组件
            componentsConfig: initComponentsConfig(),
            extras: {}
        },
        mounted(){    
        	this.extras = iot.navigator.getExtras();
        	console.log(this.extras);
        	let self = this;
        	for( let i =1 ; config.openDoorWay.hasOwnProperty(i) ;i++){
        		self.waysOpenDoor.push(
	        		{
	        			mode: i,
	        			text: Vue.t('unlockInfo.'+config.openDoorWay[i]+'')
	        		}
        		)
        	}
        	resolve(this);
        },
        methods:{
        	getRecordSelectIndex(){
        		return this.recordSelectIndex;
        	},
        	setRecordSelectIndex(index){
        		this.recordSelectIndex = index;
        	},
        	getOpenId(){
        		return this.openId;
        	},
        	setOpenId(id){
        		this.openId = id;
        	},
        	getRecordSelectList(){
        		return this.recordSelectList;
        	},
        	setRecordSelectList(list){
        		this.recordSelectList = list;
        	},
        	getSelectedMode(){
        		return this.selectedMode;
        	},
        	setSelectedMode(mode){
        		this.selectedMode = mode
        	},
            //tap <
            onBackTap(){
                backTap();
            },
            onRecordSelectChange(value) {
                recordSelectChange(this, value);
            },
            //tap 添加
            onAddButtonTap(){
                addButtonTap(this);
            },
            onSelectOpenDoorModeTap(index){
            	selectOpenDoorModeTap(this, index);
            }
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        recordSelect: {
            initParam: {
                class: 'custom-select',
                list: [],
                pagesize: Number.MAX_VALUE,
                tip: Vue.t('addUnlockInfo.noRecordTip')
            }
        },
        //添加 button 参数
        addButton: {   
            initParam: {
                class: 'custom-button',
                text: Vue.t('btn.add')
            }
        },
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }               
    }
}

function resolve(self){
	getTodayReccordByMode(self,self.getSelectedMode());
}

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

function recordSelectChange(self, value) {
	console.log(value);
    self.recordSelectIndex = value;
    console.log('recordSelectChange:' + self.recordSelectIndex);
    for(let i=0; i< self.getRecordSelectList().length; i++){
    	if(self.recordSelectIndex == self.getRecordSelectList()[i].id){
    		self.setOpenId(parseInt(self.getRecordSelectList()[i].openId));
    	}
    }
}

//tap 添加 
function addButtonTap(self){
		uComponents.showLoading(self);
    	iot.business.api.sendCustom('lock/bindOpenLog',{
		data: {
			device_id: self.extras.deviceId,
			mode: self.getSelectedMode(),
			openId: self.getOpenId(),
			rel_id: self.extras.id
		},
		success: (response) => {
			console.log(response);
			if(response.data.success){
				uComponents.openAlert(self, Vue.t('addUnlockInfo.addInfoSuccess'), {
			        text: Vue.t('dialog.confirm'), callback: function () {
			        	iot.navigator.fire(plus.webview.currentWebview().opener(),'returnPage',{ });
			        	iot.navigator.back();
			        }
			    });
			}else{}
		},
		error: (error) => {
			console.log(error);
			uComponents.openAlert(vmObj, Vue.t('addUnlockInfo.addInfoFailue'), {
		        text: Vue.t('dialog.confirm'), callback: function () {
		        }
		    });
		},
		complete: () => {
			uComponents.hideLoading(self);
		}
	});
}

function selectOpenDoorModeTap(self, index){
	self.setSelectedMode(index);
	getTodayReccordByMode(self,index);
}

function getTodayReccordByMode(self, mode){
	uComponents.showLoading(self);
	iot.business.api.sendCustom('lock/getUnbindOpen',{
		data: {
			mode: self.getSelectedMode(),
			device_id: self.extras.deviceId
		},
		success: (response) => {
			console.log(response);
			let data = response.data;
			self.setRecordSelectList(data);
				let list = [];
				for(let i = 0; i < data.length; i++){
					list[i] = {
						value: data[i].id,
						title: moment(data[i].time).format("HH:mm"),
						subtitle: 'ID:'+data[i].openId
					}
				}
				if(data.length){
					self.setRecordSelectIndex(data[0].id);
					for(let i=0; i< self.getRecordSelectList().length; i++){
				    	if(self.recordSelectIndex == self.getRecordSelectList()[i].id){
				    		self.setOpenId(parseInt(self.getRecordSelectList()[i].openId));
				    	}
				    }
				}
				uComponents.changeSelectList(self,list);
		},
		error: (error) => {
			console.log(error);
			uPublic.openRequestErrorAlert(self);
		},
		complete: () => {
			uComponents.hideLoading(self);
		}
	});
}
