<%@ 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 rsResourceImportDatasetDialog = {
        dialog:frameElement.dialog,
        $form: $("#div_look_form"),//表单对象
        $id: $("#inp_resource_id"),//资源数据源
        $btnSaveALL: $("#btn_saveALL"),//保存全部按钮对象
        $btnSaveSame: $("#btn_saveSame"),//保存部分按钮对象
        $btnCancel: $("#btn_cancel"),//关闭按钮对象
        $leftDG: $("#div_look_left_datgrid"),//左边的datagrid
        $RightDG: $("#div_look_right_datgrid"),//右边的datagrid
        $div_look_form: $("#div_look_form"),//layout对象
        $div_look_form_center: $("#div_look_form_center"),//中间的layout对象
        $searchBtn: $("#div_look_form_inp_search"),//查找对象
        datasetId: "${datasetId}",//资源对应的datasetID 如果没有则为""此ID是我们自己的dataset的ID
        resourceId: "${resourceId}",//资源ID
        datasetCode: "${datasetCode}",//资源对应的datasetCode 如果没有则为""
        init: function () {
            this.initForm();
            this.bindEvents();
            this.initDatagrid();
            this.initlayout();
        },
        initlayout: function () {
            //初始化layout
            this.$div_look_form.ligerLayout({
                height:"98%",
                bottomHeight: 30,
                topHeight: 40,
                isLeftCollapse: false,//左边区域初始化不可以隐藏
                isRightCollapse: false,//左边区域初始化不可以隐藏
                allowLeftCollapse: false,//左边区域不可以隐藏
                allowRightCollapse: false,//左边区域不可以隐藏
                allowRightResize: false,
                allowLeftResize: false,
                allowBottomResize: false,
                allowTopResize: false
            });
            //隐藏layout左右两边的标题
            $("#div_look_form .l-layout-left .l-layout-header").hide();
            $("#div_look_form .l-layout-right .l-layout-header").hide();
            //l-layout-left
            $("#div_look_form .l-layout-left").css({"border": "0px"});
            $("#div_look_form .l-layout-right").css({"border": "0px"});
            $("#div_look_form .l-layout-center").css({"border": "0px"});
            $("#div_look_form .l-layout-top").css({"border": "0px"});
            $("#div_look_form .l-layout-bottom").css({"border": "0px"});
            this.$div_look_form_center.ligerLayout({
                height:"99%",
                leftWidth: 250,
                isLeftCollapse: false,//左边区域初始化不可以隐藏
                isRightCollapse: false,//左边区域初始化不可以隐藏
                allowLeftCollapse: false,//左边区域不可以隐藏
                allowRightCollapse: false,//左边区域不可以隐藏
                allowRightResize: false,
                allowLeftResize: false,
                allowBottomResize: false,
                allowTopResize: false
            });
            //隐藏layout左右两边的标题
            $("#div_look_form .l-layout-left .l-layout-header").hide();
            $("#div_look_form .l-layout-right .l-layout-header").hide();
            //l-layout-left
            $("#div_look_form .l-layout-left").css({"border": "0px"});
            $("#div_look_form .l-layout-right").css({"border": "0px"});
            $("#div_look_form .l-layout-center").css({"border": "0px"});
            $("#div_look_form .l-layout-top").css({"border": "0px"});
        },
        initForm: function () {
            var mode = '${mode}';
            this.$form.attrScan();
            this.$form.Fields.fillValues({
                id: '${entity}'
            });
            this.$form.show();
        },
        bindEvents: function () {
            var self = this.$leftDG;
            var selfRight = this.$RightDG;
            this.$btnSaveALL.click(function () {
                var dataSet = self.ligerGrid("getSelectedRow");
                var selecrMedatata = selfRight.ligerGrid("getSelectedRows");
                if (!dataSet && !rsResourceImportDatasetDialog.datasetId != "" || dataSet.length == 0) {
                    $.ligerDialog.error("请选择数据集");
                    return;
                }
                $.ligerDialog.confirm('确认导入全部数据元吗？', function (yes) {
                    if (yes) {
                        //第一次全部导入
                        dataSet.reourceId = rsResourceImportDatasetDialog.resourceId;
                        dataSet.datasetId = rsResourceImportDatasetDialog.datasetId;
                        $.ajax({ //ajax处理
                            type: "POST",
                            url: "${contextRoot}/resource/resource/improtAllMetadata",
                            dataType: "json",
                            data: dataSet,
                            cache: false,
                            success: function (data) {
                                if (data.successFlg) {
                                    parent.window.rsResourceConfig.dialogCallBack(data.message);
                                }
                                else {
                                    $.ligerDialog.error(data.message);
                                }
                            },
                            error: function (data) {
                                $.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");
                            }
                        });

                    }
                });
            });
            this.$btnSaveSame.click(function () {
                var dataSet = self.ligerGrid("getSelectedRow");
                var selecrMedatata = selfRight.ligerGrid("getSelectedRows");
                if (!dataSet && !rsResourceImportDatasetDialog.datasetId != "" || dataSet.length == 0) {
                    $.ligerDialog.error("请选择数据集");
                    return;
                }
                if (!selecrMedatata || selecrMedatata.length == 0) {
                    $.ligerDialog.error("请选择数据元");
                    return;
                }
                $.ligerDialog.confirm('确认导入选中数据元吗？', function (yes) {
                    if (yes) {
                        var reourceId = rsResourceImportDatasetDialog.resourceId;
                        $.ajax({ //ajax处理
                            type: "POST",
                            url: "${contextRoot}/resource/resource/improtSameMetadata",
                            dataType: "json",
                            data: {
                                reourceId: reourceId,
                                dataset: JSON.stringify(dataSet),
                                datasetId: rsResourceImportDatasetDialog.datasetId,
                                selecrMedatata: JSON.stringify(selecrMedatata)
                            },
                            success: function (data) {
                                if (data.successFlg) {
                                    parent.window.rsResourceConfig.dialogCallBack(data.message);
                                }
                                else {
                                    $.ligerDialog.error(data.message);
                                }
                            },
                            error: function (data) {
                                $.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");
                            }
                        });
                    }
                });
            });

            this.$btnCancel.click(function () {
                rsResourceImportDatasetDialog.dialog.close();
            });
            this.$searchBtn.ligerSearch({
                width: 180, onClick: function (value) {
                    //rsResourceImportDatasetDialog.$leftDG.queryByText("name", value);
                    rsResourceImportDatasetDialog.$leftDG.set({
                        parms: {rsResourceImportDatasetDialogName:rsResourceImportDatasetDialog.$searchBtn.val()}
                    });

                    rsResourceImportDatasetDialog.$leftDG.reload();
                    rsResourceImportDatasetDialog.$leftDG.changePage("first");
                }
            });

        },
        initDatagrid: function () {
            var this$leftDG = this.$leftDG;
            var this$RightDG = this.$RightDG;
            this.$leftDG = this.$leftDG.ligerGrid({
                url: '${contextRoot}/resource/resource/getDataSet',
                columns: [
                    {display: '数据集名称', name: 'name', width: '93%', isAllowHide: false}
                ],
                validate: true,
                rownumbers: false,
                parms: {datasetId: rsResourceImportDatasetDialog.datasetId},
                allowAdjustColWidth: false,
                width: 240,
                fitColumn: false,
                height: 360,
                usePager: false,//不显示分页
                onSelectRow: function (rowdata, rowid, rowobj) {
                    this$RightDG.ligerGrid("loadServerData", {datasetCode: rowdata.code, datasetId: rsResourceImportDatasetDialog.datasetId});
                },
                isChecked: function (row) {
                    if (row.checked == "true") {
                        return true;
                    }
                    else {
                        return false;
                    }
                }, onCheckRow: function (checked, rowdata, rowindex) {
                    //单选
                    for (var rowid in this.records)
                        this.unselect(rowid);
                    this.select(rowindex);
                },onAfterShowData: function () {
                    this.select(0);
                }
            });

            this.$RightDG = this.$RightDG.ligerGrid({
                url: "${contextRoot}/resource/resource/getMetaDataByDatasetId",
                columns: [
                    {display: '数据元名称', name: 'name', width: '90%', isAllowHide: false}
                ],

                checkbox: true,
                validate: true,
                parms: {datasetId: rsResourceImportDatasetDialog.datasetId, datasetCode: rsResourceImportDatasetDialog.datasetCode},
                rownumbers: false,
                allowAdjustColWidth: false,
                width: 240,
                fitColumn: false,
                height: 360,
                usePager: false,//不显示分页
                isChecked: function (row) {
                    if (row.checked == "true") {
                        return true;
                    }
                    else {
                        return false;
                    }
                },
            });
        }
    }
    $(function () {
        rsResourceImportDatasetDialog.init();
    });
</script>