<?php
include 'config.php';

// --- অটোমেটিক ডাটাবেস কলাম চেক ও আপডেট ---
$check_cols = [
    'category_align' => "VARCHAR(20) DEFAULT 'center'",
    'offer_percentage' => "VARCHAR(10) DEFAULT ''",
    'show_discount' => "TINYINT(1) DEFAULT 0",
    'item_status' => "VARCHAR(50) DEFAULT 'normal'",
    'timer_deadline' => "DATETIME NULL"
];

foreach ($check_cols as $col => $definition) {
    $res = $conn->query("SHOW COLUMNS FROM site_links LIKE '$col'");
    if ($res->num_rows == 0) {
        $conn->query("ALTER TABLE site_links ADD COLUMN $col $definition");
    }
}

// --- HANDLE NEW ITEM INSERTION ---
if (isset($_POST['submit'])) {
    $title = mysqli_real_escape_string($conn, $_POST['title']);
    $desc = mysqli_real_escape_string($conn, $_POST['description']);
    $url = mysqli_real_escape_string($conn, $_POST['url']);
    $category = mysqli_real_escape_string($conn, $_POST['category']);
    $align = isset($_POST['category_align']) ? mysqli_real_escape_string($conn, $_POST['category_align']) : 'center';
    
    $offer = mysqli_real_escape_string($conn, $_POST['offer_percentage']);
    $show_discount = (int)$_POST['show_discount'];
    $item_status = mysqli_real_escape_string($conn, $_POST['item_status']);
    
    // টাইমার ফরম্যাট ফিক্স (T সরিয়ে স্পেস দেওয়া হচ্ছে)
    $timer = !empty($_POST['timer_deadline']) ? str_replace('T', ' ', $_POST['timer_deadline']) : NULL;
    $timer = $timer ? mysqli_real_escape_string($conn, $timer) : NULL;

    $img = time()."_".basename($_FILES["image"]["name"]);
    move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/".$img);

    $sql = "INSERT INTO site_links (title, description, image, url, category, category_align, offer_percentage, show_discount, item_status, timer_deadline) 
            VALUES ('$title', '$desc', '$img', '$url', '$category', '$align', '$offer', '$show_discount', '$item_status', " . ($timer ? "'$timer'" : "NULL") . ")";
    
    if($conn->query($sql)) {
        header("Location: admin/items.php");
    } else {
        die("DB Error: " . $conn->error);
    }
    exit;
}

// --- HANDLE ITEM UPDATE (EDIT FEATURE) ---
if (isset($_POST['update_item'])) {
    $id = (int)$_POST['id'];
    $title = mysqli_real_escape_string($conn, $_POST['title']);
    $desc = mysqli_real_escape_string($conn, $_POST['description']);
    $url = mysqli_real_escape_string($conn, $_POST['url']);
    $category = mysqli_real_escape_string($conn, $_POST['category']);
    $align = isset($_POST['category_align']) ? mysqli_real_escape_string($conn, $_POST['category_align']) : 'center';

    $offer = mysqli_real_escape_string($conn, $_POST['offer_percentage']);
    $show_discount = (int)$_POST['show_discount'];
    $item_status = mysqli_real_escape_string($conn, $_POST['item_status']);
    
    // টাইমার ফরম্যাট ফিক্স (T সরিয়ে স্পেস দেওয়া হচ্ছে)
    $timer = !empty($_POST['timer_deadline']) ? str_replace('T', ' ', $_POST['timer_deadline']) : NULL;
    $timer = $timer ? mysqli_real_escape_string($conn, $timer) : NULL;

    if (!empty($_FILES['image']['name'])) {
        $img = time()."_".basename($_FILES["image"]["name"]);
        move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/".$img);
        $img_sql = ", image='$img'";
    } else { $img_sql = ""; }

    $timer_val = ($timer ? "'$timer'" : "NULL");

    $sql = "UPDATE site_links SET 
            title='$title', 
            description='$desc', 
            url='$url', 
            category='$category', 
            category_align='$align', 
            offer_percentage='$offer', 
            show_discount='$show_discount', 
            item_status='$item_status', 
            timer_deadline=$timer_val 
            $img_sql 
            WHERE id=$id";

    if($conn->query($sql)) {
        header("Location: admin/items.php");
    } else {
        die("DB Error: " . $conn->error);
    }
    exit;
}
?>