//这是一个公共函数


// 移动端 后台快捷键
function mInitShortcut(){
    initShortcut('admin-shortcuts') ;
}

// 检测是否有cookie插件
function checkCookies(){
    return Cookies ? true : false ;
}

/**
 * 初始化快捷键  pc端口前台
 * @param name 本地存储name
 * @param isRemember 是否记住上次操作
 */
function initShortcut(name,isRemember){
    name = name || 'chat-shortcuts' ;

    isRemember = isRemember ||  true ;

    var shortcut = checkCookies() ? Cookies.get(name) : '' ;

    if( shortcut && isRemember ){
        $(".fuceng").find('.fuceng-li').each(function(){
            $(this).attr('type') == shortcut
                ? $(this).addClass('selected-li')
                : $(this).removeClass('selected-li');
        });
    }else{
        //默认发送
        var liObj =  $(".fuceng").find('.fuceng-li').last();
        checkCookies() && Cookies.set(name, liObj.attr('type'));
        $(liObj).addClass('selected-li').prev().removeClass('selected-li');
    }
}

var _statePage = '' ;
var pageStateEventListener ;
// 判断页面是否激活
function eventPageState(callback){
    var hidden, state, visibilityChange;
    if (typeof document.hidden !== "undefined") {
        hidden = "hidden";
        visibilityChange = "visibilitychange";
        state = "visibilityState";
    } else if (typeof document.mozHidden !== "undefined") {
        hidden = "mozHidden";
        visibilityChange = "mozvisibilitychange";
        state = "mozVisibilityState";
    } else if (typeof document.msHidden !== "undefined") {
        hidden = "msHidden";
        visibilityChange = "msvisibilitychange";
        state = "msVisibilityState";
    } else if (typeof document.webkitHidden !== "undefined") {
        hidden = "webkitHidden";
        visibilityChange = "webkitvisibilitychange";
        state = "webkitVisibilityState";
    }

    // 添加监听器,监听当前是否活动页面
    pageStateEventListener && pageStateEventListener.removeEventListener();
    pageStateEventListener = document.addEventListener(visibilityChange, function(e) {
        _statePage = document[state] ;
        callback && callback(document[state]);
    }, false);
}

function hook(event, params, once,fun) {
    var formData = new FormData();
    formData.append('event', event);
    formData.append('params', params);
    formData.append('once', once);
    $.ajax({
        url: IM_config.hook,
        method: 'post',
        data: formData,
        dataType: 'json',
        processData: false,
        contentType: false,
        cache: false,
        // async: false,
        success: function (res) {
            console.log(event,res);
            if (typeof(fun) == 'function') fun(res);
        }
    });
}

function unread(type,params,fun){
    var formData = new FormData();
    formData.append('type', type);
    formData.append('params', params);
    $.ajax({
        url: IM_config.unread,
        method: 'post',
        data: formData,
        dataType: 'json',
        processData: false,
        contentType: false,
        cache: false,
        // async: false,
        success: function (res) {
            console.log(res);
            if (typeof(fun) == 'function') fun(res);
        }
    });
}

var callcustomer = function(){

    var message = '{"from_id":"' + youke_info.from_id + '","from_nick":"' + youke_info.from_nick + '","from_avatar":"' + youke_info.from_avatar + '","to_id":"' + kefu_info.to_id + '","to_nick":"' + kefu_info.to_nick + '","to_avatar":"' + kefu_info.to_avatar + '"}';

    //给管理员发消息
    hook('callcustomer',message,0,function (data) {

        //获取等待时间
        if (data[0].waits){
            var wait = data[0].waits;

            //延时发送等待消息
            setTimeout(function () {

                //发送等待消息
                hook('callcustomer_wait', message, 0,function (res){

                    //获取失败时间
                    if (res[0].fails){
                        var fails = res[0].fails;

                        //延时发送失败消息
                        setTimeout(function () {
                            hook('callcustomer_fails', message);
                        }, fails);
                    }
                });
            }, wait);
        }
    });
};

function timestamp(time){
    var date = time ? new Date(time * 1000) : new Date();
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';
    var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';
    var h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':';
    var m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()) + '';
    var s = date.getSeconds() < 10 ? '0'+date.getSeconds():date.getSeconds();
    // return Y+M+D+h+m+s;
    return M+D+h+m;
}
