<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/Main.Master" AutoEventWireup="true" CodeBehind="RaporYetkileriListesi.aspx.cs" Inherits="Giza.Web.Moduller.RaporYetkileri.RaporYetkileriListesi" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderHeader" runat="server">
    <link href="<%= Page.ResolveUrl("~/Content/vendors/bootgrid/jquery.bootgrid.css") %>" rel="stylesheet" />
    <script src="<%= Page.ResolveUrl("~/Content/vendors/bootgrid/jquery.bootgrid.js") %>"></script>
    <script src="<%= Page.ResolveUrl("~/Content/vendors/bootstrap-wizard/jquery.bootstrap.wizard.js") %>"></script>
    <script src="<%= Page.ResolveUrl("~/Content/vendors/bower_components/bootstrap-select/dist/js/bootstrap-select.js") %>"></script>
    <link href="<%=Page.ResolveUrl("~/Content/vendors/bower_components/bootstrap-select/dist/css/bootstrap-select.css") %>" rel="stylesheet" />
    <script src="<%= Page.ResolveUrl("~/Content/vendors/bower_components/bootstrap-select/dist/js/bootstrap-select.js") %>"></script>
    <script src="<%= Page.ResolveUrl("~/Content/js/datepicker-tr.js") %>"></script>
    <style type="text/css">
        .ui-datepicker-calendar th span{
            color:#433d4a;
        }
        .table > tbody > tr > td:first-child{
            padding-left:0px!important;
        }
    </style>
    
    <script>
        $(document).ready(function () {
            var KullaniciId = '<%=Request["KullaniciId"]%>';
            var KullaniciGrupId = '<%=Request["KullaniciGrupId"]%>';
            if (KullaniciId) {
                $('#cmbKullanici').val(KullaniciId);
                Kullanici();

            }
            else if (KullaniciGrupId) {
                $('#cmbKullaniciGrup').val(KullaniciGrupId);
                KullaniciGrup();
            }


            dataListele();
        });


        function dataListele() {
            var KullaniciId = $('#cmbKullanici').val();
            var KullaniciGrupId = $('#cmbKullaniciGrup').val();
            if ((!KullaniciId || KullaniciId == "0") && (!KullaniciGrupId || KullaniciGrupId == "0")) {
                return;
            }
            var Id = "";
            var Type = "";
            if (KullaniciId == 0 && KullaniciGrupId != 0) {
                Id = KullaniciGrupId;
                Type = "KullaniciGrup";
            }
            else {
                Id = KullaniciId;
                Type = "Kullanici";
            }

            $("#grid-data").bootgrid({
                ajax: true,
                url: "<%= Page.ResolveUrl("~/DataService.asmx/GetRaporYetkileriList")%>",
                requestHandler: function (request) {
                    request.Id = Id;
                    request.Type = Type;
                    return request;
                },

                navigation: 0,
                rowCount: -1,
                labels: {
                    noResults: "Gösterilebilecek kayıt bulunamadı",
                    search: "Arama",
                    all: "Tümü",
                    infos: "{{ctx.total}} Kayıttan {{ctx.start}} - {{ctx.end}} Arası Kayıtlar",
                    loading: "Yükleniyor...",
                    refresh: "Yenile"
                },
                css: {
                    icon: 'zmdi icon',
                    iconColumns: 'zmdi-view-module',
                    iconDown: 'zmdi-expand-more',
                    iconRefresh: 'zmdi-refresh',
                    iconUp: 'zmdi-expand-less'
                },
                formatters: {
                    "checkboxFormatter": function (column, row) {
                        var columnName = column.id;
                        var checkedValue = row[columnName] == 1 ? "checked" : "";
                        var chckId = columnName + "_";
                        return "<label class=\"checkbox checkbox-inline m-r-20\" style=\"margin-top: 5px;\">" +
                                    "<input id=\"" + chckId + "\"name=\"" + chckId + "\" type=\"checkbox\" tabindex=\"1\" onclick=\"setCheckBoxesTick(this,'" + columnName + "','" + row.KullaniciId + "','" + row.Id + "')\" " + checkedValue + " />" +
                                    "<i class=\"input-helper\"></i>" +
                                "</label>";
                    },
                    "dateTimeFormatter": function (column, row) {
                        var columnName = column.id;
                        var columnValue = row[columnName];
                        return " <div class='fg-line'> <input type='text' class='datepicker1 form-control input-sm' value=\"" + columnValue + "\" placeholder='ör: 23.05.2017' onchange=\"setDateTime(this,'" + columnName + "','" + row.KullaniciId + "','" + row.Id + "')\"    /> </div>"

                        //return "<input type='text' onfocusout=\"setDateTime(this,'" + columnName + "','" + row.KullaniciId + "','" + row.Id + "')\" value=\"" + columnValue + "\" class='form-control input-mask' data-mask='00/00/0000' placeholder='ör: 23/05/2017' maxlength='10' autocomplete='off'>";
                    }
                }
            }).on("loaded.rs.jquery.bootgrid", function (e) {
                $('.datepicker1').datepicker();
                //$('.datepicker1').datepicker( "option", "dateFormat", "dd.mm.yy");
            });
        }
        function setCheckBoxesTick(checkbox, columnName, KullaniciId, Id) {

            var data = $('#grid-data').bootgrid().data('.rs.jquery.bootgrid');
            var rows = data.currentRows;

            var row = $.grep(rows, function (e) { return e.KullaniciId == KullaniciId && e.Id == Id; })[0];

            var isCanMatchColumn = columnName == "YetkisiVarMi";
            var chk = $(checkbox);

            var checkboxes = chk.parent().parent().parent().find(':checkbox');

            row[columnName] = chk.is(':checked') ? 1 : 0;

            if (chk.is(':checked')) {
                if (!isCanMatchColumn) {
                    $(checkboxes[0]).prop('checked', true);
                    row.YetkisiVarMi = 1;
                }
            }
            else {
                if (isCanMatchColumn) {
                    $(checkboxes[0]).prop('checked', false);
                    row.YetkisiVarMi = 0;

                }
            }
        }
        function setDateTime(input, columnName, KullaniciId, Id) {

            var data = $('#grid-data').bootgrid().data('.rs.jquery.bootgrid');
            var rows = data.currentRows;
            var row = $.grep(rows, function (e) { return e.KullaniciId == KullaniciId && e.Id == Id; })[0];

                row[columnName] = $(input).val();

        }

        function SaveMatchs() {
            var data = $('#grid-data').bootgrid().data('.rs.jquery.bootgrid');
            var rows = data.currentRows;
            var Type = "";
            if (document.getElementById("rdBtnKullanici").checked == true) {
                Type = "Kullanici";
            }
            else if (document.getElementById("rdBtnKullaniciGrup").checked == true) {
                Type = "KullaniciGrup";
            }
            console.log(JSON.stringify({ valuesList: rows, Type: Type }));
            try {
                $.ajax({
                    type: "POST",
                    url: "<%= Page.ResolveClientUrl("~/DataService.asmx/SaveRaporYetkileri")%>",
                    data: JSON.stringify({ valuesList: rows, Type: Type }),
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function (response) {
                        notify("Eşleştirmeler başarı ile kaydedildi.", "success", "RaporYetkileriListesi.aspx");
                    },
                    error: function (xhr, status, error) {
                        
                        notify("Eşleştirmeler kaydedilirken bir hata oluştu. Hata:" + error.responseText.substring(0, 200) + "...", "danger", "", 10000);
                    }
                });
            } catch (e) {
                console.log(e);
            }
        }
        // Tüm Yetkileri Ver
        function AllCheck() {
            var checkboxes = $('#grid-data').parent().parent().parent().find(':checkbox');
            var data = $('#grid-data').bootgrid().data('.rs.jquery.bootgrid');
            var rows = data.currentRows;
            for (var i = 0, n = checkboxes.length; i < n; i++) {
                $(checkboxes[i]).prop('checked', true);
            }
            for (var i = 0, n = rows.length; i < n; i++) {
                rows[i].YetkisiVarMi = 1;
                rows[i].YetkisiVarMiButon1 = 1;
                rows[i].YetkisiVarMiButon2 = 1;
                rows[i].YetkisiVarMiButon3 = 1;

            }
        }
        // Tüm Yetkileri Geri Al
        function RemoveAllCheck() {
            var checkboxes = $('#grid-data').parent().parent().parent().find(':checkbox');
            var data = $('#grid-data').bootgrid().data('.rs.jquery.bootgrid');
            var rows = data.currentRows;
            for (var i = 0, n = checkboxes.length; i < n; i++) {
                $(checkboxes[i]).prop('checked', false);
            }
            for (var i = 0, n = rows.length; i < n; i++) {
                rows[i].YetkisiVarMi = 0;
                rows[i].YetkisiVarMiButon1 = 0;
                rows[i].YetkisiVarMiButon2 = 0;
                rows[i].YetkisiVarMiButon3 = 0;
            }
        }

        function Kullanici() {
            document.getElementById("rdBtnKullanici").checked = true;
            document.getElementById("rdBtnKullaniciGrup").checked = false;
            document.getElementById("KullaniciDiv").style.display = '';
            document.getElementById("KullaniciGrupDiv").style.display = 'none';


            var KullaniciId = $('#cmbKullanici').val();
            if (KullaniciId != "0") {
                document.getElementById("TableDiv").style.display = '';
                document.getElementById("SaveDiv").style.display = '';
            }
            else {
                document.getElementById("TableDiv").style.display = 'none';
                document.getElementById("SaveDiv").style.display = 'none';
            }
        }
        function KullaniciGrup() {
            document.getElementById("rdBtnKullaniciGrup").checked = true;
            document.getElementById("rdBtnKullanici").checked = false;
            document.getElementById("KullaniciGrupDiv").style.display = '';
            document.getElementById("KullaniciDiv").style.display = 'none';


            var KullaniciGrupId = $('#cmbKullaniciGrup').val();
            if (KullaniciGrupId != "0") {
                document.getElementById("TableDiv").style.display = '';
                document.getElementById("SaveDiv").style.display = '';
            }
            else {
                document.getElementById("TableDiv").style.display = 'none';
                document.getElementById("SaveDiv").style.display = 'none';
            }
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderBreadcrumbs" runat="server">
    <ol class="breadcrumb" style="margin-bottom: 5px;">
        <li><a href="<%= Page.ResolveUrl("~/AnaSayfa.aspx") %>">Anasayfa</a></li>
        <li><a href="<%= Page.ResolveUrl("~/Moduller/RaporYetkileri/RaporYetkileriListesi.aspx") %>">Rapor Yetkileri</a></li>
    </ol>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolderMain" runat="server">
    <div class="card">
        <div class="card-header">
            <h2>Rapor Yetkileri</h2>
        </div>
        <div class="card-body card-padding">
            <%--Yetkilendirilecek Grup veya Kullancı Kısımları--%>
            <div class="row">
                <div class="col-sm-4">
                    <div class="form-group fg-line">
                        <p class="f-500 m-b-5 f-700">Kullanıcı</p>
                        <asp:RadioButton ID="rdBtnKullanici" ClientIDMode="Static" runat="server" onclick="Kullanici();" class="form-control input-sm" />
                    </div>
                </div>
                <div class="col-sm-4">
                    <div class="form-group fg-line">
                        <p class="f-500 m-b-5 f-700">Kullanıcı Grup</p>
                        <asp:RadioButton ID="rdBtnKullaniciGrup" ClientIDMode="Static" class="form-control input-sm" onclick="KullaniciGrup();" runat="server" />
                    </div>
                </div>

            </div>

            <div class="row">
                <div class="col-sm-4" id="KullaniciDiv" style="display: none">
                    <div class="form-group fg-line">
                        <p class="f-500 m-b-5 f-700">Kullanıcı</p>
                        <asp:DropDownList ID="cmbKullanici" data-live-search="true" class="selectpicker" ViewStateMode="Enabled" runat="server" ClientIDMode="Static" AutoPostBack="true"></asp:DropDownList>
                    </div>
                </div>
                <div class="col-sm-4" id="KullaniciGrupDiv" style="display: none">
                    <div class="form-group fg-line">
                        <p class="f-500 m-b-5 f-700">Kullanıcı Grup</p>
                        <asp:DropDownList ID="cmbKullaniciGrup" class="selectpicker" ViewStateMode="Enabled" runat="server" ClientIDMode="Static" AutoPostBack="true"></asp:DropDownList>
                    </div>
                </div>
                <div class="col-sm-4" id="SaveDiv" style="display: none">
                    <asp:Button runat="server" ID="btnSave" class="btn bgm-green waves-effect pull-right" Text="Kaydet" OnClientClick="SaveMatchs(); return false;" />
                    <div style="clear: both;"></div>
                </div>
            </div>

            <%--END Yetkilendirilecek Grup veya Kullancı Kısımları--%>

            <%--Yetki Seçimleri Grid Kısmı--%>
            <div id="TableDiv" style="display: none">
                <br />
                <table class="table table-bordered" style="border-bottom: 1px solid #F2F2F2;" aria-busy="false">
                    <tr>
                        <td>
                            <asp:HyperLink ID="HyperRemoveAll" ClientIDMode="Static" CssClass="btn bgm-teal waves-effect   pull-right" onclick="RemoveAllCheck();" runat="server" Style="width: auto; margin-right: 10px;" Text="Tüm Yetkileri Geri Al"></asp:HyperLink>
                            <asp:HyperLink ID="HyperAllCheck" ClientIDMode="Static" CssClass="btn bgm-teal waves-effect   pull-right" onclick="AllCheck();" runat="server" Style="width: auto; margin-right: 10px;" Text="Tüm Yetkileri Ver"></asp:HyperLink>
                        </td>
                    </tr>
                </table>
                <table id="grid-data" class="table table-bordered"  style="border-bottom: 1px solid #F2F2F2;" aria-busy="false">
                    <thead>
                        <tr>
                            <th data-column-id="RaporAdi"  data-sortable="true">Rapor Adı</th>
                            <th data-column-id="RaporGrupAdi" data-sortable="true" >Rapor Grup Adı</th>
                            <th data-column-id="YetkisiVarMi" data-formatter="checkboxFormatter" data-sortable="false">Görüntüleme</th>
                            <th data-column-id="YetkisiVarMiButon1" data-formatter="checkboxFormatter" data-sortable="false">1.Buton</th>
                            <th data-column-id="YetkisiVarMiButon2" data-formatter="checkboxFormatter" data-sortable="false">2.Buton</th>
                            <th data-column-id="YetkisiVarMiButon3" data-formatter="checkboxFormatter" data-sortable="false">3.Buton</th>
                            <th data-column-id="GoruntulenmeBaslangicTarihi" data-formatter="dateTimeFormatter" data-sortable="false">Baş. Tarih</th>
                            <th data-column-id="GoruntulenmeBitisTarihi" data-formatter="dateTimeFormatter"  data-sortable="false">Bit. Tarih</th>
                        </tr>
                    </thead>
                </table>
            </div>
            <%--END Yetki Seçimleri Grid Kısmı--%>
        </div>
    </div>


</asp:Content>
