<?php
// admin/brother-concerns.php - Manage Brother Concerns
session_start();
require_once '../includes/config.php';

if(!isset($_SESSION['user_id']) || $_SESSION['user_role'] != 'admin') {
    header('Location: ../auth.php');
    exit;
}

$message = '';
$msg_type = 'info';

// ==================== ADD / EDIT ====================
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['save_concern'])) {
    $id = $_POST['id'] ?? null;
    $title = trim($_POST['title']);
    $subtitle = trim($_POST['subtitle'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $icon = trim($_POST['icon'] ?? 'fa-building');
    $gradient_color = trim($_POST['gradient_color'] ?? '');
    $main_button_text = trim($_POST['main_button_text'] ?? 'বিস্তারিত');
    $main_button_url = trim($_POST['main_button_url'] ?? '#');
    $main_button_color = trim($_POST['main_button_color'] ?? '#6C5CE7');
    $show_sub_button = isset($_POST['show_sub_button']) ? 1 : 0;
    $sub_button_text = trim($_POST['sub_button_text'] ?? '');
    $sub_button_url = trim($_POST['sub_button_url'] ?? '#');
    $sub_button_color = trim($_POST['sub_button_color'] ?? '#28a745');
    $badge_text = trim($_POST['badge_text'] ?? '');
    $badge_color = trim($_POST['badge_color'] ?? '#ff4757');
    $order_number = (int)$_POST['order_number'];
    $is_active = isset($_POST['is_active']) ? 1 : 0;
    
    if(empty($title)) {
        $message = 'টাইটেল প্রয়োজন';
        $msg_type = 'danger';
    } else {
        if($id) {
            $stmt = $pdo->prepare("UPDATE brother_concerns SET title=?, subtitle=?, description=?, icon=?, gradient_color=?, main_button_text=?, main_button_url=?, main_button_color=?, show_sub_button=?, sub_button_text=?, sub_button_url=?, sub_button_color=?, badge_text=?, badge_color=?, order_number=?, is_active=? WHERE id=?");
            $stmt->execute([$title, $subtitle, $description, $icon, $gradient_color, $main_button_text, $main_button_url, $main_button_color, $show_sub_button, $sub_button_text, $sub_button_url, $sub_button_color, $badge_text, $badge_color, $order_number, $is_active, $id]);
        } else {
            $stmt = $pdo->prepare("INSERT INTO brother_concerns (title, subtitle, description, icon, gradient_color, main_button_text, main_button_url, main_button_color, show_sub_button, sub_button_text, sub_button_url, sub_button_color, badge_text, badge_color, order_number, is_active) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            $stmt->execute([$title, $subtitle, $description, $icon, $gradient_color, $main_button_text, $main_button_url, $main_button_color, $show_sub_button, $sub_button_text, $sub_button_url, $sub_button_color, $badge_text, $badge_color, $order_number, $is_active]);
        }
        $message = '✅ সংরক্ষিত হয়েছে!';
        $msg_type = 'success';
    }
}

// Delete
if(isset($_GET['delete'])) {
    $pdo->prepare("DELETE FROM brother_concerns WHERE id = ?")->execute([$_GET['delete']]);
    header('Location: brother-concerns.php?msg=ডিলিট+হয়েছে&type=success');
    exit;
}

$msg = $_GET['msg'] ?? $message;
$msg_type = $_GET['type'] ?? $msg_type;
$concerns = $pdo->query("SELECT * FROM brother_concerns ORDER BY order_number ASC")->fetchAll();

// Edit item
$edit = null;
if(isset($_GET['edit'])) {
    $stmt = $pdo->prepare("SELECT * FROM brother_concerns WHERE id = ?");
    $stmt->execute([$_GET['edit']]);
    $edit = $stmt->fetch();
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <title>ব্রাদার কনসার্ন ম্যানেজমেন্ট</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root { --primary: #6C5CE7; --gradient: linear-gradient(135deg, #667eea, #764ba2); }
        body { background:#f0f2f8; font-family:'Segoe UI',Arial,sans-serif; }
        .navbar { background:white; padding:12px 0; box-shadow:0 2px 10px rgba(0,0,0,0.06); position:sticky; top:0; z-index:100; }
        .main-card { background:white; border-radius:15px; padding:25px; box-shadow:0 3px 15px rgba(0,0,0,0.08); margin-bottom:20px; }
        .concern-card { border:1px solid #eee; border-radius:12px; padding:15px; margin-bottom:10px; transition:0.3s; }
        .concern-card:hover { box-shadow:0 3px 15px rgba(0,0,0,0.1); }
        .color-preview { width:40px; height:40px; border-radius:10px; display:inline-block; border:2px solid #ddd; }
        .form-control, .form-select { border-radius:10px; padding:10px; border:2px solid #e0e0e0; }
        .form-control:focus { border-color:var(--primary); outline:none; }
        .btn { border-radius:50px; padding:8px 20px; font-weight:600; }
        .btn-primary { background:var(--gradient); border:none; color:white; }
        .badge-status { padding:5px 12px; border-radius:20px; font-size:0.75rem; }
    </style>
</head>
<body>
<nav class="navbar">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center">
            <a href="dashboard.php" class="text-decoration-none fw-bold" style="color:var(--primary);font-size:1.2rem;">
                <i class="fas fa-graduation-cap me-2"></i>অ্যাডমিন
            </a>
            <a href="../logout.php" class="btn btn-sm rounded-pill" style="background:#ff4757;color:white;">লগআউট</a>
        </div>
    </div>
</nav>

<div class="container py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4><i class="fas fa-handshake text-primary me-2"></i>ব্রাদার কনসার্ন ম্যানেজমেন্ট</h4>
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addModal" onclick="resetForm()">
            <i class="fas fa-plus me-1"></i> নতুন যোগ করুন
        </button>
    </div>
    
    <?php if($msg): ?>
        <div class="alert alert-<?= $msg_type ?> rounded-3"><?= htmlspecialchars($msg) ?></div>
    <?php endif; ?>
    
    <!-- List -->
    <div class="main-card">
        <?php foreach($concerns as $c): ?>
            <div class="concern-card d-flex justify-content-between align-items-center flex-wrap">
                <div class="d-flex align-items-center gap-3">
                    <div class="color-preview" style="background:<?= $c['gradient_color'] ?>;"></div>
                    <div>
                        <strong><?= htmlspecialchars($c['title']) ?></strong>
                        <br><small class="text-muted"><?= $c['main_button_text'] ?> | <?= $c['badge_text'] ?: 'No badge' ?></small>
                    </div>
                </div>
                <div class="d-flex gap-2 align-items-center mt-2 mt-md-0">
                    <span class="badge-status bg-<?= $c['is_active']?'success':'secondary' ?>">
                        <?= $c['is_active']?'✅ সক্রিয়':'❌ নিষ্ক্রিয়' ?>
                    </span>
                    <button class="btn btn-outline-primary btn-sm" onclick="editConcern(<?= htmlspecialchars(json_encode($c)) ?>)">
                        <i class="fas fa-edit"></i>
                    </button>
                    <a href="?delete=<?= $c['id'] ?>" class="btn btn-outline-danger btn-sm" onclick="return confirm('ডিলিট করবেন?')">
                        <i class="fas fa-trash"></i>
                    </a>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>

<!-- Add/Edit Modal -->
<div class="modal fade" id="addModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content border-0 rounded-4">
            <div class="modal-header" style="background:var(--gradient);color:white;border-radius:15px 15px 0 0;">
                <h5 class="modal-title" id="modalTitle"><i class="fas fa-handshake me-2"></i>ব্রাদার কনসার্ন যোগ করুন</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <input type="hidden" name="save_concern" value="1">
                <input type="hidden" name="id" id="concernId">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6 mb-2"><label>টাইটেল *</label><input type="text" name="title" id="title" class="form-control" required></div>
                        <div class="col-md-6 mb-2"><label>সাব-টাইটেল</label><input type="text" name="subtitle" id="subtitle" class="form-control"></div>
                        <div class="col-12 mb-2"><label>বিবরণ</label><textarea name="description" id="description" class="form-control" rows="2"></textarea></div>
                        <div class="col-md-4 mb-2"><label>আইকন (Font Awesome)</label><input type="text" name="icon" id="icon" class="form-control" value="fa-building"></div>
                        <div class="col-md-4 mb-2"><label>গ্রেডিয়েন্ট কালার</label><input type="text" name="gradient_color" id="gradient_color" class="form-control" value="linear-gradient(135deg, #667eea, #764ba2)"></div>
                        <div class="col-md-4 mb-2"><label>ব্যাজ টেক্সট</label><input type="text" name="badge_text" id="badge_text" class="form-control" placeholder="যেমন: ফ্রি, ১০% অফার"></div>
                        <div class="col-md-6 mb-2"><label>ব্যাজ কালার</label><input type="color" name="badge_color" id="badge_color" class="form-control" value="#ff4757"></div>
                        <div class="col-md-6 mb-2"><label>ক্রমিক নং</label><input type="number" name="order_number" id="order_number" class="form-control" value="1"></div>
                        
                        <hr class="my-3"><h6>🔘 মেইন বাটন</h6>
                        <div class="col-md-4 mb-2"><label>বাটন টেক্সট</label><input type="text" name="main_button_text" id="main_button_text" class="form-control" value="বিস্তারিত"></div>
                        <div class="col-md-4 mb-2"><label>বাটন URL</label><input type="text" name="main_button_url" id="main_button_url" class="form-control"></div>
                        <div class="col-md-4 mb-2"><label>বাটন কালার</label><input type="color" name="main_button_color" id="main_button_color" class="form-control" value="#6C5CE7"></div>
                        
                        <hr class="my-3"><h6>🔘 সাব বাটন</h6>
                        <div class="col-12 mb-2"><div class="form-check"><input type="checkbox" name="show_sub_button" id="show_sub_button" class="form-check-input"><label class="form-check-label">সাব বাটন দেখান</label></div></div>
                        <div class="col-md-4 mb-2"><label>সাব বাটন টেক্সট</label><input type="text" name="sub_button_text" id="sub_button_text" class="form-control" placeholder="অর্ডার করুন"></div>
                        <div class="col-md-4 mb-2"><label>সাব বাটন URL</label><input type="text" name="sub_button_url" id="sub_button_url" class="form-control"></div>
                        <div class="col-md-4 mb-2"><label>সাব বাটন কালার</label><input type="color" name="sub_button_color" id="sub_button_color" class="form-control" value="#28a745"></div>
                        
                        <div class="col-12 mt-2"><div class="form-check"><input type="checkbox" name="is_active" id="is_active" class="form-check-input" checked><label class="form-check-label">সক্রিয়</label></div></div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary rounded-pill" data-bs-dismiss="modal">বাতিল</button>
                    <button type="submit" class="btn btn-primary rounded-pill"><i class="fas fa-save me-1"></i> সংরক্ষণ</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
function resetForm() {
    document.getElementById('concernId').value = '';
    document.getElementById('modalTitle').innerHTML = '<i class="fas fa-handshake me-2"></i>ব্রাদার কনসার্ন যোগ করুন';
    ['title','subtitle','description','icon','gradient_color','badge_text','main_button_text','main_button_url','sub_button_text','sub_button_url','order_number'].forEach(id => document.getElementById(id).value = '');
    document.getElementById('icon').value = 'fa-building';
    document.getElementById('gradient_color').value = 'linear-gradient(135deg, #667eea, #764ba2)';
    document.getElementById('main_button_text').value = 'বিস্তারিত';
    document.getElementById('show_sub_button').checked = false;
    document.getElementById('is_active').checked = true;
    document.getElementById('order_number').value = '1';
}

function editConcern(data) {
    document.getElementById('modalTitle').innerHTML = '<i class="fas fa-edit me-2"></i>এডিট করুন';
    document.getElementById('concernId').value = data.id;
    document.getElementById('title').value = data.title;
    document.getElementById('subtitle').value = data.subtitle || '';
    document.getElementById('description').value = data.description || '';
    document.getElementById('icon').value = data.icon || 'fa-building';
    document.getElementById('gradient_color').value = data.gradient_color || '';
    document.getElementById('badge_text').value = data.badge_text || '';
    document.getElementById('badge_color').value = data.badge_color || '#ff4757';
    document.getElementById('order_number').value = data.order_number || 1;
    document.getElementById('main_button_text').value = data.main_button_text || '';
    document.getElementById('main_button_url').value = data.main_button_url || '';
    document.getElementById('main_button_color').value = data.main_button_color || '#6C5CE7';
    document.getElementById('show_sub_button').checked = data.show_sub_button == 1;
    document.getElementById('sub_button_text').value = data.sub_button_text || '';
    document.getElementById('sub_button_url').value = data.sub_button_url || '';
    document.getElementById('sub_button_color').value = data.sub_button_color || '#28a745';
    document.getElementById('is_active').checked = data.is_active == 1;
    
    new bootstrap.Modal(document.getElementById('addModal')).show();
}
</script>
</body>
</html>