// 代金券消费
function   queryEnterpriseList(urls, queryParams) {
    // 默认查询第一页 query查询时保存当前条件
    var TempPageNumber = 1, pageSize = 10;
    try {
        TempPageNumber = $('#datatable').datagrid('getPager').pagination("options").pageNumber;
        pageSize = $('#datatable').datagrid('getPager').pagination("options").pageSize;
    } catch (e) {

    }
    $('#datatable')
        .datagrid(
            {
                title: "信息列表",
                iconCls: 'icon-search',
                pageNumber: TempPageNumber,
                pageSize: pageSize,
                pageList: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
                nowrap: true,
                striped: true,
                border: true,
                noheader: true,
                url: urls,
                loadMsg: '数据加载中......',
                queryParams: queryParams,
                remoteSort: false,
                fitColumns: true,
                singleSelect: false,
                columns: [[

                    {
                        field: 'name',
                        title: '经销商名称',
                        sortable: 'true',
                        halign: 'center',
                        align: 'left',
                        width: 10
                    }, {
                        field: 'abbr_code',
                        title: '经销商代码',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }, {
                        field: 'address',
                        title: '地址',
                        sortable: 'true',
                        halign: 'center',
                        align: 'left',
                        width: 15
                    }
                    , {
                        field: 'contact',
                        title: '联系人',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'operate', title: '操作', align: 'center', width: 5,
                        formatter: function (value, row, index) {

                            //将数组序列化为json字符串
//								    	row = json_encode(row);
//								    	var obj = eval("("+row+")");
//								    	alert(JSON.stringify(row));
                            var str = '<a href="#" name="opera" class="easyui-linkbutton opera" onclick="showEnterprisePanel(' + row.id + ');">编辑</a>&nbsp;&nbsp;';
                            str = str + '<a href="#" name="opera" class="easyui-linkbutton opera" onclick="deleteEnterprise(' + row.id + ');">删除</a>';

                            return str;
                        }
                    }
                ]],
                pagination: true,
                rownumbers: false,
                onLoadSuccess: function (data) {
                },
                toolbar: [],
                onCheck: function (rowIndex, rowData) {
                },
                onCheckAll: function (rows) {
                },
                onUncheck: function (rowIndex, rowData) {
                },
                onLoadSuccess: function (data) {
                },
                onUncheckAll: function (rows) {
                }

            });
    $('#datatable').datagrid('getPager').pagination({
        displayMsg: '',
        onBeforeRefresh: function (pageNumber, pageSize) {
            $(this).pagination('loading');
            $(this).pagination('loaded');
        }
    });
}

function showEnterprisePanel(id) {
    $("#enterpriseform").form("clear");
    var params = {};
    params["id"] = id;
    var result = ajaxgetresultpost($("#paths").val()
        + '/enterprise/get_by_id.do', params);
    if (result.flag == 0) {
        alert(result.msg);
    } else {
        //alert(result.data);
        //getMapUrl();
        loadData2(result.data);
        var lng = $("#lng").val();
        var lat = $("#lat").val();
        //alert(lng);
        var url = $("#paths").val() + "/base/amap.do?lng=" + lng + "&lat=" + lat;
        setTimeout(function () {
            $("#map_frame").attr("src", url);
        }, 1);
        $("#addedit_dialog").show();

    }


}

function deleteEnterprise(id) {
    $.messager.confirm('确定', '你确定删除该经销商么?', function (r) {
        if (r) {
            var params = {};
            params["id"] = id;
            var result = ajaxgetresultpost($("#paths").val()
                + '/enterprise/delete_by_id.do', params);
            if (result.flag == 0) {
                alert(result.msg);
            } else {
                query();
            }
        }
    });

}


function loadData2(obj) {
    var key, value, tagName, type, arr;
    for (x in obj) {
        key = x;
        value = obj[x];

        $("#addedit_dialog").find($("[name='" + key + "'],[name='" + key + "[]']")).each(function () {
            tagName = $(this)[0].tagName;
            type = $(this).attr('type');
            if (tagName == 'INPUT') {
                if (type == 'radio') {
                    $(this).attr('checked', $(this).val() == value);
                } else if (type == 'checkbox') {
                    arr = value.split(',');
                    for (var i = 0; i < arr.length; i++) {
                        //alert($(this).val()+'&&'+arr[i]+i);
                        if ($(this).val() == arr[i]) {
                            //alert(1);
                            $(this).prop('checked', true);
                            break;
                        }
                    }
                } else {
                    $(this).val(value);
                }
            } else if (tagName == 'SELECT' || tagName == 'TEXTAREA') {
                $(this).val(value);
            }

        });
    }
}