<?= 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="campus_location_add_form" method="post" class="card-body" enctype="multipart/form-data">
                        <h4 class="card-title">Add Campus Location <hr></h4>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="font-weight-bold">Title</label>
                                    <input type="text" name="title" class="form-control" required>
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="font-weight-bold">Floor</label>
                                    <input type="text" name="floor" class="form-control" required>
                                </div>
                            </div>
                        </div>

                        <div class="row mt-2">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="font-weight-bold">Access</label>
                                    <input type="text" name="access" class="form-control" required>
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="font-weight-bold">Parking</label>
                                    <input type="text" name="parking" class="form-control" required>
                                </div>
                            </div>
                        </div>

                        <div class="form-group mt-3">
                            <label class="font-weight-bold">Departments</label>
                            <input type="text" id="department_input" class="form-control" placeholder="Type department name and press comma" />
                            <div id="department_tags" class="mt-2"></div>
                            <input type="hidden" name="departments" id="departments_hidden" />
                        </div>

                        <button type="submit" class="btn btn-success btn-sm mt-4">
                            <i class="mdi mdi-check"></i>&nbsp;&nbsp;Create
                        </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') ?>

<style>
    #department_tags .badge {
        font-size: 14px;
        padding: 6px 10px;
        margin-right: 5px;
        margin-top: 5px;
        display: inline-block;
    }
    .remove-tag {
        cursor: pointer;
    }
</style>

<script>
    $(document).ready(function () {
        let departments = [];

        $('#department_input').on('keyup', function (e) {
            if (e.key === ',' || e.keyCode === 188) {
                let val = $(this).val().trim().replace(',', '');
                if (val !== '') {
                    if (!departments.includes(val)) {
                        departments.push(val);
                        $('#department_tags').append(
                            `<span class="badge badge-info">
                                ${val}
                                <i class="mdi mdi-close text-white ml-1 remove-tag" data-val="${val}"></i>
                            </span>`
                        );
                        updateDepartmentsHiddenInput();
                    }
                }
                $(this).val('');
            }
        });

        $(document).on('click', '.remove-tag', function () {
            let val = $(this).data('val');
            departments = departments.filter(item => item !== val);
            $(this).parent().remove();
            updateDepartmentsHiddenInput();
        });

        function updateDepartmentsHiddenInput() {
            $('#departments_hidden').val(departments.join(','));
        }

        $('#campus_location_add_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/campus-location/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/contacts/campus-locations/index') ?>';
                    } else {
                        toast('danger', response.message || 'Something went wrong!');
                    }
                }
            });
        });
    });
</script>
</body>
</html>
