var cwtable = "cwtable";// 保证金提取 表格id
var hytable = "hytable";// 会员 表格id
var yjtable = "yjtable";//押金提取表格id
var tqjltable = "tqjltable";//提取记录
var modalId = "modelPage";// 模态框id
$(function () {
    var tablid = $(".nav-tabs .tabstiltlefont").eq(0).attr("id");
    // 初始化列表
    initTab(hytable);
    $(".bjlist-search .swtfId,.bjlist-search .sfgsId").change(function () {
        reloadTabsData();//刷新选项卡数据
    });
    if ($("#hytable").length != 0) initTab(hytable);
    if ($("#cwtable").length != 0) initTab(cwtable);
    if ($("#yjtable").length != 0) initTab(yjtable);
    if ($("#yjtable").length != 0) initTab(tqjltable);
    $(".nav-tabs .tabstiltlefont").eq(0).click();//选中第一个选项卡
    // 模态框 关闭时  里面的 清除内容 (解决模态框只请求一次服务器页面，内容不好刷新)
    Common.modalHideDelHtml(modalId);
});


/**
 * 表格初始化
 */
function initTab(initTabId) {
    url = "/grab/list";
    var column = [
        {
            "data": "fgsmc", "title": "分公司", "sWidth": "5%", "className": "text-sxcenter"
        },
        {"data": "createName", "title": "所属录入员", "sWidth": "5%", "className": "text-sxcenter"},
        {"data": "account", "title": "登录账号", "sWidth": "5%", "className": "text-sxcenter"},
        {"data": "password", "title": "登录密码", "sWidth": "8%", "className": "text-sxcenter"},
        {
            "data": "logIn", "title": "cookie", "sWidth": "8%", "className": "text-sxcenter",
            "render": function (data, type, row, meta) {
                var html = "********";
                if (row.logIn != 1) {
                    html = "<input type='text' id='cookie" + row.id + "' />"
                }
                return html;
            }
        },
        {
            "data": "logIn", "title": "token", "sWidth": "8%", "className": "text-sxcenter",
            "render": function (data, type, row, meta) {
                var html = "已登录";
                if (row.logIn != 1) {
                    html = "<input type='text' id='token" + row.id + "' />" +
                        "<input type='button' id='log" + row.id + "' onclick='Login(" + row.id + ",this)' value='登录''>  "
                }
                return html;
            }
        },
    ]

    var table = $('#' + initTabId).DataTable({
        dom: '<"top">rt<"row"<"dom_l col-sm-2"l><"dom_i col-sm-4 col-sm-offset-1"i><"col-sm-5"p>>',
        ajax: {
            url: url,
            type: "post",
            contentType: "application/json;charset=utf-8",
            cache: false,	//禁用缓存
            data: function (d) {	//传入已封装的参数
                // 添加额外的参数传给服务器
                var data = {start: d.start, length: d.length};
                getSearchParam(data);// 添加额外查询条件
                return JSON.stringify(data);
            }
        },
        lengthChange: true,
        searching: false,
        ordering: false,
        processing: true,//表格 loading 加载层
        info: true,
        autoWidth: false,
        pageLength: 20,
        aLengthMenu: [10, 20, 50, 100], //更改显示记录数选项
        serverSide: true,
        paginate: true,
        columnDefs: [{//表格默认属性
            "defaultContent": "-",
            "targets": "_all"
        }],
        language: {
            url: "inspinia/js/plugins/dataTables/Chinese.lang"
        },
        columns: column,
        "drawCallback": function (settings) {//表格每次重绘回调函数
            // 更新选项卡标题数量
        }
    });
    //初始化表格事件  入参表格id
    Common.initTabEvent(initTabId);
}

function Login(id, that) {
    var cookieValue = $("#cookie" + id).val();
    if (cookieValue == "" || cookieValue == undefined) {
        Common.alert("请填写cookie", "cookie不能为空", 2);
        return false
    }
    var tokenValue = $("#token" + id).val();
    if (tokenValue == "" || tokenValue == undefined) {
        Common.alert("请填写token", "token不能为空", 2);
        return false
    }
    $.ajax({
        url: "/grabLogin/RBlogin?id=" + id + "&cookie=" + cookieValue+ "&token="+tokenValue,
        type: "get",
        success: function (data) {
            Common.closeload();//去除加载层
            if (data.success) {
                $("#" + modalId).modal('hide');
                var tabid = $("#" + modalId).data("tabid");
                $(that).hide();
                Common.tabRefresh(tabid);//刷新
                Common.alert("登录成功", "数据已保存", 1);

                $("#cookie" + id).before("********")
                $("#cookie" + id).remove()
                $("#token" + id).remove()
                $("#log" + id).before("已登录")

            } else {
                Common.alert("登录失败", data.msg, 2);
            }
        }
    });
}


function reloadCode(that,id) {
    var url ="http://"+ window.location.host + "/grabLogin/getCode?id=" + id + "&random=" +  Date.parse(new Date());
    $(that).attr('src', url)
}

/**
 * 获取查询条件
 */
function getSearchParam(param) {
    param["shyxm"] = $("#toolbar .shyxm").val() || "";
    param["ssjh"] = $("#toolbar .ssjh").val() || "";
    param["srzstatus"] = $("#toolbar .srzstatus").val() || "";
    param["shyid"] = $("#toolbar .shyid").val() || "";
    param["qyrzstatus"] = $("#toolbar .qyrzstatus").val() || "";
    param["fgsId"] = $("#toolbar .fgs").val() || "";
    return param;
}

// 查看大图
function showBigImgs( src, name){
    $("#modalLabel").text(name);
    $("#imgInModalID").attr("src", src);
    $("#imgModal").modal('show');
}