<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script>
    var resourceBrowse = {
        resourceId: "${resourceModel.id}",//资源ID
        $btnBack: $("#btn_back"),//返回按钮
        $btnSearch: $("#btn_search"),//搜索按钮
        $btnSelect: $("#btn_select"),//导出选中结果按钮
        $btnSelectALL: $("#btn_selectALL"),//导出查询结果按钮
        $layoutDivWrapper: $("#div_wrapper"),//导出查询结果按钮
        $layoutDivWrapperRight: $("#div_wrapper_right"),//导出查询结果按钮
        $grid: $("#div_wrapper_right_bottom_datagrid"),//datagrid
        $condition:null,
        $title: $("#title"),//title
        init: function () {
            this.initLayout();
            //初始化按钮事件
            this.initEvent();
            this.initGrid();
        },
        initLayout: function () {
            this.$title.css("margin-right", document.body.scrollWidth / 2 - 200);
            //初始化layout
            this.$layoutDivWrapper.ligerLayout({
                height: "98%",
                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();
            this.$layoutDivWrapperRight.ligerLayout({
                topHeight: (window.screen.height - 200) / 4,
                allowTopResize: false,//左边区域初始化不可以隐藏
                allowTopCollapse: false//左边区域不可以隐藏
            });
            $("#div_wrapper_right_top").css("height",(window.screen.height - 200) / 4).css("overflow","auto");
            $("#resourceCondition").css("height",(window.screen.height - 200) / 4).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"});
        },
        //给按钮添加触发事件
        initEvent: function () {
            this.$btnBack.click(function () {
                indexPage.openMenu({
                    data: {
                        id: "31",
                        pid: "3",
                        text: "资源注册",
                        treedataindex: "10",
                        url: "${contextRoot}/resource/resource/initial"
                    }
                });
            });
            this.$btnSearch.click(function () {
                //查询
                resourceBrowse.$grid.loadServerData({id:resourceBrowse.resourceId,queryParams:resourceBrowse.$condition.getValue()});
            });
            this.$btnSelect.click(function () {
                //导出部分

                var selectData = resourceBrowse.$grid.getSelecteds();
                var field = [];
                var columns = resourceBrowse.$grid.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: '${resourceModel.name}'},
                    prepareCallback: function (url) {
                        // intervalCode = setInterval("exportProcess('#exportProcess')", 500);//1000为1秒钟
                    },
                    successCallback: function (url) {

                    },
                    failCallback: function (html, url) {
                        $.ligerDialog.error("下载失败");
                    }
                });
            });
            this.$btnSelectALL.click(function () {

                var field = [];
                var columns = resourceBrowse.$grid.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:  '${resourceModel.name}',id: resourceBrowse.resourceId,
                        queryParams: resourceBrowse.$condition.getValue()},
                    prepareCallback: function (url) {
                        // intervalCode = setInterval("exportProcess('#exportProcess')", 500);//1000为1秒钟
                    },
                    successCallback: function (url) {

                    },
                    failCallback: function (html, url) {
                        $.ligerDialog.error("下载失败");
                    }
                });
            });

        },
        initGrid: function () {
            $.ajax({
                type: "POST",
                url: "${contextRoot}/resource/resource/searchResourceDatagridColunm",
                data: {"id": resourceBrowse.resourceId},
                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]};
                    }
                    resourceBrowse.$condition=$('#resourceCondition').ligerCondition({
                        fields:rowColunms,
                        stdDictUrl:"${contextRoot}/std/getDictByScheme"
                    });
                    resourceBrowse.$grid = resourceBrowse.$grid.ligerGrid({
                        columns: syColumns,
                        enabledEdit: true,//是否允许编辑
                        validate: true,
                        url: "${contextRoot}/resource/resource/searchRomoteResourceList",
                        parms: {"id": resourceBrowse.resourceId},
                        usePager: true,//是否用分页
                        unSetValidateAttr: false,
                        isScroll: true,
                        width: "97%",
                        // 表头行的高度
                        headerRowHeight: 40,
                        // 行的高度
                        rowHeight: 40,
                        height: "96%",
                        //  isScroll:true,//是否滚动
                        checkbox: true,//选择框
                        // 编辑器位置误差调整
                        editorTopDiff: 41,
                        allowAdjustColWidth: true
                    });
                }
            });

        }
    }
    $(function () {
        resourceBrowse.init();
    });

</script>