/**
 * 消息管理相关js
 *
 * @author CaiAoLin
 * @date 2017/9/21
 * @version
 */
$(document).ready(function() {
    // 选择框
    $(".selector > li > a").click(function() {
        let value = $(this).data("value");
        let string = $(this).text();
        let selector = $(this).parent().parent();
        selector.next("input:hidden").val(value);
        selector.prev("button").children("lable").text(string);
        $('#searchUser').submit();
        //selector.prev("button").html(string + ' <span class="caret"></span>');
    });
    $("#deleteConfirm").click(async function () {
        let userID = $("#userID").val();
        let delCount = parseInt($("#delCount").val());
        delCount = delCount+1;
        $("#delCount").val(delCount);
        if(delCount == 3){//连续点3次才做真删除
            if(userID!=""){
                try {
                    $.bootstrapLoading.start();
                   let  result  = await ajaxPost("/user/deleteUser",{userID:userID});
                    $.bootstrapLoading.end();
                   if(result == "success"){
                       window.location.reload();
                   }else {
                       alert("删除失败！");
                       $("#delCount").val(0);
                   }
                }catch (err){
                    $.bootstrapLoading.end();
                    $("#delCount").val(0);
                }
            }
        }
    });
});
let cacheUser = null;

async function getOnlineInfo(filter) {
    try {
        let htmlString = `<tbody><tr><th>日期/时间</th><th>在线时长</th></tr>`;
        let info = await ajaxPost("/user/getOnlineInfo",JSON.parse(filter));
        for(let i of info){
            htmlString += `<tr><td>${i.dateString}</td><td>${i.online_times}</td></tr>`
        }
        htmlString +="<tbody>";
        $("#time-detail-table").html(htmlString);
    }catch (e){
        console.log(e)
    }
}

async function getUserInfo(ID) {
    let user = await ajaxPost("/user/findByID",{ID:ID});
    let infoString = `<tr><th>注册时间</th><td>${user.create_time}</td><th>最近登录</th><td>${user.last_login}</td></tr>
                      <tr><th>手机</th><td >${user.mobile}</td><th>邮箱</th><td>${user.email}</td></tr>
                      <tr><th>姓名</th><td colspan="3" id>${user.real_name}</td></tr>
                      <tr><th>企业名称</th><td colspan="3">${user.company}</td></tr>
                      <tr><th>企业地区</th><td>${user.province}</td><th>企业类型</th><td>${user.company_type}</td></tr>
                      <tr><th>企业规模</th><td colspan="3">${user.company_scale}</td></tr>`;
    $('#userInfoTable').html(infoString);
}

function deleteUser(userID) {
    $("#delCount").val(0);
    $("#userID").val(userID);
}



async function getUserUpgradeInfo(ID){
    try {
        cacheUser  = await ajaxPost("/user/findByID",{ID:ID});
        refreshUpgradeTable(cacheUser);
    }catch (err){
        console.log(err);
    }

}

function refreshUpgradeTable(user) {
    let compilationTable = ' <tr><th colspan="2">专业版升级</th></tr>';
    let test = true;
    for(let c of compilationList){
        compilationTable += `<tr><th>${c.name}</th>
                                <td>${getButtonHtml(c._id)}
                                 </td> 
                             </tr>`;
    }
    $('#upgrade_table').html(compilationTable);

    function getButtonHtml(ID) {
        let updateString = `<div class="btn-group"><button class="btn btn-success  disabled" disabled="disabled">已启用</button><button class="btn btn-default" title="关闭" onclick="updateUser('${ID}','close')">关闭</button></div>`;
        let closeString = `<div class="btn-group"> <button class="btn btn-default" title="启用" onclick="updateUser('${ID}','upgrade')">启用</button> <button class="btn btn-danger disabled" disabled="disabled">已关闭</button> </div>`;
        let upgradeInfo = _.find(user.upgrade_list,{'compilationID':ID});
        if(upgradeInfo){
            if(upgradeInfo.isUpgrade == true){
                return updateString + upgradeInfo.remark;
            }else {
                return closeString + upgradeInfo.remark;
            }
        }else {
            return closeString;
        }
    }
}


async function updateUser(compilationID,type) {
    if(cacheUser){
        let upgrade_list = cacheUser.upgrade_list?cacheUser.upgrade_list:[];
        let upgradeInfo = _.find(upgrade_list,{'compilationID':compilationID});
        if(!upgradeInfo){
            upgradeInfo = {
                compilationID:compilationID,//编办ID
                upgrade_time:new Date().getTime(),
                isUpgrade:true,
            };
            upgrade_list.push(upgradeInfo);
        }
        if(type == 'upgrade'){
            upgradeInfo.isUpgrade = true;
            upgradeInfo.remark = adminName + " "+ moment().format("YYYY-MM-DD") +" 启用";
        }else {
            upgradeInfo.isUpgrade = false;
            upgradeInfo.remark = adminName + " "+ moment().format("YYYY-MM-DD") +" 关闭";
        }
        try {
            await ajaxPost("/user/updateUser",{ID:cacheUser._id,updateData:{upgrade_list:upgrade_list}});
            cacheUser.upgrade_list = upgrade_list;
            refreshUpgradeTable(cacheUser);
        }catch (err){
            console.log(err);
        }

    }

}
