<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{fragments/layout :: head('Reports')}"></head>
<body>
<nav th:replace="~{fragments/layout :: adminbar}"></nav>

<section>
    <div class="container">
        <div class="page-header">
            <h1>Reports</h1>
            <div>
                <a class="btn btn-outline btn-sm" th:href="@{/admin/reports/vendors}">Vendor-wise</a>
                <a class="btn btn-outline btn-sm" th:href="@{/admin/reports/drivers}">Driver-wise</a>
            </div>
        </div>

        <div class="form-card" style="max-width:none;margin:0 0 20px">
            <form th:action="@{/admin/reports}" method="get">
                <div class="report-filters">
                    <div class="field"><label>From</label><input type="date" name="from" th:value="${p['from']}"/></div>
                    <div class="field"><label>To</label><input type="date" name="to" th:value="${p['to']}"/></div>
                    <div class="field">
                        <label>Vehicle</label>
                        <select name="carId">
                            <option value="">All</option>
                            <option th:each="c : ${cars}" th:value="${c.id}"
                                    th:text="${c.make + ' ' + c.model + ' (' + c.registrationNumber + ')'}"
                                    th:selected="${p['carId'] == #strings.toString(c.id)}"></option>
                        </select>
                    </div>
                    <div class="field">
                        <label>Vendor</label>
                        <select name="vendorId">
                            <option value="">All</option>
                            <option th:each="v : ${vendors}" th:value="${v.id}" th:text="${v.name}"
                                    th:selected="${p['vendorId'] == #strings.toString(v.id)}"></option>
                        </select>
                    </div>
                    <div class="field">
                        <label>Driver</label>
                        <select name="driverId">
                            <option value="">All</option>
                            <option th:each="d : ${drivers}" th:value="${d.id}" th:text="${d.name}"
                                    th:selected="${p['driverId'] == #strings.toString(d.id)}"></option>
                        </select>
                    </div>
                    <div class="field">
                        <label>Customer Category</label>
                        <select name="customerCategoryId">
                            <option value="">All</option>
                            <option th:each="cc : ${customerCategories}" th:value="${cc.id}" th:text="${cc.name}"
                                    th:selected="${p['customerCategoryId'] == #strings.toString(cc.id)}"></option>
                        </select>
                    </div>
                    <div class="field">
                        <label>Ownership</label>
                        <select name="ownership">
                            <option value="">All</option>
                            <option th:each="o : ${ownerships}" th:value="${o}" th:text="${o}"
                                    th:selected="${p['ownership'] == o.name()}"></option>
                        </select>
                    </div>
                    <div class="field">
                        <label>Status</label>
                        <select name="status">
                            <option value="">All</option>
                            <option th:each="s : ${statuses}" th:value="${s}" th:text="${s}"
                                    th:selected="${p['status'] == s.name()}"></option>
                        </select>
                    </div>
                </div>
                <div style="margin-top:14px;display:flex;gap:8px">
                    <button class="btn btn-primary" type="submit">Apply Filters</button>
                    <button class="btn btn-outline" type="submit" th:formaction="@{/admin/reports/excel}">Export Excel</button>
                    <button class="btn btn-outline" type="submit" th:formaction="@{/admin/reports/pdf}" formtarget="_blank">Export PDF</button>
                </div>
            </form>
        </div>

        <div class="stat-grid">
            <div class="stat-card"><h4>Bookings</h4><div class="num" th:text="${summary.count}">0</div></div>
            <div class="stat-card"><h4>Gross</h4><div class="num" style="font-size:20px">[[${currencySymbol}]]<span th:text="${summary.gross}">0</span></div></div>
            <div class="stat-card"><h4>Tax</h4><div class="num" style="font-size:20px">[[${currencySymbol}]]<span th:text="${summary.tax}">0</span></div></div>
            <div class="stat-card"><h4>Total</h4><div class="num" style="font-size:20px">[[${currencySymbol}]]<span th:text="${summary.total}">0</span></div></div>
        </div>

        <div class="table-wrap">
            <table>
                <thead><tr><th>ID</th><th>Customer</th><th>Car</th><th>Driver</th><th>Owner</th><th>Pickup</th><th>Return</th><th>Vehicle</th><th>Total</th><th>Status</th></tr></thead>
                <tbody>
                <tr th:each="b : ${bookings}">
                    <td th:text="${b.id}">1</td>
                    <td th:text="${b.user.fullName}">name</td>
                    <td th:text="${b.car.make + ' ' + b.car.model}">car</td>
                    <td th:text="${b.withDriver and b.driver != null ? b.driver.name : 'Self-drive'}">Self</td>
                    <td th:text="${b.revenueOwner}">OWNED</td>
                    <td th:text="${b.pickupDate}">date</td>
                    <td th:text="${b.returnDate}">date</td>
                    <td>[[${currencySymbol}]]<span th:text="${b.vehicleCharge}">0</span></td>
                    <td>[[${currencySymbol}]]<span th:text="${b.totalAmount}">0</span></td>
                    <td><span class="badge" th:classappend="'badge-' + ${#strings.toLowerCase(b.status)}" th:text="${b.status}">PENDING</span></td>
                </tr>
                <tr th:if="${#lists.isEmpty(bookings)}"><td colspan="10" style="color:#888">No bookings match the filters.</td></tr>
                </tbody>
            </table>
        </div>
    </div>
</section>

<footer th:replace="~{fragments/layout :: footer}"></footer>
</body>
</html>
