<div class="max-w-4xl mx-auto">
    <h1 class="text-2xl font-bold text-gray-900 mb-6">Settings</h1>
    <form method="POST" action="?page=settings&action=update" class="space-y-6"><?= CSRF::field() ?>

        <!-- Company Info -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Company Information</h3>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div class="md:col-span-2"><label class="block text-sm font-medium text-gray-700 mb-1">Company Name</label><input type="text" name="company_name" value="<?= htmlspecialchars($company['name'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Email</label><input type="email" name="company_email" value="<?= htmlspecialchars($company['email'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Phone</label><input type="text" name="company_phone" value="<?= htmlspecialchars($company['phone'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div class="md:col-span-2"><label class="block text-sm font-medium text-gray-700 mb-1">Address</label><input type="text" name="company_address" value="<?= htmlspecialchars($company['address'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">City</label><input type="text" name="company_city" value="<?= htmlspecialchars($company['city'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div class="flex gap-4"><div class="flex-1"><label class="block text-sm font-medium text-gray-700 mb-1">State</label><input type="text" name="company_state" value="<?= htmlspecialchars($company['state'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div><div class="flex-1"><label class="block text-sm font-medium text-gray-700 mb-1">ZIP</label><input type="text" name="company_zip" value="<?= htmlspecialchars($company['zip'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Website</label><input type="url" name="company_website" value="<?= htmlspecialchars($company['website'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
            </div>
        </div>

        <!-- Lease & Fee -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Lease & Late Fees</h3>
            <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Default Lease Term (months)</label><input type="number" name="default_lease_term" value="<?= $settings['default_lease_term'] ?? '12' ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Late Fee Amount ($)</label><input type="number" name="late_fee_amount" value="<?= $settings['late_fee_amount'] ?? '50' ?>" step="0.01" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Grace Period (days)</label><input type="number" name="late_fee_days" value="<?= $settings['late_fee_days'] ?? '5' ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
            </div>
        </div>

        <!-- Notifications -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Notifications</h3>
            <div class="space-y-3">
                <label class="flex items-center gap-3"><input type="checkbox" name="email_notifications" value="1" <?= ($settings['email_notifications'] ?? '1') === '1' ? 'checked' : '' ?> class="rounded border-gray-300 text-indigo-600 focus:ring-indigo-500"><span class="text-sm text-gray-700">Email Notifications</span></label>
                <label class="flex items-center gap-3"><input type="checkbox" name="sms_notifications" value="1" <?= ($settings['sms_notifications'] ?? '0') === '1' ? 'checked' : '' ?> class="rounded border-gray-300 text-indigo-600 focus:ring-indigo-500"><span class="text-sm text-gray-700">SMS Notifications</span></label>
            </div>
        </div>

        <!-- Payment Gateway -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Payment Gateway</h3>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Gateway</label>
                    <select name="payment_gateway" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                    <?php foreach (['none'=>'None','stripe'=>'Stripe','plaid'=>'Plaid'] as $v=>$l): ?><option value="<?= $v ?>" <?= ($settings['payment_gateway'] ?? 'none') === $v ? 'selected' : '' ?>><?= $l ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Stripe Publishable Key</label><input type="text" name="stripe_key" value="<?= htmlspecialchars($settings['stripe_key'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500" placeholder="pk_..."></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Stripe Secret Key</label><input type="password" name="stripe_secret" value="<?= htmlspecialchars($settings['stripe_secret'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500" placeholder="sk_..."></div>
            </div>
        </div>

        <!-- General -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">General</h3>
            <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Timezone</label>
                    <select name="timezone" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                    <?php foreach (['America/New_York','America/Chicago','America/Denver','America/Los_Angeles','America/Phoenix','Pacific/Honolulu'] as $tz): ?><option value="<?= $tz ?>" <?= ($settings['timezone'] ?? 'America/New_York') === $tz ? 'selected' : '' ?>><?= str_replace(['America/','_'],['',' '],$tz) ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Date Format</label>
                    <select name="date_format" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                    <?php foreach (['m/d/Y'=>'MM/DD/YYYY','d/m/Y'=>'DD/MM/YYYY','Y-m-d'=>'YYYY-MM-DD'] as $v=>$l): ?><option value="<?= $v ?>" <?= ($settings['date_format'] ?? 'm/d/Y') === $v ? 'selected' : '' ?>><?= $l ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Currency</label>
                    <select name="currency" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                    <?php foreach (['USD'=>'USD ($)','EUR'=>'EUR (€)','GBP'=>'GBP (£)','CAD'=>'CAD (C$)'] as $v=>$l): ?><option value="<?= $v ?>" <?= ($settings['currency'] ?? 'USD') === $v ? 'selected' : '' ?>><?= $l ?></option><?php endforeach; ?></select></div>
            </div>
        </div>

        <div class="flex justify-end"><button type="submit" class="px-6 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium">Save Settings</button></div>
    </form>
</div>
