<?php include 'includes/header.php'; ?>
<?php include 'includes/sidebar.php'; ?>
<?php require 'includes/db_connect.php'; ?>

<?php
if (isset($_POST['submit'])) {
    $subject = trim($_POST['subject']);
    $summary = trim($_POST['summary']);
    $tracking_no = trim($_POST['tracking_no']);
    $mail_type = $_POST['mail_type'];
    $status = "Pending";
    $department = $_POST['department'];
    $assigned_to = $_POST['assigned_to'];
    $reg_date = date('Y-m-d');
    $random_serial = rand(3, 3000000);

// Concatenate with TRACK3
$tracking_no = "TRACK" . $random_serial;

    // Image Upload
    $image_name = '';
    if (isset($_FILES['image']) && $_FILES['image']['name'] != '') {
        $target_dir = "uploads/mails/";
        if (!is_dir($target_dir)) mkdir($target_dir, 0777, true);
        $image_name = time() . '_' . basename($_FILES['image']['name']);
        $target_file = $target_dir . $image_name;
        move_uploaded_file($_FILES['image']['tmp_name'], $target_file);
    }

    $stmt = $conn->prepare("INSERT INTO mails (subject, summary, tracking_no, mail_type, status, department, assigned_to, reg_date, image) 
                            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("sssssssss", $subject, $summary, $tracking_no, $mail_type, $status, $department, $assigned_to, $reg_date, $image_name);

    if ($stmt->execute()) {
        echo "<script>alert('Mail entry added successfully'); window.location='all_mails.php';</script>";
    } else {
        echo "<div class='alert alert-danger'>Error adding mail: " . $conn->error . "</div>";
    }
}
?>

<div class="container mt-4">
    <h4 class="text-orange">Enter New Mail</h4>
    <form method="POST" enctype="multipart/form-data" class="mt-3">
        <div class="mb-3">
            <label>Subject</label>
            <input type="text" name="subject" class="form-control" required>
        </div>
        <div class="mb-3">
            <label>Summary</label>
            <textarea name="summary" class="form-control" rows="3" required></textarea>
        </div>
       <!--  <div class="mb-3">
            <label>Tracking Number</label>
            <input type="hidden" name="tracking_no" value="TRACK" class="form-control" required>
        </div> -->
        <div class="mb-3">
            <label>Mail Type</label>
            <select name="mail_type" class="form-control" required>
                <option value="">-- Select Type --</option>
                <option value="Incoming">Incoming</option>
                <option value="Outgoing">Outgoing</option>
            </select>
        </div>
        <div class="mb-3">
            <label>Department</label>
            <input type="text" name="department" class="form-control" required>
        </div>
        <div class="mb-3">
            <label>Assign To (Staff)</label>
            <select name="assigned_to" class="form-control" required>
                <option value="">-- Select Staff --</option>
                <?php
                $staffs = $conn->query("SELECT * FROM staff ORDER BY fullname ASC");
                while ($staff = $staffs->fetch_assoc()) {
                    echo "<option value='{$staff['id']}'>{$staff['fullname']} - {$staff['department']}</option>";
                }
                ?>
            </select>
        </div>
        <div class="mb-3">
            <label>Upload Mail Image (Optional)</label>
            <input type="file" name="image" class="form-control">
        </div>
        <button type="submit" name="submit" class="btn btn-lg btn-success text-whit" style="background-color: orange;">Save Mail</button>
    </form>
</div>

<?php include 'includes/footer.php'; ?>
