<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script>
    var rsResourceConfig = {
        appInfoDialog: null,//dialog对象
        $btnBack: $("#btn_back"),//返回按钮
        $layout: $("#div_wrapper"),//layout对象
        $btnImportData: $("#btn_import_data"),//导入数据集按钮
        $btnAddField: $("#btn_add_field"),//新增字段按钮
        $title: $("#title"),//title
        $datagrid: $("#div_wrapper_center_datagird"),
        datasetId: "${resourceModel.datasetCode}",//datasetID
        resourceId: "${resourceModel.id}",//资源ID
        init: function () {
            this.initEvent();
            this.$title.css("margin-right", document.body.scrollWidth / 2 - 200);
            this.$layout.ligerLayout({
                height: "99%",
                bottomHeight: document.body.clientHeight - 275,
                topHeight: 35,
                allowBottomResize: false,
                allowTopResize: 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"});
            this.initDatagrid();
        },
        //给按钮添加触发事件
        initEvent: function () {
            this.$btnBack.click(function () {
                indexPage.openMenu({
                    data: {
                        id: "31",
                        pid: "3",
                        text: "资源注册",
                        treedataindex: "10",
                        url: "${contextRoot}/resource/resource/initial"
                    }
                });
            });
            this.$btnAddField.click(function () {
                rsResourceConfig.editor("", "new");
            });

            this.$btnImportData.click(function () {
                rsResourceConfig.appInfoDialog = $.ligerDialog.open({
                    height: 540,
                    width: 620,
                    title: "资源导入",
                    url: '${contextRoot}/resource/resource/rsResourceImportDataset',
                    urlParms: {
                        resourceId: rsResourceConfig.resourceId,
                        datasetId: rsResourceConfig.datasetId
                    },
                    isHidden: false,
                    opener: true
                });
            });
        },
        dialogSuccess: function (message) {
            $.Notice.success(message);
            location.reload();
            //master.$datagrid.ligerGrid("reload");
            rsResourceConfig.appInfoDialog.close();
        },

        initDatagrid: function () {
            this.$datagrid = this.$datagrid.ligerGrid({
                url: '${contextRoot}/resource/resource/searchResourceFieldList',
                parms: {
                    resourceId: rsResourceConfig.resourceId
                },
                columns: [
                    {display: '字段名称', name: 'name', width: '15%', isAllowHide: false},
                    {
                        display: '类型',
                        name: 'columnType',
                        width: '25%',
                        dict: true,
                        dictName: "RESOURCE_METADATA_TYPE"
                    },
                    {display: '长度', name: 'columnLength', width: '10%', resizable: true},
                    {
                        display: '是否为空', name: 'nullable', width: '12%', resizable: true, render: function (row) {
                        if (row.nullable == "1") {
                            return "是";
                        } else {
                            return "否";
                        }
                    }
                    },
                    {display: '定义', name: 'definition', width: '25%', resizable: true},
                    {
                        display: '操作', name: 'operator', width: '10%', render: function (row) {
                        var html = '<div class="m-inline-buttons" style="width:80px;">';
                        if (row.dataSupportType == "1") {
                            html += "<a class=\"m-btn-view\" onclick=\"rsResourceConfig.editor('" + row.id + "','view')\"></a>";
                        } else {
                            html += "<a class=\"m-btn-edit\" onclick=\"rsResourceConfig.editor('" + row.id + "','modify')\"></a>";
                        }
                        html += "<a class=\"m-btn-delete\" onclick=\"rsResourceConfig.delete('" + row.id + "')\"></a>";
                        html += '</div>';
                        return html;
                    }
                    }
                ],
                height: "98%",
                enabledEdit: true,
                validate: true,
                onDblClickRow: function (data, rowid, row) {
                    if (row.dataSupportType == "1") {
                        rsResourceConfig.editor(data.id,'view');
                    } else {
                        rsResourceConfig.editor(data.id,'modify');
                    }
                },
                unSetValidateAttr: false,
                usePager: true//不显示分页
            });
        },
        editor: function (id, mode) {
            var title = '';
            //只有new 跟 modify两种模式会到这个函数
            if (mode == 'modify') {
                title = '修改逻辑数据元';
            }
            else if (mode == 'view') {
                title = '查看事实数据元';
            } else {

                title = '新增逻辑数据元';
            }
            this.appInfoDialog = $.ligerDialog.open({
                //判断屏幕是大屏还是小屏
                height: document.body.scrollHeight - 20 <= 765 ? document.body.scrollHeight - 20 : 765,
                width: 600,
                title: title,
                url: '${contextRoot}/resource/resource/rsResourceFieldInfo',
                urlParms: {
                    id: id,
                    mode: mode,
                    datasetId: rsResourceConfig.datasetId,
                    resourceId: rsResourceConfig.resourceId
                },
                isHidden: false,
                opener: true
            });
        },
        delete: function (id) {
            var status = "delete";
            $.ligerDialog.confirm('确认删除该行信息？', function (yes) {
                if (yes) {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url: "${contextRoot}/resource/resource/deleteMetadata",
                        dataType: "json",
                        data: {id: id},
                        cache: false,
                        success: function (data) {
                            if (data.successFlg) {
                                $.ligerDialog.success(data.message);
                                rsResourceConfig.$datagrid.reload();
                            }
                            else {
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error: function (data) {
                            $.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");
                        }
                    });
                }
            });
        }, dialogCallBack: function (message) {
            $.ligerDialog.success(message);
            rsResourceConfig.$datagrid.reload();
            rsResourceConfig.appInfoDialog.close();
        }
    }
    $(function () {
        rsResourceConfig.init();
    });
</script>