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

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

$date_from = $_GET['date_from'] ?? date('Y-m-01');
$date_to = $_GET['date_to'] ?? date('Y-m-d');
$status = $_GET['status'] ?? '';

$sql = "SELECT e.*, i.invoice_number as local_invoice_number, i.total_amount, i.invoice_date 
        FROM etims_invoices e
        JOIN invoices i ON e.invoice_id = i.invoice_id
        WHERE i.invoice_date BETWEEN ? AND ?";

if ($status) {
    $sql .= " AND e.submission_status = '" . $conn->real_escape_string($status) . "'";
}

$sql .= " ORDER BY e.created_at DESC";

$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $date_from, $date_to);
$stmt->execute();
$result = $stmt->get_result();
?>

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'E-TIMS Submissions')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">E-TIMS Submissions</h4>
                            </div>
                        </div>
                    </div>

                    <!-- Filter -->
                    <div class="card mb-4">
                        <div class="card-body">
                            <form method="GET" class="row g-3">
                                <div class="col-md-3">
                                    <label class="form-label">From Date</label>
                                    <input type="date" class="form-control" name="date_from"
                                        value="<?php echo $date_from; ?>">
                                </div>
                                <div class="col-md-3">
                                    <label class="form-label">To Date</label>
                                    <input type="date" class="form-control" name="date_to"
                                        value="<?php echo $date_to; ?>">
                                </div>
                                <div class="col-md-3">
                                    <label class="form-label">Status</label>
                                    <select class="form-select" name="status">
                                        <option value="">All</option>
                                        <option value="submitted" <?php echo $status == 'submitted' ? 'selected' : ''; ?>>
                                            Submitted</option>
                                        <option value="pending" <?php echo $status == 'pending' ? 'selected' : ''; ?>>Pending
                                        </option>
                                        <option value="failed" <?php echo $status == 'failed' ? 'selected' : ''; ?>>Failed
                                        </option>
                                    </select>
                                </div>
                                <div class="col-md-3 d-flex align-items-end">
                                    <button type="submit" class="btn btn-primary w-100">Filter</button>
                                </div>
                            </form>
                        </div>
                    </div>

                    <div class="card">
                        <div class="card-body">
                            <table id="submissionsTable" class="table table-bordered dt-responsive nowrap">
                                <thead>
                                    <tr>
                                        <th>Local Invoice</th>
                                        <th>E-TIMS Number</th>
                                        <th>Date</th>
                                        <th>Amount</th>
                                        <th>Status</th>
                                        <th>Message</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while ($row = $result->fetch_assoc()): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars($row['local_invoice_number']); ?></td>
                                            <td><?php echo htmlspecialchars($row['etims_invoice_number'] ?? '-'); ?></td>
                                            <td><?php echo $row['invoice_date']; ?></td>
                                            <td><?php echo number_format($row['total_amount'], 2); ?></td>
                                            <td>
                                                <span class="badge bg-<?php
                                                echo $row['submission_status'] == 'submitted' ? 'success' :
                                                    ($row['submission_status'] == 'failed' ? 'danger' : 'warning');
                                                ?>">
                                                    <?php echo ucfirst($row['submission_status']); ?>
                                                </span>
                                            </td>
                                            <td><small><?php echo htmlspecialchars(substr($row['result_message'] ?? '', 0, 50)); ?></small>
                                            </td>
                                            <td>
                                                <button class="btn btn-sm btn-primary">Details</button>
                                                <?php if ($row['submission_status'] == 'failed'): ?>
                                                    <button class="btn btn-sm btn-warning">Retry</button>
                                                <?php endif; ?>
                                            </td>
                                        </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="assets/libs/datatables.net-bs5/js/dataTables.bootstrap5.min.js"></script>
    <script>$('#submissionsTable').DataTable();</script>
</body>

</html>