<?php
// ─────────────────────────────────────────────────────────────────
// Settings: Organization Settings View
// ─────────────────────────────────────────────────────────────────
$pageTitle = 'Organization Settings';
$breadcrumb = [['label' => 'Settings', 'url' => null]];
ob_start();
?>
<div class="page-header">
    <div>
        <h1 class="page-title">Organization Settings</h1>
        <p class="page-subtitle">Manage your organization profile, locale, and preferences</p>
    </div>
</div>

<!-- Settings Tabs -->
<div class="tabs" id="settingsTabs">
    <a href="<?= APP_URL ?>/settings" class="tab-item active">General</a>
    <a href="<?= APP_URL ?>/settings/security" class="tab-item">Security</a>
    <a href="<?= APP_URL ?>/settings/billing" class="tab-item">Billing</a>
    <?php if (\Core\Auth::isSuperAdmin()): ?>
    <a href="<?= APP_URL ?>/settings/email" class="tab-item">Email/SMTP</a>
    <a href="<?= APP_URL ?>/admin/system" class="tab-item">System</a>
    <a href="<?= APP_URL ?>/settings/audit" class="tab-item">Audit Log</a>
    <?php endif; ?>
</div>

<form method="POST" action="<?= APP_URL ?>/settings" enctype="multipart/form-data">
    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">

    <div style="display:grid;grid-template-columns:1fr 1fr;gap:24px;">

        <!-- Left Column: Org Profile -->
        <div>
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">Organization Profile</h3>
                </div>
                <div class="card-body">

                    <!-- Logo Upload -->
                    <div class="form-group" style="text-align:center;">
                        <?php if (!empty($org['logo'])): ?>
                            <img src="<?= APP_URL . '/' . htmlspecialchars($org['logo']) ?>" alt="Logo" style="height:80px;max-width:200px;object-fit:contain;margin-bottom:12px;border-radius:8px;">
                        <?php else: ?>
                            <div style="width:80px;height:80px;background:linear-gradient(135deg,var(--primary),#12C2CE);border-radius:16px;display:flex;align-items:center;justify-content:center;margin:0 auto 12px;">
                                <svg width="36" height="36" fill="white" viewBox="0 0 24 24"><path d="M3 3h8v8H3V3zm0 10h8v8H3v-8zm10-10h8v8h-8V3zm0 10h8v8h-8v-8z"/></svg>
                            </div>
                        <?php endif; ?>
                        <div>
                            <label class="btn btn-secondary btn-sm" style="cursor:pointer;">
                                📷 Upload Logo
                                <input type="file" name="logo" accept="image/*" style="display:none;" onchange="previewLogo(this)">
                            </label>
                            <p class="form-hint">PNG, JPG, SVG. Max 2MB. Recommended: 200×80px</p>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="form-label required" for="org_name">Organization Name</label>
                        <input type="text" id="org_name" name="name" class="form-control" required value="<?= htmlspecialchars($org['name'] ?? '') ?>">
                    </div>

                    <div class="form-group">
                        <label class="form-label" for="org_industry">Industry</label>
                        <select id="org_industry" name="industry" class="form-control form-select">
                            <option value="">Select Industry</option>
                            <?php foreach (getIndustryGroups() as $sector => $subIndustries): ?>
                                <?php if (empty($subIndustries)): ?>
                                    <option value="<?= htmlspecialchars($sector) ?>" <?= ($org['industry'] ?? '') === $sector ? 'selected' : '' ?>>
                                        <?= htmlspecialchars($sector) ?>
                                    </option>
                                <?php else: ?>
                                    <optgroup label="<?= htmlspecialchars($sector) ?>">
                                        <?php foreach ($subIndustries as $sub): ?>
                                            <option value="<?= htmlspecialchars($sub) ?>" <?= ($org['industry'] ?? '') === $sub ? 'selected' : '' ?>>
                                                <?= htmlspecialchars($sub) ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </optgroup>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="form-group">
                        <label class="form-label" for="org_email">Email Address</label>
                        <input type="email" id="org_email" name="email" class="form-control" value="<?= htmlspecialchars($org['email'] ?? '') ?>">
                    </div>

                    <div class="form-group">
                        <label class="form-label" for="org_phone">Phone Number</label>
                        <input type="text" id="org_phone" name="phone" class="form-control" value="<?= htmlspecialchars($org['phone'] ?? '') ?>">
                    </div>

                    <div class="form-group">
                        <label class="form-label" for="org_address">Address</label>
                        <textarea id="org_address" name="address" class="form-control form-textarea" rows="3"><?= htmlspecialchars($org['address'] ?? '') ?></textarea>
                    </div>
                </div>
            </div>
        </div>

        <!-- Right Column: Locale & Preferences -->
        <div style="display:flex;flex-direction:column;gap:24px;">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">Locale & Preferences</h3>
                </div>
                <div class="card-body">
                    <div class="form-group">
                        <label class="form-label" for="locale">Language / Locale</label>
                        <select id="locale" name="locale" class="form-control form-select">
                            <?php foreach ($locales as $code): ?>
                                <option value="<?= htmlspecialchars($code) ?>" <?= ($org['locale'] ?? DEFAULT_LOCALE) === $code ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($code) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="form-group">
                        <label class="form-label" for="currency">Default Currency</label>
                        <select id="currency" name="currency" class="form-control form-select">
                            <?php foreach ($currencies as $code => $symbol): ?>
                                <option value="<?= htmlspecialchars($code) ?>" <?= ($org['currency'] ?? DEFAULT_CURRENCY) === $code ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($symbol . ' ' . $code) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="form-group">
                        <label class="form-label" for="timezone">Timezone</label>
                        <select id="timezone" name="timezone" class="form-control form-select">
                            <?php foreach ($timezones as $tz): ?>
                                <option value="<?= htmlspecialchars($tz) ?>" <?= ($org['timezone'] ?? DEFAULT_TIMEZONE) === $tz ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($tz) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="form-group">
                        <label class="form-label" for="date_format">Date Format</label>
                        <select id="date_format" name="date_format" class="form-control form-select">
                            <option value="d/m/Y" <?= ($org['date_format'] ?? '') === 'd/m/Y' ? 'selected' : '' ?>>DD/MM/YYYY (25/07/2026)</option>
                            <option value="m/d/Y" <?= ($org['date_format'] ?? '') === 'm/d/Y' ? 'selected' : '' ?>>MM/DD/YYYY (07/25/2026)</option>
                            <option value="Y-m-d" <?= ($org['date_format'] ?? '') === 'Y-m-d' ? 'selected' : '' ?>>YYYY-MM-DD (2026-07-25)</option>
                            <option value="d M Y" <?= ($org['date_format'] ?? '') === 'd M Y' ? 'selected' : '' ?>>DD Mon YYYY (25 Jul 2026)</option>
                        </select>
                    </div>
                </div>
            </div>

            <!-- Subscription Info -->
            <div class="card" style="border-left:4px solid var(--primary);">
                <div class="card-body">
                    <div style="display:flex;align-items:center;gap:12px;margin-bottom:12px;">
                        <div style="width:40px;height:40px;background:linear-gradient(135deg,var(--primary),#12C2CE);border-radius:10px;display:flex;align-items:center;justify-content:center;">
                            <svg width="18" height="18" fill="white" viewBox="0 0 24 24"><path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/></svg>
                        </div>
                        <div>
                            <div style="font-weight:600;font-size:0.9rem;">Current Plan</div>
                            <div style="color:var(--text-muted);font-size:0.8rem;"><?= htmlspecialchars($org['plan_name'] ?? 'Trial') ?></div>
                        </div>
                        <span class="badge badge-<?= ($org['subscription_status'] ?? 'trial') === 'active' ? 'active' : 'warning' ?>" style="margin-left:auto;">
                            <?= ucfirst($org['subscription_status'] ?? 'trial') ?>
                        </span>
                    </div>
                    <a href="<?= APP_URL ?>/settings/billing" class="btn btn-outline-primary btn-sm w-full" style="justify-content:center;">
                        Manage Subscription
                    </a>
                </div>
            </div>
        </div>

    </div>

    <div style="display:flex;justify-content:flex-end;margin-top:24px;gap:12px;">
        <a href="<?= APP_URL ?>/dashboard" class="btn btn-secondary">Cancel</a>
        <button type="submit" class="btn btn-primary">
            <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/><polyline points="17 21 17 13 7 13 7 21"/><polyline points="7 3 7 8 15 8"/></svg>
            Save Changes
        </button>
    </div>
</form>

<script>
function previewLogo(input) {
    if (input.files && input.files[0]) {
        const reader = new FileReader();
        reader.onload = (e) => {
            let img = document.querySelector('img[alt="Logo"]');
            if (!img) {
                img = document.createElement('img');
                img.alt = 'Logo';
                img.style.cssText = 'height:80px;max-width:200px;object-fit:contain;margin-bottom:12px;border-radius:8px;';
                input.closest('.form-group').prepend(img);
            }
            img.src = e.target.result;
        };
        reader.readAsDataURL(input.files[0]);
    }
}
</script>

<?php
$content = ob_get_clean();
require ROOT_PATH . '/modules/shared/views/layouts/main.php';
?>
