<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script>
    var totalRows = 0;
    var resource = {
        $resourcelayout: null,//layout对象
        $resourcelayoutRight: null,//layout对象
        $resourceTree: null,//树对象
        $condition: null,
        $datagrid: null,//datagrid
        $searchBox: null,//树的文本框
        $btnSearch: $("#btn_search"),//搜索按钮
        $btnSelect: $("#btn_select"),//导出选中结果按钮
        $btnSelectALL: $("#btn_selectALL"),//导出查询结果按钮
        currentSelectID: null,//当前选择的资源ID
        tablename: "",//导出的excel名字
        pageInit: function () {
            //初始化布局
            this.layoutInit();
            //初始化树
            this.treeInit();

            resource.$searchBox = $("#div_wrapper_left_inp_search").ligerSearch({
                width: 180,onClick: function (value) {
                    if (value != "" && value != null && value != undefined && value.length > 0)
                        resource.treeInit(value);
                       //$.ligerui.get('ul_tree1').queryByText(value);
                }
            });
            resource.$condition = $('#resourceCondition').ligerCondition({
                stdDictUrl: "${contextRoot}/std/getDictByScheme",
            });

            resource.$btnSearch.click(function () {
                resource.$datagrid.loadServerData({
                    id: resource.currentSelectID,
                    queryParams: resource.$condition.getValue()
                });
            });
            resource.$btnSelect.click(function () {
                var selectData = resource.$datagrid.getSelecteds();
                var field = [];
                var columns = resource.$datagrid.columns;
                //拼凑表头
                for (var i = 0; i < columns.length; i++) {
                    if (columns[i].columnindex > 1) {
                        field[i - 2] = {key: columns[i].columnname, value: columns[i].display,};
                    }
                }
                $.fileDownload("${contextRoot}/resource/exportSomeExcel", {
                    httpMethod: 'POST',
                    data: {field: JSON.stringify(field), data: JSON.stringify(selectData), name: resource.tablename},
                    prepareCallback: function (url) {
                       // intervalCode = setInterval("exportProcess('#exportProcess')", 500);//1000为1秒钟
                    },
                    successCallback: function (url) {

                    },
                    failCallback: function (html, url) {
                        $.ligerDialog.error("下载失败");
                    }
                });
            });
            resource.$btnSelectALL.click(function () {
                var field = [];
                var columns = resource.$datagrid.columns;
                //拼凑表头
                for (var i = 0; i < columns.length; i++) {
                    if (columns[i].columnindex > 1) {
                        field[i - 2] = {key: columns[i].columnname, value: columns[i].display};
                    }
                }
                $.fileDownload("${contextRoot}/resource/exportAllExcel", {
                    httpMethod: 'POST',
                    data: {field: JSON.stringify(field), name: resource.tablename,id: resource.currentSelectID,
                        queryParams: resource.$condition.getValue(),rows:totalRows},
                    prepareCallback: function (url) {
                        // intervalCode = setInterval("exportProcess('#exportProcess')", 500);//1000为1秒钟
                    },
                    successCallback: function (url) {

                    },
                    failCallback: function (html, url) {
                        $.ligerDialog.error("下载失败");
                    }
                });
            });
        },
        layoutInit: function () {
            //初始化layout
            debugger
            $resourcelayout = $("#div_wrapper").ligerLayout({
                height: "99%",
                leftWidth: 200,
                isLeftCollapse: false,//左边区域初始化不可以隐藏
                allowLeftCollapse: false//左边区域不可以隐藏
            });
            //l-layout-left
            $("#div_wrapper .l-layout-left").css({"border": "0px"});
            $("#div_wrapper .l-layout-right").css({"border": "0px"});
            $("#div_wrapper .l-layout-center").css({"border": "0px"});
            $("#div_wrapper .l-layout-top").css({"border": "0px"});
            //隐藏左边的标题
            $("#div_wrapper .l-layout-left .l-layout-header").hide();
            $resourcelayoutRight = $("#div_wrapper_right").ligerLayout({
                topHeight: (window.screen.height - 200) / 5,
                allowTopReid: false,//左边区域初始化不可以隐藏
                allowTopCollapse: false//左边区域不可以隐藏
            });
            $("#div_wrapper_right_top").css("height", (window.screen.height - 200) / 5).css("overflow", "auto");
            $("#resourceCondition").css("height", (window.screen.height - 200) / 5).css("overflow", "auto");

            $("#div_wrapper_right .l-layout-left").css({"border": "0px"});
            $("#div_wrapper_right .l-layout-right").css({"border": "0px"});
            $("#div_wrapper_right .l-layout-center").css({"border": "0px"});
            $("#div_wrapper_right .l-layout-top").css({"border": "0px"});
        },
        treeInit: function (name) {
            $.ajax({
                type: "POST",
                url: "${contextRoot}/resource/resourceCategoryTreeList",
                data:{name:name},
                success: function (msg) {
                    // 初始化树形菜单
                    $resourceTree = $('#ul_tree1').ligerTree({
                        data: msg.data,
                        idFieldName: 'id',
                        nodeWidth: 200,
                        parentIDFieldName: 'pid',
                        isExpand: false,
                        onClick: function (obj) {
                            //判断是否是接口
                            if (obj.data.type == "2") {
                                resource.tablename = obj.data.text;
                                // resource.showOverlay();
                                resource.initDatagrid (obj.data);
                            }
                        },
                        onSuccess: function (data) {

                        }
                    });
                }
            });


            resource.$datagrid = $("#div_wrapper_right_bottom_datagrid").ligerGrid({
                enabledEdit: true,//是否允许编辑
                validate: true,
                url: "${contextRoot}/resource/resource/searchRomoteResourceList",
                usePager: true,//是否用分页
                unSetValidateAttr: false,
                isScroll: true,
                root: "detailModelList",
                width: "97%",
                // 表头行的高度
                headerRowHeight: 40,
                // 行的高度
                rowHeight: 40,
                //  isScroll:true,//是否滚动
                checkbox: true,//选择框
                allowAdjustColWidth: true,
                onSuccess: function (msg) {
                    // resource.hideOverlay();
                    if(msg.totalCount != 0) {
                        totalRows = msg.totalCount;
                    }
                }
            });
        },
        initDatagrid: function (data) {
            resource.currentSelectID = data.id;
            $.ajax({
                type: "POST",
                url: "${contextRoot}/resource/resource/searchResourceDatagridColunm",
                data: {"rows": 10, "page": 1, "id": data.id},
                success: function (msg) {
                    msg = eval("(" + msg + ")");
                    var colunmName = msg.colunmName;
                    var colunmCode = msg.colunmCode;
                    var colunmDict = msg.colunmDict;
                    var colunmType = msg.colunmType;
                    var syColumns = [];
                    var rowColunms = [];
                    for (var i = 0; i < colunmCode.length; i++) {
                        syColumns[i] = {
                            display: colunmName[i],
                            name: colunmCode[i],
                            width: 200,
                            resizable: true
                        };
                        rowColunms[i] = {
                            code: colunmCode[i],
                            text: colunmName[i],
                            type: colunmType[i],
                            dict: colunmDict[i]
                        };
                    }
                    resource.$datagrid.set("columns", syColumns);
                    resource.$datagrid.set("parms", {"id": data.id});
                    resource.$datagrid.reRender();
                    resource.$datagrid.loadServerData({"id": data.id});
                    //初始化条件选择控件
                    resource.$condition.setFields(rowColunms);
                }
            });
        }
    }
    $(function () {
        resource.pageInit();
    });
</script>