/**
 * Created by MyPC on 2019/11/11.
 */

var mobile2InfoTimeout = null;
var searchCuringTimeout = null;
var mobile = '';
var getClentListTimeout = null;
var clientname = '';
var columnLength = 10;



/*function fnSearchCuring(){
    searchCuringTimeout = null;
    if((/^1[34578]\d{9}$/.test(mobile))){
        $.ajax({
            cache :false,
            type: 'GET',
            url: '/cloud/curing/ajax/checkMobile/'+mobile,
            //data: { mobile: mobile },
            dataType: 'json',

            context: $('body'),
            success: function(data){
                if(data.status==1||data.status==2){
                    fnGetCuringDetail(data.data.id,data.data.client_id);
                }else if(data.status==3){
                    $('div[upCuringBox]').hide();
                    $('p[upCuringBox]').show();
                }
            },
            error: function(xhr, type){
                console.log('Ajax error!')
            }
        })
    }
}*/








//获得CLD客户list数据
function getClentList() {
    if (!isExistence(clientname)) {
        console.log('参数错误');
        return;
    }

    $.ajax({
        cache: false,
        type: 'GET',
        url: '/contacts/client/ajax/getClentList',
        data: { clientname: encodeURI(clientname) },
        dataType: 'json',
        async: false,
        success: function (data) {
            if (data.status == 1) {
                var html = buildClientBySearch(data.list);
                $('div[searchClient] curingClient').html(html);
            } else {
                console.log(data.msg);
            }
        },
        error: function (xhr, type) {
            console.log('Ajax error!')
        }
    })
}



$(document).ready(function () {
    init();
})


$(function () {


    //------用户列表界面相关----------
    /**
     * 绑定分页-点击事件-刷新用户列表和分页列表
     * parameter 传递参数
     * currentPage
     */
    $('div[pageDetail]').delegate('a', 'click', function () {
        // 只获取第一个选中的值
        var parameter = $(this).attr('parameter');
        var currentPage = $(this).attr('currentPage');

        $('button[closeRefresh]').attr('currentPage', currentPage);
        $('button[closeRefresh]').attr('parameter', parameter);

        refreshCuringPagination(parameter, currentPage);
    });

    /**
     * 编办下拉选择
     */
    $('select[curingByCompilation]').on('change', function (e) {
        var compilation = $(this).val();
        var latestCompilation = $('select[curingByLatestCompilation]').val();
        refreshCuringPage(compilation, latestCompilation);
    });
    $('select[curingByLatestCompilation]').on('change', function (e) {
        var latestCompilation = $(this).val();
        var compilation = $('select[curingByCompilation]').val();
        refreshCuringPage(compilation, latestCompilation);
    });

    /**
     * 用户列表排序相关 
     */
    $('input[type=radio][name=sortField]').on('change', function (e) {
        var sortField = $(this).attr('data');
        var latestCompilation = $('select[curingByLatestCompilation]').val();
        var compilation = $('select[curingByCompilation]').val();

        refreshCuringPage(compilation, latestCompilation, sortField);
    });
    $('input[type=radio][name=sort]').on('change', function (e) {
        var sort = $(this).attr('data');
        var latestCompilation = $('select[curingByLatestCompilation]').val();
        var compilation = $('select[curingByCompilation]').val();

        refreshCuringPage(compilation, latestCompilation, '', sort);
    });

    /**
     * 通行证账号检索养护用户--缓冲300
     */
    $('input[mobile2Info]').on('keyup', function (e) {
        if (mobile2InfoTimeout != null) {
            clearTimeout(mobile2InfoTimeout);
        }
        mobile = $(this).val();

        //if((/^1[34578]\d{9}$/.test(mobile))) {
        mobile2InfoTimeout = setTimeout(fnMobile2Info, 300);
        //}
    });


    //--------------用户详情界面相关---------
    /**
     * 动态绑定养护用户列表 打开用户详情页面事件 
     * ssoid
     * id
     * client_id
     */
    var coordinate = {};
    var ssoid = '', client_id = '';
    $('tbody[curingUser]').delegate('tr', 'mousedown', function (e) {
        coordinate.x = e.pageX;
        coordinate.y = e.pageY;
        ssoid = $(this).attr('data');
        client_id = $(this).attr('dataCid');
    }).click(function (e) {
        if (coordinate.x == e.pageX && coordinate.y == e.pageY) {
            $(this).attr('data-toggle', ' ');
            refreshCuringPageDetail(ssoid, client_id);
        }

    });



    //    $('tbody[curingUser]').delegate( 'tr','mouseup', function(){
    //        // 只获取第一个选中的值
    //        var ssoid = $(this).attr('data');
    //        var client_id=$(this).attr('dataCid')
    //        $(this).attr('data-toggle',' ');
    //        
    //        refreshCuringPageDetail(ssoid,client_id);
    //
    //    });


    /**
     * 确定升级专业版--用户详情界面
     */
    $('button[upCuringDo]').on('click', function (e) {
        // 只获取第一个选中的值
        var compilationId = $('#_id').val();
        var name = $('#name').val();
        var curingSsoid = $('#curingSsoid').val();
        var mobile = $('#curingMobile').val();
        var client_id = $('#client_id').val();
        var deadline1=$('#deadline').val();
        var smssend=$('input[smssend]').get(0).checked
        curingDo(compilationId,name,curingSsoid,1,mobile,client_id,deadline1,smssend);
    });

    /**
     * 确定降级免费版--用户详情界面
     */
    $('button[downCuringDo]').on('click', function (e) {
        // 只获取第一个选中的值
        var compilationId = $('#_id').val();
        var name = $('#name').val();
        var curingSsoid = $('#curingSsoid').val();
        var mobile = $('#curingMobile').val();
        var client_id = $('#client_id').val();
        var deadline1=$('#deadline').val();
        var smssend=$('input[downsmssend]').get(0).checked;
        curingDo(compilationId,name,curingSsoid,2,mobile,client_id,deadline1,smssend);
    });

    $('button[deadlineCuringDo]').on('click', function (e) {
        // 只获取第一个选中的值
        var compilationId = $('#_id').val();
        var name = $('#name').val();
        var curingSsoid = $('#curingSsoid').val();
        var mobile = $('#curingMobile').val();
        var client_id = $('#client_id').val();

        var deadline=$('#deadlineInput').val();
        var smssend=$('input[deadlineSmssend]').get(0).checked;
        curingDo(compilationId,name,curingSsoid,3,mobile,client_id,deadline,smssend);
    });
    

    /**
     * 用户绑定CLD客户--用户详情页
     */
    $('div[relevanceClientDo]').delegate('span', 'click', function () {
        // 只获取第一个选中的值
        var cidKey = $(this).attr('data');
        var ssoId = $('#curingSsoid').val();
        //var id=$('#id').val();

        $.ajax({
            cache: false,
            type: 'POST',
            url: '/cloud/curing/ajax/relevanceClientDo',
            data: { cidKey: encodeURI(cidKey), ssoId: encodeURI(ssoId) },
            dataType: 'json',
            success: function (data) {
                if (data.status == 1) {
                    refreshCuringPageDetail(ssoId, cidKey);
                    $('#linkcld').modal('hide');
                } else {
                    alert(data.msg);
                }
            },
            error: function (xhr, type) {
                console.log('Ajax error!')
            }
        })

    });

    /**
     * 用户取消关联CLD客户--用户详情页
     */
    $('button[relieveClientDo]').on('click', function (e) {
        // 只获取第一个选中的值
        var cid = $(this).attr('data');
        var ssoId = $('#curingSsoid').val();
        $('#unlinkcld').modal('hide');
        $.ajax({
            cache: false,
            type: 'POST',
            url: '/cloud/curing/ajax/relieveClientDo',
            data: { cid: encodeURI(cid), ssoId: encodeURI(ssoId) },
            dataType: 'json',
            success: function (data) {
                if (data.status == 1) {
                    refreshCuringPageDetail(ssoId, cid);

                } else {
                    alert(data.msg);
                }
            },
            error: function (xhr, type) {
                console.log('Ajax error!')
            }
        })

    });

    /**
     * 关闭详情页--刷新用户列表
     */
    $('button[closeRefresh]').on('click', function (e) {
        // 只获取第一个选中的值

        var parameter = $('button[closeRefresh]').attr('parameter');
        if (parameter != '') {
            var currentPage = $('button[closeRefresh]').attr('currentPage');
            refreshCuringPagination(parameter, currentPage);
        } else {
            var latestCompilation = $('select[curingByLatestCompilation]').val();
            var compilation = $('select[curingByCompilation]').val();
            refreshCuringPage(compilation, latestCompilation, '', '', mobile);

        }

    });

    //
    /**
     * 检索cld用户
     */
    $('input[getClentList]').on('keyup', function (e) {
        if (getClentListTimeout != null) {
            clearTimeout(getClentListTimeout);
        }
        clientname = $(this).val();
        if (isExistence(clientname)) {

            getClentListTimeout = setTimeout(getClentList, 500);
        }


    });








    //列表 号码检索事件绑定
    $('div[mobile2Info]').on('click', function (e) {
        mobile = $('input[mobile2Info]').val();
        if (mobile == '') {
            location.reload()
        }
        fnMobile2Info();
    });




    /*
          $('a[upNewClient]').on('click', function(e){
            $('input[fnsearchcuring]').val('');
            $('div[upCuringBox]').hide();
            $('p[upCuringBox]').hide();
    
        });
          $('input[fnSearchCuring]').on('keyup', function(e){
            if(searchCuringTimeout != null){
                clearTimeout(searchCuringTimeout);
            }
            mobile=$(this).val();
            if((/^1[34578]\d{9}$/.test(mobile))) {
                searchCuringTimeout = setTimeout(fnSearchCuring, 500);
            }
        });
        $('a[upClient]').on('click', function(e){
            var mobile=$('b[curingMobile]').html();
            if(mobile==''){
                alert('参数错误');
            }
            $.ajax({
                cache :false,
                type: 'GET',
                url: '/cloud/curing/ajax/checkMobile/'+mobile,
                //data: { mobile: mobile },
                dataType: 'json',
    
                success: function(data){
                    if(data.status==1||data.status==2){
                        $('input[fnsearchcuring]').val(mobile);
                        fnGetCuringDetail(data.data.id,data.data.client_id);
                    }else if(data.status==3){
                        $('div[upCuringBox]').hide();
                        $('p[upCuringBox]').show();
                    }
                },
                error: function(xhr, type){
                    console.log('Ajax error!')
                }
            })
        });*/








})






//构建升级用户信息--预计废弃
/*function fnGetCuringDetail(ssoid,client_id){

    $('tbody[upMajorBox]').html('');
    $('#curingSsoid').val('');
    $('#curingMobile').val('');
    $('#client_id').val('');
    $('#name').val('');
    $('#_id').val('');
    $.ajax({
        cache :false,
        type: 'GET',
        url: '/cloud/curing/ajax/getCuringBySsoid',
        data: {ssoid: ssoid, client_id: client_id},
        dataType: 'json',
        context: $('body'),
        success: function(data){
            if(data.status==1||data.status==2){
                $('div[upCuringBox]').show();
                $('p[upCuringBox]').hide();

                var objHtml=buildCuringInfo(data.detail.userInfo);
                $('div[curingInfo] curing').html(objHtml);

                var upMajorBoxHtml=buildCuringCompilation(data);
                $('tbody[upMajorBox]').html(upMajorBoxHtml);

                var operateLogHtml=buildCuringOperateLog(data.detail.operateLog);
                $('div[operateLog] curingOperate').html(operateLogHtml);

            }else if(data.status==3){
                $('div[upCuringBox]').hide();
                $('p[upCuringBox]').show();
            }
        },
        error: function(xhr, type){
            console.log('Ajax error!')
        }
    })
}*/

/**
 * 分页列表--刷新用户列表
 * @param parameter
 * @param currentPage
 * @returns
 */
function refreshCuringPagination(parameter, currentPage) {
    $.ajax({
        cache: false,
        type: 'get',
        url: '/cloud/curing/ajax/getCuringPage/' + currentPage + parameter,
        dataType: 'json',
        success: function (data) {
            if (data.status == 1) {
                fnStructureCuringUser(data.curingList);
                fnPage(data.pageData);
                refreshColumn(columnLength);
            } else {
                alert(data.msg);
            }
        }
    })
}

/**
 * 筛选-统一请求--刷新用户列表
 * @returns
 */
function refreshCuringPage(compilation, latestCompilation, sortField, sort, wd) {
    $.ajax({
        cache: false,
        type: 'get',
        url: '/cloud/curing/ajax/refreshCuringPage?sortField=' + sortField + '&sort=' + sort + '&compilation=' + compilation + '&latestCompilation=' + latestCompilation +
            '&wd=' + wd,
        dataType: 'json',
        success: function (data) {
            if (data.status == 1) {
                fnStructureCuringUser(data.curingList);
                fnPage(data.pageData);
                refreshColumn(columnLength);
            } else {
                alert(data.msg);
            }
        }
    })

}

/**
 * 通行证账号检索用户--刷新列表和分页
 * @returns
 */
function fnMobile2Info() {
    mobile2InfoTimeout = null;

    //if((/^1[34578]\d{9}$/.test(mobile))) {
    refreshCuringPage('', '', '', '', mobile);
    //}
}

/**
 * 刷新用户详情页
 * @param ssoid
 * @param client_id
 * @returns
 */
function refreshCuringPageDetail(ssoid, client_id) {
    $.ajax({
        cache: false,
        type: 'GET',
        url: '/cloud/curing/ajax/getCuringBySsoid',
        data: { ssoid: encodeURI(ssoid), client_id: encodeURI(client_id) },
        dataType: 'json',
        success: function (data) {
            if (data.status == 1 || data.status == 2) {
                //设置 养护用户信息
                var objHtml = buildCuringInfo(data.detail.userInfo);
                $('div[curingInfo] curing').html(objHtml);

                $('div[curingInfo] curingClient').html('');
                //设置CLD客户信息
                //if(data.detail.curingInfo.updateTotal!=0){
                //$("#id").val(id);
                var clientHtml = relevanceClient(data.detail.clientInfo);
                $('div[curingInfo] curingClient').html(clientHtml);
                //}

                //设置养护详情页里产品升级信息
                var upMajorBoxHtml = buildCuringCompilation(data);
                $('tbody[upMajorBox]').html(upMajorBoxHtml);

                //更新在线时长
                var onlineTimesHtml = buildCuringOnlineTimes(data.detail.userInfo.online_list);
                $('tbody[onlineTimes]').html(onlineTimesHtml);

                //设置参数
                $('#curingSsoid').val(data.detail.userInfo.ssoIdKey);
                $('#curingMobile').val(data.detail.userInfo.mobile);
                $('#client_id').val(data.detail.clientInfo.cidKey);

                //设置养护日志
                var operateLogHtml = buildCuringOperateLog(data.detail.operateLog);
                $('div[operateLog] curingOperate').html(operateLogHtml);

                setTimeout(autoHeight, 400);

                //autoHeight();
                $('#detail').modal('show');
            } else if (data.status == 3) {
                alert(data.msg);
            }
        },
        error: function (xhr, type) {
            console.log('Ajax error!')
        }
    })
}

/**
 * DOM加载完毕后初始化
 * @returns
 */
function init() {
    refreshColumn(columnLength);
}

/**
 * 用户升级专业版 弹框提示--用户详情界面
 * @param name
 * @param _id
 * @param username
 * @returns
 */
function upCuring(name, _id, username) {
    $('#name').val(name);
    $('#_id').val(_id);
    var curingMobile = $('#curingMobile').val();
    var html = '<p>为用户 <b>' + curingMobile + ',' + username + '</b> 升级 <b>' + name + '</b></p>';
    html += '<div class="form-group"><div class="custom-control custom-switch"><input type="checkbox" class="custom-control-input" deadline id="customSwitch2" ><label class="custom-control-label" deadlineLabel for="customSwitch2">限期</label></div></div>';
    
    html += '<div class="form-group"><label class="form-label" for="example-date">到期时间</label><input class="form-control" id="deadline" type="date"  name="deadline" disabled  value=""></div>';
    
    html += '<div class="form-group"><div class="custom-control custom-switch"><input type="checkbox" class="custom-control-input" smssend id="customSwitch22" ><label class="custom-control-label" for="customSwitch22">给客户发送短信通知</label></div></div>';

    $('div[upCuring] span').html(html);
}

/**
 * 养护产品降级
 * @param {} name 
 * @param {*} _id 
 * @param {*} username 
 */
function downCuring(name, _id, username) {
    $('#name').val(name);
    $('#_id').val(_id);
    var curingMobile = $('#curingMobile').val();
    // let html = `<p>为用户 <b>${curingMobile},${username}</b> <span class="text-danger">降级</span> <b>${name}免费版</b></p>
    // <div class="form-group">
    //     <div class="custom-control custom-switch">
    //         <input type="checkbox" class="custom-control-input" downSmssend id="customSwitch23" >
    //         <label class="custom-control-label"  for="customSwitch23">给客户发送短信通知</label>
    //     </div>
    // </div>`;
    var html = '<p>为用户 <b>'+curingMobile+','+username+'</b> <span class="text-danger">降级</span> <b>'+name+'免费版</b></p><div class="form-group"><div class="custom-control custom-switch"><input type="checkbox" class="custom-control-input" downSmssend id="customSwitch23" ><label class="custom-control-label"  for="customSwitch23">给客户发送短信通知</label></div></div>';
    
    $('div[downCuring] span').html(html);
}

function deadlineCuring(name, _id, username,deadline){
    $('#name').val(name);
    $('#_id').val(_id);
    var curingMobile = $('#curingMobile').val();

    var html = '<p>为用户 <b>'+curingMobile+','+username+'</b> <b>'+name+'</b> 设置延期</p><p>当前限期为：'+deadline+'</p>';

    html +='<div class="form-group"><label class="form-label" for="example-date">到期时间</label><input class="form-control"  id="deadlineInput" type="date" name="date" value=""></div><div class="form-group"><div class="custom-control custom-switch"><input type="checkbox" class="custom-control-input" deadlineSmssend id="customSwitch222" ><label class="custom-control-label" for="customSwitch222">给客户发送短信通知</label></div></div>';
    

    // let html = `<p>为用户 <b>${curingMobile},${username}</b> <b>${name}</b> 设置延期</p>
    // <p>当前限期为：${deadline}</p>
    // <div class="form-group">
    //     <label class="form-label" for="example-date">到期时间</label>
    //     <input class="form-control"  id="deadlineInput" type="date" name="date" value="">
    // </div>
    // <div class="form-group">
    //     <div class="custom-control custom-switch">
    //         <input type="checkbox" class="custom-control-input" deadlineSmssend id="customSwitch222" >
    //         <label class="custom-control-label" for="customSwitch222">给客户发送短信通知</label>
    //     </div>
    // </div>`;

    $('div[deadlineCuring] span').html(html);
}

function curingDo(compilationId, name, curingSsoid, status, mobile, client_id, deadline, smssend) {
    if (curingSsoid != '' && compilationId != '') {
        $.ajax({
            cache: false,
            type: 'post',
            url: '/cloud/curing/ajax/upCuringDo',
            data: { ssoid: curingSsoid, compilationId: compilationId, mobile: mobile, client_id: client_id, name: name, deadline: deadline, smssend: smssend, status: status },
            dataType: 'json',
            success: function (data) {
                if (data.status == 1) {
                    refreshCuringPageDetail(curingSsoid, client_id);
                } else {
                    alert(data.msg);
                }
            }
        })
    } else {
        alert('请选择需要升级的版本');
    }
}

$(function () {

    $('div[upcuring]').delegate('label[deadlineLabel]', 'click', function () {
        $('#deadline').attr('disabled', 'disabled');
        
        if($('input[deadline]').prop('checked')){
            $('input[deadline]').prop("checked",false);
        }else{
            $('input[deadline]').prop("checked",true);
        }
        if ($('input[deadline]').get(0).checked) {
            $('#deadline').removeAttr('disabled');
        }
    });

})
/**
 * Created by cp on 2019/12/17.
 */

/**
 * 用户列表--首页分页列表刷新
 * @param data
 * @returns
 */
function fnStructureCuringUser(data){
    var html='';
    $.each(data, function(k, v) {
        html+='<tr class="pointer-hover" curingInfoEdi dataCid="'+setUndefined(v.client_id)+'" data='+setUndefined(v.ssoId)+' dataId="'+setUndefined(v._id)+'" data-toggle="modal" data-target="">';
        html+='<td>'+setUndefined(v.mobile)+'</td>';
        html+='<td>'+setUndefined(v.username)+'</td>';
        html+='<td>'+setUndefined(v.real_name)+'</td>';
        html+='<td>'+setUndefined(v.qq)+'</td>';
        html+='<td>'+setUndefined(v.company)+'</td>';
        html+='<td>'+setUndefined(v.clientInfo.clientname)+'</td>';
        html+='<td>'+setUndefined(v.clientInfo.companyname)+'</td>';
        
        html+='<td>'+setUndefined(v.latest_login)+'</td>';
        html+='<td>'+setUndefined(v.latest_usedName)+'</td>';
        html+='<td>'+setUndefined(v.online_newest)+'</td>';
        html+='<td>'+setUndefined(v.addtime)+'</td>';
        html+='<td>'+setUndefined(v.upgradeListName)+'</td>';
        
        html+='</tr>';
    });
    $('tbody[curingUser]').html(html);
    $('div[curingListBox]').show();
    $('p[upClientBox]').hide();
}


/**
 * 用户详情页 养护信息更新
 * @param data
 * @returns
 */
function buildCuringInfo(data){

    var html=' ' +
        ' <div class="form-header">' +
        ' <div class="d-flex justify-content-between"><span class="text-muted">养护用户</span></div> ' ;

    if(data.mobile==''){
        html+='<legend>'+setUndefined(data.email)+'</legend> ';
    }else{
        html+='<legend>'+setUndefined(data.mobile)+'</legend> ';
    }

    html+='</div><div class="cldform clearfix"> <div class="cldform-line"></div> <div class="cldform-item  cldform-item-half pr-3"> ' +
        '<div class="cldform-label">手机</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox cldform-contarl-blank">' +
        '<span>'+setUndefined(data.mobile)+'</span></div> </div> </div> <div class="cldform-item cldform-item-half pl-3"> ' +
        '<div class="cldform-label">邮箱</div> ' +
        '<div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox cldform-contarl-blank">' +
        '<span>'+setUndefined(data.email)+'</span></div></div>';

    html+='</div> <div class="cldform-line"></div> <div class="cldform-item cldform-item-half pr-3"> ' +
        '<div class="cldform-label">称呼</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox cldform-contarl-blank"> ' +
        '<span>'+setUndefined(data.username)+'</span></div> </div> </div>';
        html+='<div class="cldform-item cldform-item-half pl-3">'+
        '<div class="cldform-label">QQ</div>'+
        '<div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox cldform-contarl-blank">'+
        '<span>'+setUndefined(data.qq)+
        	'</span></div></div></div>';
    
    
    
            html+='<div class="cldform-line"></div> <div class="cldform-item cldform-item-half pr-3">'+
            '<div class="cldform-label">真实姓名</div>'+
            '<div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox cldform-contarl-blank"> '+
            '<span>'+setUndefined(data.real_name)+
                '</span></div></div></div>';
        html+='<div class="cldform-item cldform-item-half pl-3"> ' +
        '<div class="cldform-label">注册云版</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox cldform-contarl-blank">' +
        '<span>'+setUndefined(data.create_time)+'</span></div> </div> </div>';

    html+='<div class="cldform-line"></div> <div class="cldform-item cldform-item-half pr-3"> ' +
        '<div class="cldform-label">企业名称</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox cldform-contarl-blank">' +
        '<span>'+setUndefined(data.company)+'</span></div> </div> </div>';
    html+='<div class="cldform-item cldform-item-half pl-3"> ' +
        '<div class="cldform-label">企业地区</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox cldform-contarl-blank">' +
        '<span>'+setUndefined(data.province)+'</span></div> </div> </div> ';

    html+='<div class="cldform-line"></div><div class="cldform-item cldform-item-half pr-3">'+
        '<div class="cldform-label">最近使用</div><div class="cldform-control"><div class="cldform-contarl-box cldform-contarl-textareabox cldform-contarl-blank">'+
            '<span>'+setUndefined(data.latest_usedName)+'</span>'+
            '</div></div></div>';
    html+='<div class="cldform-item cldform-item-half pl-3">'+
        '<div class="cldform-label">累计时长</div><div class="cldform-control"><div class="cldform-contarl-box cldform-contarl-textareabox cldform-contarl-blank">'+
            '<span>'+setUndefined(data.online_times)+'</span>'+
            '</div></div></div>'+     
            '</div>';
    
    return html;
}

/**
 * 关联CLD客户--检索CLD客户后的结果列表
 * @param data
 * @returns
 */
function buildClientBySearch(data){
    var html='';
    $.each(data, function(k, v) {
        html+='<span data="'+ v.cidKey+'" ><div   class="card pointer-hover p-3 mb-3" data-toggle="tooltip" data-placement="bottom" title="点击关联该客户" data-original-title="点击关联该客户"> <div class="row">' +
            '<div class="col-1 pr-0">'+ v.clientname+'</div>' +
            '<div class="col-2 pr-0">'+ v.position+'</div>' +
            '<div class="col-2 pr-0">'+ v.telephone+'</div>' +
            '<div class="col-4 pr-0">'+ v.companyname+'</div></div> </div></span>';
    });
    return html;
}


//构建养护升级产品信息
function buildCuringCompilation(data){
    var upMajorBoxHtml='';
    $.each(data.detail.compilationList, function(k, v) {
        upMajorBoxHtml+='<tr>';
        upMajorBoxHtml+='<td></td>';
        upMajorBoxHtml+='<td>'+ v.name+'</td>';
        upMajorBoxHtml+='<td>';
        
        var deadline='';
        data.detail.userInfo.upgrade_list.forEach(element => {
            if(v._id===element.compilationID){
                deadline=element.deadline;
            }
        });

        if(v.isUpgrade !== undefined && v.isUpgrade === true){
            upMajorBoxHtml+='已升级<a href="#degrade-confirm" onclick=downCuring("'+ v.name+'","'+ v._id+'","'+data.detail.userInfo.username+'") class="btn btn-xs btn-outline-danger ml-2 waves-effect waves-themed" data-toggle="modal" data-target="#degrade-confirm">降级</a>';
        }else{
            upMajorBoxHtml+='<a onclick=upCuring("'+ v.name+'","'+ v._id+'","'+data.detail.userInfo.username+'")  ' +
                'href="#update-confirm"  class="btn btn-xs btn-outline-primary waves-effect waves-themed" data-toggle="modal" data-target="#update-confirm">升级</a>';
        }
        upMajorBoxHtml+='</td>';

        if(v.isUpgrade !== undefined && v.isUpgrade === true){
            upMajorBoxHtml+='<td>'+deadline+'<a href="#deadline-confirm" onclick=deadlineCuring("'+ v.name+'","'+ v._id+'","'+data.detail.userInfo.username+'","'+deadline+'") class="btn btn-xs btn-outline-primary ml-2 waves-effect waves-themed" data-toggle="modal" data-target="#deadline-confirm">限期</a></td>';
            upMajorBoxHtml+='</tr>';
        }else{
            upMajorBoxHtml+=`<td></td>`;
        }

    });
    
    return upMajorBoxHtml;
}

function buildCuringOnlineTimes(data){
	var onlineTimesHtml='';
	var i=1;
	$.each(data, function(k, v) {
		onlineTimesHtml+='<tr><td width="20">'+i+'</td><td width="140">'+v.dateString+'</td><td>'+v.online_times+'</td></tr>';
		i++;
	});
	return onlineTimesHtml;
}

//关联CLD客户
function relevanceClient(data){
    var html='';
    if(isExistence(data)){
        html=buildClient(data);
    }else{
        html='<div class="form-header mt-3"> <div class="d-flex justify-content-between"><span class="text-muted">CLD客户</span><span class="text-muted"></span></div> <div class="text-center">' +
            '<a href="#linkcld" class="btn btn-sm btn-outline-primary" data-toggle="modal" data-target="#linkcld"><i class="far fa-link"></i> 关联CLD客户</a></div> </div>';
    }
    return html;
}

/**
 * 构建操作日志
 * @param data
 * @returns
 */
function buildCuringOperateLog(data){

    var html='';
    if(isExistence(data)){
        $.each(data, function(k, v) {
            html+='<p>' +
                '<a href="#">@'+ v.username+'</a>' +
                ' '+ v.operation+'<br>' +
                '<span class="text-muted">'+ v.createDate+'</span></p></p> ';
        });
    }else{
        html='';
    }
    html+='';
    return html;
}

function autoHeight(){
    var html='<script type="text/javascript">autoFlashHeight();</script> <script type="text/javascript">$(".slimScroll1").slimScroll({position: \'right\', height:\'auto\' , railVisible: true}); </script> <script type="text/javascript">        $(".slimScrol2").slimScroll({            position: \'right\',            height:\'auto\' ,            railVisible: true        });</script>    <script type="text/javascript">        $(".slimScrol3").slimScroll({            position: \'right\',            height:\'auto\' ,            railVisible: true});</script> <script type="text/javascript"> $(".slimScrol4").slimScroll({position: \'right\', height:\'auto\' , railVisible: true});</script>';
    $('div[autoHeight]').html(html);

}

//构建养护用户分页
function fnPage(data){
    var html='<ul  class="pagination pagination-sm">';
    if( Number(data.previousPage) === Number(0)) {
        html+='<li class="page-item disabled"><span class="page-link" aria-label="Previous"> <span aria-hidden="true"><i class="fal fa-chevron-left"></i></span> </span> </li>';
    }else{
        html+='<li class="page-item "> <a' +
            ' parameter="'+ data.parameter+'" currentPage="'+ data.previousPage +'" class="page-link" href="javascript:void(0)" aria-label="Previous"> <span aria-hidden="true"><i class="fal fa-chevron-left"></i></span> </a> </li>';
    }

    if( Number(data.currentPage)>=Number(data.pageWidth) ) {
    	html+='<li class="page-item">'+
    		'<a parameter="'+data.parameter+'" currentPage="1" class="page-link" href="javascript:void(0)">1</a></li><li class="page-item "><span ><span aria-hidden="true">...</span></span></li>';
    }
    
    for (var i = data.startPage; i <= data.endPage; i++) {
        if(Number(data.currentPage) === Number(i)){
            html+='<li class="page-item active" aria-current="page"> <span class="page-link"> ' +
                ''+ i +'<span class="sr-only">(current)</span> </span> </li>';
        }else{
            html+='<li class="page-item">' +
                ' <a parameter="'+data.parameter+'" currentPage="'+i+'" class="page-link" href="javascript:void(0)">' +
                ''+i+'</a> ' +
                '</li>';
        }
    }

    if( Number(data.totalPage) >Number(data.pageWidth) && Number(data.totalPage)!== Number(data.currentPage) ) {
    	html+='<li class="page-item "><span><span aria-hidden="true">...</span></span></li>';
    }
    
    
    if( Number(data.nextPage) === Number(data.currentPage)||Number(data.totalPage)===Number(0)) {
        html+='<li class="page-item disabled"> <span class="page-link"  aria-label="Next"> <span aria-hidden="true"><i class="fal fa-chevron-right"></i></span> </span> </li>';
    }else{
        html+='<li class="page-item"> <a ' +
            'parameter="'+ data.parameter +'" currentPage="'+ data.nextPage +'" class="page-link" href="#" aria-label="Next"  > <span aria-hidden="true"><i class="fal fa-chevron-right"></i></span> </a> </li>';
    }
    html+='</ul>';
    
    
    $('div[pageDetail]').html(html);
    $('div[page]').show();
}
/**
 * Created by MyPC on 2019/12/16.
 */
function setUndefined(data){
    if(data===null||typeof(data) == "undefined"||data.length==0){
        return '';
    }else{
        return data;
    }
}

//
function isExistence(data){
    if(Array.isArray(data)){
        if(data.length==0){
            return false;
        }else{
            return true;
        }
    }else if(data==''||data===null||typeof(data) == 'undefined'||data=='undefined'){
        return false;
    }else{
        return true;
    }
}
$(function() {

	/**
	 * change事件-隐藏-显示数据列
	 */
	$('input[type=checkbox][name=columnShow]').on('change', function(e) {
		var column = $(this).attr('column');
		var isAccept = $(this).prop("checked");
		
		isColumnShow(isAccept,column);
		
		localStorage.setItem('curingColumn_'+column, isAccept);
	});

})

/**
 * 初始化数据列--是否展示
 * @param columnLength
 * @returns
 */
function refreshColumn(columnLength){
	var checkBoxList=$('input[type=checkbox][name=columnShow]');
	var cbi=0;
	
	for(var i=1;i<=columnLength;i++){
		var isAccept=localStorage.getItem("curingColumn_"+i);

		if(isAccept=='false'){
			isAccept=false;
		}
		
		if (isAccept==null||isAccept) {
			//console.log(isAccept);
			checkBoxList.eq(cbi).attr("checked",'true');
		}else{
			//console.log(isAccept+'dddd');
			checkBoxList.eq(cbi).removeAttr("checked");
		}
		
		isColumnShow(isAccept,i);
		cbi++;
	}
}

function isColumnShow(isAccept,column){

	if (isAccept==null||isAccept) {
		//console.log(isAccept==null||isAccept);
		$('table[columnShow] tr').find('th:eq('+column+')').show();
		$('table[columnShow] tr').find('td:eq('+column+')').show();
	} else {
		//console.log(column);
		$('table[columnShow] tr').find('th:eq('+column+')').hide();
		$('table[columnShow] tr').find('td:eq('+column+')').hide();
	}
}
/**
 * Created by MyPC on 2019/12/17.
 */

//构建地区HTML
function buildArea(data){
    //data-toggle="dropdown"
    var html='<div class="cldform-contarl-box cldform-contarl-textareabox" >' +
        '<span class="cldform-contarl-dropdown">'+setUndefined(data.local)+'<i class="far fa-map-marker-alt mt-1"></i></span></div>' +
        '<div class="dropdown-menu" style="width:300px"> ' +
        '<ul class="nav nav-tabs nav-tabs-clean justify-content-center" role="tablist"> ' +
        '<li class="nav-item"><a class="nav-link text-center active" data-toggle="tab" href="#tab-province" role="tab" aria-selected="true">省份</a></li> ' +
        '<li class="nav-item"><a class="nav-link" data-toggle="tab" href="#tab-city" role="tab" aria-selected="false">市区</a></li> ' +
        '<li class="nav-item"><a class="nav-link" data-toggle="tab" href="#tab-area" role="tab" aria-selected="false">县区</a></li> ' +
        '</ul>';

    html+='<div class="tab-content p-3"> <div class="tab-pane city-item fade active show" id="tab-province" role="tabpanel" aria-labelledby="tab-province">';

    html+='<button type="button" class="btn btn-xs btn-outline-default">黑龙江省</button>';

    html+='</div>';


    html+='<div class="tab-pane city-item fade" id="tab-city" role="tabpanel" aria-labelledby="tab-city">';
    html+='<button type="button" class="btn btn-xs btn-outline-default">广州市</button>';
    html+='</div>';

    html+='<div class="tab-pane city-item fade" id="tab-area" role="tabpanel" aria-labelledby="tab-area">';

    html+='<button type="button" class="btn btn-xs btn-primary">香洲区</button>';

    html+='</div> </div> </div>';

    return html;
}

//构建客户HTML
function buildClient(data){
    var html='';
    html+='<div class="form-header mt-3">';
    html+='<div class="d-flex justify-content-between"><span class="text-muted">CLD客户</span>' +
        '<span class="text-muted">'+setUndefined(data.staff.username)+' 创建于 '+setUndefined(data.createTime)+'</span></div>';
    html+='<legend>'+setUndefined(data.clientname)+'</legend> ' +
        '</div>';

    html+='<div class="cldform clearfix">' +
        '<div class="cldform-line"></div> <div class="cldform-item"> ' +
        '<div class="cldform-label">姓名</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox">' +
        ' <span>'+setUndefined(data.clientname)+'</span></div> </div> </div>';

    //地区
    var areaHtml=buildArea(data);
    html+='<div class="cldform-line"></div> <div class="cldform-item cldform-item-wauto"> <div class="cldform-label">客户地区</div>';
    html+='<div class="cldform-control">';

    html+=areaHtml;

    html+='</div> </div>';

    if(isExistence(data.companyname)){
        /*
         '<a href="javascript:void(0);" data-original-title="移除单位" class="btn btn-outline-danger btn-xs btn-icon mr-1" data-toggle="modal" data-target="#remover-com"><i class="far fa-unlink" data-toggle="tooltip" data-placement="left" ></i></a>' +
         '<a href="javascript:void(0);" data-original-title="更换单位" class="btn btn-outline-primary btn-xs btn-icon" data-toggle="modal" data-target="#link-com"><i class="far fa-retweet" data-toggle="tooltip" data-placement="left" ></i></a>' +
        * */

        //单位
        html+='<div class="cldform-line"></div> <div class="cldform-item">';
        html+='<div class="cldform-label">单位名称</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox"><div class="card p-2 text-primary" href="#"><div class="d-flex justify-content-between">' +
            '<span>'+setUndefined(data.companyname)+'</span><span>' +

            '</span></div></div></div> </div> </div>';
    }else{
        html+='<div class="cldform-line"></div> <div class="cldform-item"> <div class="cldform-label">单位名称</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox"><span>' +
            '<a href="#link-com" data-toggle="modal" data-target="#link-com"><i class="far fa-link"></i> 关联单位</a></span></div> </div> </div>';
    }





    html+='<div class="cldform-line"></div> ' +
        '<div class="cldform-item cldform-item-half pr-3"> ' +
        '<div class="cldform-label">性别</div> ' +
        '<div class="cldform-control"> ' +
        '<div class="cldform-contarl-box cldform-contarl-textareabox" data-toggle="dropdown">' +
        '<span class="cldform-contarl-dropdown">'+setUndefined(data.gender)+'</span></div> </div> </div>';

    html+='<div class="cldform-item cldform-item-half pl-3">' +
        '<div class="cldform-label">昵称</div> ' +
        '<div class="cldform-control"> ' +
        '<div class="cldform-contarl-box cldform-contarl-textareabox">' +
        '<span>'+setUndefined(data.nicename)+'</span></div> </div> </div>' +
        '' +
        '<div class="cldform-line"></div>' +
        '<div class="cldform-item cldform-item-half pr-3"> ' +
        '<div class="cldform-label">手机</div> ' +
        '<div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox">' +
        '<span>'+setUndefined(data.telephone)+'</span></div> </div> </div> ' +

        '<div class="cldform-item cldform-item-half pl-3"> ' +
        '<div class="cldform-label">QQ</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox">' +
        '<span>'+setUndefined(data.qq)+'</span></div> </div> </div> ' +
        '' +
        '<div class="cldform-line"></div> <div class="cldform-item cldform-item-half pr-3"> ' +
        '<div class="cldform-label">电话</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox">' +
        '<span>'+setUndefined(data.phone)+'</span></div> </div> </div> <div class="cldform-item cldform-item-half pl-3"> ' +

        '<div class="cldform-label">邮箱</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox">' +
        '<span>'+setUndefined(data.email)+'</span></div> </div> </div> ' +
        '' +
        '<div class="cldform-line"></div> <div class="cldform-item cldform-item-half pr-3"> ' +
        '<div class="cldform-label">传真</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox">' +
        '<span>'+setUndefined(data.fax)+'</span></div> </div> </div> ' +
        '' +
        '<div class="cldform-line"></div> <div class="cldform-item cldform-item-half pr-3"> ' +
        '<div class="cldform-label">部门/职称</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox text-muted">' +
        '<span>'+setUndefined(data.position)+'</span></div> </div> </div> <div class="cldform-item cldform-item-half pl-3"> ' +

        '<div class="cldform-label">办公室</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox">' +
        '<span>'+setUndefined(data.office)+'</span></div> </div> </div> ' +
        '' +
        '<div class="cldform-line"></div> <div class="cldform-item">' +
        '<div class="cldform-label">客户地址</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox">' +
        '<span>'+setUndefined(data.address)+'</span></div> </div> </div> ' +
        '' +
        '<div class="cldform-line"></div> <div class="cldform-item"> ' +
        '<div class="cldform-label">客户乘车</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox text-muted">' +
        '<span>'+setUndefined(data.ride)+'</span></div> </div> </div> ' +
        '' +
        '<div class="cldform-line"></div> <div class="cldform-item"> ' +
        '<div class="cldform-label">客户地标</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox text-muted">' +
        '<span>'+setUndefined(data.landmarks)+'</span></div></div> </div> ' +
        '' +
        '<div class="cldform-line"></div> <div class="cldform-item">' +
        '<div class="cldform-label">客户住宿</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox text-muted">' +
        '<span>'+setUndefined(data.stay)+'</span></div> </div> </div> ' +
        '' +
        '<div class="cldform-line"></div> <div class="cldform-item"> ' +
        '<div class="cldform-label">备注</div> <div class="cldform-control"> <div class="cldform-contarl-box cldform-contarl-textareabox text-muted">' +
        '<span>'+setUndefined(data.mark)+'&nbsp;</span></div> </div> </div>';

    html+='<div class="cldform-line"></div> <div class="cldform-item text-center">' +
        '<a data="'+setUndefined(data.cid)+'" href="javascript:void(0)" class="btn btn-sm btn-outline-danger" data-toggle="modal" data-target="#unlinkcld"><i class="far fa-unlink"></i> 移除CLD客户</a> ' +
        '</div>';

    html+='';

    html+='</div>' +
        '<script type="text/javascript">autoFlashHeight();</script> <script type="text/javascript">$(".slimScroll1").slimScroll({position: \'right\', height:\'auto\' , railVisible: true}); </script> <script type="text/javascript">        $(".slimScrol2").slimScroll({            position: \'right\',            height:\'auto\' ,            railVisible: true        });</script>    <script type="text/javascript">        $(".slimScrol3").slimScroll({            position: \'right\',            height:\'auto\' ,            railVisible: true});</script> <script type="text/javascript"> $(".slimScrol4").slimScroll({position: \'right\', height:\'auto\' , railVisible: true});</script>' +
    '';

    return html;
}

