<div class="page-header">
    <div>
        <h1>Settings & Preferences</h1>
        <div class="page-subtitle">Manage your business profile, logos, and system-wide default settings.</div>
    </div>
</div>

<div class="grid-2 gap-6" style="grid-template-columns: 280px 1fr;">
    <!-- Settings Navigation -->
    <div>
        <div class="card">
            <div class="card-header"><h3 class="card-title">Settings</h3></div>
            <nav class="settings-nav">
                <a href="<?= APP_URL ?>/settings" class="nav-item active">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width:16px;">
                        <circle cx="12" cy="12" r="3"></circle>
                        <path d="M12 1v6m0 6v6m11-7h-6m-6 0H1"></path>
                    </svg>
                    Business Profile
                </a>
                <a href="<?= APP_URL ?>/settings/categories" class="nav-item">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width:16px;">
                        <path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"></path>
                    </svg>
                    Categories
                </a>
                <a href="<?= APP_URL ?>/settings/units" class="nav-item">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width:16px;">
                        <path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"></path>
                    </svg>
                    Units
                </a>
                <a href="<?= APP_URL ?>/settings/brands" class="nav-item">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width:16px;">
                        <path d="M9 11V6a3 3 0 1 1 6 0v5M3 13h18l-1.5 7.5a2 2 0 0 1-2 1.5h-11a2 2 0 0 1-2-1.5L3 13z"></path>
                    </svg>
                    Brands
                </a>
                <a href="<?= APP_URL ?>/settings/locations" class="nav-item">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width:16px;">
                        <path d="M3 21h18M5 21V7l8-4 8 4v14M8 21v-4h8v4"></path>
                    </svg>
                    Warehouses & Shops
                </a>
                <a href="<?= APP_URL ?>/settings/batch-expiry" class="nav-item">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width:16px;">
                        <rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect>
                        <line x1="16" y1="2" x2="16" y2="6"></line>
                        <line x1="8" y1="2" x2="8" y2="6"></line>
                        <line x1="3" y1="10" x2="21" y2="10"></line>
                    </svg>
                    Batch & Expiry
                </a>
                <a href="<?= APP_URL ?>/settings/payment-methods" class="nav-item">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width:16px;">
                        <rect x="1" y="4" width="22" height="16" rx="2" ry="2"></rect>
                        <line x1="1" y1="10" x2="23" y2="10"></line>
                    </svg>
                    Payment Methods
                </a>
            </nav>
        </div>
    </div>

    <div class="flex flex-column gap-6">
        <!-- Business Profile -->
        <div class="card">
            <div class="card-header"><h3 class="card-title">Business Profile</h3></div>
            <form action="<?= APP_URL ?>/settings/business" method="POST" enctype="multipart/form-data">
                <?= Security::csrfField() ?>
                <div class="form-group mb-4">
                    <label class="form-label">Business Name <span class="required">*</span></label>
                    <input type="text" name="business_name" class="form-control" value="<?= e($tenant['business_name']) ?>" required>
                </div>
                <div class="form-group mb-4">
                    <label class="form-label">Business Email <span class="required">*</span></label>
                    <input type="email" name="business_email" class="form-control" value="<?= e($tenant['business_email'] ?? '') ?>" required>
                </div>
                <div class="form-group mb-4">
                    <label class="form-label">Phone Number</label>
                    <input type="text" name="business_phone" class="form-control" value="<?= e($tenant['business_phone'] ?? '') ?>">
                </div>
                <div class="form-group mb-4">
                    <label class="form-label">Business Logo</label>
                    <input type="file" name="logo" class="form-control" accept=".jpg,.jpeg,.png,.webp">
                    <div class="form-hint">Max 2MB. Preferred size 200x200px.</div>
                </div>
                <div class="form-group mb-6">
                    <label class="form-label">Physical Address</label>
                    <textarea name="business_address" class="form-control" rows="2"><?= e($tenant['business_address'] ?? '') ?></textarea>
                </div>
                <div class="flex justify-end">
                    <button type="submit" class="btn btn-primary">Save Business Profile</button>
                </div>
            </form>
        </div>

        <!-- Receipt & System Settings -->
        <div class="card">
            <div class="card-header"><h3 class="card-title">System Preferences</h3></div>
            <form action="<?= APP_URL ?>/settings/preferences" method="POST">
                <?= Security::csrfField() ?>
                
                <div class="form-group mb-4">
                    <label class="form-label">Invoice Prefix</label>
                    <input type="text" name="prefs[sale_prefix]" class="form-control" value="<?= e($settings['sale_prefix'] ?? 'INV-') ?>">
                </div>
                <div class="form-group mb-4">
                    <label class="form-label">Default Currency</label>
                    <input type="text" name="prefs[currency_symbol]" class="form-control" value="<?= e($settings['currency_symbol'] ?? 'GH₵') ?>">
                </div>
                <div class="form-group mb-4">
                    <label class="form-label">Purchase Prefix</label>
                    <input type="text" name="prefs[purchase_prefix]" class="form-control" value="<?= e($settings['purchase_prefix'] ?? 'PO-') ?>">
                </div>
                <div class="form-group mb-4">
                    <label class="form-label">Low Stock Threshold</label>
                    <input type="number" name="prefs[low_stock_threshold]" class="form-control" value="<?= e($settings['low_stock_threshold'] ?? '5') ?>">
                </div>
                <div class="form-group mb-4">
                    <label class="form-label">Amount Rounding</label>
                    <select name="prefs[rounding]" class="form-control">
                        <option value="none" <?= ($settings['rounding'] ?? 'none') === 'none' ? 'selected' : '' ?>>No Rounding (2 decimal places)</option>
                        <option value="whole" <?= ($settings['rounding'] ?? 'none') === 'whole' ? 'selected' : '' ?>>Round to Whole Number</option>
                        <option value="half" <?= ($settings['rounding'] ?? 'none') === 'half' ? 'selected' : '' ?>>Round to 0.5 (Half)</option>
                    </select>
                    <div class="form-hint">Applies to all monetary amounts displayed and calculated throughout the system</div>
                </div>
                <div class="form-group mb-4">
                    <label class="form-label">Default Customer</label>
                    <select name="prefs[default_customer_id]" class="form-control">
                        <option value="">Walk-in Customer</option>
                        <?php foreach ($customers as $c): 
                            if (trim(strtolower($c['name'])) === 'walk-in customer') continue;
                        ?>
                        <option value="<?= $c['id'] ?>" <?= ($settings['default_customer_id'] ?? '') == $c['id'] ? 'selected' : '' ?>>
                            <?= e($c['name']) ?>
                        </option>
                        <?php endforeach; ?>
                    </select>
                    <div class="form-hint">Selected automatically for new POS transactions and Invoices</div>
                </div>
                <div class="form-group mb-6">
                    <label class="form-label">Default Location</label>
                    <select name="prefs[default_location_id]" class="form-control">
                        <option value="">None (Force User to Select)</option>
                        <?php foreach ($locations as $l): ?>
                        <option value="<?= $l['id'] ?>" <?= ($settings['default_location_id'] ?? '') == $l['id'] ? 'selected' : '' ?>>
                            <?= e($l['name']) ?>
                        </option>
                        <?php endforeach; ?>
                    </select>
                    <div class="form-hint">Selected automatically for new POS transactions, Invoices, and Purchases</div>
                </div>

                <div class="form-group mb-6">
                    <input type="hidden" name="prefs[allow_negative_stock]" value="0">
                    <label class="flex items-center gap-2" style="cursor:pointer;">
                        <input type="checkbox" name="prefs[allow_negative_stock]" value="1" <?= ($settings['allow_negative_stock'] ?? 0) ? 'checked' : '' ?>>
                        <span>Allow negative stock (oversell)</span>
                    </label>
                    <div class="form-hint">When enabled, sales can reduce stock below zero. When disabled, the system prevents overselling if insufficient stock is available.</div>
                </div>

                <div class="mb-6">
                    <div class="text-xs text-muted font-bold tracking-widest uppercase mb-4 opacity-50">Tax Settings</div>
                    <div class="form-group mb-4">
                        <input type="hidden" name="prefs[tax_enabled]" value="0">
                        <label class="flex items-center gap-2" style="cursor:pointer;">
                            <input type="checkbox" name="prefs[tax_enabled]" value="1" <?= ($settings['tax_enabled'] ?? 0) ? 'checked' : '' ?>>
                            <span>Enable Tax Calculation</span>
                        </label>
                        <div class="form-hint">When enabled, tax will be calculated on sales based on product tax groups or default rate</div>
                    </div>
                    <div class="form-group mb-4">
                        <label class="form-label">Default Tax Rate (%)</label>
                        <input type="number" step="0.01" name="prefs[tax_rate]" class="form-control" value="<?= e($settings['tax_rate'] ?? '0.00') ?>">
                        <div class="form-hint">Used as fallback when product has no tax group assigned</div>
                    </div>
                    <div class="form-group mb-4">
                        <label class="form-label">Default Tax Group</label>
                        <select name="prefs[default_tax_group_id]" class="form-control">
                            <option value="">None (Use Default Rate)</option>
                            <?php
                            $taxGroups = TaxGroup::getActive();
                            foreach ($taxGroups as $tg):
                            ?>
                            <option value="<?= $tg['id'] ?>" <?= ($settings['default_tax_group_id'] ?? '') == $tg['id'] ? 'selected' : '' ?>>
                                <?= e($tg['name']) ?> (<?= $tg['rate'] ?><?= $tg['type'] === 'percentage' ? '%' : '' ?>)
                            </option>
                            <?php endforeach; ?>
                        </select>
                        <div class="form-hint">Default tax group for new products</div>
                    </div>
                    <div class="form-group">
                        <a href="<?= APP_URL ?>/tax" class="btn btn-outline btn-sm" style="display:inline-flex; align-items:center; gap:4px;">
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width:14px;"><rect x="4" y="4" width="16" height="16" rx="2"></rect><line x1="12" y1="8" x2="12" y2="16"></line><line x1="8" y1="12" x2="16" y2="12"></line></svg>
                            Manage Tax Groups
                        </a>
                    </div>
                </div>

                <div class="flex justify-end">
                    <button type="submit" class="btn btn-primary">Update Preferences</button>
                </div>
            </form>
        </div>

        <!-- Visual Brand -->
        <div class="card">
            <div class="card-header"><h3 class="card-title">Visual Brand</h3></div>
            <div class="p-4 flex flex-column items-center justify-center bg-raised rounded" style="min-height: 120px;">
                <?php if ($tenant['logo']): ?>
                    <img src="<?= APP_URL ?>/uploads/<?= $tid ?>/<?= e($tenant['logo']) ?>" style="max-width: 80px; height: auto; border-radius: 8px;">
                <?php else: ?>
                    <div style="width:60px; height:60px; border-radius:12px; background:var(--primary); display:flex; align-items:center; justify-content:center; color:white; font-size:1.5rem; font-weight:900;">
                        <?= strtoupper(substr($tenant['business_name'] ?? 'D', 0, 1)) ?>
                    </div>
                    <p class="text-xs text-muted mt-2">No logo uploaded yet</p>
                <?php endif; ?>
            </div>
            <div class="mt-4 border-t border-soft pt-3">
                 <div class="text-xs text-muted uppercase font-bold tracking-widest mb-1">Theme Preview</div>
                 <div class="flex gap-2 mt-2">
                    <div style="width:20px; height:20px; border-radius:4px; background:var(--primary);"></div>
                    <div style="width:20px; height:20px; border-radius:4px; background:var(--accent);"></div>
                    <div style="width:20px; height:20px; border-radius:4px; background:var(--bg-surface); border:1px solid var(--border);"></div>
                 </div>
            </div>
        </div>
    </div>
</div>

<?php if (Auth::role() === 'admin'): ?>
    <div class="mt-8 pt-8 border-t border-soft">
        <div class="card" style="border-color: rgba(239, 68, 68, 0.2); background: rgba(239, 68, 68, 0.02);">
            <div class="card-header">
                <h3 class="card-title text-danger">Danger Zone</h3>
            </div>
            <div class="p-4">
                <div class="flex items-center justify-between gap-4">
                    <div style="flex: 1;">
                        <h4 class="font-bold text-sm mb-1">Factory Reset Account</h4>
                        <p class="text-xs text-muted">This will permanently delete all sales, purchases, stock movements, and financial records for this business. Master data like products and customers will be kept. <strong>This action cannot be undone.</strong></p>
                    </div>
                    <form action="<?= APP_URL ?>/settings/reset" method="POST" onsubmit="return confirm('WARNING: This will permanently delete ALL transactional data. Are you absolutely sure you want to proceed?');">
                        <?= Security::csrfField() ?>
                        <button type="submit" class="btn btn-outline btn-danger btn-sm">Reset Account Data</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
<?php endif; ?>

