
/**
 * 分页列表--刷新用户列表
 * @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);
            }
        }
    })
}

function GetRequest(url) {
    //var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

/**
 * 筛选-统一请求--刷新用户列表
 * @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) {
                $('button[closeRefresh]').attr('currentPage', 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);

                //设置服务日志
                var serviceLogHtml=buildServiceLog(data.detail.serviceLog);
                $('tbody[serveRecord]').html(serviceLogHtml);

                //设置参数
                $('#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"><div class="custom-control custom-switch"><input type="checkbox" class="custom-control-input" deadlineEver 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="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');
        }
    });

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


})