<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="QueryBuilderBlankPage.aspx.cs" Inherits="BPMS.FormGeneratorDepartment.QueryBuilderBlankPage" %>

<%@ Register Src="~/lib/modules/aqb/userControl/QueryBuilder.ascx" TagPrefix="uc1" TagName="QueryBuilder" %>
<%--<%@ Register Src="~/FormGeneratorDepartment/Module/jqGrid.ascx" TagPrefix="uc1" TagName="jqGrid" %>--%>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script src="/assets/js/jquery.min.js"></script>
    <script src="/assets/js/bootstrap.min.js"></script>
    <script src="/assets/js/jquery1x.min.js"></script>
    <link href="/assets/css/bootstrap.css" rel="stylesheet" />
    <%--<link href="/assets/css/ace-rtl.min.css" rel="stylesheet" />--%>
    <script>
        var Query = '';
        function setvalue(input) {
            Query = input;
        }
        function Refresh() {
            GetValue();
            window.setTimeout(function () { $('#myspan').val(Query); }, 1000);
        }
    </script>
    <style>
        .mag-10 {
            margin: 10px;
        }
    </style>
</head>
<body id="bodyTag"  style=" font-family: Tahoma;">
    <form id="form1" runat="server">
        <asp:ScriptManager ID="UpdatePanel1" runat="server"></asp:ScriptManager>
        <uc1:QueryBuilder runat="server" ID="QueryBuilder1" Visible="true" />
        <%--<ul class="nav nav-tabs">
            <li class="active"><a data-toggle="tab" href="#home">ساخت کوئری</a></li>
            <li><a data-toggle="tab" href="#menu1" onclick="LoadParameter();">تست کوئری</a></li>
            <li><a data-toggle="tab" href="#menu2">راهنمایه جداول</a></li>
            <li>
                <asp:Button ID="ReturnSqlQueryBtn" class="ReturnSqlQueryBtn btn btn-success btn-sm"
                    Style="font-size: 14px; margin-top: 5px;" runat="server" Text="ثبت"
                    OnClientClick="return CheckExistParam();" OnClick="ReturnSqlQueryBtn_Click" /><!--UseSubmitBehavior="false"-->
            </li>
        </ul>
        <div class="tab-content" id="Loading">
            <div id="home" class="tab-pane fade in active">

            </div>
            <div id="menu1" class="tab-pane fade">
                <div style="margin: 5px;">
                    <button type="button" id="BtnmyModal1" class="btn btn-info" data-toggle="modal" data-target="#myModal1"
                        style="display: none;">
                        تست کوئری</button>
                </div>
                <div class="mag-10">
                </div>
                <!-- Modal -->
                <div class="modal fade" id="myModal1" role="dialog" data-url="/DataGrid/LoadData">
                    <div class="modal-dialog">
                        <!-- Modal content-->
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                                <h4 class="modal-title">لیست پارامتر ها</h4>
                            </div>
                            <div class="modal-body" id="param"></div>
                            <div class="modal-footer">
                                <input type="button" class="btn btn-success btn-xs btn btn-primary btn-xs ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="font-size: 14px;"
                                    value="ثبت" onclick='LoadjqGridParam();' />
                                <input type="button" class="btn btn-xs btn btn-primary btn-xs ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="font-size: 14px"
                                    value="انصراف" data-dismiss="modal" id="btnClose" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div id="menu2" class="tab-pane fade">
                <asp:GridView ID="GridView1" runat="server" DataKeyNames="CortexID" CssClass="table table-striped table-bordered table-hover" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField HeaderText="شماره فرم" DataField="CortexID" />
                        <asp:BoundField HeaderText="نام فرم" DataField="CortexName" />
                        <asp:BoundField HeaderText="نام جدول پایگاه داده" DataField="CortexTableName" />
                        <asp:TemplateField HeaderText="نمایش فیلدهای جدول">
                            <ItemTemplate>
                                <a href="#" data-toggle="modal" data-target="#ViewField" onclick="ViewField(<%# Eval("CortexID") %>,'<%# Eval("CortexTableName") %>');">نمایش فیلدها</a>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <!-- Modal -->
                <div class="modal fade" id="ViewField" role="dialog">
                    <div class="modal-dialog">
                        <!-- Modal content-->
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                                <h4 class="modal-title">لیست فیلدها</h4>
                            </div>
                            <div class="modal-body" id="Tbl_Field"></div>
                            <div class="modal-footer">
                                <input type="button" class="btn btn-xs btn btn-primary btn-xs ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="font-size: 14px"
                                    value="انصراف" data-dismiss="modal" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>--%>
    </form>
</body>
</html>



<%--<link href="../assets/css/showLoading.css" rel="stylesheet" />
<script src="../assets/js/jquery.showLoading.min.js"></script>
<link href="/assets/css/jquery-confirm.min.css" rel="stylesheet" />
<script src="/assets/js/jquery-confirm.min.js"></script>
<script src="/Scripts/DevExpress/jszip.min.js" ></script>
<link href="../Styles/DevExpress/dx.common.css" rel="stylesheet" />
<link href="../Styles/DevExpress/dx.spa.css" rel="stylesheet" />
<link href="../Styles/DevExpress/dx.light.css" rel="stylesheet" />
<script src="../Scripts/DevExpress/dx.all.js"></script>
<script src="../Scripts/GridHelper.js"></script>
<script src="../Scripts/DevExpress/devextreme-localization/dx.messages.fa.js"></script>--%>

<style>
* {
    font-family: verdana,tahoma !important;
    font-size: 12px;
}
html, body{
    background-color:#F6F6F6 !important;
}
#qb-ui-tree{
    margin-bottom:0px !important;
}
#qb-ui-grid{
    background-color:#FFF;
    border:solid 1px #DDD !important;
    overflow-y:scroll;
    overflow-x:hidden;
}
#qb-ui-canvas{
    height:200px;
}
.qb-ui-tree-item-block {
    height: 258px !important;
}
.block-flat{
    box-shadow:unset !important;
    border:solid 1px #DDD !important;
}
.qb-ui-tree .qb-ui-tree.ui-widget{
    padding:10px !important;
}
.qb-ui-tree .qb-ui-tree-selects{
    padding:10px !important;
    padding-left: 5px !important;
}
.qb-ui-table-titlebar .ui-icon {
    height: 20px;
}
.titlebar-button:hover, .titlebar-button:focus{
    padding: 1px 0 0 1px;
}
.titlebar-button {
    height: 20px;
    padding-top: 0px;
}
.foot{
    height:5px;
}
.qb-ui-table {
    box-shadow: unset;
    border: solid 1px #3d566d;
}
</style>

<script type="text/javascript">
    $(document).ready(function () {
        $("input[name='txtfilter']").keydown(function (event) {
            if (event.keyCode == 13) {
                LoadjqGridParam();
                return false;
            }
        });
        $(document).ajaxStart(function () {
            // jQuery('#bodyTag').showLoading();
            if (parent && parent.loadPanel && parent.loadPanel.indicatorShow) {
                parent.loadPanel.indicatorShow();
            }
        })
        //.ajaxComplete(function () { $("#loadingTreeView").hide();})
        .ajaxStop(function () {
            if (parent && parent.loadPanel && parent.loadPanel.indicatorHide) {
                parent.loadPanel.indicatorHide();
            }
            //jQuery('#bodyTag').hideLoading();
        });
    });
    function LoadParameter() {
        var ValParams = new Array();
        var FieldParams = new Array();


        QB.Web.Application.getQueryParams(getQueryParams);

    }
    var params;
    function getQueryParams(queryParams) {
        $('#menu1 .mag-10').html('').append($('<div id="datagrid"></div>'));

        params = queryParams;
        console.log(queryParams);
        if (params == null || params.length == 0) {
            var querySQL = QB.Web.Application.SQL;//SQL;//.QueryTransformerSQL;
            if (typeof (querySQL) == "undefined" || querySQL == "") {
                dialogPlugin.toastShow("error", "هیچ دستوری ثبت نشده است");
            }
            else {

                LoadjqGridParam();
            }
        }
        else {
            $('#BtnmyModal1').removeAttr("style");
            $('#BtnmyModal1').trigger('click');
            var html = '<table class="table table-striped table-bordered table-hover no-margin-bottom no-border-top">';
            html += '<tr><th>نام پارامتر</th><th>نوع پارامتر</th><th>مقدار</th></tr>';
            for (var i = 0; i < params.length; i++) {
                var param = params[i];
                if (html.indexOf(param.FullName) == -1) {
                    html += '<tr>';
                    html += '<td>' + param.FullName + '</td>';
                    html += '<td>' + param.DataType + '</td>';
                    html += '<td><input type="text" class="form-control" id="' + param.FullName + '" name="' + param.Name + '" onkeydown="if(event.keyCode == 13) LoadjqGridParam();"></td>';
                    html += '</tr>';
                }
            }
            html += '</table>';
            $("#param").html($('<form>' + html + '</form>'));
            //$("#param").html(html);
        }
    }
    function LoadjqGridParam() {
        var url = $('#myModal1').data('url'),
            data = {
                ConnectionId: getUrlVars()["ConnectionId"],
                querySQL: QB.Web.Application.SQL,
                paramInfo: $("#param form").serializeArray()
            };

        gridHelper.buidDataTableGrid(url, data, "#datagrid", function () {
            $('#btnClose').trigger('click');
            return true;
        });
    }

    function ViewField(CortexID, TableName) {
        $("#Tbl_Field").html("");
        $.ajax({
            type: "POST",
            url: "QueryBuilderBlankPage.aspx/ViewField",
            data: "{'CortexID':" + CortexID + ",'TableName':'" + TableName + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            },
            success: function (r) {
                $("#Tbl_Field").html(r.d);
            },
            error: function (error) {
                alert(error);
            }
        });
    }


    function getParamsCheck(queryParams) {

        if (queryParams == null || queryParams.length == 0) {
            return true;
        }
        else {

            var Params = "";
            for (var i = 0; i < queryParams.length; i++) {
                if (Params.indexOf(queryParams[i].Name) == -1) {
                    Params += (Params == "" ? "" : ",") + "'" + queryParams[i].Name + "'";
                }
            }
            var jsonData = JSON.stringify({ ResourceID: eval(<%=ResourceID%>), DBType: eval(<%=DBType%>), Params: Params });
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                url: "/FormGeneratorDepartment/QueryBuilderBlankPage.aspx/CheckExistParam",
                data: jsonData,
                success: function (Data) {
                    if (Data.d.toString() == "0") {
                        return true;
                    }
                    else {
                        $.confirm({
                            title: 'هشدار',
                            content: 'شما تغییر پارامتر داشته اید.با این عمل ممکن است نمایش داده گزارشات  یا داشبورد تغییر کند. <br />آیا مایل هستید؟',
                            confirmButton: 'تایید',
                            cancelButton: 'انصراف',
                            confirmButtonClass: 'btn-info',
                            cancelButtonClass: 'btn-danger',
                            //  confirmButtonClass: 'btn-primary',
                            closeIcon: true, // close icon will be moved to left if RTL is set to true.
                            rtl: true,
                            confirm: function () {
                                return true;
                            },
                            cancel: function () {
                                return false;
                            }
                        });
                    }
                },
                error: function (x) {
                    alert(x.toString());
                }
            });
        }
    }
    function CheckExistParam() {

        QB.Web.Application.getQueryParams(getParamsCheck);
        return getParamsCheck();
    }

    function getUrlVars() {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }

    //$(function () {
    //    var core = QB.Web.Core;
        
    //    core.on(core.Events.SyncFinished, firstSyncHandler);

    //    function firstSyncHandler() {
    //        alert('sad');
    //        // do awesome actions
    //        core.removeListener(core.Events.SyncFinished, firstSyncHandler);
    //    }
    //});

    (function () {
        function disableSpellCheck() {
            let selector = 'input[type=text], textarea';
            let textFields = document.querySelectorAll(selector);

            textFields.forEach(
                function (field, _currentIndex, _listObj) {
                    field.spellcheck = false;
                }
            );
        }

        disableSpellCheck();
    })();

</script>
