<!-- Form -->
<div class="content-animation animated">
    <div class="row clearfix fBTitrBold ">

        <!-- #END# CKEditor -->
        <!-- TinyMCE -->
        <div class="row clearfix">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <div class="card">
                    <div class="header text-right">
                        <h2>ایجاد پیام عمومی برای کاربران</h2>
                    </div>
                    <div class="body">
                            <textarea id="tinymce">
                                <h2>WYSIWYG Editor</h2>
                                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ullamcorper sapien non nisl facilisis bibendum in quis tellus. Duis in urna bibendum turpis pretium fringilla. Aenean neque velit, porta eget mattis ac, imperdiet quis nisi. Donec non dui et tortor vulputate luctus. Praesent consequat rhoncus velit, ut molestie arcu venenatis sodales.</p>
                                <h3>Lacinia</h3>
                                <ul>
                                    <li>Suspendisse tincidunt urna ut velit ullamcorper fermentum.</li>
                                    <li>Nullam mattis sodales lacus, in gravida sem auctor at.</li>
                                    <li>Praesent non lacinia mi.</li>
                                    <li>Mauris a ante neque.</li>
                                    <li>Aenean ut magna lobortis nunc feugiat sagittis.</li>
                                </ul>
                                <h3>Pellentesque adipiscing</h3>
                                <p>Maecenas quis ante ante. Nunc adipiscing rhoncus rutrum. Pellentesque adipiscing urna mi, ut tempus lacus ultrices ac. Pellentesque sodales, libero et mollis interdum, dui odio vestibulum dolor, eu pellentesque nisl nibh quis nunc. Sed porttitor leo adipiscing venenatis vehicula. Aenean quis viverra enim. Praesent porttitor ut ipsum id ornare.</p>
                            </textarea>
                    </div>
                </div>
            </div>
        </div>
        <!-- #END# TinyMCE -->

        <div class="row clearfix text-center">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 ">
                <div class="card ">
                    <div class="header">
                        <h2>لیست پیام ها</h2>
                    </div>
                    <div class="body ">
                        <div class="row">
                            <div class="col-md-12 ">
                                <div class="table-responsive text-center p-t-20 align-center no-overflow-y no-overflow-x">
                                    <table class="table table-bordered table-striped table-hover dataTable js-exportable text-center full_width align-center">
                                        <thead>
                                        <tr class="align-center">
                                            <td class="text-center">تنظیمات</td>
                                            <td class="text-center">موضوع پیام</td>
                                            <td class="text-center">نوع پیام</td>
                                            <td class="text-center">شماره پیام</td>
                                            <td class="text-center">شماره</td>
                                        </tr>
                                        </thead>
                                        <tbody class="bordered align-center text-center">


                                        </tbody>
                                        <tfoot>
                                        <tr class="align-center text-center">
                                            <td class="text-center">تنظیمات</td>
                                            <td class="text-center">موضوع پیام</td>
                                            <td class="text-center">نوع پیام</td>
                                            <td class="text-center">شماره پیام</td>
                                            <td class="text-center">شماره</td>
                                        </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<!-- #END# Form -->

<script type="text/javascript">
    (function ($) {
        let DataStructuresNotification = {
            OnInit: null,
            global: {
                PatternName: "",
                NotificationID: -1,
                NotificationAdd: true,
                NotificationList: null,
            },
            OnSaveName: null,
            OnSaveContent: null,

            DataTable: {
                OnInit: null,
                table: null,
                OnEventListener: null,
                event: {
                    OnDelete: null,
                    OnEdit: null,
                    OnActive: null
                }
            }
        };

        DataStructuresNotification.DataTable.event.OnDelete = function (index, tr) {
            swal({
                title: "آیا این مورد حذف شود؟",
                text: "شما قادر به بازگردانی این مورد نخواهید بود",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "بله حذف شود",
                cancelButtonText: "نه حذف نشود",
                closeOnConfirm: false,
                closeOnCancel: false
            }, function (isConfirm) {
                if (isConfirm) {
                    LoadData({
                        request: 'proxy',
                        object: {
                            user: user.uid,
                            mode: 0,//notification
                            ID: index
                        },
                        action: 'notificationController',
                        "header-type": 'delete'
                    }, null, false, function (data) {
                        if (data === '1') {
                            tr.fadeOut(function (e) {
                                DataStructuresNotification.DataTable.table.row(tr)
                                    .remove()
                                    .draw();
                            });
                            swal("حذف شد", "این مورد حذف شد", "success");
                        } else {
                            swal("خطا", "این درخواست انجام نشد", "error");
                        }
                    });
                } else {
                    swal("لغو شد", "این درخواست لغو شد", "error");
                }
            });
        };
        DataStructuresNotification.DataTable.event.OnEdit = function (id, index) {
            let global = DataStructuresNotification.global;
            if (global.NotificationList[index].ID === id) {
                global.NotificationID = id;
                global.PatternName = global.NotificationList[index].name;
                global.NotificationAdd = false;
                tinyMCE.activeEditor.setContent(global.NotificationList[index].content);
            } else {
                swal({
                    title: "اخطار",
                    text: "<span style=\"color: #CC0000\">خطا در یافتن پیام <span>",
                    html: true
                });
            }

        };
        DataStructuresNotification.DataTable.event.OnActive = function (index, btn) {
            let data_value = btn.attr("data-value"), text = "";
            if (data_value === '0') {
                text = "غیر قابل نمایش";
            } else {
                text = "قابل نمایش";
            }
            swal({
                title: "آیا نمایش این مورد ویرایش شود؟",
                text: " این مورد " + text + " می شود ",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "بله " + text + " شود",
                cancelButtonText: "نه " + text + " نشود",
                closeOnConfirm: false,
                closeOnCancel: false
            }, function (isConfirm) {
                if (isConfirm) {
                    LoadData({
                        request: 'proxy',
                        object: {
                            user: user.uid,
                            mode: 0,//notification
                            show: data_value,
                            ID: index
                        },
                        action: 'notificationController',
                        "header-type": 'active'
                    }, null, false, function (data) {
                        if (data === '1') {
                            if (data_value === '1') {
                                btn.attr('data-value', 0);
                                btn.removeClass('btn-warning').addClass('btn-info');
                                btn.find('i').empty().append('visibility');
                            } else {
                                btn.attr('data-value', 1);
                                btn.removeClass('btn-info').addClass('btn-warning');
                                btn.find('i').empty().append('visibility_off');
                            }
                            swal(text + " شد", "این مورد " + text + " شد", "success");
                        } else {
                            swal("خطا", "این درخواست انجام نشد", "error");
                        }
                    });
                } else {
                    swal("لغو شد", "این درخواست لغو شد", "error");
                }
            });
        };

        DataStructuresNotification.DataTable.OnEventListener = function () {
            $(".js-exportable tbody").find("button").unbind().bind("tap, click", function (e) {
                let btn = $(this),
                    mode = btn.attr("data-mod");
                switch (mode) {
                    case "notification":
                        let action = btn.attr("data-action"),
                            id = btn.attr("data-id");
                        switch (action) {
                            case "delete-item":
                                DataStructuresNotification.DataTable.event.OnDelete(id, btn.parents('tr'));
                                // OnDeleteButton(id, btn.parents('tr'));
                                break;
                            case "edit-item":
                                DataStructuresNotification.DataTable.event.OnEdit(id, btn.attr("data-index"));
                                // OnEditButton(id, btn.attr("data-index"));
                                break;
                            case "show-item":
                                DataStructuresNotification.DataTable.event.OnActive(id, btn);
                                //OnDeActiveButton(id, btn);
                                break;
                            default:
                                break;
                        }
                        break;
                    default:
                        break;
                }
            });

        };

        DataStructuresNotification.DataTable.OnInit = function () {
            LoadData({
                request: 'proxy',
                object: {
                    user: user.uid,
                    mode: 0,
                },
                action: 'notificationController',
                "header-type": "load"
            }, null, true, function (data) {
                DataStructuresNotification.global.NotificationList = data;
                if (DataStructuresNotification.DataTable.table !== null)
                    DataStructuresNotification.DataTable.table.destroy();
                DataStructuresNotification.DataTable.table = $('.js-exportable').DataTable({
                    "data": data,
                    "fnInitComplete": function (e) {
                        DataStructuresNotification.DataTable.OnEventListener();
                    },
                    columns: [
                        {"data": "option-btn"},
                        {"data": "name"},
                        {"data": "type"},
                        {"data": "nCode"},
                        {"data": "counter"}
                    ],
                    responsive: true,
                    "language": {
                        "lengthMenu": "نمایش _MENU_ کاربر در صفحه",
                        "zeroRecords": " چیزی یافت نشد",
                        "info": "نمایش از _START_ تا _END_ در _TOTAL_ رکورد",
                        "infoEmpty": "<center>رکوردی پیدا نشد</center>",
                        "infoFiltered": "(filtered from _MAX_ total records)",
                        "paginate": {
                            "next": "بعدی",
                            "previous": "قبلی"
                        }
                    }
                });
            });
        };

        DataStructuresNotification.OnSaveName = function (e = null) {
            swal({
                title: "نام پیام",
                text: "نام پیام را وارد کنید",
                type: "input",
                showCancelButton: true,
                closeOnConfirm: false,
                animation: "slide-from-top",
                inputPlaceholder: "نام پیام",
                inputValue: DataStructuresNotification.global.PatternName
            }, function (inputValue) {
                if (inputValue === false) return false;
                if (inputValue === "") {
                    swal.showInputError("شما باید نام پیام را وارد کنید");
                    return false
                }
                DataStructuresNotification.global.PatternName = inputValue;
                swal("انجام شد", inputValue, "success");
                if (e != null) {
                    e();
                }
            });
        };

        DataStructuresNotification.OnSaveContent = function (title, content, ItemID = 0, IsAddNew = true) {
            LoadData({
                request: 'proxy',
                object: {
                    user: user.uid,
                    title: title,
                    content: content,
                    mode: 0,//notification
                    ID: ItemID
                },
                action: 'notificationController',
                "header-type": (IsAddNew ? "add" : "edit")
            }, null, false, function (data) {
                if (data === '1') {
                    tinyMCE.activeEditor.setContent('');
                    DataStructuresNotification.global.NotificationID = -1;
                    DataStructuresNotification.global.PatternName = '';
                    DataStructuresNotification.global.NotificationAdd = true;
                    DataStructuresNotification.DataTable.OnInit();
                    swal("انجام شد", "", "success");
                } else {
                    swal("انجام نشد", "", "error");
                }
            });
        };

        DataStructuresNotification.OnInit = function () {
            try {
                tinymce.EditorManager.execCommand('mceRemoveEditor', true, "tinymce");
                tinymce.init({
                    selector: "textarea#tinymce",
                    theme: 'modern',
                    directionality: 'ltr',
                    language: 'fa_IR',
                    removed_menuitems: 'newdocument',
                    height: 300,
                    plugins: [
                        'advlist autolink lists link image charmap print preview hr anchor pagebreak',
                        'searchreplace wordcount visualblocks visualchars code fullscreen',
                        'insertdatetime media nonbreaking save table contextmenu directionality',
                        'emoticons template paste textcolor colorpicker textpattern imagetools codesample '
                    ],
                    toolbar1: 'newdoc save undo redo | insert | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
                    toolbar2: 'print preview media | forecolor backcolor emoticons | codesample help',
                    setup: function (editor) {
                        editor.addButton('newdoc', {
                            text: 'نام پیام را وارد کنید',
                            icon: false,
                            onclick: function () {
                                DataStructuresNotification.OnSaveName();
                            }
                        });

                    },
                    save_enablewhendirty: false,
                    save_onsavecallback: function () {
                        if (DataStructuresNotification.global.PatternName.length > 0) {
                            DataStructuresNotification.OnSaveContent(
                                DataStructuresNotification.global.PatternName,
                                tinymce.activeEditor.getContent(),
                                DataStructuresNotification.global.NotificationID,
                                DataStructuresNotification.global.NotificationAdd);
                        } else {
                            DataStructuresNotification.OnSaveName(function () {
                                DataStructuresNotification.OnSaveContent(
                                    DataStructuresNotification.global.PatternName,
                                    tinymce.activeEditor.getContent(),
                                    DataStructuresNotification.global.NotificationID,
                                    DataStructuresNotification.global.NotificationAdd);
                            });
                        }
                        return false;
                    },
                    image_advtab: true,
                    content_css: [
                        'http://fonts.googleapis.com/css?family=Lato:300,300i,400,400i',
                        'http://www.tinymce.com/css/codepen.min.css'
                    ]
                });
                tinymce.suffix = ".min";
                tinyMCE.baseURL = 'ResourcesHolder/resource/plugins/tinymce';
            } catch (e) {
                console.log(e.message);
            }
        };

        $(document).ready(function () {
            DataStructuresNotification.OnInit();
            DataStructuresNotification.DataTable.OnInit();
        });
    }(jQuery));
</script>