<?= 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($company)): ?>
                        <!-- EDIT FORM -->
                        <form id="company_edit_form" method="post" enctype="multipart/form-data" class="card-body">
                            <h4 class="card-title">Edit Company <hr></h4>
                            <input type="hidden" name="id" value="<?= esc($company['id']) ?>">
                    <?php else: ?>
                        <!-- ADD FORM -->
                        <form id="company_add_form" method="post" enctype="multipart/form-data" class="card-body">
                            <h4 class="card-title">Add Company <hr></h4>
                    <?php endif; ?>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Company Name</label>
                                <input type="text" name="company_name" class="form-control" value="<?= esc($company['company_name'] ?? '') ?>" required>
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Email</label>
                                <input type="email" name="email" class="form-control" value="<?= esc($company['email'] ?? '') ?>">
                            </div>
                        </div>

                        <div class="col-md-6 mt-3">
                            <div class="form-group">
                                <label>Mobile</label>
                                <input type="text" name="mobile" class="form-control" value="<?= esc($company['mobile'] ?? '') ?>">
                            </div>
                        </div>

                       

                        

                        <div class="col-md-6 mt-3">
                            <div class="form-group">
                                <label>Small Description</label>
                                <textarea name="small_description" class="form-control" rows="3"><?= esc($company['small_description'] ?? '') ?></textarea>
                            </div>
                        </div>

                        <div class="col-md-12 mt-3">
                            <div class="form-group">
                                <label>Long Description</label>
                                <textarea name="long_description" class="form-control" rows="5"><?= esc($company['long_description'] ?? '') ?></textarea>
                            </div>
                        </div>
                        <div class="col-md-6 mt-3">
                            <div class="form-group">
                                <label>Logo</label>
                                <input type="file" name="logo" class="form-control-file">
                                <?php if (!empty($company['logo'])): ?>
                                    <img src="<?= base_url('uploads/logos/' . $company['logo']) ?>" alt="Logo" style="max-height: 80px;" class="mt-2 d-block">
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>

                    <!-- Hidden Fields -->
                    <input type="hidden" name="created_at" value="<?= esc($company['created_at'] ?? date('Y-m-d H:i:s')) ?>">
                    <input type="hidden" name="created_by" value="<?= esc($company['created_by'] ?? session()->get('user_id')) ?>">
                    <input type="hidden" name="updated_at" value="<?= date('Y-m-d H:i:s') ?>">
                    <input type="hidden" name="updated_by" value="<?= session()->get('user_id') ?>">

                    <button type="submit" class="btn <?= !empty($company) ? 'btn-primary' : 'btn-success' ?> btn-sm mt-3">
                        <i class="mdi mdi-content-save"></i> <?= !empty($company) ? 'Update' : 'Add Company' ?>
                    </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>

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

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

        <?php if (!empty($company)): ?>
        $('#company_edit_form').on('submit', function (e) {
            e.preventDefault();

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

            $.ajax({
                url: '<?= base_url('api/home/update-basic-details') ?>',
                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/basic-details') ?>';
                    } else {
                        toast('danger', response.message || 'Something went wrong!');
                    }
                },
                error: function (xhr) {
                    toast('danger', 'API error: ' + xhr.responseText);
                }
            });
        });
        <?php else: ?>
        $('#company_add_form').on('submit', function (e) {
            e.preventDefault();

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

            $.ajax({
                url: '<?= base_url('api/home/create-basic-details') ?>',
                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/basic-details') ?>';
                    } else {
                        toast('danger', response.message || 'Something went wrong!');
                    }
                },
                error: function (xhr) {
                    toast('danger', 'API error: ' + xhr.responseText);
                }
            });
        });
        <?php endif; ?>
    });
</script>

</body>
</html>
