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

<section>
    <div class="container">

        <div th:if="${success}" class="alert alert-success" th:text="${success}"></div>

        <div class="form-card" style="max-width:760px">
            <h2 th:text="${driver.id == null} ? 'Add Driver' : 'Edit Driver'">Add Driver</h2>
            <p class="subtitle">Personal details, category, charges &amp; availability</p>

            <form th:action="@{/admin/drivers}" th:object="${driver}" method="post">
                <input type="hidden" th:field="*{id}"/>

                <h4 class="form-section">Personal details</h4>
                <div class="form-grid2">
                    <div class="form-group field">
                        <label>Name</label>
                        <input type="text" th:field="*{name}" required/>
                        <small class="err" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></small>
                    </div>
                    <div class="form-group field">
                        <label>Phone</label>
                        <input type="tel" th:field="*{phone}" placeholder="+91 9XXXXXXXXX"/>
                    </div>
                    <div class="form-group field">
                        <label>Licence Number</label>
                        <input type="text" th:field="*{licenceNumber}" required/>
                        <small class="err" th:if="${#fields.hasErrors('licenceNumber')}" th:errors="*{licenceNumber}"></small>
                    </div>
                    <div class="form-group field">
                        <label>Licence Expiry</label>
                        <input type="date" th:field="*{licenceExpiry}"/>
                    </div>
                    <div class="form-group field">
                        <label>Years of Experience</label>
                        <input type="number" th:field="*{yearsExperience}" min="0"/>
                    </div>
                    <div class="form-group field">
                        <label>Photo URL</label>
                        <input type="text" th:field="*{photoUrl}" placeholder="/images/driver.png"/>
                    </div>
                </div>

                <h4 class="form-section">Category &amp; employment</h4>
                <div class="form-grid2">
                    <div class="form-group field">
                        <label>Category</label>
                        <select name="categoryId">
                            <option value="">— none —</option>
                            <option th:each="c : ${categories}" th:value="${c.id}" th:text="${c.name}"
                                    th:selected="${driver.category != null and driver.category.id == c.id}"></option>
                        </select>
                    </div>
                    <div class="form-group field">
                        <label>Employment Type</label>
                        <select th:field="*{employmentType}">
                            <option th:each="e : ${T(com.mvrent.carrental.model.DriverEmploymentType).values()}"
                                    th:value="${e}" th:text="${e}"></option>
                        </select>
                    </div>
                    <div class="form-group field">
                        <label>Vendor (if vendor-supplied)</label>
                        <select name="vendorId">
                            <option value="">— none (employed) —</option>
                            <option th:each="v : ${vendors}" th:value="${v.id}" th:text="${v.name}"
                                    th:selected="${driver.vendor != null and driver.vendor.id == v.id}"></option>
                        </select>
                    </div>
                    <div class="form-group field">
                        <label>Status</label>
                        <select th:field="*{status}">
                            <option th:each="s : ${T(com.mvrent.carrental.model.DriverStatus).values()}"
                                    th:value="${s}" th:text="${s}"></option>
                        </select>
                    </div>
                </div>

                <h4 class="form-section">Charges ([[${currencySymbol}]])</h4>
                <div class="form-grid2">
                    <div class="form-group field">
                        <label>Hourly</label>
                        <input type="number" step="0.01" th:field="*{hourlyCharge}" placeholder="optional"/>
                    </div>
                    <div class="form-group field">
                        <label>Daily</label>
                        <input type="number" step="0.01" th:field="*{dailyCharge}" placeholder="optional"/>
                    </div>
                    <div class="form-group field">
                        <label>Monthly</label>
                        <input type="number" step="0.01" th:field="*{monthlyCharge}" placeholder="optional"/>
                    </div>
                </div>

                <button class="btn btn-primary" type="submit" style="width:100%">Save Driver</button>
                <a class="btn btn-ghost" th:href="@{/admin/drivers}" style="display:block;margin-top:8px;text-align:center">Cancel</a>
            </form>
        </div>

        <!-- Documents (edit only) -->
        <div class="form-card" style="max-width:760px;margin-top:24px" th:if="${driver.id != null}">
            <h2 style="font-size:18px">Documents</h2>
            <p class="subtitle">Licence, ID proof, police verification — with expiry tracking</p>

            <div class="table-wrap" th:if="${not #lists.isEmpty(driver.documents)}">
                <table>
                    <thead><tr><th>Type</th><th>File</th><th>Issued</th><th>Expiry</th><th>State</th><th></th></tr></thead>
                    <tbody>
                    <tr th:each="d : ${driver.documents}">
                        <td th:text="${d.docType}">LICENCE</td>
                        <td><a th:if="${d.fileUrl}" th:href="${d.fileUrl}" target="_blank">view</a><span th:unless="${d.fileUrl}">—</span></td>
                        <td th:text="${d.issuedDate} ?: '—'">—</td>
                        <td th:text="${d.expiryDate} ?: '—'">—</td>
                        <td>
                            <span th:if="${d.expired}" class="badge badge-cancelled">EXPIRED</span>
                            <span th:if="${d.expiringSoon}" class="badge badge-pending">EXPIRING</span>
                            <span th:if="${!d.expired and !d.expiringSoon}" class="badge badge-confirmed">OK</span>
                        </td>
                        <td>
                            <form th:action="@{/admin/drivers/{id}/documents/{docId}/delete(id=${driver.id},docId=${d.id})}"
                                  method="post" onsubmit="return confirm('Remove document?');">
                                <button class="btn btn-danger btn-sm" type="submit">Delete</button>
                            </form>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>

            <form th:action="@{/admin/drivers/{id}/documents(id=${driver.id})}" method="post" enctype="multipart/form-data"
                  style="display:grid;grid-template-columns:150px 1fr 130px 130px auto;gap:10px;align-items:end;margin-top:14px">
                <div class="field">
                    <label>Type</label>
                    <select name="docType" required>
                        <option th:each="t : ${T(com.mvrent.carrental.model.DriverDocumentType).values()}"
                                th:value="${t}" th:text="${t}"></option>
                    </select>
                </div>
                <div class="field"><label>Upload file</label><input type="file" name="file" accept=".pdf,.jpg,.jpeg,.png"/></div>
                <div class="field"><label>Issued</label><input type="date" name="issuedDate"/></div>
                <div class="field"><label>Expiry</label><input type="date" name="expiryDate"/></div>
                <button class="btn btn-primary" type="submit">Add</button>
            </form>
        </div>

    </div>
</section>

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