/**
* v_deviceList.js
* Version: 0.1
* User: wy
* Date: 2017-11-03
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 产品型号页面
*/

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

iot.ready(init);
function init() {
    //通用 list、dialog、loading组件
    uPublic.componentsInit(['u-list','u-dialog','u-loading']);
    uPublic.closeSlideBack();
    const app = new Vue({
        data:{            
            //配置组件
            componentsConfig: initComponentsConfig(),
            deviceList: null
        },
        mounted(){     
        	getDeviceList(this);
        },
        methods:{
        	getDeviceList(){
        		return this.deviceList;
        	},
        	setDeviceList(list){
        		this.deviceList = list;
        	},
            //tap <
            onBackTap(){
                backTap();
            },
            onDeviceListRowTap(value){
                deviceListRowTap(this, value);
            }                  
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    let model = Vue.t('deviceList.model');
    return {
        deviceList:{   
            initParam: {
                class: 'custom-device-list',
                list: [],
                tip: ''
            }
        },
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }              
    }
}

function getDeviceList(self){
	iot.business.sds.getProductList({
	    success: (response) => {
	    	console.log(response);
	    	let list = [];
	    	let data = response.data;
	    	self.setDeviceList(data);
	    	for(let i =0; i<data.length; i++){
	    		list[i] = {
	    			leftImage: data[i].icon,
	    			title: data[i].deviceName,
	    			subtitle: Vue.t('deviceList.model')+data[i].model,
	    			rightIcon: '&#xe6a7;'
	    		}
	    	}
	    	uComponents.changeList(self, list);
	    },
	    error: (error) => {
	    	if(window.iotDebug){
                iotDebug.push('end: 获取设备列表失败'+JSON.stringify(error));
           	}
	    	if (window.iotDebug) {
                iotDebug.upload(iot,'获取设备列表失败');
            }
	    }
	});
}

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

function deviceListRowTap(self, value){
	console.log(self.getDeviceList()[value].model);
    iot.navigator.openWindow({
        url: './wifiAddHelp.html',
        id: 'wifiAddHelp',
        extras: {
        	model: self.getDeviceList()[value].model
        }
    });  
}