<?php
session_start();

// Check if user is authenticated
if (!isset($_SESSION['po_authenticated']) || $_SESSION['po_authenticated'] !== true) {
    header('Location: login.php');
    exit;
}

// Session timeout after 2 hours
if (isset($_SESSION['po_login_time']) && (time() - $_SESSION['po_login_time'] > 7200)) {
    session_destroy();
    header('Location: login.php?timeout=1');
    exit;
}

// Refresh session time
$_SESSION['po_login_time'] = time();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Purchase Order - McGRATH™ MAC Video Laryngoscope</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <header>
            <div class="logo">◆ ADAMANT™ Anesthesia</div>
            <h1>CONDITIONAL PURCHASE ORDER</h1>
            <p class="subtitle">Date: December 30, 2025</p>
        </header>

        <div class="agreement-content">
            <div class="party-section">
                <div class="party-box">
                    <h3>BUYER</h3>
                    <p><strong>Adam Powell, CRNA</strong></p>
                    <p>Adam Powell, LLC</p>
                    <p>2004 E Village Dr</p>
                    <p>Ellensburg, WA 98926</p>
                    <p><a href="mailto:adam@adamantanesthesia.com">adam@adamantanesthesia.com</a></p>
                </div>

                <div class="party-box">
                    <h3>SELLER</h3>
                    <p id="seller-display">To be completed below</p>
                </div>
            </div>

            <div class="section">
                <h2>ITEM DESCRIPTION</h2>
                <table class="item-table">
                    <thead>
                        <tr>
                            <th>Description</th>
                            <th>Qty</th>
                            <th>Price</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>
                                <strong>McGRATH™ MAC Video Laryngoscope with Extra Blades</strong><br>
                                <em>(Pending functional testing - see conditions below)</em>
                            </td>
                            <td>1</td>
                            <td>$600.00</td>
                        </tr>
                        <tr class="total-row">
                            <td colspan="2"><strong>TOTAL:</strong></td>
                            <td><strong>$600.00</strong></td>
                        </tr>
                    </tbody>
                </table>
            </div>

            <div class="section conditions-section">
                <h2>CONDITIONS OF PURCHASE</h2>
                <div class="warning-box">
                    <p><strong>This purchase order is CONDITIONAL upon successful functional testing of the device.</strong></p>
                </div>

                <p><strong>Note:</strong> The screen was unable to be tested at time of inspection due to a dead/missing battery. Full functionality of the display and device must be verified before this purchase is finalized.</p>

                <p><strong>Payment Terms:</strong> Upon successful testing confirming full functionality of the McGRATH™ MAC Video Laryngoscope, payment of $600.00 will be sent directly to the selling party.</p>
                <p><strong>Shipping:</strong> The purchase price includes shipping costs. Seller must provide tracking number upon shipment.</p>

                <p>If testing reveals the device is non-functional or defective, this purchase order is void and the item shall be returned to the seller at no cost to the buyer.</p>
            </div>
        </div>

        <div class="signature-section">
            <h2>SIGNATURES</h2>

            <form id="po-form" method="POST" action="submit.php">
                <div class="form-group">
                    <label for="seller_name"><strong>Seller Full Legal Name:</strong></label>
                    <input type="text" id="seller_name" name="seller_name" required placeholder="Enter your full legal name" value="Zachary Anthony">
                    <p class="help-text">This name will be used as the Seller in this purchase order</p>
                </div>

                <div class="form-group">
                    <label for="seller_company"><strong>Company/Business Name:</strong></label>
                    <input type="text" id="seller_company" name="seller_company" required placeholder="Enter company name" value="Bernards Junk Removal">
                </div>

                <div class="form-group">
                    <label for="seller_address"><strong>Address:</strong></label>
                    <input type="text" id="seller_address" name="seller_address" required placeholder="Street address" value="1104 Planz Rd">
                </div>

                <div class="form-row">
                    <div class="form-group">
                        <label for="seller_city"><strong>City:</strong></label>
                        <input type="text" id="seller_city" name="seller_city" required placeholder="City" value="Bakersfield">
                    </div>
                    <div class="form-group">
                        <label for="seller_state"><strong>State:</strong></label>
                        <input type="text" id="seller_state" name="seller_state" required placeholder="State" value="CA">
                    </div>
                    <div class="form-group">
                        <label for="seller_zip"><strong>ZIP:</strong></label>
                        <input type="text" id="seller_zip" name="seller_zip" required placeholder="ZIP" value="93304">
                    </div>
                </div>

                <div class="form-group">
                    <label for="seller_email"><strong>Email Address:</strong></label>
                    <input type="email" id="seller_email" name="seller_email" required placeholder="your@email.com" value="bernardsjunkremoval2025@gmail.com">
                </div>

                <div class="form-group">
                    <label for="seller_phone"><strong>Phone Number:</strong></label>
                    <input type="tel" id="seller_phone" name="seller_phone" required placeholder="Enter your phone number" value="661-610-9706">
                </div>

                <div class="signature-type-selector">
                    <h3>Choose Signature Method:</h3>
                    <div class="radio-group">
                        <label>
                            <input type="radio" name="signature_type" value="drawn" checked onchange="toggleSignatureMethod()">
                            Draw Signature (Handwritten)
                        </label>
                        <label>
                            <input type="radio" name="signature_type" value="typed" onchange="toggleSignatureMethod()">
                            Type Signature
                        </label>
                    </div>
                </div>

                <div id="drawn-signature-container" class="signature-container">
                    <h3>Seller Signature:</h3>
                    <p class="help-text">Please sign below using your mouse, trackpad, or touchscreen</p>
                    <div class="signature-pad-wrapper">
                        <canvas id="signature-pad" width="600" height="200"></canvas>
                    </div>
                    <button type="button" onclick="clearSignature()" class="btn-secondary">Clear Signature</button>
                    <input type="hidden" id="signature_data" name="signature_data">
                </div>

                <div id="typed-signature-container" class="signature-container" style="display: none;">
                    <h3>Type Your Signature:</h3>
                    <input type="text" id="typed_signature" name="typed_signature" placeholder="Type your full legal name" class="signature-input">
                    <p class="help-text">By typing your name, you agree that this constitutes a legal signature</p>
                </div>

                <div class="form-group checkbox-group">
                    <label>
                        <input type="checkbox" id="agreement_consent" name="agreement_consent" required>
                        <strong>I have read, understood, and agree to be bound by all terms and conditions of this Conditional Purchase Order</strong>
                    </label>
                </div>

                <div class="form-group checkbox-group">
                    <label>
                        <input type="checkbox" id="legal_authority" name="legal_authority" required>
                        <strong>I certify that I have the legal authority to enter into this Purchase Order</strong>
                    </label>
                </div>

                <button type="submit" class="btn-primary" id="submit-btn">Sign Purchase Order</button>
            </form>
        </div>

        <footer>
            <p class="footer-text">This document will be legally binding upon submission. A copy will be sent to your email address.</p>
            <p class="footer-text">Questions? Contact: <a href="mailto:Adam@adamantanesthesia.com">Adam@adamantanesthesia.com</a> | <a href="logout.php">Logout</a></p>
        </footer>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/signature_pad@4.1.7/dist/signature_pad.umd.min.js"></script>
    <script src="script.js"></script>
</body>
</html>
