/**
* 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 {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,
            recordSelectList: [],   
            waysOpenDoor: [{mode: 0, text: '指纹'},{mode: 1, text: '密码'},{mode: 2, text: 'IC卡'},{mode: 3, text: '钥匙'},{mode: 4, text: '劫持指纹'},{mode: 5, text: '远程开门'},{mode: 6, text: '遥控器'}],
            selectedMode: 0,
            //配置组件
            componentsConfig: initComponentsConfig()   
        },
        mounted(){    
        	resolve(this);
        },
        methods:{
        	getRecordSelectIndex(){
        		return this.recordSelectIndex;
        	},
        	setRecordSelectIndex(index){
        		this.recordSelectIndex = index;
        	},
        	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) {
    self.recordSelectIndex = value;
    console.log('recordSelectChange:' + self.recordSelectIndex);
}

//tap 添加 
function addButtonTap(self){
		uComponents.showLoading(self);
//  	iot.business.api.sendCustom('lock/bindOpenDoorInfo',{
//		data: {
//			mode: self.getSelectedMode(),
//			openId: self.getRecordSelectIndex()
//		},
//		success: (response) => {
//			console.log(response);
//			let data = uPublic.checkResponseData(response.data);
//			if(data){
				uComponents.openAlert(self, Vue.t('addUnlockInfo.addInfoSuccess'), {
			        text: Vue.t('dialog.confirm'), callback: function () {
			        	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){
//	iot.business.api.sendCustom('lock/getTodayReccordByMode',{
//		data: {
//			mode: self.getSelectedMode()
//		},
//		success: (response) => {
//			console.log(response);
//			let data = uPublic.checkResponseData(response.data);
//			if(data){
//				let record = data.record;
				let record = [];
				switch(self.getSelectedMode()){
					case 0:
						let date = new Date();
						record = [
							{openId: '001', time: date},
							{openId: '002', time: date},
							{openId: '003', time: date},
							{openId: '004', time: date},
							{openId: '005', time: date},
						];
						break;
					case 1:
						date = new Date();
						record = [
							{openId: '001', time: date},
							{openId: '002', time: date}
						]
						break;
					
				}
				
				let list = [];
				for(let i = 0; i < record.length; i++){
					list[i] = {
						value: record[i].openId,
						title: moment(record[i].time).format("HH:mm"),
						subtitle: 'ID:'+record[i].openId
					}
				}
				uComponents.changeSelectList(self,list);
				if(record.length > 0){
					self.setRecordSelectIndex(record[0].openId);
				}
//			}else{}
//		},
//		error: (error) => {
//			console.log(error);
//			uPublic.openRequestErrorAlert(self);
//		},
//		complete: () => {}
//	});
}
