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

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$user_id = $_SESSION['user_id'];

// Get vendor ID
$sql_vendor = "SELECT id FROM vendors WHERE user_id = ?";
$stmt_vendor = $conn->prepare($sql_vendor);
$stmt_vendor->bind_param("i", $user_id);
$stmt_vendor->execute();
$result_vendor = $stmt_vendor->get_result();
$vendor = $result_vendor->fetch_assoc();

if (!$vendor) {
    header('Location: index.php');
    exit();
}

$vendor_id = $vendor['id'];

// Get completed jobs for invoicing
$sql = "SELECT mr.*, p.name as property_name, u.unit_number
        FROM maintenance_requests mr
        LEFT JOIN properties p ON mr.property_id = p.id
        LEFT JOIN units u ON mr.unit_id = u.id
        WHERE mr.assigned_vendor_id = ?
        AND mr.status = 'Completed'
        AND mr.final_cost > 0
        ORDER BY mr.completed_date DESC";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $vendor_id);
$stmt->execute();
$result = $stmt->get_result();
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Vendor Invoices')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <?php includeFileWithVariables('layouts/page-title.php', array('pagetitle' => 'Invoicing', 'title' => 'Vendor Invoices')); ?>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">Completed Jobs - Ready for Invoicing</h4>
                                </div>
                                <div class="card-body">
                                    <div class="table-responsive">
                                        <table class="table table-nowrap align-middle mb-0">
                                            <thead>
                                                <tr>
                                                    <th>Ticket #</th>
                                                    <th>Property/Unit</th>
                                                    <th>Category</th>
                                                    <th>Completed Date</th>
                                                    <th>Final Cost</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if ($result && $result->num_rows > 0): ?>
                                                    <?php while($job = $result->fetch_assoc()): ?>
                                                        <tr>
                                                            <td><?php echo htmlspecialchars($job['ticket_number']); ?></td>
                                                            <td>
                                                                <div>
                                                                    <h5 class="fs-14 mb-0"><?php echo htmlspecialchars($job['property_name'] ?? 'N/A'); ?></h5>
                                                                    <p class="text-muted mb-0 small">Unit: <?php echo htmlspecialchars($job['unit_number'] ?? 'N/A'); ?></p>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <span class="badge bg-soft-info text-info">
                                                                    <?php echo htmlspecialchars($job['category']); ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <?php echo $job['completed_date'] ? date('M d, Y', strtotime($job['completed_date'])) : 'N/A'; ?>
                                                            </td>
                                                            <td>
                                                                <span class="fw-semibold">KES <?php echo number_format($job['final_cost'], 2); ?></span>
                                                            </td>
                                                            <td>
                                                                <button class="btn btn-primary btn-sm" 
                                                                        onclick="generateInvoice(<?php echo $job['id']; ?>)">
                                                                    <i class="ri-file-add-line me-1"></i>Generate Invoice
                                                                </button>
                                                            </td>
                                                        </tr>
                                                    <?php endwhile; ?>
                                                <?php else: ?>
                                                    <tr>
                                                        <td colspan="6" class="text-center py-4">
                                                            <div class="text-muted">No completed jobs found</div>
                                                        </td>
                                                    </tr>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
    <script>
        function generateInvoice(requestId) {
            window.location.href = 'create_vendor_invoice.php?request_id=' + requestId;
        }
    </script>
</body>
</html>
