@section styles
{
    <link href="~/App_Css/app/jstree.min.css" rel="stylesheet" />
    <style>
        body {
            background-color: #f6f6f6;
        }
        #FaqDefine .navbar-fixed-bottom {
            position: absolute !important;
        }
        .dropdown-toggle::after {
            display: none !important;
        }
        button.close {
            background-color: unset !important;
            border: none !important;
        }
    </style>
}

@*لیست سوالات متداول*@
<div id="faq-list" style="padding:15px;">
    <div class="d-flex align-items-center justify-end flex-row-reverse py-2 w-100">
        <button type="button" class="button-default" onclick="deleteFaqList()">حذف</button>
        <button type="button" class="button-default" onclick="openFaqDefine('edit')">ویرایش</button>
        <button type="button" class="button-accept" onclick="openFaqDefine('new')">جدید</button>
    </div>
    <div class="grid" id="gridFaqList" style="height: calc(100vh - 78px);position: relative;">
        <table>
            <thead>
                <tr>
                    <td data-id="code" style="min-width:80px;width:80px;">کد</td>
                    <td data-id="question" style="min-width:50%;width:50%;">پرسش</td>
                    <td data-id="answer" style="min-width:50%;width:50%;">پاسخ</td>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</div>


@*فرم تعریف سوالات متداول*@
<div class="modal fade" id="FaqDefine" role="dialog">
    <div class="modal-dialog" style="max-width:538px;">
        <div class="modal-content">
            <div class="modal-header">
                <label class="modal-title" style="font-size: 14px; font-weight: bold;">تعریف سوالات متداول</label>
                <button type="button" class="close" data-dismiss="modal">
                    <img src="~/App_Images/index/Close.svg" class="close-svg" />
                </button>
            </div>
            <div class="modal-body position-relative" style="padding:20px;background-color: #f0f8f9;height: calc(100vh - 120px);">
                    <div class="has-float-label" style="margin-bottom: 25px;">
                        <textarea class="textarea-control" id="question" style="height:150px;"></textarea>
                        <span>پرسش</span>
                    </div>
                    <div class="has-float-label">
                        <textarea class="textarea-control" id="answer" style="height:150px;"></textarea>
                        <span>پاسخ</span>
                    </div>

                <div class="navbar-fixed-bottom header-bar">
                    <input type="button" class="button-accept" onclick="SaveFaqDefine()" value="تایید">
                    <label class="label-cancel end" onclick="$('#FaqDefine').modal('hide');">انصراف</label>
                </div>
            </div>
        </div>
    </div>
</div>

@section scripts{
    <script src="~/App_Scripts/app/jstree.min.js"></script>
    <script>
        var mode = "new", codeFaq = -1;
        closeLoading();
        loadGrid("gridFaqList");
        $("#gridFaqList").gridBuilder().setCaption(" لیست سوالات متداول ");

        function loadGrid(gridId) {
            var gridBuilder = $("#gridFaqList").gridBuilder();
            ajax({
                loading: true,
                url: "Admin/LoadGridFaqList",
                data: JSON.stringify({
                    codeFaq: codeFaq,
                    filter: gridBuilder.getFilter(),
                    sort: gridBuilder.getSortFields(),
                    pageNo: gridBuilder.getCurrentPageNumber()
                }),
                success: function (result) {
                    gridBuilder.loadData(result);
                }
            });
        }

        var mode = "";
        function openFaqDefine(state) {
            if (state === "edit") {
                mode = "edit";
                codeFaq = Number($("#gridFaqList").gridBuilder().getActiveRowCellValue("Code"));
                ajax({
                    loading: true,
                    url: "Admin/SetDataFaqDefine",
                    data: JSON.stringify({ code: codeFaq }),
                    success: function (result) {
                        $("#question").val(result.Question);
                        $("#answer").val(result.Answer);
                    }
                });
            }
            $("#FaqDefine").modal("show");
        }


        function SaveFaqDefine() {
            codeFaq = Number($("#gridFaqList").gridBuilder().getActiveRowCellValue("Code"));
            if (mode === "edit") {
                ajax({
                    loading: true,
                    url: "Admin/EditFaqDefine",
                    data: JSON.stringify({ question: $("#question").val(), answer: $("#answer").val(), code: codeFaq }),
                    success: function (result) {
                        window.location.reload();
                        $(".label-cancel.end").click();
                    },
                    error: function (err) {
                        var errorMsg;
                        if (err.responseJSON)
                            errorMsg = err.responseJSON.Message;
                        else
                            errorMsg = err.responseText;
                    }
                });
            }
            else {
                ajax({
                    loading: true,
                    url: "Admin/SaveFaqDefine",
                    data: JSON.stringify({ question: $("#question").val(), answer: $("#answer").val() }),
                    success: function (result) {
                        window.location.reload();
                        $(".label-cancel.end").click();
                    },
                    error: function (err) {
                        var errorMsg;
                        if (err.responseJSON)
                            errorMsg = err.responseJSON.Message;
                        else
                            errorMsg = err.responseText;
                    }
                });
            }
        }


        function deleteFaqList() {
            if (!confirm("آیا برای حذف مطمئن هستید؟"))
                return;
            codeFaq = Number($("#gridFaqList").gridBuilder().getActiveRowCellValue("Code"));

            ajax({
                loading: true,
                url: "Admin/DeleteFaqList",
                data: JSON.stringify({ code: codeFaq }),
                success: function (result) {
                    window.location.reload();
                }
            });
        }

    </script>
}
