<?php
session_start();
include 'layouts/dbconn.php';

// Set timezone to East Africa Time
date_default_timezone_set('Africa/Nairobi');

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

// Fetch user details
$user_id = $_SESSION['user_id'];
$sql_user_details = "SELECT u.*, ur.role_name FROM users u 
                     INNER JOIN user_roles ur ON u.role_id = ur.role_id 
                     WHERE u.user_id = ?";
$stmt_user = $conn->prepare($sql_user_details);
$stmt_user->bind_param("i", $user_id);
$stmt_user->execute();
$user_details = $stmt_user->get_result()->fetch_assoc();

if ($user_details) {
    $username = $user_details['username'];
    $role_name = $user_details['role_name'];
    $full_name = $user_details['full_name'];
    $email = $user_details['email'];
}

$success_message = '';
$error_message = '';

// Handle form submission
if ($_POST) {
    try {
        $category_id = $_POST['category_id'];
        $expense_title = trim($_POST['expense_title']);
        $description = trim($_POST['description']);
        $amount = floatval($_POST['amount']);
        $expense_date = $_POST['expense_date'];
        $payment_method = $_POST['payment_method'];
        $payment_reference = trim($_POST['payment_reference']);
        $vendor_name = trim($_POST['vendor_name']);
        $vendor_contact = trim($_POST['vendor_contact']);
        $receipt_number = trim($_POST['receipt_number']);
        $location = trim($_POST['location']);
        $fund_id = !empty($_POST['fund_id']) ? $_POST['fund_id'] : NULL;
        $status = $_POST['status'];
        $notes = trim($_POST['notes']);
        $tags = trim($_POST['tags']);
        
        // Handle recurring expense settings
        $is_recurring = isset($_POST['is_recurring']) ? 1 : 0;
        $recurring_frequency = $is_recurring ? $_POST['recurring_frequency'] : NULL;
        $next_due_date = $is_recurring ? $_POST['next_due_date'] : NULL;
        
        // Check if category requires approval
        $category_query = "SELECT requires_approval FROM expense_categories WHERE category_id = ?";
        $category_stmt = $conn->prepare($category_query);
        $category_stmt->bind_param("i", $category_id);
        $category_stmt->execute();
        $category_result = $category_stmt->get_result()->fetch_assoc();
        
        $requires_approval = $category_result['requires_approval'];
        
        // Set status based on approval requirement and user role
        if ($requires_approval && $role_name !== 'Administrator') {
            $status = 'PENDING';
            $approved_by = NULL;
            $approved_at = NULL;
        } else if ($status === 'APPROVED' && $role_name === 'Administrator') {
            $approved_by = $user_id;
            $approved_at = date('Y-m-d H:i:s');
        } else {
            $approved_by = NULL;
            $approved_at = NULL;
        }
        
        // Insert expense
        $insert_query = "INSERT INTO expenses (
            category_id, expense_title, description, amount, expense_date,
            payment_method, payment_reference, vendor_name, vendor_contact,
            receipt_number, location, fund_id, status, requires_approval,
            approved_by, approved_at, created_by, notes, is_recurring,
            recurring_frequency, next_due_date, tags
        ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        
        $insert_stmt = $conn->prepare($insert_query);
        $insert_stmt->bind_param(
            "issdssssssisissisissss",
            $category_id, $expense_title, $description, $amount, $expense_date,
            $payment_method, $payment_reference, $vendor_name, $vendor_contact,
            $receipt_number, $location, $fund_id, $status, $requires_approval,
            $approved_by, $approved_at, $user_id, $notes, $is_recurring,
            $recurring_frequency, $next_due_date, $tags
        );
        
        if ($insert_stmt->execute()) {
            $expense_id = $conn->insert_id;
            
            // If recurring, create template
            if ($is_recurring) {
                $template_query = "INSERT INTO recurring_expense_templates (
                    template_name, category_id, expense_title, description, amount,
                    payment_method, vendor_name, recurring_frequency, next_due_date,
                    created_by, notes
                ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
                
                $template_name = "Recurring: " . $expense_title;
                $template_stmt = $conn->prepare($template_query);
                $template_stmt->bind_param(
                    "sissdssssss",
                    $template_name, $category_id, $expense_title, $description,
                    $amount, $payment_method, $vendor_name, $recurring_frequency,
                    $next_due_date, $user_id, $notes
                );
                $template_stmt->execute();
            }
            
            $success_message = "Expense has been successfully created!";
            
            // Redirect after successful creation
            header("Location: view_expense.php?id=" . $expense_id . "&success=1");
            exit();
        } else {
            $error_message = "Failed to create expense. Please try again.";
        }
    } catch (Exception $e) {
        $error_message = "Error: " . $e->getMessage();
    }
}

// Get expense categories
$categories_query = "SELECT * FROM expense_categories WHERE is_active = 1 ORDER BY category_type, category_name";
$categories_result = $conn->query($categories_query);

// Get funds for dropdown
$funds_query = "SELECT fund_id, fund_name, current_balance FROM funds WHERE is_active = 1 AND status = 'ACTIVE' ORDER BY fund_name";
$funds_result = $conn->query($funds_query);
?>

<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Add Expense - Personal Fund Management System')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <style>
        body {
            background-color: #f8fafc;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        }
        
        .page-content {
            padding: 1.5rem;
        }
        
        /* Page Header */
        .page-header {
            background: linear-gradient(135deg, #8e44ad 0%, #9b59b6 100%);
            color: white;
            border-radius: 12px;
            padding: 2rem;
            margin-bottom: 2rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }
        
        .page-title {
            font-size: 1.75rem;
            font-weight: 600;
            margin-bottom: 0.5rem;
        }
        
        .page-subtitle {
            opacity: 0.9;
            margin: 0;
        }
        
        /* Form Card */
        .form-card {
            background: white;
            border-radius: 12px;
            padding: 2rem;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
            border: 1px solid #e2e8f0;
            margin-bottom: 2rem;
        }
        
        .form-section {
            margin-bottom: 2rem;
        }
        
        .form-section-title {
            font-size: 1.125rem;
            font-weight: 600;
            color: #1e293b;
            margin-bottom: 1rem;
            padding-bottom: 0.5rem;
            border-bottom: 2px solid #e2e8f0;
        }
        
        /* Form Controls */
        .form-label {
            font-weight: 600;
            color: #374151;
            margin-bottom: 0.5rem;
        }
        
        .form-control, .form-select, .form-control-file {
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            padding: 0.75rem;
            font-size: 0.875rem;
            transition: all 0.2s ease;
        }
        
        .form-control:focus, .form-select:focus {
            border-color: #8b5cf6;
            box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.1);
        }
        
        .required-field {
            color: #dc2626;
        }
        
        /* Buttons */
        .btn {
            padding: 0.75rem 1.5rem;
            border-radius: 8px;
            font-weight: 600;
            font-size: 0.875rem;
            border: none;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        
        .btn-primary {
            background: #8b5cf6;
            color: white;
        }
        
        .btn-primary:hover {
            background: #7c3aed;
        }
        
        .btn-secondary {
            background: #6b7280;
            color: white;
        }
        
        .btn-secondary:hover {
            background: #4b5563;
        }
        
        .btn-outline-secondary {
            background: transparent;
            border: 1px solid #e2e8f0;
            color: #64748b;
        }
        
        .btn-outline-secondary:hover {
            background: #f8fafc;
            border-color: #64748b;
        }
        
        /* Alert Messages */
        .alert {
            border-radius: 8px;
            padding: 1rem;
            margin-bottom: 1.5rem;
            border: none;
        }
        
        .alert-success {
            background: #dcfce7;
            color: #166534;
        }
        
        .alert-danger {
            background: #fee2e2;
            color: #991b1b;
        }
        
        /* Recurring Options */
        .recurring-options {
            background: #f8fafc;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            padding: 1rem;
            margin-top: 1rem;
            display: none;
        }
        
        .recurring-options.show {
            display: block;
        }
        
        /* Category Type Indicators */
        .category-type-indicator {
            display: inline-block;
            padding: 0.25rem 0.5rem;
            border-radius: 4px;
            font-size: 0.75rem;
            font-weight: 500;
            margin-left: 0.5rem;
        }
        
        .type-shop { background: #fee2e2; color: #991b1b; }
        .type-company { background: #dbeafe; color: #1e40af; }
        .type-family { background: #dcfce7; color: #166534; }
        
        /* Input Groups */
        .input-group {
            margin-bottom: 1rem;
        }
        
        .input-group-text {
            background: #f8fafc;
            border: 1px solid #e2e8f0;
            color: #64748b;
            font-weight: 500;
        }
        
        /* Responsive */
        @media (max-width: 768px) {
            .page-content {
                padding: 1rem;
            }
            
            .form-card {
                padding: 1.5rem;
            }
        }
    </style>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <!-- Page Header -->
                    <div class="page-header">
                        <h1 class="page-title">Add New Expense</h1>
                        <p class="page-subtitle">Create a new expense entry for your records</p>
                    </div>

                    <!-- Success/Error Messages -->
                    <?php if ($success_message): ?>
                        <div class="alert alert-success">
                            <i class="ph-check-circle me-2"></i>
                            <?php echo htmlspecialchars($success_message); ?>
                        </div>
                    <?php endif; ?>

                    <?php if ($error_message): ?>
                        <div class="alert alert-danger">
                            <i class="ph-warning-circle me-2"></i>
                            <?php echo htmlspecialchars($error_message); ?>
                        </div>
                    <?php endif; ?>

                    <!-- Form Card -->
                    <div class="form-card">
                        <form method="POST" enctype="multipart/form-data">
                            <!-- Basic Information -->
                            <div class="form-section">
                                <h3 class="form-section-title">Basic Information</h3>
                                <div class="row">
                                    <div class="col-md-6">
                                        <label class="form-label">Expense Title <span class="required-field">*</span></label>
                                        <input type="text" class="form-control" name="expense_title" required 
                                               placeholder="Enter expense title" value="<?php echo isset($_POST['expense_title']) ? htmlspecialchars($_POST['expense_title']) : ''; ?>">
                                    </div>
                                    <div class="col-md-6">
                                        <label class="form-label">Category <span class="required-field">*</span></label>
                                        <select class="form-select" name="category_id" required id="categorySelect">
                                            <option value="">Select Category</option>
                                            <?php 
                                            $current_type = '';
                                            while ($category = $categories_result->fetch_assoc()): 
                                                if ($current_type != $category['category_type']) {
                                                    if ($current_type != '') echo '</optgroup>';
                                                    echo '<optgroup label="' . ucfirst(str_replace('_', ' ', $category['category_type'])) . ' Expenses">';
                                                    $current_type = $category['category_type'];
                                                }
                                            ?>
                                                <option value="<?php echo $category['category_id']; ?>" 
                                                        data-type="<?php echo $category['category_type']; ?>"
                                                        data-requires-approval="<?php echo $category['requires_approval']; ?>"
                                                        <?php echo (isset($_POST['category_id']) && $_POST['category_id'] == $category['category_id']) ? 'selected' : ''; ?>>
                                                    <?php echo htmlspecialchars($category['category_name']); ?>
                                                    <?php if ($category['requires_approval']): ?>
                                                        (Requires Approval)
                                                    <?php endif; ?>
                                                </option>
                                            <?php 
                                            endwhile; 
                                            if ($current_type != '') echo '</optgroup>';
                                            ?>
                                        </select>
                                        <small class="text-muted" id="categoryInfo"></small>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                        <label class="form-label">Amount (KES) <span class="required-field">*</span></label>
                                        <div class="input-group">
                                            <span class="input-group-text">KES</span>
                                            <input type="number" class="form-control" name="amount" step="0.01" min="0" required 
                                                   placeholder="0.00" value="<?php echo isset($_POST['amount']) ? htmlspecialchars($_POST['amount']) : ''; ?>">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <label class="form-label">Expense Date <span class="required-field">*</span></label>
                                        <input type="date" class="form-control" name="expense_date" required 
                                               value="<?php echo isset($_POST['expense_date']) ? $_POST['expense_date'] : date('Y-m-d'); ?>">
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-12">
                                        <label class="form-label">Description</label>
                                        <textarea class="form-control" name="description" rows="3" 
                                                  placeholder="Enter expense description"><?php echo isset($_POST['description']) ? htmlspecialchars($_POST['description']) : ''; ?></textarea>
                                    </div>
                                </div>
                            </div>

                            <!-- Payment Information -->
                            <div class="form-section">
                                <h3 class="form-section-title">Payment Information</h3>
                                <div class="row">
                                    <div class="col-md-6">
                                        <label class="form-label">Payment Method <span class="required-field">*</span></label>
                                        <select class="form-select" name="payment_method" required>
                                            <option value="">Select Payment Method</option>
                                            <option value="CASH" <?php echo (isset($_POST['payment_method']) && $_POST['payment_method'] == 'CASH') ? 'selected' : ''; ?>>Cash</option>
                                            <option value="BANK_TRANSFER" <?php echo (isset($_POST['payment_method']) && $_POST['payment_method'] == 'BANK_TRANSFER') ? 'selected' : ''; ?>>Bank Transfer</option>
                                            <option value="MPESA" <?php echo (isset($_POST['payment_method']) && $_POST['payment_method'] == 'MPESA') ? 'selected' : ''; ?>>M-Pesa</option>
                                            <option value="CARD" <?php echo (isset($_POST['payment_method']) && $_POST['payment_method'] == 'CARD') ? 'selected' : ''; ?>>Card</option>
                                            <option value="CHEQUE" <?php echo (isset($_POST['payment_method']) && $_POST['payment_method'] == 'CHEQUE') ? 'selected' : ''; ?>>Cheque</option>
                                            <option value="MOBILE_MONEY" <?php echo (isset($_POST['payment_method']) && $_POST['payment_method'] == 'MOBILE_MONEY') ? 'selected' : ''; ?>>Mobile Money</option>
                                        </select>
                                    </div>
                                    <div class="col-md-6">
                                        <label class="form-label">Payment Reference</label>
                                        <input type="text" class="form-control" name="payment_reference" 
                                               placeholder="Transaction ID, Cheque number, etc." 
                                               value="<?php echo isset($_POST['payment_reference']) ? htmlspecialchars($_POST['payment_reference']) : ''; ?>">
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                        <label class="form-label">Receipt Number</label>
                                        <input type="text" class="form-control" name="receipt_number" 
                                               placeholder="Receipt or invoice number" 
                                               value="<?php echo isset($_POST['receipt_number']) ? htmlspecialchars($_POST['receipt_number']) : ''; ?>">
                                    </div>
                                    <div class="col-md-6">
                                        <label class="form-label">Fund Source (Optional)</label>
                                        <select class="form-select" name="fund_id">
                                            <option value="">Select Fund (Optional)</option>
                                            <?php while ($fund = $funds_result->fetch_assoc()): ?>
                                                <option value="<?php echo $fund['fund_id']; ?>" 
                                                        <?php echo (isset($_POST['fund_id']) && $_POST['fund_id'] == $fund['fund_id']) ? 'selected' : ''; ?>>
                                                    <?php echo htmlspecialchars($fund['fund_name']); ?> 
                                                    (Balance: KES <?php echo number_format($fund['current_balance']); ?>)
                                                </option>
                                            <?php endwhile; ?>
                                        </select>
                                        <small class="text-muted">Link this expense to a specific fund</small>
                                    </div>
                                </div>
                            </div>

                            <!-- Vendor Information -->
                            <div class="form-section">
                                <h3 class="form-section-title">Vendor Information</h3>
                                <div class="row">
                                    <div class="col-md-6">
                                        <label class="form-label">Vendor Name</label>
                                        <input type="text" class="form-control" name="vendor_name" 
                                               placeholder="Enter vendor or supplier name" 
                                               value="<?php echo isset($_POST['vendor_name']) ? htmlspecialchars($_POST['vendor_name']) : ''; ?>">
                                    </div>
                                    <div class="col-md-6">
                                        <label class="form-label">Vendor Contact</label>
                                        <input type="text" class="form-control" name="vendor_contact" 
                                               placeholder="Phone number or email" 
                                               value="<?php echo isset($_POST['vendor_contact']) ? htmlspecialchars($_POST['vendor_contact']) : ''; ?>">
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                        <label class="form-label">Location</label>
                                        <input type="text" class="form-control" name="location" 
                                               placeholder="Where the expense was incurred" 
                                               value="<?php echo isset($_POST['location']) ? htmlspecialchars($_POST['location']) : ''; ?>">
                                    </div>
                                    <div class="col-md-6">
                                        <label class="form-label">Tags</label>
                                        <input type="text" class="form-control" name="tags" 
                                               placeholder="Comma-separated tags (e.g., urgent, office, monthly)" 
                                               value="<?php echo isset($_POST['tags']) ? htmlspecialchars($_POST['tags']) : ''; ?>">
                                        <small class="text-muted">Use tags to organize and search expenses</small>
                                    </div>
                                </div>
                            </div>

                            <!-- Status and Recurring Options -->
                            <div class="form-section">
                                <h3 class="form-section-title">Status and Options</h3>
                                <div class="row">
                                    <div class="col-md-6">
                                        <label class="form-label">Status</label>
                                        <select class="form-select" name="status" id="statusSelect">
                                            <option value="DRAFT" <?php echo (isset($_POST['status']) && $_POST['status'] == 'DRAFT') ? 'selected' : ''; ?>>Draft</option>
                                            <option value="PAID" <?php echo (isset($_POST['status']) && $_POST['status'] == 'PAID') ? 'selected' : 'selected'; ?>>Paid</option>
                                            <?php if ($role_name === 'Administrator'): ?>
                                                <option value="APPROVED" <?php echo (isset($_POST['status']) && $_POST['status'] == 'APPROVED') ? 'selected' : ''; ?>>Approved</option>
                                            <?php endif; ?>
                                        </select>
                                        <small class="text-muted" id="statusInfo">Select the current status of this expense</small>
                                    </div>
                                    <div class="col-md-6">
                                        <label class="form-label">&nbsp;</label>
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" name="is_recurring" id="isRecurring" 
                                                   <?php echo (isset($_POST['is_recurring'])) ? 'checked' : ''; ?>>
                                            <label class="form-check-label" for="isRecurring">
                                                This is a recurring expense
                                            </label>
                                        </div>
                                    </div>
                                </div>

                                <!-- Recurring Options -->
                                <div class="recurring-options" id="recurringOptions">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <label class="form-label">Frequency</label>
                                            <select class="form-select" name="recurring_frequency">
                                                <option value="WEEKLY" <?php echo (isset($_POST['recurring_frequency']) && $_POST['recurring_frequency'] == 'WEEKLY') ? 'selected' : ''; ?>>Weekly</option>
                                                <option value="MONTHLY" <?php echo (isset($_POST['recurring_frequency']) && $_POST['recurring_frequency'] == 'MONTHLY') ? 'selected' : 'selected'; ?>>Monthly</option>
                                                <option value="QUARTERLY" <?php echo (isset($_POST['recurring_frequency']) && $_POST['recurring_frequency'] == 'QUARTERLY') ? 'selected' : ''; ?>>Quarterly</option>
                                                <option value="YEARLY" <?php echo (isset($_POST['recurring_frequency']) && $_POST['recurring_frequency'] == 'YEARLY') ? 'selected' : ''; ?>>Yearly</option>
                                            </select>
                                        </div>
                                        <div class="col-md-6">
                                            <label class="form-label">Next Due Date</label>
                                            <input type="date" class="form-control" name="next_due_date" 
                                                   value="<?php echo isset($_POST['next_due_date']) ? $_POST['next_due_date'] : date('Y-m-d', strtotime('+1 month')); ?>">
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- Additional Notes -->
                            <div class="form-section">
                                <h3 class="form-section-title">Additional Notes</h3>
                                <div class="row">
                                    <div class="col-md-12">
                                        <label class="form-label">Notes</label>
                                        <textarea class="form-control" name="notes" rows="4" 
                                                  placeholder="Any additional notes or comments about this expense"><?php echo isset($_POST['notes']) ? htmlspecialchars($_POST['notes']) : ''; ?></textarea>
                                    </div>
                                </div>
                            </div>

                            <!-- Action Buttons -->
                            <div class="form-section">
                                <div class="d-flex gap-3">
                                    <button type="submit" class="btn btn-primary">
                                        <i class="ph-check me-2"></i>Create Expense
                                    </button>
                                    <button type="button" class="btn btn-secondary" onclick="saveDraft()">
                                        <i class="ph-file-dashed me-2"></i>Save as Draft
                                    </button>
                                    <a href="list_expenses.php" class="btn btn-outline-secondary">
                                        <i class="ph-x me-2"></i>Cancel
                                    </a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <script>
        $(document).ready(function() {
            // Handle recurring checkbox
            $('#isRecurring').change(function() {
                if ($(this).is(':checked')) {
                    $('#recurringOptions').addClass('show');
                } else {
                    $('#recurringOptions').removeClass('show');
                }
            });

            // Show recurring options if checkbox was checked on page load
            if ($('#isRecurring').is(':checked')) {
                $('#recurringOptions').addClass('show');
            }

            // Handle category selection
            $('#categorySelect').change(function() {
                var selectedOption = $(this).find('option:selected');
                var requiresApproval = selectedOption.data('requires-approval');
                var type = selectedOption.data('type');
                
                if (selectedOption.val()) {
                    var infoText = 'Category Type: ' + type.replace('_', ' ');
                    if (requiresApproval) {
                        infoText += ' (Requires approval before payment)';
                        // If requires approval and user is not admin, set status to pending
                        <?php if ($role_name !== 'Administrator'): ?>
                        $('#statusSelect').val('PENDING');
                        $('#statusInfo').text('This category requires approval. Status set to Pending.');
                        <?php endif; ?>
                    }
                    $('#categoryInfo').text(infoText);
                } else {
                    $('#categoryInfo').text('');
                }
            });

            // Trigger category change on page load if category is selected
            if ($('#categorySelect').val()) {
                $('#categorySelect').trigger('change');
            }

            // Form validation
            $('form').submit(function(e) {
                var amount = parseFloat($('input[name="amount"]').val());
                if (amount <= 0) {
                    alert('Please enter a valid amount greater than 0');
                    e.preventDefault();
                    return false;
                }

                var title = $('input[name="expense_title"]').val().trim();
                if (title.length < 3) {
                    alert('Please enter a meaningful expense title (at least 3 characters)');
                    e.preventDefault();
                    return false;
                }
            });
        });

        function saveDraft() {
            // Set status to draft and submit form
            $('#statusSelect').val('DRAFT');
            $('form').submit();
        }
    </script>

    <!-- App js -->
    <script src="assets/js/app.js"></script>
</body>
</html>

<?php $conn->close(); ?>