<?php
$pageTitle = 'Restaurant Profile';
require_once __DIR__ . '/includes/header.php';
$upload = new Upload();
?>

<div class="row">
    <div class="col-lg-8">
        <form class="ajax-form" action="<?php echo $baseUrl; ?>/restaurant-admin/profile_save.php" method="POST" enctype="multipart/form-data">
            <?php echo $csrf->field(); ?>
            <input type="hidden" name="action" value="update_profile">

            <div class="form-card">
                <div class="card-title"><i class="fas fa-store me-2"></i>Basic Information</div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Restaurant Name <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" name="name" id="name" value="<?php echo sanitize($restaurant['name']); ?>" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Slug</label>
                        <input type="text" class="form-control" name="slug" id="slug" value="<?php echo sanitize($restaurant['slug']); ?>">
                    </div>
                    <div class="col-12 mb-3">
                        <label class="form-label">Description</label>
                        <textarea class="form-control" name="description" rows="3"><?php echo sanitize($restaurant['description'] ?? ''); ?></textarea>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Logo</label>
                        <input type="file" class="form-control" name="logo" accept="image/*" data-preview="#logoPreview">
                        <?php if (!empty($restaurant['logo'])): ?>
                            <img src="<?php echo $baseUrl . '/' . sanitize($restaurant['logo']); ?>" id="logoPreview" class="img-preview mt-2">
                        <?php else: ?>
                            <img src="" id="logoPreview" class="img-preview mt-2" style="display:none;">
                        <?php endif; ?>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Cover Image</label>
                        <input type="file" class="form-control" name="cover_image" accept="image/*" data-preview="#coverPreview">
                        <?php if (!empty($restaurant['cover_image'])): ?>
                            <img src="<?php echo $baseUrl . '/' . sanitize($restaurant['cover_image']); ?>" id="coverPreview" class="img-preview-lg mt-2">
                        <?php else: ?>
                            <img src="" id="coverPreview" class="img-preview-lg mt-2" style="display:none;">
                        <?php endif; ?>
                    </div>
                </div>
            </div>

            <div class="form-card">
                <div class="card-title"><i class="fas fa-phone me-2"></i>Contact Information</div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Phone</label>
                        <input type="text" class="form-control" name="phone" value="<?php echo sanitize($restaurant['phone'] ?? ''); ?>">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">WhatsApp</label>
                        <input type="text" class="form-control" name="whatsapp" value="<?php echo sanitize($restaurant['whatsapp'] ?? ''); ?>">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Email</label>
                        <input type="email" class="form-control" name="email" value="<?php echo sanitize($restaurant['email'] ?? ''); ?>">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">City</label>
                        <input type="text" class="form-control" name="city" value="<?php echo sanitize($restaurant['city'] ?? ''); ?>">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Country</label>
                        <input type="text" class="form-control" name="country" value="<?php echo sanitize($restaurant['country'] ?? ''); ?>">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Currency</label>
                        <select class="form-select" name="currency">
                            <?php
                            $currencies = ['USD'=>'USD - $','LKR'=>'LKR - Rs.','EUR'=>'EUR - Euro','GBP'=>'GBP - Pound','INR'=>'INR - Rupee'];
                            foreach ($currencies as $val => $lbl):
                            ?>
                            <option value="<?php echo $val; ?>" <?php echo ($restaurant['currency'] ?? '') === $val ? 'selected' : ''; ?>><?php echo $lbl; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-12 mb-3">
                        <label class="form-label">Address</label>
                        <textarea class="form-control" name="address" rows="2"><?php echo sanitize($restaurant['address'] ?? ''); ?></textarea>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Timezone</label>
                        <select class="form-select" name="timezone">
                            <?php
                            $timezones = ['UTC'=>'UTC','Asia/Colombo'=>'Asia/Colombo','Asia/Dubai'=>'Asia/Dubai','Asia/Kolkata'=>'Asia/Kolkata','Europe/London'=>'Europe/London','America/New_York'=>'America/New_York','America/Los_Angeles'=>'America/Los_Angeles','Asia/Singapore'=>'Asia/Singapore'];
                            foreach ($timezones as $val => $lbl):
                            ?>
                            <option value="<?php echo $val; ?>" <?php echo ($restaurant['timezone'] ?? '') === $val ? 'selected' : ''; ?>><?php echo $lbl; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
            </div>

            <div class="form-card">
                <div class="card-title"><i class="fas fa-globe me-2"></i>Social & Online Presence</div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Google Maps Link</label>
                        <input type="url" class="form-control" name="google_maps_link" value="<?php echo sanitize($restaurantSettings['google_maps_link'] ?? ''); ?>">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Facebook URL</label>
                        <input type="url" class="form-control" name="facebook_url" value="<?php echo sanitize($restaurantSettings['facebook_url'] ?? ''); ?>">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Instagram URL</label>
                        <input type="url" class="form-control" name="instagram_url" value="<?php echo sanitize($restaurantSettings['instagram_url'] ?? ''); ?>">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Twitter URL</label>
                        <input type="url" class="form-control" name="twitter_url" value="<?php echo sanitize($restaurantSettings['twitter_url'] ?? ''); ?>">
                    </div>
                </div>
            </div>

            <div class="text-end mb-4">
                <button type="submit" class="btn btn-primary btn-lg" data-submit-text="<i class='fas fa-save me-1'></i>Save Profile">
                    <i class="fas fa-save me-1"></i>Save Profile
                </button>
            </div>
        </form>
    </div>
    <div class="col-lg-4">
        <div class="form-card">
            <div class="card-title"><i class="fas fa-info-circle me-2"></i>Restaurant Info</div>
            <table class="table table-borderless small">
                <tr><td class="text-muted">Code</td><td><strong><?php echo sanitize($restaurant['code']); ?></strong></td></tr>
                <tr><td class="text-muted">Status</td><td><span class="badge bg-<?php echo $restaurant['status'] === 'active' ? 'success' : 'warning'; ?>"><?php echo ucfirst($restaurant['status']); ?></span></td></tr>
                <tr><td class="text-muted">Plan</td><td><?php echo $restaurant['plan_id'] ? 'ID: '.$restaurant['plan_id'] : 'None'; ?></td></tr>
                <tr><td class="text-muted">Created</td><td><?php echo formatDate($restaurant['created_at'], 'M d, Y'); ?></td></tr>
            </table>
        </div>
        <div class="form-card">
            <div class="card-title"><i class="fas fa-link me-2"></i>Public Menu Link</div>
            <div class="input-group mb-3">
                <input type="text" class="form-control" value="<?php echo $baseUrl; ?>/menu.php?r=<?php echo sanitize($restaurant['slug']); ?>" readonly id="menuLink">
                <button class="btn btn-outline-secondary" onclick="navigator.clipboard.writeText(document.getElementById('menuLink').value);showAlert('success','Link copied!');"><i class="fas fa-copy"></i></button>
            </div>
            <div class="d-grid gap-2">
                <button type="button" class="btn btn-outline-primary" onclick="showMenuQR()"><i class="fas fa-qrcode me-1"></i>Generate QR Code</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="menuQRModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fas fa-qrcode me-2"></i>Menu QR Code</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body text-center">
                <div id="qrLoading" class="py-4"><i class="fas fa-spinner fa-spin fa-2x text-primary"></i><p class="mt-2">Generating QR code...</p></div>
                <div id="qrContent" style="display:none;">
                    <img id="qrImage" class="img-fluid mb-3" style="max-width:280px;border:1px solid #dee2e6;border-radius:8px;padding:10px;background:#fff;">
                    <p class="text-muted small mb-3" id="qrUrl"></p>
                </div>
            </div>
            <div class="modal-footer justify-content-center">
                <a id="qrDownload" href="#" class="btn btn-primary" download><i class="fas fa-download me-1"></i>Download PNG</a>
                <button type="button" class="btn btn-outline-secondary" onclick="window.print()"><i class="fas fa-print me-1"></i>Print</button>
            </div>
        </div>
    </div>
</div>

<script>
function showMenuQR() {
    var slug = '<?php echo addslashes($restaurant["slug"]); ?>';
    var baseUrl = '<?php echo addslashes($baseUrl); ?>';
    var menuUrl = baseUrl + '/menu.php?r=' + slug;
    var qrApiUrl = baseUrl + '/qrcode.php?restaurant_id=<?php echo (int)$restaurant["id"]; ?>&format=png';

    var modal = new bootstrap.Modal(document.getElementById('menuQRModal'));
    document.getElementById('qrLoading').style.display = 'block';
    document.getElementById('qrContent').style.display = 'none';
    document.getElementById('qrUrl').textContent = menuUrl;
    document.getElementById('qrDownload').href = qrApiUrl;
    document.getElementById('qrDownload').download = 'qr-' + slug + '.png';
    modal.show();

    var img = document.getElementById('qrImage');
    img.onload = function() {
        document.getElementById('qrLoading').style.display = 'none';
        document.getElementById('qrContent').style.display = 'block';
    };
    img.onerror = function() {
        document.getElementById('qrLoading').innerHTML = '<i class="fas fa-exclamation-triangle fa-2x text-warning"></i><p class="mt-2">Failed to generate QR code</p>';
    };
    img.src = qrApiUrl;
}
</script>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
