<?php
$pageTitle = 'Edit Restaurant';
require_once __DIR__ . '/includes/header.php';

$db = Database::getInstance();
$id = (int) ($_GET['id'] ?? 0);

if ($id <= 0) {
    redirect($baseUrl . '/admin/restaurants.php');
}

$restaurant = $db->query("SELECT * FROM restaurants WHERE id = ?", [$id])->fetch();
if (!$restaurant) {
    redirect($baseUrl . '/admin/restaurants.php');
}

$plans = $db->query("SELECT id, name FROM subscription_plans WHERE is_active = 1 ORDER BY sort_order ASC")->fetchAll();

$adminUser = $db->query("SELECT * FROM users WHERE restaurant_id = ? AND role_id = 2 LIMIT 1", [$id])->fetch();
?>

<div class="form-card">
    <div class="card-title"><i class="fas fa-store me-2"></i>Edit Restaurant: <?php echo sanitize($restaurant['name']); ?></div>

    <form method="POST" action="restaurant_save.php" enctype="multipart/form-data">
        <?php echo $csrf->field(); ?>
        <input type="hidden" name="action" value="edit">
        <input type="hidden" name="id" value="<?php echo $restaurant['id']; ?>">

        <h6 class="text-primary mb-3">Restaurant Information</h6>
        <div class="row g-3 mb-4">
            <div class="col-md-6">
                <label class="form-label">Restaurant Name <span class="text-danger">*</span></label>
                <input type="text" class="form-control" name="name" value="<?php echo sanitize($restaurant['name']); ?>" required>
            </div>
            <div class="col-md-3">
                <label class="form-label">Slug <span class="text-danger">*</span></label>
                <input type="text" class="form-control" name="slug" value="<?php echo sanitize($restaurant['slug']); ?>" required>
            </div>
            <div class="col-md-3">
                <label class="form-label">Code</label>
                <input type="text" class="form-control" value="<?php echo sanitize($restaurant['code']); ?>" readonly>
            </div>
            <div class="col-md-6">
                <label class="form-label">Owner Name</label>
                <input type="text" class="form-control" name="owner_name" value="<?php echo sanitize($restaurant['owner_name'] ?? ''); ?>">
            </div>
            <div class="col-md-6">
                <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-4">
                <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-4">
                <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-4">
                <label class="form-label">Currency</label>
                <select class="form-select" name="currency">
                    <?php foreach (['USD'=>'USD ($)','LKR'=>'LKR (Rs.)','EUR'=>'EUR','GBP'=>'GBP','INR'=>'INR'] 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-md-6">
                <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-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-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-4">
                <label class="form-label">Timezone</label>
                <input type="text" class="form-control" name="timezone" value="<?php echo sanitize($restaurant['timezone'] ?? 'UTC'); ?>">
            </div>
            <div class="col-md-4">
                <label class="form-label">Domain</label>
                <input type="text" class="form-control" name="domain" value="<?php echo sanitize($restaurant['domain'] ?? ''); ?>">
            </div>
            <div class="col-md-4">
                <label class="form-label">Description</label>
                <textarea class="form-control" name="description" rows="2"><?php echo sanitize($restaurant['description'] ?? ''); ?></textarea>
            </div>
            <div class="col-md-6">
                <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 id="logoPreview" class="img-preview mt-2" src="<?php echo getBaseUrl() . '/' . $restaurant['logo']; ?>">
                <?php else: ?>
                    <img id="logoPreview" class="img-preview mt-2" src="" style="display:none;">
                <?php endif; ?>
            </div>
            <div class="col-md-6">
                <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 id="coverPreview" class="img-preview-lg mt-2" src="<?php echo getBaseUrl() . '/' . $restaurant['cover_image']; ?>">
                <?php else: ?>
                    <img id="coverPreview" class="img-preview-lg mt-2" src="" style="display:none;">
                <?php endif; ?>
            </div>
        </div>

        <h6 class="text-primary mb-3">Subscription & Limits</h6>
        <div class="row g-3 mb-4">
            <div class="col-md-3">
                <label class="form-label">Plan</label>
                <select class="form-select" name="plan_id">
                    <option value="">-- No Plan --</option>
                    <?php foreach ($plans as $plan): ?>
                        <option value="<?php echo $plan['id']; ?>" <?php echo $restaurant['plan_id'] == $plan['id'] ? 'selected' : ''; ?>><?php echo sanitize($plan['name']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label">Status</label>
                <select class="form-select" name="status">
                    <?php foreach (['trial','active','suspended','expired','cancelled'] as $st): ?>
                        <option value="<?php echo $st; ?>" <?php echo $restaurant['status'] === $st ? 'selected' : ''; ?>><?php echo ucfirst($st); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label">Operating Mode</label>
                <select class="form-select" name="operating_mode">
                    <option value="1" <?php echo $restaurant['operating_mode'] == 1 ? 'selected' : ''; ?>>View Only</option>
                    <option value="2" <?php echo $restaurant['operating_mode'] == 2 ? 'selected' : ''; ?>>Ordering</option>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label">Max Menu Items</label>
                <input type="number" class="form-control" name="max_menu_items" value="<?php echo $restaurant['max_menu_items']; ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">Max Staff</label>
                <input type="number" class="form-control" name="max_staff" value="<?php echo $restaurant['max_staff']; ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">Max Tables</label>
                <input type="number" class="form-control" name="max_tables" value="<?php echo $restaurant['max_tables']; ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">Trial Start</label>
                <input type="date" class="form-control" name="trial_start" value="<?php echo $restaurant['trial_start'] ?? ''; ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">Trial End</label>
                <input type="date" class="form-control" name="trial_end" value="<?php echo $restaurant['trial_end'] ?? ''; ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">Subscription Start</label>
                <input type="date" class="form-control" name="subscription_start" value="<?php echo $restaurant['subscription_start'] ?? ''; ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">Subscription End</label>
                <input type="date" class="form-control" name="subscription_end" value="<?php echo $restaurant['subscription_end'] ?? ''; ?>">
            </div>
        </div>

        <h6 class="text-primary mb-3">Admin Account<?php echo $adminUser ? ' (Update)' : ' (Create)'; ?></h6>
        <div class="row g-3 mb-4">
            <div class="col-md-4">
                <label class="form-label">Admin Name <span class="text-danger">*</span></label>
                <input type="text" class="form-control" name="admin_name" value="<?php echo sanitize($adminUser['name'] ?? ''); ?>" required>
            </div>
            <div class="col-md-4">
                <label class="form-label">Admin Email <span class="text-danger">*</span></label>
                <input type="email" class="form-control" name="admin_email" value="<?php echo sanitize($adminUser['email'] ?? ''); ?>" required>
            </div>
            <div class="col-md-4">
                <label class="form-label">Admin Password <?php echo $adminUser ? '(leave blank to keep current)' : '<span class="text-danger">*</span>'; ?></label>
                <input type="password" class="form-control" name="admin_password" <?php echo $adminUser ? '' : 'required'; ?> minlength="6">
            </div>
        </div>

        <div class="d-flex gap-2">
            <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i>Update Restaurant</button>
            <a href="restaurants.php" class="btn btn-outline-secondary">Cancel</a>
        </div>
    </form>
</div>

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