window.JIM = new JMessage({
	debug: true
});
JIM.onDisconnect(function() {
	console.log("【disconnect】");
}); //异常断线监听
function appendToDashboard(text) {
	$('#dashboard').append('<p style="margin-left: 16px">' + text + '</p>');
}
init();
function init() {//初始化
	JIM.init({
		"appkey": param.appkey,
		"random_str": param.random_str,
		"signature": sign,
		"timestamp": param.timestamp
	}).onAck(function(data) {
		// console.log('ack【】:' + JSON.stringify(data));
	}).onSuccess(function(data) {
		console.log('success_init:' + JSON.stringify(data));
		if (shops.login_pwd) {
			login();
		} else {
			register();
		}
	}).onFail(function(data) {
		// console.log('error:' + JSON.stringify(data))
	});
}
function loginOut() {//注销
	JIM.loginOut();
}
function register() {//注册
	JIM.register({
		'username': across_user,
		'password': password,
		'is_md5': true
	}).onAck(function(data) {
		//console.log('ack:' + JSON.stringify(data));
	}).onSuccess(function(data) {
		console.log('success_reg:' + JSON.stringify(data));
		login();
		add_shops_call_pwd(across_user, password);
	}).onFail(function(data) {
		// console.log('error:' + JSON.stringify(data))
	});
}
function login() {//登录
	JIM.login({
		'username': across_user,
		'password': password,
		'is_md5': true
	}).onSuccess(function(data) {
		console.log('success_login:' + JSON.stringify(data));
		JIM.onMsgReceive(function(data) {
			var msg = data;
			var body = data.messages[0].content.msg_body.text;
			var takeout_body ='' ;
			//console.log(body);
			data = JSON.stringify(data);
            var ret=strtoarray(body);
            switch(ret[0]){
                case "caller":
        			get_info(ret[1]);
        			call_log(ret[1]);
                break;
				case "takeout":
					get_takeout(ret[1]);
					break;
            }
			//console.log('msg_receive:' + data);
		});
		JIM.onEventNotification(function(data) {
			//console.log('event_receive: ' + JSON.stringify(data));
		});
		JIM.onSyncConversation(function(data) { //离线消息同步监听
			console.log('event_receive: ' + data);
		});
		JIM.onUserInfUpdate(function(data) {
			console.log('onUserInfUpdate : ' + JSON.stringify(data));
		});
		JIM.onSyncEvent(function(data) {
			console.log('onSyncEvent : ' + JSON.stringify(data));
		});
	}).onFail(function(data) {
		console.log('error:' + JSON.stringify(data));
	}).onTimeout(function(data) {
		console.log('timeout:' + JSON.stringify(data));
	});
}
function strtoarray(str){
    if(!str)return false;
    var newarr=new Object;
    var arr=str.split(",");
    for(i=0;i<arr.length;i++){
        if(arr[i].indexOf(':')>=0){
            var keyarr = arr[i].split(":");
            newarr[keyarr[0]]=keyarr[1];
        }else{newarr[i]=arr[i];}
    }return newarr;
}
function get_info(mobile) {//获取通话人信息
	var query = new Object();
	query.mobile = mobile;
	$.ajax({
		type: 'post',
		data: query,
		url: APP + "Shop/Cashier/get_info.html",
		success: function(data) {
			if (data.status == 1) {
				$('#dl_show').attr('style', 'display:display;');
				$('#no_call').attr('class', 'hide');
				$('#phone').html(data.info.caller);
				$('#username').html(data.info.username);
				$('#level').html(data.info.level);
				$('#money').html(data.info.money);
				$('#usepoints').html(data.info.usepoints);
				$('#reg_time').html(data.info.reg_time);
				$('#last_buy').html(data.info.last_buy);
				$('#last_buy_time').html(data.info.last_buy_time);
				$('#member_address').html(data.info.member_address);
				$('#condiyou').animate({right: '0'}, 300);
			}
			//console.log(data);return;
		}
	});
}
function get_takeout(num) {//获取通话人信息
	$('#takeout').html(num);
}
function add_shops_call_pwd(across_user, password) {//注册成功同步密码
	var query = new Object();
	query.across_user = across_user;
	query.password = password;
	$.ajax({
		type: 'post',
		data: query,
		url: APP + "Shop/Cashier/add_shops_call_pwd.html",
		success: function(data) {}
	});
}
function call_log(mobile) {////加载通话记录
	var query = new Object();
	query.mobile = mobile;
	$.ajax({
		type: 'post',
		data: query,
		url: APP + "Shop/Cashier/call_log.html",
		success: function(data) {
			if (data.status == 1) {
				var html = data.info;
				$('#calllog').prepend(html);
				$('.hoverimg').html(data.call_total);
			}
			// console.log(data);return;
		}
	});
}
function call(mobile) {//呼叫
	var query = new Object();
	query.calles = mobile;
	$.ajax({
		type: 'post',
		data: query,
		url: APP + "Shop/Cashier/call.html",
		success: function(data) {
			if (data.status == 1) {
				showtoast('呼叫成功');
			} else {
				showtoast('呼叫失败');
			}
			// console.log(data);return;
		}
	});
}
function call_handle(type) {//修改呼叫状态
	var mobile = $('#phone').html();
	var query = new Object();
	query.mobile = mobile;
	query.type = type;
	$.ajax({
		type: 'post',
		data: query,
		url: APP + "Shop/Cashier/call_handle.html",
		success: function(data) {
			if (data.status == 1) {
				showtoast('操作成功');
			} else {
				showtoast('操作失败');
			}
			//	console.log(data);return;
		}
	});
}