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

$db = Database::getInstance();
$plans = $db->query("SELECT id, name FROM subscription_plans WHERE is_active = 1 ORDER BY sort_order ASC")->fetchAll();
$defaultCode = generateCode(6);
?>

<div class="form-card">
    <div class="card-title"><i class="fas fa-store me-2"></i>Add New Restaurant</div>

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

        <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" id="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" id="slug" required>
            </div>
            <div class="col-md-3">
                <label class="form-label">Code</label>
                <input type="text" class="form-control" name="code" value="<?php echo $defaultCode; ?>" readonly>
            </div>
            <div class="col-md-6">
                <label class="form-label">Owner Name</label>
                <input type="text" class="form-control" name="owner_name">
            </div>
            <div class="col-md-6">
                <label class="form-label">Email</label>
                <input type="email" class="form-control" name="email">
            </div>
            <div class="col-md-4">
                <label class="form-label">Phone</label>
                <input type="text" class="form-control" name="phone">
            </div>
            <div class="col-md-4">
                <label class="form-label">WhatsApp</label>
                <input type="text" class="form-control" name="whatsapp">
            </div>
            <div class="col-md-4">
                <label class="form-label">Currency</label>
                <select class="form-select" name="currency">
                    <option value="USD">USD ($)</option>
                    <option value="LKR">LKR (Rs.)</option>
                    <option value="EUR">EUR</option>
                    <option value="GBP">GBP</option>
                    <option value="INR">INR</option>
                </select>
            </div>
            <div class="col-md-6">
                <label class="form-label">Address</label>
                <textarea class="form-control" name="address" rows="2"></textarea>
            </div>
            <div class="col-md-3">
                <label class="form-label">City</label>
                <input type="text" class="form-control" name="city">
            </div>
            <div class="col-md-3">
                <label class="form-label">Country</label>
                <input type="text" class="form-control" name="country">
            </div>
            <div class="col-md-4">
                <label class="form-label">Timezone</label>
                <input type="text" class="form-control" name="timezone" value="UTC">
            </div>
            <div class="col-md-4">
                <label class="form-label">Domain</label>
                <input type="text" class="form-control" name="domain" placeholder="e.g. myrestaurant.com">
            </div>
            <div class="col-md-4">
                <label class="form-label">Description</label>
                <textarea class="form-control" name="description" rows="2"></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">
                <img id="logoPreview" class="img-preview mt-2" src="" style="display:none;">
            </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">
                <img id="coverPreview" class="img-preview-lg mt-2" src="" style="display:none;">
            </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 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">
                    <option value="trial">Trial</option>
                    <option value="active">Active</option>
                    <option value="suspended">Suspended</option>
                    <option value="expired">Expired</option>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label">Operating Mode</label>
                <select class="form-select" name="operating_mode">
                    <option value="1">View Only</option>
                    <option value="2">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="50">
            </div>
            <div class="col-md-3">
                <label class="form-label">Max Staff</label>
                <input type="number" class="form-control" name="max_staff" value="3">
            </div>
            <div class="col-md-3">
                <label class="form-label">Max Tables</label>
                <input type="number" class="form-control" name="max_tables" value="10">
            </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 date('Y-m-d'); ?>">
            </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 date('Y-m-d', strtotime('+14 days')); ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">Subscription Start</label>
                <input type="date" class="form-control" name="subscription_start">
            </div>
            <div class="col-md-3">
                <label class="form-label">Subscription End</label>
                <input type="date" class="form-control" name="subscription_end">
            </div>
        </div>

        <h6 class="text-primary mb-3">Admin Account</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" 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" required>
            </div>
            <div class="col-md-4">
                <label class="form-label">Admin Password <span class="text-danger">*</span></label>
                <input type="password" class="form-control" name="admin_password" 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>Create Restaurant</button>
            <a href="restaurants.php" class="btn btn-outline-secondary">Cancel</a>
        </div>
    </form>
</div>

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