<?= view('common/top_header_backdesk.php') ?>

<body>
<div class="container-scroller">
    <?= view('common/top_navbar') ?>
    <div class="container-fluid page-body-wrapper">
        <?= view('common/side_navbar') ?>
        <div class="main-panel">
            <div class="content-wrapper">
                <div class="card">
                    <form id="notice_add_form" method="post" class="card-body" enctype="multipart/form-data">
                        <h4 class="card-title">Add Notice or Tender <hr></h4>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="font-weight-bold">Type</label>
                                    <select name="type" class="form-control" required>
                                        <option value="">-- Select Type --</option>
                                        <option value="Notice">Notice</option>
                                        <option value="Tender">Tender</option>
                                    </select>
                                </div>
                            </div>
                        </div>

                        <div class="form-group mt-3">
                            <label class="font-weight-bold">Content</label>
                            <textarea name="content" id="editor" class="form-control" rows="10"></textarea>
                        </div>

                        <button type="submit" class="btn btn-success btn-sm mt-3">
                            <i class="mdi mdi-check"></i>&nbsp;&nbsp;Submit
                        </button>
                    </form>
                </div>

                <footer class="footer mt-3">
                    <div class="d-sm-flex justify-content-center justify-content-sm-between">
                        <span class="text-muted">© <?= date('Y') ?> <?= COMPANY_FULL_NAME ?>. All rights reserved.</span>
                        <span class="float-sm-right">With gratitude from <i class="mdi mdi-heart text-danger"></i>
                            <a href="<?= CREDIT_LINK ?>" target="_blank"><?= CREDIT_TITLE ?></a></span>
                    </div>
                </footer>
            </div>
        </div>
    </div>
</div>

<?= view('common/footer_backdesk.php') ?>


<script>
$(document).ready(function () {
    const csrfTokenName = '<?= csrf_token() ?>';
    const csrfTokenValue = '<?= csrf_hash() ?>';
    const jwtToken = localStorage.getItem('jwt_token');

    // Initialize Trumbowyg on textarea with id="editor"
    $('#editor').trumbowyg({
        btns: [['viewHTML'], ['formatting'], 'bold', 'italic', 'underline'],
        autogrow: true,
        resetCss: true,
        semantic: false
    });

    $('#notice_add_form').on('submit', function (e) {
        e.preventDefault();

        // Get HTML content from Trumbowyg
        const content = $('#editor').trumbowyg('html');

        const formData = new FormData(this);
        formData.append('content', content);  // append editor content
        formData.append(csrfTokenName, csrfTokenValue);

        $.ajax({
            url: '<?= base_url('api/home/notices/add') ?>',
            method: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            headers: {
                'Authorization': 'Bearer ' + jwtToken
            },
            success: function (response) {
                if (response.status) {
                    toast('success', response.message);
                    window.location.href = '<?= base_url('backdesk/home-tender-notices/index') ?>';
                } else {
                    toast('danger', response.message || 'Something went wrong!');
                }
            },
            error: function (xhr) {
                toast('danger', 'API error: ' + xhr.responseText);
            }
        });
    });
});
</script>
</body>
</html>
