<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>

<script type="text/javascript">
    var rsResourceFieldInfoDialog = {
        dialog: frameElement.dialog,
        $form: $("#div_info_form"),//表单对象
        $formLayout: $("#div_info_form_layout"),//layout
        $fieldName: $("#inp_field_name"),//字段名称
        $fieldCode: $("#inp_field_code"),//字段代码
        $fieldType: $("#inp_field_type"),//
        $fieldLength: $('#inp_field_length'),//字段长度
        $fieldIsPK: $('input[name="inp_field_is_PK"]', this.$form),//是否主键
        $fieldIsNull: $('input[name="inp_field_is_null"]', this.$form),//是否可为空
        $fieldRemark: $("#inp_field_remark"),//字段说明
        $datasetFrom:$("#inp_dataset_from"),
       // $datasetFrom: $('input[name="inp_dataset_from"]', this.$form),//是否可为空
        $leftDatagrid: $("#left_datagrid"),//左边的datagrid
        $rightDatagrid: $("#right_datagrid"),//右边的datagrid
        $centerTab1: $("#centerTab1"),//右边的datagrid
        $centerTab2: $("#centerTab2"),//右边的datagrid
        $logicDataSource:$("#logicDataSource"),


        $id: $("#inp_resource_id"),//资源数据源
        $btnSave: $("#btn_save"),//保存按钮对象
        $btnCancel: $("#btn_cancel"),//关闭按钮对象

        init: function () {
            this.initForm();//初始化表单
            this.initlayout();//初始化layout
            this.bindEvents();//初始化按钮的事件
            this.initTab1();//初始化tab1
            this.initTab2();//初始化tab2
            //如果是视图模式
            if ('${mode}' == 'view') {
                this.$fieldName.attr("disabled", "false");
                this.$fieldCode.attr("disabled", "false");
                this.$fieldType.attr("disabled", "false");
                this.$fieldLength.attr("disabled", "false");
                this.$fieldIsPK.attr("disabled", "false");
                this.$fieldIsNull.attr("disabled", "false");
                this.$fieldRemark.attr("disabled", "false");
              //  this.$datasetFrom.attr("disabled", "false");
                this.$btnSave.hide();
            }
        },
        initlayout: function () {
            //初始化layout
            this.$formLayout.ligerLayout({
                height: 690,
                bottomHeight: 25,
                topHeight: 240,
                isLeftCollapse: false,//左边区域初始化不可以隐藏
                isRightCollapse: false,//左边区域初始化不可以隐藏
                allowLeftCollapse: false,//左边区域不可以隐藏
                allowRightCollapse: false,//左边区域不可以隐藏
                allowRightResize: false,
                allowLeftResize: false,
                allowBottomResize: false,
                allowTopResize: false
            });
            //隐藏layout左右两边的标题
            $("#div_info_form_layout .l-layout-left .l-layout-header").hide();
            $("#div_info_form_layout .l-layout-right .l-layout-header").hide();
            //l-layout-left
            $("#div_info_form_layout .l-layout-left").css({"border": "0px"});
            $("#div_info_form_layout .l-layout-right").css({"border": "0px"});
            $("#div_info_form_layout .l-layout-center").css({"border": "0px"});
            $("#div_info_form_layout .l-layout-top").css({"border": "0px"});
            $("#div_info_form_layout .l-layout-bottom").css({"border": "0px"});
        },
        initTab1: function () {
            this.$leftDatagrid.ligerGrid({
                url: '${contextRoot}/resource/resource/getDataSet',
                columns: [
                    {display: '数据集', name: 'name', width: '202', isAllowHide: false}
                ],
                validate: true,
                rownumbers: false,
                parms: {datasetId: "${entity.datasetId}"},
                allowAdjustColWidth: false,
                width: 220,
                fitColumn: false,
                height: 370,
                usePager: false//不显示分页
            });

            this.$rightDatagrid.ligerGrid({
                url: '${contextRoot}/resource/resource/getMetaData',
                columns: [
                    {display: '数据元', name: 'name', width: '202', isAllowHide: false}
                ],
                validate: true,
                parms: {id: "${entity.id}"},
                rownumbers: false,
                allowAdjustColWidth: false,
                width: 220,
                fitColumn: false,
                height: 370,
                usePager: false//不显示分页
            });
        },
        initTab2: function () {

        },
        initForm: function () {
            var mode = '${mode}';

            if (mode == 'view') {
                $("input,select", this.$form).prop('disabled', false);
            }
            this.$fieldName.ligerTextBox({width: 240});
            this.$fieldCode.ligerTextBox({width: 240});
            this.$fieldLength.ligerSpinner({width: 240,type:'int',isNegative:false });
            this.$fieldType.ligerComboBox({dict: true, dictName: "RESOURCE_METADATA_TYPE"});
            rsResourceFieldInfoDialog.$fieldIsPK.ligerRadio();
            rsResourceFieldInfoDialog.$fieldIsNull.ligerRadio();
            //this.$datasetFrom.ligerRadio();
            this.$fieldRemark.ligerTextBox({width: 240});
            var data={
                name: '${entity.name}',
                columnType: '${entity.columnType}',
                columnCode: '${entity.code}',
                columnLength: '${entity.columnLength}',
                inp_field_is_PK: '${entity.primaryKey}',
                id: '${entity.id}',
                inp_field_is_null: '${entity.nullable}',
                definition: '${entity.definition}',
                logicDataSource: '${entity.logicDataSource}',
                inp_dataset_from: '${entity.dataSupportType}'
            };
            $("#div_info_form").ligerAutoForm({
                data:data,
                validate:{
                    name:"required",
                    columnType:"required",
                    columnCode:"required",
                    columnLength:"required"
                }
            });
            rsResourceFieldInfoDialog.$datasetFrom.text('${entity.dataSupportType}'==1?"事实表":"逻辑表")
            /*
            if ("${entity.dataSupportType}" == "1") {
                rsResourceFieldInfoDialog.$centerTab2.hide();
                rsResourceFieldInfoDialog.$centerTab1.show();
            } else if ("${entity.dataSupportType}" == "2") {
                rsResourceFieldInfoDialog.$centerTab1.hide();
                rsResourceFieldInfoDialog.$centerTab2.show();
            }*/
        },
        bindEvents: function () {
            this.$btnSave.click(function () {
                if(!$("#div_info_form").ligerAutoForm("validate")){
                    return;
                }
                var data = $("#div_info_form").ligerAutoForm("getData");
                if ('${mode}' == 'new') {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url: "${contextRoot}/resource/resource/createMetaData",
                        dataType: "json",
                        data: data,
                        cache: false,
                        success: function (data) {
                            if(data.successFlg){
                                //调用主页面接口，重新刷新Grid
                                parent.window.rsResourceConfig.dialogCallBack(data.message);
                            }else{
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error: function (data) {
                            $.ligerDialog.error(data.message);
                        }
                    });
                } else if ('${mode}' == 'modify') {

                    $.ajax({ //ajax处理
                        type: "POST",
                        url: "${contextRoot}/resource/resource/updateMetaData",
                        dataType: "json",
                        data: data,
                        cache: false,
                        success: function (data) {
                            if(data.successFlg){
                                //调用主页面接口，重新刷新Grid
                                parent.window.rsResourceConfig.dialogCallBack(data.message);
                            }else{
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error: function (data) {
                            $.ligerDialog.error(data.message);
                        }
                    });
                }
            });

            this.$btnCancel.click(function () {
                rsResourceFieldInfoDialog.dialog.close();
            });
        }
    }
    $(function () {
        rsResourceFieldInfoDialog.init();
    });
</script>