$.jgrid.defaults.responsive = true;
$.jgrid.defaults.styleUI = 'Bootstrap';
$(function () {
    /*判断浏览器是否是ie8  解决ie8弹出框居中问题*/
    var ua = navigator.userAgent.toLowerCase();
    if (window.ActiveXObject) {
        if (ua.match(/msie ([\d.]+)/)[1] == '8.0') {
            $(window).resize(function () {
                $.each($(".moveModel > .modal-dialog"), function () {
                    $(this).css("left", ($(window).width() - $(this).width()) / 2);
                    $(this).css("top", "40px");
                })
            })
        }
    }
    //下拉框  含搜索的
    $('.chosen-select').chosen({allow_single_deselect: true, no_results_text: "无匹配数据", width: "100%"});
    //resize the chosen on window resize
    $(window).on('resize.chosen', function () {
        $.each($('.chosen-select'), function (index, obj) {
            $(obj).next().css("width", 0);
            var w = $(obj).parent().width();
            $(obj).next().css("width", w);
        });
        $.each($('.chosen-single-select'), function (index, obj) {
            $(obj).next().css("width", 0);
            var e = $(obj).parent().width();
            $(obj).next().css("width", e);
        })
    }).trigger('resize.chosen');
    $(".hideButton").click(function () {
        $("#tableForm").find(".form-control").tooltip("destroy").removeAttr("aria-invalid").removeAttr("title").removeAttr("style");
        $('#tableForm')[0].reset();
        $(".Pop").hide();
        $("#valiteType").val("");
        $("#sqrid").removeAttr("disabled");
    });
    var valiteType=$("#valiteType").val();
    if (valiteType =="save"){
        $('input,select',$('form[name="tableForm"]')).prop('readonly',true);
        $("#xb,#sfczr").prop("disabled", "disabled");
    } else if(valiteType == "update"){
        $("#sqrid").attr("disabled", "disabled");
    }

    //绑定回车键
    $(document).keydown(function(event){
        if(event.keyCode==13){
            $("#search").trigger("click");
        }
    });
//保存事件
    $("#SaveBtn").click(function () {
        var msg = valiteData(".tableA");
        if (msg && msg != 'undefined')
            tipInfo(msg);
        else
            valiteValue();
    });

    /*表单校验 end*/
    //生成表格
    BdcSqrTableConfigGrid();
    fillTable();

    //新增
    $("#addData").click(function () {
        var url = configUrl+'/bdcSqr/addSqr?type=add';
        window.parent.parent.showModel(url, "新增申请人");
    });
    //删除
    $("#delData").click(function () {
        var ids = $('#limit-table-grid-table').jqGrid('getGridParam', 'selarrrow');
        delRule(ids, configUrl+'/bdcSqr/delBdcSqr', "limit-table-grid-table");
    });
    //修改
    $("#updateData").click(function () {
        var ids = $('#limit-table-grid-table').jqGrid('getGridParam', 'selarrrow');
        if (ids.length == 0) {
            tipInfo("请选择一条数据!");
            return;
        }
        if (ids.length > 1) {
            tipInfo("只能同时修改一条数据!");
            return;
        }
        var ids = $('#limit-table-grid-table').jqGrid('getGridParam', 'selarrrow');
        var data = $('#limit-table-grid-table').getRowData(ids);
        var url = configUrl+'/bdcSqr/addSqr?type=update&sqrmc='+ encodeURI(data.SQRMC) +
            "&zjh=" + encodeURI(data.ZJH) + "&xb=" + encodeURI(data.XB)+ "&lxdh=" + encodeURI(data.LXDH)+ "&sqrid=" + encodeURI(data.SQRID)
            + "&zjzl=" + encodeURI(data.ZJZL)+ "&txdz=" + encodeURI(data.TXDZ)+ "&yb=" + encodeURI(data.YB)+ "&frmc=" + encodeURI(data.FRMC)+ "&frdh=" + encodeURI(data.FRDH)
            + "&dlrmc=" + encodeURI(data.DLRMC)+ "&dlrdh=" + encodeURI(data.DLRDH)+ "&dljg=" + encodeURI(data.DLJG)+ "&sqrlb=" + encodeURI(data.SQRLB)
            + "&xh=" + encodeURI(data.XH)+ "&bz=" + encodeURI(data.BZ)+ "&sfczr=" + encodeURI(data.SFCZR)+ "&gyfs=" + encodeURI(data.SQRLB)
            + "&qlbl=" + encodeURI(data.QLBL)+ "&sshy=" + encodeURI(data.SSHY)+ "&qlmj=" + encodeURI(data.QLMJ)+ "&qygyr=" + encodeURI(data.QYGYR);
        window.parent.parent.showModel(url, "修改信息");
    });

    $("#search").click(function () {
        var mc = $("#mc").val();
        $("#limit-table-grid-table").jqGrid('setGridParam', {
            datatype: 'json',
            page: 1,
            url: configUrl+'/bdcSqr/getBdcSqrPagesJson?mc=' + encodeURI(mc)
        }).trigger("reloadGrid"); //重新载入
    });
    //resize to fit page size
    $(window).on('resize.jqGrid', function () {
        var contentWidth;
        if ($("#limitTableContent").width() > 0) {
            contentWidth = $("#limitTableContent").width();
        }
        $("#limit-table-grid-table").jqGrid('setGridWidth', contentWidth);
    });
});
//获取列表数据
function fillTable() {
    $("#limit-table-grid-table").jqGrid('setGridParam', {
        datatype: 'json',
        page: 1,
        url: configUrl + "/bdcSqr/getBdcSqrPagesJson",
        postData: {}
    }).trigger("reloadGrid"); //重新载入
}
//删除判断是否没有选择数据
function delRule(ids, url, gridId) {
    if (ids.length == 0) {
        tipInfo("请选择一条数据!");
        return;
    }
    showConfirmDialog("删除", "是否删除？", "del", "'" + ids + "','" + url + "','" + gridId + "'", "", "");
}
//提示
function tipInfo(msg) {
    bootbox.dialog({
        message: "<h3><b>" + msg + "</b></h3>",
        title: "",
        buttons: {
            main: {
                label: "关闭",
                className: "btn-primary"
            }
        }
    });
}

//auth表格初始化
function BdcSqrTableConfigGrid() {
    var grid_selector = "#limit-table-grid-table";
    var pager_selector = "#limit-table-grid-pager";
    //resize on sidebar collapse/expand
    var parent_column = $(grid_selector).closest('[class*="col-"]');
    $(document).on('settings.ace.jqGrid', function (ev, event_name, collapsed) {
        if (event_name === 'sidebar_collapsed' || event_name === 'main_container_fixed') {
            $(grid_selector).jqGrid('setGridWidth', parent_column.width());
        }
    });
    jQuery(grid_selector).jqGrid({
        datatype: "local",
        height: 'auto',
        jsonReader: {id: 'SQRID'},
        colNames: ['申请人ID','申请人名称','证件号','通讯地址','邮编', '性别','联系电话',
            '证件种类','法人名称','法人电话','代理人名称','代理人电话','代理机关','申请人类别',
            '序号','备注','详细信息','是否持证人','共有方式','权利比例','所属行业','权利面积','其余共有人'],
        colModel: [
            {name: 'SQRID', index: 'SQRID', width: '0%', sortable: false,hidden: true},
            {name: 'SQRMC', index: 'SQRMC', width: '15%', sortable: false},
            {name: 'ZJH', index: 'ZJH', width: '15%', sortable: false},
            {name: 'TXDZ', index: 'TXDZ', width: '15%', sortable: false},
            {name: 'YB', index: 'YB', width: '10%', sortable: false},
            {name: 'XB', index: 'XB', width: '0%', sortable: false,hidden: true},
            {name: 'LXDH', index: 'LXDH', width: '15%', sortable: false},
            {name: 'ZJZL', index: 'ZJZL', width: '0%', sortable: false,hidden: true},
            {name: 'FRMC', index: 'FRMC', width: '0%', sortable: false,hidden: true},
            {name: 'FRDH', index: 'FRDH', width: '0%', sortable: false,hidden: true},
            {name: 'DLRMC', index: 'DLRMC', width: '0%', sortable: false,hidden: true},
            {name: 'DLRDH', index: 'DLRDH', width: '0%', sortable: false,hidden: true},
            {name: 'DLJG', index: 'DLJG', width: '0%', sortable: false,hidden: true},
            {name: 'SQRLB', index: 'SQRLB', width: '0%', sortable: false,hidden: true},
            {name: 'XH', index: 'XH', width: '0%', sortable: false,hidden: true},
            {name: 'BZ', index: 'BZ', width: '15%', sortable: false},
            {name: 'view',
                index: '',
                width: '20%',
                sortable: false,
                formatter: function (cellvalue, options, rowObject) {
                    return '<div style="margin-left:15px;"><div title="详细信息"  style="float:left;cursor:pointer; margin-left: 10px;" class="ui-pg-div ui-inline-edit" id="" onclick="xx(\'' + rowObject.SQRID + '\')" onmouseover="jQuery(this).addClass(\'ui-state-hover\');" onmouseout="jQuery(this).removeClass(\'ui-state-hover\');"><span class="fa fa-list-alt black"></span>详细信息</div></div>'
                }
            },
            {name: 'SFCZR', index: 'SFCZR', width: '0%', sortable: false,hidden: true},
            {name: 'GYFS', index: 'GYFS', width: '0%', sortable: false,hidden: true},
            {name: 'QLBL', index: 'QLBL', width: '0%', sortable: false,hidden: true},
            {name: 'SSHY', index: 'SSHY', width: '0%', sortable: false,hidden: true},
            {name: 'QLMJ', index: 'QLMJ', width: '0%', sortable: false,hidden: true},
            {name: 'QYGYR', index: 'QYGYR', width: '0%', sortable: false,hidden: true}

        ],
        viewrecords: true,
        rowNum: 5,
        rowList: [5, 10, 15],
        pagerpos: "left",
        pager: pager_selector,
        altRows: false,
        multiboxonly: true,
        multiselect: true,
        loadComplete: function () {
            var table = this;
            setTimeout(function () {
                updatePagerIcons(table);
                enableTooltips(table);
                $(grid_selector).jqGrid('setGridWidth', $("#limitTableContent").width());
            }, 0);
        },
        ondblClickRow: function (rowid) {
        },
        caption: "",
        autowidth: true
    });

}

//replace icons with FontAwesome icons like above
function updatePagerIcons(table) {
    var replacement =
        {
            'ui-icon-seek-first': 'ace-icon fa fa-angle-double-left bigger-140',
            'ui-icon-seek-prev': 'ace-icon fa fa-angle-left bigger-140',
            'ui-icon-seek-next': 'ace-icon fa fa-angle-right bigger-140',
            'ui-icon-seek-end': 'ace-icon fa fa-angle-double-right bigger-140'
        };
    $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function () {
        var icon = $(this);
        var $class = $.trim(icon.attr('class').replace('ui-icon', ''));

        if ($class in replacement) icon.attr('class', 'ui-icon ' + replacement[$class]);
    })
}

function xx(sqrid){
    debugger;
    var ids = $('#limit-table-grid-table').jqGrid('getGridParam', 'selarrrow');
    if (ids.length == 0) {
        tipInfo("请选择一条数据查看!");
        return;
    }
    if (ids.length > 1) {
        tipInfo("只能同时查看一条数据!");
        return;
    }
    var ids = $('#limit-table-grid-table').jqGrid('getGridParam', 'selarrrow');
    var data = $('#limit-table-grid-table').getRowData(ids);
    var url = configUrl+'/bdcSqr/addSqr?type=save&sqrmc='+ encodeURI(data.SQRMC) +
        "&zjh=" + encodeURI(data.ZJH) + "&xb=" + encodeURI(data.XB)+ "&lxdh=" + encodeURI(data.LXDH)+ "&sqrid=" + encodeURI(data.SQRID)
        + "&zjzl=" + encodeURI(data.ZJZL)+ "&txdz=" + encodeURI(data.TXDZ)+ "&yb=" + encodeURI(data.YB)+ "&frmc=" + encodeURI(data.FRMC)+ "&frdh=" + encodeURI(data.FRDH)
        + "&dlrmc=" + encodeURI(data.DLRMC)+ "&dlrdh=" + encodeURI(data.DLRDH)+ "&dljg=" + encodeURI(data.DLJG)+ "&sqrlb=" + encodeURI(data.SQRLB)
        + "&xh=" + encodeURI(data.XH)+ "&bz=" + encodeURI(data.BZ)+ "&sfczr=" + encodeURI(data.SFCZR)+ "&gyfs=" + encodeURI(data.SQRLB)
        + "&qlbl=" + encodeURI(data.QLBL)+ "&sshy=" + encodeURI(data.SSHY)+ "&qlmj=" + encodeURI(data.QLMJ)+ "&qygyr=" + encodeURI(data.QYGYR);
    window.parent.parent.showModel(url, "详细信息");
}



function enableTooltips(table) {
    $('.navtable .ui-pg-button').tooltip({container: 'body'});
    $(table).find('.ui-pg-div').tooltip({container: 'body'});
}

function valiteValue() {
    $.ajax({
        url: configUrl + "/bdcSqr/valite?valiteType=" + valiteType,
        type: 'get',
        data: {
            sqrid: $("#sqrid").val()
        },
        success: function (data) {
            if (data && data != 'undefiend') {
                if (data && data == "1"){
                    alert("请输入申请人ID!");
                }else if (data && data=="3"){
                    alert("表中数据已存在!");
                }else {
                    saveorupdate();
                }
            }
        },
        error: function (data) {
        }
    });
}

function saveorupdate() {
    $.ajax({
        url: configUrl + '/bdcSqr/saveBdcSqr?sqrmc=' + encodeURI($("#sqrmc").val()) +
        "&zjh=" + encodeURI($("#zjh").val()) + "&xb=" + encodeURI($("#xb").val())+ "&lxdh=" + encodeURI($("#lxdh").val())+ "&sqrid=" + encodeURI($("#sqrid").val())
        + "&zjzl=" + encodeURI($("#zjzl").val())+ "&txdz=" + encodeURI($("#txdz").val())+ "&yb=" + encodeURI($("#yb").val())+ "&frmc=" + encodeURI($("#frmc").val())+ "&frdh=" + encodeURI($("#frdh").val())
        + "&dlrmc=" + encodeURI($("#dlrmc").val())+ "&dlrdh=" + encodeURI($("#dlrdh").val())+ "&dljg=" + encodeURI($("#dljg").val())+ "&sqrlb=" + encodeURI($("#sqrlb").val())
        + "&xh=" + encodeURI($("#xh").val())+ "&bz=" + encodeURI($("#bz").val())+ "&sfczr=" + encodeURI($("#sfczr").val())+ "&gyfs=" + encodeURI($("#gyfs").val())
        + "&qlbl=" + encodeURI($("#qlbl").val())+ "&sshy=" + encodeURI($("#sshy").val())+ "&qlmj=" + encodeURI($("#qlmj").val())+ "&qygyr=" + encodeURI($("#qygyr").val()),
        type: 'post',
        dataType: 'json',
        success: function (data) {
            if (data && data.result =="1"||data.result=="2") {
                alert("保存成功");
                window.parent.resourceRefresh();//刷新父页面jqGrid;
            }else{
                alert(data.result);
            }
        },
        error: function (data) {
            alert("保存失败");
            $('#tableForm')[0].reset();
        }
    });
}

function del(ids, url, gridId) {
    $.getJSON(url + "?ids=" + encodeURIComponent(ids), {}, function (jsonData) {
        setTimeout($.unblockUI, 10);
        if (jsonData.result == "0"){
            alert("删除成功");
        } else {
            alert ("删除失败");
        }
        $('#' + gridId).trigger("reloadGrid");
    })
}