<?php 
if (!defined('IN_SITE')) {
    die('The Request Not Found');
}
$title = 'Quản Lý Thành Viên | ' . $DVR->site('title');
$body = [
    'title' => 'Danh sách Đơn Tố Cáo'
];
$body['header'] = '';
$body['footer'] = '';
require_once(__DIR__.'/Header.php');
require_once(__DIR__.'/Sidebar.php');
require_once(__DIR__ . '/../../../core/is_user.php');
CheckLogin();
CheckAdmin();
?>

<div class="main-content app-content">
    <div class="container-fluid">
        <div class="d-md-flex d-block align-items-center justify-content-between my-4 page-header-breadcrumb">
            <h1 class="page-title fw-semibold fs-18 mb-0">
                <a type="button" class="btn btn-dark btn-raised-shadow btn-wave btn-sm me-1" href="/Cpanel/news">
                    <i class="fa-solid fa-arrow-left"></i>
                </a> 
                Thêm Bài Viết
            </h1>
        </div>
        <form id="form-new" method="POST" enctype="multipart/form-data">
            <input type="hidden" name="token" value="<?=$getUser['token']?>" id="token">
            <div class="row">
                <div class="col-xl-12">
                    <div class="card custom-card">
                        <div class="card-header justify-content-between">
                            <div class="card-title">
                                Thêm Bài Viết
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="row mb-3 gy-2">
                                <div class="col-sm-12 mb-2">
                                    <label class="form-label" for="example-hf-email">Tiêu Đề <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" placeholder="Nhập Tiêu Đề" name="tieude">
                                </div>
                                <div class="col-sm-12 mb-2">
                                    <label class="form-label" for="example-hf-email">Link Ảnh Bìa <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" placeholder="Nhập Link Ảnh" name="image-news">
                                </div>
                                <div class="col-sm-12 mb-2">
                                    <label class="form-label" for="example-hf-email">Nội Dung<span class="text-danger">*</span></label>
                                  <textarea id="noidung"
                                                    name="noidung"></textarea>
                                </div>
                                
                            <div class="d-grid gap-2 mb-3">
                                <button type="submit" id="submitnew" class="btn btn-primary shadow-primary">
                                    <i class="fa fa-fw fa-save me-1"></i> Save
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
               
            </div>
        </form>
    </div>
</div>


<script>
$(document).ready(function() {
    // Replace the <textarea id="noidung"> with a CKEditor instance
    CKEDITOR.replace('noidung');

    // Listen to the form submit event
    $("#form-new").on("submit", function(event) {
        event.preventDefault(); // Prevent the default form submission

        // Update the textarea with CKEditor's data
        for (instance in CKEDITOR.instances) {
            CKEDITOR.instances[instance].updateElement();
        }

        // Disable the submit button and show the loading spinner
        $('#submitnew').html('<i class="fa fa-spinner fa-spin"></i> Đang xử lý...').prop('disabled', true);

        // Collect the form data
        var formData = new FormData(this);

        // Send the form data via AJAX
        $.ajax({
            url: "/ajaxs/admin/add_new.php",
            type: "POST",
            dataType: "json",
            data: formData,
            processData: false,
            contentType: false,
            success: function(res) {
                if (res.status == 'success') {
                    showMessage(res.msg, "success");
                    setTimeout(function() {
                        location.reload();
                    }, 2000);
                } else {
                    showMessage(res.msg, res.status);
                }
                $('#submitnew').html('Save').prop('disabled', false);
            },
            error: function(xhr, status, error) {
                Swal.fire({
                    icon: 'error',
                    title: 'Lỗi',
                    text: 'Có lỗi xảy ra khi gửi đơn tố cáo. Vui lòng thử lại!'
                });
                $('#submitnew').html('Save').prop('disabled', false);
            }
        });
    });
});

</script>


<?php
require_once(__DIR__.'/Footer.php');
?>
