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

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    // Clean and validate inputs
    $cphone = $_POST['cphone'] ?? '';
    $cemail = $_POST['cemail'] ?? '';
    $cname = $_POST['cname'] ?? '';

    $id = $_POST['id'] ?? '';
    $name = $_POST['name'] ?? '';
    $email = $_POST['email'] ?? '';
    $title = $_POST['title'] ?? '';
    $phone = $_POST['phone'] ?? '';
    $acc_no = $_POST['acc_no'] ?? '';
    $curr = $_POST['curr'] ?? '';
    $amount = $_POST['amount'] ?? '';
    $msg = $_POST['msg'] ?? '';
    $ticket_id = $_POST['ticket_id'] ?? '';
    $date = $_POST['date'] ?? '';

    // Check if this is a new message (you need to define when $enumrows = 0)
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM message WHERE ticket_id = ?");
    $stmt->execute([$ticket_id]);
    $count = $stmt->fetchColumn();

    if ($count < 1) {
        // Insert new message
        $sql = "INSERT INTO message 
            (log_id, title, name, date, msg, curr, amount, email, acc_no, phone, ticket_id, status) 
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)";
        $stmt = $pdo->prepare($sql);
        $stmt->execute([$id, $title, $name, $date, $msg, $curr, $amount, $email, $acc_no, $phone, $ticket_id]);

        $success = true;

        // ===== SEND EMAIL TO ADMIN =====
        if ($success) {
            $admin_subject = "MESSAGE DELIVERED TO ($name)";
            $admin_body = "
                <p>Your message has been delivered to the contact below:</p>
                NAME: $name<br>
                PHONE: $phone<br>
                EMAIL: $email<br>
                MESSAGE: $msg<br>";

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

            @mail($cemail, $admin_subject, $admin_body, $headers);
        }

        // ===== SEND EMAIL TO USER =====
        if ($success) {
            $user_subject = "MESSAGE FROM ADMIN";
            $user_body = "
                <p>You have a new message from $cname.</p>
                <img src='{$url}/img/LOGO.png' width='100%' height='100%' alt='logo'><br>
                TITLE: $title<br>
                PHONE: $cphone<br>
                EMAIL: $cemail<br>
                MESSAGE: $msg<br>";

            $user_headers = "From: $cemail\r\n";
            $user_headers .= "MIME-Version: 1.0\r\n";
            $user_headers .= "Content-Type: text/html; charset=UTF-8\r\n";

            @mail($email, $user_subject, $user_body, $user_headers);
        }

        // Output success alert
        echo "
        <div class='alert alert-success'>
            <strong>Processing</strong><br>
            <div class='loader'></div>
            <meta http-equiv='refresh' content='8; url=' />
        </div>";
    }
}
?>


	  
	  
	  
	  
	  
	  
    
 
 <!---begin of Mobile View Here   only from Digital Forest Team-->
 
     
   
   
  				
  <script>
    $(document).ready(function() {
      $('#mybutton').hide().delay(5000).fadeIn(2200);
});
    </script> 

 <style>
input[type=text2] {
  width: 60%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 16px;
  resize: vertical;
} 

input[type=text], select, textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 16px;
  resize: vertical;
}

input[type=number], date{
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 16px;
  resize: vertical;
}


input[type=submit] {
  padding: 12px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

input[type=submit]:hover {
  background-color: #45a049;
}

.container {
  border-radius: 5px;
  background-color: ;
  padding: 20px;
}
</style>
						
				
				
				
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.6/jspdf.plugin.autotable.min.js"></script> 
  
				
                                    
           
  
 
 
 <br><br>
 
 
 <div class="container-fluid py-4">
 
  <?php
                       if(isset($sucess)){
                            echo $sucess;
                        }
                        ?>
           <?php

// Ensure $id is defined and is numeric
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die('Invalid ID');
}
$id = (int) $_GET['id'];

// Fetch message
$stmt = $pdo->prepare("SELECT * FROM message WHERE id = ?");
$stmt->execute([$id]);
$ordrow = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$ordrow) {
    die("Message not found.");
}

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

// Generate ticket ID
$ticket_id = rand(10000000000000, 100000000000000);
?>

<div class='card'>
    <div class='card-body wizard-content'>
        <h4 class='card-title'>Reply Message</h4>

        <form class='form-horizontal' action='' method='post'>
            <div class='card-body'>

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

                <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' class='form-control' name='name' 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' class='form-control' name='email' 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' class='form-control' name='phone' 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' class='form-control' name='title' 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="<?= date("jS F Y g.iA") ?>" readonly>

                <?php if ($settings): ?>
                    <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']) ?>">
                <?php endif; ?>

                <div class="col-md-12">
                    <div class="form-group app-label">
                        <label class="control-label">Ticket ID</label>
                        <input name="ticket_id" type="number" class="form-control" value="<?= $ticket_id ?>" readonly>
                    </div>
                </div>

            </div>

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

             
 
 
 </div>
 
 
 
  
  
 
	
	 <script type="text/javascript"> 
 
 
function generate() {  
    var doc = new jsPDF('p', 'pt', 'letter');  
    var htmlstring = '';  
    var tempVarToCheckPageHeight = 0;  
    var pageHeight = 0;  
    pageHeight = doc.internal.pageSize.height;  
    specialElementHandlers = {  
        // element with id of "bypass" - jQuery style selector  
        '#bypassme': function(element, renderer) {  
            // true = "handled elsewhere, bypass text extraction"  
            return true  
        }  
    };  
    margins = {  
        top: 150,  
        bottom: 60,  
        left: 40,  
        right: 40,  
        width: 600  
    };  
    var y = 20;  
    doc.setLineWidth(2);  
   
    
     doc.text(200, y = y + 30,"MY BANK MESSAGES"); 
 
    doc.autoTable({  
        html: '#dataexample',  
        startY: 70,  
        theme: 'striped',  
        columnStyles: {  
            0: {  
                cellWidth: 30,  
            },  
            1: {  
                cellWidth: 120,  
            },  
            2: {  
                cellWidth: 70,  
            },
            3: {  
                cellWidth: 80,  
            },
            4: {  
                cellWidth: 80,  
            },
            5: {  
                cellWidth: 80,  
            },
            6: {  
                cellWidth: 80,  
            }
       
        },  
        styles: {  
            minCellHeight: 20  
        }  
    })  
    doc.save('bank_messages.pdf');  
}  
</script> 
 























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