<?php
include("connection.php");
ini_set('display_errors', 1);
error_reporting(E_ALL);

$error_message = '';

if (isset($_POST['form1'])) {

    $allowed_exts = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
    $files = $_FILES['logo'];
    $total = count($files['name']);

    if ($total > 0) {
        for ($i = 0; $i < $total; $i++) {

            $file_name = $files['name'][$i];
            $file_tmp  = $files['tmp_name'][$i];
            $ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

            if (empty($file_name)) continue;

            if (!in_array($ext, $allowed_exts)) {
                $error_message .= "Invalid image format: $file_name<br>";
                continue;
            }

            $new_name = 'partner-' . date('YmdHis') . '-' . uniqid() . '.' . $ext;
            $upload_path = 'assets/images/partner_logo/' . $new_name;

            if (move_uploaded_file($file_tmp, $upload_path)) {
                $stmt = $con->prepare("INSERT INTO partner_logo (logo) VALUES (?)");
                $stmt->bind_param("s", $new_name);
                $stmt->execute();
            } else {
                $error_message .= "Upload failed: $file_name<br>";
            }
        }

        if (empty($error_message)) {
            header("Location: viewpartner.php");
            exit;
        }
    } else {
        $error_message = "Please select at least one logo.";
    }
}
?>

<?php include("header.php"); ?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Add Partner Logo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link href="assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/icons.min.css" rel="stylesheet">
    <link href="assets/css/app.min.css" rel="stylesheet">

    <style>
        .dropzone {
            border: 2px dashed #ccc;
            padding: 40px;
            text-align: center;
            border-radius: 10px;
            background: #f9f9f9;
            cursor: pointer;
            position: relative;
        }
        .dropzone input {
            position: absolute;
            inset: 0;
            opacity: 0;
            cursor: pointer;
        }
        .preview-container img {
            max-width: 150px;
            border: 1px solid #ddd;
            padding: 5px;
            border-radius: 6px;
        }
    </style>
</head>

<body data-sidebar="dark">

<div id="layout-wrapper">
    <div class="page-content" style="width:80%;margin-left:260px;">
        <div class="container">

            <!-- Page Title -->
            <div class="row">
                <div class="col-12">
                    <h4 class="mb-4">Add Partner Logo</h4>
                </div>
            </div>

            <div class="card">
                <div class="card-body">

                    <?php if (!empty($error_message)) { ?>
                        <div class="alert alert-danger"><?= $error_message; ?></div>
                    <?php } ?>

                    <form method="POST" enctype="multipart/form-data">
                        <input type="hidden" name="form1" value="1">

                        <!-- Upload Box -->
                        <div class="dropzone mb-3">
                            <input type="file" name="logo[]" id="logoInput" multiple>
                            <i class="bx bxs-cloud-upload" style="font-size:40px;"></i>
                            <h5>Click or Drop Partner Logos</h5>
                        </div>

                        <!-- Preview -->
                        <div class="preview-container d-flex flex-wrap gap-3 mb-3"
                             id="previewContainer"></div>

                        <div class="text-center">
                            <button type="submit" class="btn btn-primary">Upload</button>
                            <a href="viewpartner.php" class="btn btn-danger">View Logos</a>
                        </div>
                    </form>

                </div>
            </div>

        </div>
    </div>
</div>

<script>
const input = document.getElementById('logoInput');
const preview = document.getElementById('previewContainer');

input.addEventListener('change', () => {
    preview.innerHTML = '';
    [...input.files].forEach(file => {
        if (file.type.startsWith('image/')) {
            const reader = new FileReader();
            reader.onload = e => {
                const img = document.createElement('img');
                img.src = e.target.result;
                preview.appendChild(img);
            };
            reader.readAsDataURL(file);
        }
    });
});
</script>

<?php include("footer.php"); ?>
</body>
</html>
