<?= 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">
                    <?php if (!empty($connect)): ?>
                        <!-- EDIT FORM -->
                        <form id="connect_edit_form" method="post" class="card-body" enctype="multipart/form-data">
                            <h4 class="card-title">Edit Social Media Links <hr></h4>

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

                            <div class="form-group">
                                <label class="font-weight-bold">Facebook Link</label>
                                <input type="url" name="facebook_link" class="form-control" value="<?= esc($connect['facebook_link']) ?>" placeholder="https://facebook.com/yourpage">
                            </div>

                            <div class="form-group mt-3">
                                <label class="font-weight-bold">Twitter Link</label>
                                <input type="url" name="twiter_link" class="form-control" value="<?= esc($connect['twiter_link']) ?>" placeholder="https://twitter.com/yourhandle">
                            </div>

                            <div class="form-group mt-3">
                                <label class="font-weight-bold">YouTube Link</label>
                                <input type="url" name="youtube_link" class="form-control" value="<?= esc($connect['youtube_link']) ?>" placeholder="https://youtube.com/channel/yourchannel">
                            </div>

                            <div class="form-group mt-3">
                                <label class="font-weight-bold">LinkedIn Link</label>
                                <input type="url" name="linkedin_link" class="form-control" value="<?= esc($connect['linkedin_link']) ?>" placeholder="https://linkedin.com/in/yourprofile">
                            </div>

                            <button type="submit" class="btn btn-primary btn-sm mt-4">
                                <i class="mdi mdi-content-save"></i>&nbsp;&nbsp;Update
                            </button>
                        </form>
                    <?php else: ?>
                        <!-- ADD NEW FORM -->
                        <form id="connect_add_form" method="post" class="card-body" enctype="multipart/form-data">
                            <h4 class="card-title">Add Social Media Links <hr></h4>

                            <div class="form-group">
                                <label class="font-weight-bold">Facebook Link</label>
                                <input type="url" name="facebook_link" class="form-control" placeholder="https://facebook.com/yourpage">
                            </div>

                            <div class="form-group mt-3">
                                <label class="font-weight-bold">Twitter Link</label>
                                <input type="url" name="twiter_link" class="form-control" placeholder="https://twitter.com/yourhandle">
                            </div>

                            <div class="form-group mt-3">
                                <label class="font-weight-bold">YouTube Link</label>
                                <input type="url" name="youtube_link" class="form-control" placeholder="https://youtube.com/channel/yourchannel">
                            </div>

                            <div class="form-group mt-3">
                                <label class="font-weight-bold">LinkedIn Link</label>
                                <input type="url" name="linkedin_link" class="form-control" placeholder="https://linkedin.com/in/yourprofile">
                            </div>

                            <button type="submit" class="btn btn-success btn-sm mt-4">
                                <i class="mdi mdi-plus"></i>&nbsp;&nbsp;Add Links
                            </button>
                        </form>
                    <?php endif; ?>
                </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>
<?php if (!empty($connect)): ?>
    $(document).ready(function () {
        $('#connect_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-connect') ?>',
                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/connect/index') ?>';
                    } else {
                        toast('danger', response.message || 'Something went wrong!');
                    }
                },
                error: function (xhr) {
                    toast('danger', 'API error: ' + xhr.responseText);
                }
            });
        });
    });
<?php else: ?>
    $(document).ready(function () {
        $('#connect_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/create-connect') ?>',
                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/connect/index') ?>';
                    } else {
                        toast('danger', response.message || 'Something went wrong!');
                    }
                },
                error: function (xhr) {
                    toast('danger', 'API error: ' + xhr.responseText);
                }
            });
        });
    });
<?php endif; ?>
</script>

</body>
</html>
