<?php
// member/organization.php - Organization Management
require_once '../config.php';
requireLogin();

$userId = $_SESSION['user_id'];
$org = getUserOrganization($userId);
$message = '';
$error = '';

// Handle organization creation/update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = [
        'name' => trim($_POST['name'] ?? ''),
        'website' => trim($_POST['website'] ?? ''),
        'industry' => trim($_POST['industry'] ?? ''),
        'size' => trim($_POST['size'] ?? ''),
        'address' => trim($_POST['address'] ?? ''),
        'phone' => trim($_POST['phone'] ?? ''),
        'email' => trim($_POST['email'] ?? ''),
        'registration_number' => trim($_POST['registration_number'] ?? ''),
        'tax_id' => trim($_POST['tax_id'] ?? '')
    ];
    
    if ($org) {
        if (updateOrganization($userId, $org['id'], $data)) {
            $message = 'Organization updated successfully';
            $org = getUserOrganization($userId);
        } else {
            $error = 'Failed to update organization';
        }
    } else {
        $orgId = createOrganization($userId, $data);
        if ($orgId) {
            $message = 'Organization created successfully';
            $org = getUserOrganization($userId);
        } else {
            $error = 'Failed to create organization';
        }
    }
}

$industries = [
    'Technology', 'Financial Services', 'Healthcare', 'Education', 
    'Government', 'Retail', 'Manufacturing', 'Legal', 'Consulting',
    'Non-Profit', 'Hospitality', 'Transportation', 'Real Estate', 'Other'
];

$sizes = ['1-10', '11-50', '51-200', '201-500', '501-1000', '1000+'];

$theme = getTheme();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Organization - Soul Guard</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            transition: all 0.3s ease;
        }
        
        body.dark {
            background: #0a0a0f;
            color: #e4e4e7;
        }
        
        body.light {
            background: #f5f5f5;
            color: #1a1a2e;
        }
        
        .navbar {
            background: rgba(19, 19, 26, 0.95);
            backdrop-filter: blur(10px);
            border-bottom: 1px solid rgba(255, 255, 255, 0.06);
            padding: 15px 30px;
            position: sticky;
            top: 0;
            z-index: 100;
        }
        
        body.light .navbar {
            background: rgba(255, 255, 255, 0.95);
            border-bottom: 1px solid rgba(0, 0, 0, 0.1);
        }
        
        .nav-container {
            max-width: 1400px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
        }
        
        .logo h2 {
            background: linear-gradient(135deg, #06b6d4, #14b8a6);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-size: 24px;
        }
        
        .nav-links {
            display: flex;
            gap: 10px;
            align-items: center;
            flex-wrap: wrap;
        }
        
        .nav-links a {
            color: #a1a1aa;
            text-decoration: none;
            padding: 8px 14px;
            border-radius: 8px;
            transition: all 0.2s;
        }
        
        .nav-links a:hover, .nav-links a.active {
            background: rgba(6, 182, 212, 0.1);
            color: #06b6d4;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 40px 20px;
        }
        
        .card {
            background: rgba(255, 255, 255, 0.03);
            border: 1px solid rgba(255, 255, 255, 0.06);
            border-radius: 20px;
            padding: 30px;
            margin-bottom: 30px;
        }
        
        body.light .card {
            background: #fff;
            border: 1px solid #e0e0e0;
        }
        
        .card h3 {
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid rgba(6, 182, 212, 0.3);
        }
        
        .form-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            font-weight: 500;
        }
        
        input, select, textarea {
            width: 100%;
            padding: 12px 16px;
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 10px;
            color: inherit;
            font-size: 14px;
        }
        
        body.light input, body.light select, body.light textarea {
            background: #f9f9f9;
            border: 1px solid #ddd;
            color: #1a1a2e;
        }
        
        input:focus, select:focus, textarea:focus {
            outline: none;
            border-color: #06b6d4;
        }
        
        textarea {
            resize: vertical;
            min-height: 80px;
        }
        
        .btn {
            display: inline-block;
            padding: 12px 24px;
            background: linear-gradient(135deg, #06b6d4, #14b8a6);
            color: white;
            text-decoration: none;
            border-radius: 10px;
            border: none;
            cursor: pointer;
            font-size: 14px;
            font-weight: 600;
            width: 100%;
        }
        
        .btn:hover {
            transform: translateY(-2px);
        }
        
        .message {
            background: rgba(34, 197, 94, 0.2);
            border: 1px solid rgba(34, 197, 94, 0.3);
            color: #22c55e;
            padding: 12px;
            border-radius: 10px;
            margin-bottom: 20px;
        }
        
        .error {
            background: rgba(239, 68, 68, 0.2);
            border: 1px solid rgba(239, 68, 68, 0.3);
            color: #ef4444;
            padding: 12px;
            border-radius: 10px;
            margin-bottom: 20px;
        }
        
        .theme-toggle {
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 50%;
            width: 36px;
            height: 36px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            font-size: 18px;
        }
        
        @media (max-width: 600px) {
            .form-row {
                grid-template-columns: 1fr;
            }
            .container {
                padding: 20px;
            }
            .card {
                padding: 20px;
            }
        }
    </style>
</head>
<body class="<?php echo $theme; ?>">
    <nav class="navbar">
        <div class="nav-container">
            <div class="logo">
                <h2>🛡️ Soul Guard</h2>
            </div>
            <div class="nav-links">
                <a href="dashboard.php">Dashboard</a>
                <a href="profile.php">Profile</a>
                <a href="organization.php" class="active">Organization</a>
                <a href="reports.php">Reports</a>
                <a href="logout.php">Logout</a>
                <div class="theme-toggle" onclick="toggleTheme()">
                    <?php echo $theme === 'dark' ? '☀️' : '🌙'; ?>
                </div>
            </div>
        </div>
    </nav>
    
    <div class="container">
        <?php if ($message): ?>
            <div class="message"><?php echo htmlspecialchars($message); ?></div>
        <?php endif; ?>
        
        <?php if ($error): ?>
            <div class="error"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>
        
        <div class="card">
            <h3><?php echo $org ? '🏢 Edit Organization' : '🏢 Create Organization'; ?></h3>
            
            <form method="POST">
                <div class="form-group">
                    <label>Organization Name *</label>
                    <input type="text" name="name" required value="<?php echo htmlspecialchars($org['name'] ?? ''); ?>">
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label>Industry</label>
                        <select name="industry">
                            <option value="">Select Industry</option>
                            <?php foreach ($industries as $ind): ?>
                                <option value="<?php echo $ind; ?>" <?php echo (($org['industry'] ?? '') == $ind) ? 'selected' : ''; ?>><?php echo $ind; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label>Organization Size</label>
                        <select name="size">
                            <option value="">Select Size</option>
                            <?php foreach ($sizes as $s): ?>
                                <option value="<?php echo $s; ?>" <?php echo (($org['size'] ?? '') == $s) ? 'selected' : ''; ?>><?php echo $s; ?> employees</option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label>Website</label>
                        <input type="text" name="website" placeholder="www.example.com" value="<?php echo htmlspecialchars($org['website'] ?? ''); ?>">
                    </div>
                    
                    <div class="form-group">
                        <label>Email</label>
                        <input type="email" name="email" placeholder="contact@example.com" value="<?php echo htmlspecialchars($org['email'] ?? ''); ?>">
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label>Phone</label>
                        <input type="tel" name="phone" placeholder="+254 XXX XXX XXX" value="<?php echo htmlspecialchars($org['phone'] ?? ''); ?>">
                    </div>
                    
                    <div class="form-group">
                        <label>Registration Number</label>
                        <input type="text" name="registration_number" placeholder="Business Registration Number" value="<?php echo htmlspecialchars($org['registration_number'] ?? ''); ?>">
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label>Tax ID / PIN</label>
                        <input type="text" name="tax_id" placeholder="KRA PIN" value="<?php echo htmlspecialchars($org['tax_id'] ?? ''); ?>">
                    </div>
                </div>
                
                <div class="form-group">
                    <label>Address</label>
                    <textarea name="address" placeholder="Physical address"><?php echo htmlspecialchars($org['address'] ?? ''); ?></textarea>
                </div>
                
                <button type="submit" class="btn"><?php echo $org ? 'Update Organization' : 'Create Organization'; ?></button>
            </form>
        </div>
        
        <?php if ($org): ?>
        <div class="card">
            <h3>ℹ️ Organization Info</h3>
            <table style="width: 100%;">
                <tr><th>Created</th><td><?php echo date('F j, Y', strtotime($org['created_at'])); ?></td></tr>
                <tr><th>Last Updated</th><td><?php echo date('F j, Y', strtotime($org['updated_at'])); ?></td></tr>
                <tr><th>DPA Status</th><td><span style="color:#06b6d4">Ready for assessment</span></td></tr>
            </table>
        </div>
        <?php endif; ?>
    </div>
    
    <script>
        function toggleTheme() {
            const body = document.body;
            const isDark = body.classList.contains('dark');
            const newTheme = isDark ? 'light' : 'dark';
            
            body.classList.remove('dark', 'light');
            body.classList.add(newTheme);
            
            document.cookie = `soulguard_theme=${newTheme}; path=/; max-age=31536000`;
            
            location.reload();
        }
    </script>
</body>
</html>