<?php
require_once 'includes/db.php';

echo "<div style='font-family:sans-serif; max-width:800px; margin:20px auto; padding:20px; border:1px solid #ddd; border-radius:10px; background:#f9f9f9;'>";
echo "<h2 style='color:#A20C39; border-bottom:2px solid #A20C39; padding-bottom:10px;'>Fixing Database for Online Server...</h2>";

// 1. Fix Notices Table Columns
$queries = [
    "ALTER TABLE `notices` ADD COLUMN IF NOT EXISTS `category` varchar(100) DEFAULT 'General' AFTER `id` text",
    "ALTER TABLE `notices` ADD COLUMN IF NOT EXISTS `image` varchar(255) DEFAULT '' AFTER `title`",
    "ALTER TABLE `notices` ADD COLUMN IF NOT EXISTS `content` longtext AFTER `image`",
    "ALTER TABLE `notices` ADD COLUMN IF NOT EXISTS `pdf_file` varchar(255) DEFAULT '' AFTER `content`"
];

// Wait, the first query had a typo in my previous thought (id text?). Let me fix it.
$queries = [
    "ALTER TABLE `notices` ADD COLUMN IF NOT EXISTS `category` varchar(100) DEFAULT 'General' AFTER `id` ",
    "ALTER TABLE `notices` ADD COLUMN IF NOT EXISTS `image` varchar(255) DEFAULT '' AFTER `title` ",
    "ALTER TABLE `notices` ADD COLUMN IF NOT EXISTS `content` longtext AFTER `image` ",
    "ALTER TABLE `notices` ADD COLUMN IF NOT EXISTS `pdf_file` varchar(255) DEFAULT '' AFTER `content` "
];

echo "<h3>1. Updating Notices Table:</h3>";
foreach ($queries as $q) {
    try {
        if ($conn->query($q)) {
            echo "<p style='color:green; font-size:0.9rem;'>SUCCESS: Column check/add.</p>";
        }
    } catch (Exception $e) {
        echo "<p style='color:orange; font-size:0.9rem;'>INFO: Column already exists or handled.</p>";
    }
}

// 2. Fix Board Members Table & Data
echo "<h3>2. Updating Board Members (Officers Section):</h3>";
$conn->query("CREATE TABLE IF NOT EXISTS `board_members` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `designation` varchar(100) NOT NULL,
  `cell` varchar(50) NOT NULL,
  `image` varchar(255) NOT NULL,
  `description` text,
  `sort_order` int(11) DEFAULT '0',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");

// Check if we need to insert dummy/local data
$check = $conn->query("SELECT id FROM board_members LIMIT 1");
if ($check->num_rows == 0) {
    echo "<p style='color:blue;'>Populating Board Members table with local data...</p>";
    $members = [
        ['name' => 'Mrs. Pallavi Antwadiya', 'designation' => 'President', 'cell' => 'BOARD MINISTERS', 'image' => 'president_pallavi.png', 'sort_order' => 1],
        ['name' => 'Rahul Sharma', 'designation' => 'Secretary', 'cell' => 'BOARD MINISTERS', 'image' => 'secretary_rahul.png', 'sort_order' => 2],
        ['name' => 'Mr. Rajiv', 'designation' => 'Dist President', 'cell' => 'STATE', 'image' => 'indian_male_1.png', 'sort_order' => 5],
        ['name' => 'Ms. Sneha', 'designation' => 'Dist President', 'cell' => 'STATE', 'image' => 'indian_female_1.png', 'sort_order' => 6],
        ['name' => 'Mr. Amit', 'designation' => 'Dist President', 'cell' => 'STATE', 'image' => 'indian_male_1.png', 'sort_order' => 7],
        ['name' => 'Dr. Pooja', 'designation' => 'Dist President', 'cell' => 'STATE', 'image' => 'indian_female_1.png', 'sort_order' => 8],
        ['name' => 'Mr. Sameer', 'designation' => 'Dist President', 'cell' => 'STATE', 'image' => 'indian_male_1.png', 'sort_order' => 9],
        ['name' => 'SHALINI RANI', 'designation' => 'Advocate', 'cell' => 'LEGAL', 'image' => 'adv_female.png', 'sort_order' => 1],
        ['name' => 'Adv Nitin', 'designation' => 'Advocate High Court', 'cell' => 'LEGAL', 'image' => 'adv_female.png', 'sort_order' => 2],
        ['name' => 'Adv Sandeep', 'designation' => 'Advocate High Court', 'cell' => 'LEGAL', 'image' => 'adv_male.png', 'sort_order' => 3],
        ['name' => 'Adv Naveen', 'designation' => 'Advocate High Court', 'cell' => 'LEGAL', 'image' => 'adv_female.png', 'sort_order' => 4],
        ['name' => 'Adv Rajesh', 'designation' => 'Advocate High Court', 'cell' => 'LEGAL', 'image' => 'adv_male.png', 'sort_order' => 5],
        ['name' => 'Rajesh Kumar', 'designation' => 'Media In-charge', 'cell' => 'MEDIA', 'image' => 'indian_young_female.png', 'sort_order' => 1],
        ['name' => 'Sita Singh', 'designation' => 'Public Relations', 'cell' => 'MEDIA', 'image' => 'indian_young_male.png', 'sort_order' => 2],
        ['name' => 'Amit Verma', 'designation' => 'Social Media Manager', 'cell' => 'MEDIA', 'image' => 'indian_young_female.png', 'sort_order' => 3],
    ];

    foreach ($members as $m) {
        $stmt = $conn->prepare("INSERT INTO board_members (name, designation, cell, image, sort_order) VALUES (?, ?, ?, ?, ?)");
        $stmt->bind_param("ssssi", $m['name'], $m['designation'], $m['cell'], $m['image'], $m['sort_order']);
        $stmt->execute();
    }
    echo "<p style='color:green;'>SUCCESS: " . count($members) . " members added.</p>";
} else {
    echo "<p style='color:orange;'>INFO: Board Members table already has data. Skipping population.</p>";
}

echo "<hr><p><b>Database fixed!</b> Your content below the slider should now show up correctly on the online server.</p>";
echo "<p><small>Note: Please make sure to upload the <b>uploads/members/</b> folder to your online server as well.</small></p>";
echo "<a href='index.php' style='display:inline-block; padding:10px 20px; background:#A20C39; color:white; text-decoration:none; border-radius:5px;'>Go back to website</a>";
echo "</div>";
?>
