<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
include 'config.php';


if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $customer_name = $_POST['customer_name'];
    $customer_number = $_POST['customer_number'];
    $device_brand = $_POST['device_brand'];
    $device_model = $_POST['device_model'];
    $problem = $_POST['problem'];
    $price = $_POST['price'];
    $status = $_POST['status'];

    if ($_FILES['phone_picture']['error'] == 0) {
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES["phone_picture"]["name"]);
        move_uploaded_file($_FILES["phone_picture"]["tmp_name"], $target_file);
        $phone_picture = $target_file;
    } else {
        $phone_picture = "";
    }

    $sql = "INSERT INTO job_sheets (customer_name, customer_number, device_brand, device_model, problem, price, status, phone_picture)
            VALUES ('$customer_name', '$customer_number', '$device_brand', '$device_model', '$problem', '$price', '$status', '$phone_picture')";

    if ($conn->query($sql) === TRUE) {
        $work_id = $conn->insert_id; // Get the last inserted ID
        echo "<!DOCTYPE html>
        <html lang='en'>
        <head>
            <meta charset='UTF-8'>
            <meta name='viewport' content='width=device-width, initial-scale=1.0'>
            <title>Receipt</title>
            <link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css' rel='stylesheet'>
            <style>
            body {
                font-family: Arial, sans-serif;
                margin: 0;
                padding: 0;
                background-color: #f4f4f4;
            }
            .receipt-container {
                width: 210mm; /* Full A5 width */
                height: 148mm; /* Full A5 height */
                margin: 10px auto; /* Adjusted margin for higher position */
                border: 2px solid black; /* Black border */
                border-radius: 10px;
                padding: 20px;
                background: #fff;
                box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
                display: flex;
                flex-direction: column;
                justify-content: space-between;
            }
            .receipt-header {
                text-align: center;
                margin-bottom: 10px;
                position: relative;
            }
            .receipt-header img {
                max-width: 100px;
                margin-bottom: 10px;
            }
            .receipt-header h2 {
                margin: 5px 0;
                font-size: 20px;
                color: #333;
            }
            .receipt-header p {
                margin: 0;
                font-size: 12px;
                color: #555;
            }
            .receipt-header .qr-code {
                position: absolute;
                top: 0;
                right: 20px;
                max-width: 100px;
            }
            .receipt-body {
                font-size: 12px;
                color: #333;
            }
            .receipt-body table {
                width: 100%;
                border-collapse: collapse;
            }
            .receipt-body table th, .receipt-body table td {
                padding: 8px;
                border: 1px solid black; /* Black border for table cells */
                text-align: left;
            }
            .receipt-body table th {
                background-color: #f8f9fa;
                font-weight: bold;
            }
            .receipt-footer {
                text-align: center;
                margin-top: 10px;
            }
            .receipt-footer button {
                padding: 8px 16px;
                font-size: 14px;
                border: none;
                background-color: #007bff;
                color: #fff;
                border-radius: 5px;
                cursor: pointer;
            }
            .receipt-footer button:hover {
                background-color: #0056b3;
            }
            </style>
        </head>
        <body>
            <div class='receipt-container'>
            <div class='receipt-header'>
                <img src='https://i.ibb.co/zH5SpC1v/Untitled-designddd.png' alt='Quick Phone Repair Logo'>
                <h2>QUICK PHONE REPAIR</h2>
                <p>Online Quick Repair WLL</p>
                <p>Number: +973-39174200</p>
                <p>Address: Manama, Near Goldcity Block 304, Road 445, Building 2584</p>
                <img src='https://i.ibb.co/nqTYHrMB/IMG-5445-F1-E3-BF37-1.jpg' alt='WhatsApp QR Code' class='qr-code'>
            </div>
            <div class='receipt-body'>
                <table class='table'>
                <tr><th>Work ID</th><td>$work_id</td></tr>
                <tr><th>Customer Name</th><td>$customer_name</td></tr>
                <tr><th>Customer Number</th><td>$customer_number</td></tr>
                <tr><th>Device Brand</th><td>$device_brand</td></tr>
                <tr><th>Device Model 📋</th><td><strong>$device_model</strong></td></tr>
                <tr><th>Problem Reported By The Customer 🛠️</th><td><strong>$problem</strong></td></tr>
                <tr><th>Estimate Price (BHD) 💰</th><td>$price</td></tr>
                <tr><th>Status</th><td>$status</td></tr>
                <tr><th>Phone Picture</th><td><img src='$phone_picture' alt='Phone Picture' class='img-thumbnail' style='max-width: 80px;'></td></tr>
                </table>
            </div>
            <div class='receipt-footer'>
                <button onclick='window.print()'>Print Receipt</button>
            </div>
            </div>
        </body>
        </html>";
        exit;
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create Job Sheet</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <h1 class="text-center mb-4">Create Job Sheet</h1>
        <form method="POST" action="create.php" enctype="multipart/form-data" class="row g-3">
            <div class="col-md-6">
                <label for="customer_name" class="form-label fw-bold">Customer Name 🧑‍💼</label>
                <input type="text" class="form-control" id="customer_name" name="customer_name" required>
            </div>
            <div class="col-md-6">
                <label for="customer_number" class="form-label fw-bold">Customer Number 📞</label>
                <input type="number" class="form-control" id="customer_number" name="customer_number" required>
            </div>
            <div class="col-md-6">
                <label for="device_brand" class="form-label fw-bold">Device Brand 📱</label>
                <select class="form-select" id="device_brand" name="device_brand" required>
                    <option value="Samsung">Samsung 📱</option>
                    <option value="Apple">Apple </option>
                    <option value="Huawei">Huawei 📲</option>
                    <option value="Xiaomi">Xiaomi 📱</option>
                    <option value="OnePlus">OnePlus 📱</option>
                    <option value="Google">Google 📱</option>
                    <option value="Sony">Sony 📱</option>
                    <option value="LG">LG 📱</option>
                    <option value="Nokia">Nokia 📱</option>
                    <option value="Motorola">Motorola 📱</option>
                </select>
            </div>
            <div class="col-md-6">
                <label for="device_model" class="form-label fw-bold">Device Model 📋</label>
                <input type="text" class="form-control" id="device_model" name="device_model" required>
            </div>
            <div class="col-md-6">
                <label for="problem" class="form-label">Problem</label>
                <select class="form-select" id="problem" name="problem" required>
                <option value="No Power">No Power ⚡</option>
                <option value="LCD Replace">LCD Replace 🖥️</option>
                    <option value="Battery Problem">Battery Problem 🔋</option>
                    <option value="Charging Issue">Charging Issue 🔌</option>
                    <option value="Water Damage">Water Damage 💧</option>
                    <option value="Software Issue">Software Issue 🖥️</option>
                    <option value="Camera Issue">Camera Issue 📷</option>
                    <option value="Speaker Problem">Speaker Problem 🔊</option>
                    <option value="Microphone Issue">Microphone Issue 🎤</option>
                    <option value="Network Issue">Network Issue 📡</option>
                    <option value="Overheating">Overheating 🔥</option>

                </select>
            </div>
            <div class="col-md-6">
                <label for="price" class="form-label fw-bold">Estimate Price (BHD) 💰</label>
                <input type="number" class="form-control" id="price" name="price" step="0.01" min="0" placeholder="Optional" pattern="^\d+(\.\d{1,2})?$" value="0">
            </div>
            <div class="col-md-6">
                <label for="status" class="form-label">Status</label>
                <select class="form-select" id="status" name="status" required>
                    <option value="Pending">Pending</option>
                    <option value="Completed">Completed</option>
                    <option value="Cancelled">Cancelled</option>
                </select>
            </div>
            <div class="col-md-6">
    <label for="phone_picture" class="form-label fw-bold">Phone Picture (Upload or Capture) 📷</label>
    <input type="file" class="form-control" id="phone_picture" name="phone_picture" accept="image/*;capture=camera">
</div>

            <div class="col-12 text-center">
                <button type="submit" class="btn btn-success">Submit</button>
            </div>
        </form>
    </div>
</body>
</html>
<?php
$conn->close();
?>
