<?php require ("header.php")?>


    
    
    
              
 

  <?php

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {

    // Sanitize inputs
    $log_id = $_POST['id'] ?? '';
    $email = $_POST['email'] ?? '';
    $name = $_POST['name'] ?? '';
    $phone = $_POST['phone'] ?? '';
    $msg = $_POST['msg'] ?? '';
    $title = $_POST['title'] ?? '';
    $date = $_POST['date'] ?? '';
    $cemail = $_POST['cemail'] ?? '';
    $cphone = $_POST['cphone'] ?? '';
    $cname = $_POST['cname'] ?? '';

    try {
        $stmt = $pdo->prepare("INSERT INTO message (log_id, email, name, phone, msg, title, status, date) 
            VALUES (:log_id, :email, :name, :phone, :msg, :title, '1', :date)");
        $stmt->execute([
            ':log_id' => $log_id,
            ':email' => $email,
            ':name' => $cname,
            ':phone' => $phone,
            ':msg' => $msg,
            ':title' => $title,
            ':date' => $date
        ]);

        // If successful, send emails
        $adminMessage = "
            <p>Your message has been delivered to the contact below:</p>
            <b>NAME:</b> $name<br>
            <b>PHONE:</b> $phone<br>
            <b>EMAIL:</b> $email<br>
            <b>MESSAGE:</b> $msg<br>
        ";

        $userMessage = "
            <p>You have a new message from $cname:</p>
            <b>TITLE:</b> $title<br>
            <b>PHONE:</b> $cphone<br>
            <b>EMAIL:</b> $cemail<br>
            <b>MESSAGE:</b> $msg<br>
        ";

        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8\r\n";
        $headers .= "From: $email\r\n";

        // Send to admin
        mail($cemail, "MESSAGE DELIVERED TO ($name)", $adminMessage, $headers);
        // Send to user
        mail($email, "MESSAGE FROM ADMIN", $userMessage, $headers);

        echo "<div class='alert alert-success'>
                <center>
                    <strong>Processing</strong> <br>
                    <div class='loader'></div>
                </center>
              </div>
              <meta http-equiv='refresh' content='5; url=inbox.php' />";
    } catch (PDOException $e) {
        echo "<div class='alert alert-danger'>Error: " . $e->getMessage() . "</div>";
    }
}
?>





<div class="page-wrapper">
            <!-- ============================================================== -->
            <!-- Bread crumb and right sidebar toggle -->
            <!-- ============================================================== -->
            <div class="page-breadcrumb">
                <div class="row">
                    <div class="col-12 d-flex no-block align-items-center">
                        <h4 class="page-title">Reply Message</h4>
                        <div class="ml-auto text-right">
                            <nav aria-label="breadcrumb">
                                <ol class="breadcrumb">
                                    <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                                    <li class="breadcrumb-item active" aria-current="page">Message Details</li>
                                </ol>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>

            <!-- ============================================================== -->
            <!-- End Bread crumb and right sidebar toggle -->
            <!-- ============================================================== -->
            <!-- ============================================================== -->
            <!-- Container fluid  -->
            <!-- ============================================================== -->
            <div class="container-fluid">
                <!-- ============================================================== -->
                <!-- Start Page Content -->
                <!-- ============================================================== -->
               
                <?php
                       if(isset($sucess)){
                            echo $sucess;
                        }
                        ?>
           <?php

// Ensure ID is present and is numeric
$id = isset($_GET['id']) && is_numeric($_GET['id']) ? (int) $_GET['id'] : 0;

if ($id > 0) {
    // Fetch the message to reply to
    $stmt = $pdo->prepare("SELECT * FROM message WHERE id = :id");
    $stmt->execute([':id' => $id]);
    $ordrow = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($ordrow) {
        // Fetch latest settings
        $settingsStmt = $pdo->query("SELECT * FROM settings ORDER BY id DESC LIMIT 1");
        $settings = $settingsStmt->fetch(PDO::FETCH_ASSOC);

        // Generate a ticket ID
        $ticket_id = rand(10000000000000, 100000000000000);
        $current_date = date("jS F Y g.iA");
?>
<div class='card'>
    <div class='card-body wizard-content'>
        <h4 class='card-title'>Reply Messages</h4>
        <form class='form-horizontal' method='post' action=''>
            <div class='card-body'>

                <input type='hidden' name='id' value='<?= htmlspecialchars($ordrow['log_id']) ?>'>

                <div class='form-group row'>
                    <label class='col-sm-3 text-right control-label col-form-label'>Name</label>
                    <div class='col-sm-9'>
                        <input type='text' name='name' class='form-control' value='<?= htmlspecialchars($ordrow['name']) ?>' readonly>
                    </div>
                </div>

                <div class='form-group row'>
                    <label class='col-sm-3 text-right control-label col-form-label'>Email</label>
                    <div class='col-sm-9'>
                        <input type='email' name='email' class='form-control' value='<?= htmlspecialchars($ordrow['email']) ?>' readonly>
                    </div>
                </div>

                <div class='form-group row'>
                    <label class='col-sm-3 text-right control-label col-form-label'>Mobile Number</label>
                    <div class='col-sm-9'>
                        <input type='text' name='phone' class='form-control' value='<?= htmlspecialchars($ordrow['phone']) ?>'>
                    </div>
                </div>

                <div class='form-group row'>
                    <label class='col-sm-3 text-right control-label col-form-label'>Title</label>
                    <div class='col-sm-9'>
                        <input type='text' name='title' class='form-control' value='RE:<?= htmlspecialchars($ordrow['title']) ?>'>
                    </div>
                </div>

                <div class='form-group row'>
                    <label class='col-sm-3 text-right control-label col-form-label'>Message</label>
                    <div class='col-sm-9'>
                        <textarea name='msg' id='editor1' class='form-control'><?= htmlspecialchars($ordrow['msg']) ?></textarea>
                    </div>
                </div>

                <input type='hidden' name='date' value='<?= $current_date ?>' readonly>
                <input type='hidden' name='cname' value='<?= htmlspecialchars($settings['domain_name']) ?>'>
                <input type='hidden' name='cemail' value='<?= htmlspecialchars($settings['email2']) ?>'>
                <input type='hidden' name='cphone' value='<?= htmlspecialchars($settings['phone']) ?>'>

                <div class='form-group row'>
                    <label class='col-sm-3 text-right control-label col-form-label'>Ticket ID</label>
                    <div class='col-sm-9'>
                        <input type='text' name='ticket_id' class='form-control' value='<?= $ticket_id ?>' readonly>
                    </div>
                </div>

                <div class='border-top'>
                    <div class='card-body'>
                        <button type='submit' name='action' class='btn btn-primary'>Submit</button>
                    </div>
                </div>

            </div>
        </form>
    </div>
</div>
<?php
    } else {
        echo "<div class='alert alert-danger'>Message not found.</div>";
    }
} else {
    echo "<div class='alert alert-danger'>Invalid message ID.</div>";
}
?>

             
             
            </div>
           
           
           <?php require ("footer.php")?>
           
           
           
           
               