<?= 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="office_hours_edit_form" method="post" class="card-body" enctype="multipart/form-data">
                        <h4 class="card-title">Edit Office Hour <hr></h4>

                        <input type="hidden" name="id" value="<?= esc($office['id']) ?>">

                        <div class="form-group">
                            <label class="font-weight-bold">Title</label>
                            <input type="text" name="title" class="form-control" value="<?= esc($office['title']) ?>" required>
                        </div>

                        <div class="form-group mt-2">
                            <label class="font-weight-bold">Text 1</label>
                            <input type="text" name="text1" class="form-control" value="<?= esc($office['text1']) ?>" required>
                        </div>

                        <div class="form-group mt-2">
                            <label class="font-weight-bold">Text 2</label>
                            <input type="text" name="text2" class="form-control" value="<?= esc($office['text2']) ?>">
                        </div>

                        <div class="form-group mt-2">
                            <label class="font-weight-bold">Text 3</label>
                            <input type="text" name="text3" class="form-control" value="<?= esc($office['text3']) ?>">
                        </div>

                        <button type="submit" class="btn btn-primary btn-sm mt-4">
                            <i class="mdi mdi-content-save"></i>&nbsp;&nbsp;Update
                        </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 () {
        $('#office_hours_edit_form').on('submit', function (e) {
            e.preventDefault();

            const csrfTokenName = '<?= csrf_token() ?>';
            const csrfTokenValue = '<?= csrf_hash() ?>';
            const jwtToken = localStorage.getItem('jwt_token');

            const formData = new FormData(this);
            formData.append(csrfTokenName, csrfTokenValue);

            $.ajax({
                url: '<?= base_url('api/contact/update-office-hours') ?>',
                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/contacts/office-hours/index') ?>';
                    } else {
                        toast('danger', response.message || 'Something went wrong!');
                    }
                }
            });
        });
    });
</script>
</body>
</html>
