<?php
/**
 * partner_logic.php - Handles CRUD operations for Connected Partners
 */

// Create table if not exists
mysqli_query($conn, "CREATE TABLE IF NOT EXISTS partners (
    id INT AUTO_INCREMENT PRIMARY KEY,
    partner_name VARCHAR(255) NOT NULL,
    logo_url TEXT,
    status ENUM('Active', 'Inactive') DEFAULT 'Active',
    sort_order INT DEFAULT 0
)");

$msg = "";

// Action: Add Partner
if (isset($_POST['add_partner'])) {
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $url = mysqli_real_escape_string($conn, $_POST['url']);
    $order = intval($_POST['order']);
    
    $q = mysqli_query($conn, "INSERT INTO partners (partner_name, logo_url, sort_order) VALUES ('$name', '$url', '$order')");
    $msg = $q ? "পার্টনার যুক্ত হয়েছে!" : "ভুল হয়েছে।";
}

// Action: Delete Partner
if (isset($_GET['delete_partner']) && $is_admin) {
    $id = intval($_GET['delete_partner']);
    mysqli_query($conn, "DELETE FROM partners WHERE id = $id");
    echo "<script>window.location='admin.php?view=partners';</script>";
}

$all_partners = mysqli_query($conn, "SELECT * FROM partners ORDER BY sort_order ASC");
?>

<div class="section-card">
    <h3><i class="fas fa-handshake"></i> পার্টনার ম্যানেজমেন্ট</h3>
    <?php if($msg) echo "<p style='color:green;'>$msg</p>"; ?>

    <form method="POST" style="display: flex; gap: 10px; margin-bottom: 20px; flex-wrap: wrap;">
        <input type="text" name="name" placeholder="পার্টনারের নাম" required style="padding: 8px; border: 1px solid var(--border); border-radius: 5px; flex: 1;">
        <input type="text" name="url" placeholder="লোগো URL (যেমন: images/logo.png)" style="padding: 8px; border: 1px solid var(--border); border-radius: 5px; flex: 2;">
        <input type="number" name="order" value="0" style="padding: 8px; border: 1px solid var(--border); border-radius: 5px; width: 60px;">
        <button type="submit" name="add_partner" class="btn" style="background: var(--primary); color: white; padding: 8px 20px; border-radius: 5px;">যুক্ত করুন</button>
    </form>

    <table>
        <thead>
            <tr>
                <th>নাম</th>
                <th>লোগো</th>
                <th>অর্ডার</th>
                <th>অ্যাকশন</th>
            </tr>
        </thead>
        <tbody>
            <?php while($p = mysqli_fetch_assoc($all_partners)): ?>
            <tr>
                <td><?php echo $p['partner_name']; ?></td>
                <td><img src="<?php echo $p['logo_url']; ?>" height="30" onerror="this.src='https://via.placeholder.com/30'"></td>
                <td><?php echo $p['sort_order']; ?></td>
                <td>
                    <?php if($is_admin): ?>
                        <a href="admin.php?view=partners&delete_partner=<?php echo $p['id']; ?>" style="color: var(--red);" onclick="return confirm('মুছে ফেলতে চান?')"><i class="fas fa-trash"></i></a>
                    <?php else: ?>
                        -
                    <?php endif; ?>
                </td>
            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
</div>