<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>
        All Listings
    </title>

    {% include 'admin/includes/imports.top.html' %}
    <!-- <link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote.min.css" rel="stylesheet"> -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.20/summernote-bs5.min.css"
        integrity="sha512-ngQ4IGzHQ3s/Hh8kMyG4FC74wzitukRMIcTOoKT3EyzFZCILOPF0twiXOQn75eDINUfKBYmzYn2AA8DkAk8veQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />

</head>

<body class="g-sidenav-show  bg-gray-100">
    {% include 'admin/includes/part.menu.html' %}
    <main class="main-content position-relative max-height-vh-100 h-100 border-radius-lg ">
        <!-- Navbar -->
        {% include 'admin/includes/part.header.html' %}
        <!-- End Navbar -->
        <div class="container-fluid py-4">
            <div class="row g-1">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-header pb-0 px-3">
                            <div class="d-flex justify-content-between">
                                <h6 class="mb-0">All Listings</h6>
                                <a href="{{url_for('add_listing')}}" class="btn btn-sm btn-info">Add New Listing</a>
                            </div>
                        </div>
                        <div class="card-body p-3" style="overflow: visible !important;">
                            <div class="table-responsive p-0" style="overflow: visible !important;">
                                <table class="table table-responsive align-items-center justify-content-center mb-0"
                                    style="overflow: visible !important;">
                                    <thead>
                                        <tr>

                                            <th
                                                class="text-uppercase text-secondary text-xxs text-black font-weight-bolder opacity-7">
                                                Car Info</th>
                                            <th
                                                class="text-uppercase text-secondary text-xxs text-black font-weight-bolder opacity-7 ps-2">
                                                Make/Brand
                                            </th>
                                            <th
                                                class="text-uppercase text-secondary text-xxs text-black font-weight-bolder opacity-7 ps-2">
                                                Model
                                            </th>
                                            <th
                                                class="text-uppercase text-secondary text-xxs text-black font-weight-bolder opacity-7 ps-2">
                                                Publish
                                            </th>
                                            <th
                                                class="text-uppercase text-secondary text-xxs text-black font-weight-bolder opacity-7 ps-2">
                                                Price</th>
                                            <th
                                                class="text-uppercase text-secondary text-xxs text-black font-weight-bolder opacity-7 ps-2">
                                                Status</th>
                                            <th
                                                class="text-uppercase text-secondary text-xxs text-black font-weight-bolder opacity-7 ps-2">
                                                Action</th>

                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% for car in cars %}
                                        <tr>
                                            <td>
                                                <div class="d-flex px-2">
                                                    <div>
                                                        <img height="45" class="rounded me-2" loading="lazy"
                                                            src="{{url_for('static', filename='files')}}/{{car.images[0]}}"
                                                            alt="">
                                                    </div>
                                                    <div class="my-auto">
                                                        <h6 class="mb-0 text-sm text-black">{{car.title}}</h6>
                                                        <small class="badge badge-sm rounded-1 p-1 bg-info">by
                                                            {{car.staff.first_name}} {{car.staff.last_name}}</small>
                                                    </div>
                                                </div>
                                            </td>
                                            <td>
                                                <p class="text-sm text-black font-weight-bold mb-0">
                                                    {{car.model2.car_make.name}}</p>
                                            </td>
                                            <td>
                                                <p class="text-sm text-black font-weight-bold mb-0">{{car.model2.name}}
                                                </p>
                                            </td>
                                            <td>
                                                <p class="text-sm text-black font-weight-bold mb-0">
                                                    {{car.created_at.strftime('%a, %b %d %Y')}}</p>
                                            </td>
                                            <td>
                                                <p class="text-sm text-black font-weight-bold mb-0">{{ "Kshs
                                                    {:,.0f}".format(car.price)}}</p>
                                            </td>
                                            <td>
                                                <p class="text-sm text-black font-weight-bold mb-0">{{car.status2.name}}
                                                </p>
                                            </td>
                                            <td>
                                                <div class="btn-group my-auto">
                                                    <button id="menu-1" type="button"
                                                        class="py-1 border-secondary badge badge-sm bg-secondary dropdown-toggle"
                                                        data-bs-toggle="dropdown" aria-haspopup="true"
                                                        aria-expanded="false">
                                                        Action
                                                    </button>
                                                    <div class="dropdown-menu dropdown-menu-dark border shadow-3"
                                                        style="z-index: 2500 !important;" aria-labelledby="menu-1">
                                                        <!-- <a class="dropdown-item" href="#">View</a> -->
                                                        <a class="dropdown-item text-success" data-bs-toggle="modal"
                                                            data-bs-target="#status_change" href="#status_change"
                                                            onclick="status_change('{{url_for('change_listing_status', id=car.id)}}')">Change
                                                            Status</a>
                                                        <a class="dropdown-item" href="#"></a>
                                                        <a class="dropdown-item text-primary" data-bs-toggle="modal"
                                                            data-bs-target="#feature" href="#feature"
                                                            onclick="MarkFeature('{{url_for('mark_featured_listing', id=car.id)}}')">
                                                            {% if car.featured %}
                                                            Unmark Featured
                                                            {% else %}
                                                            Mark as Featured
                                                            {% endif %}
                                                        </a>
                                                        <a class="dropdown-item"
                                                            href="{{url_for('edit_listing', id = car.id)}}">Edit</a>
                                                        <a class="dropdown-item text-danger" data-bs-toggle="modal"
                                                            data-bs-target="#delete" href="#delete"
                                                            onclick="Delete('{{url_for('delete_listing', id=car.id)}}')">Delete</a>
                                                    </div>
                                                </div>
                                            </td>
                                        </tr>
                                        {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                            <div class="modal fade" id="feature" data-bs-backdrop="static" data-bs-keyboard="false"
                                tabindex="-1" aria-labelledby="featureLabel" aria-hidden="true">
                                <div class="modal-dialog modal-dialog-centered">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h1 class="modal-title fs-5" id="featureLabel">Feature Item</h1>
                                            <button type="button" class="btn-close" data-bs-dismiss="modal"
                                                aria-label="Close"></button>
                                        </div>
                                        <div class="modal-body">
                                            Are you sure you want to proceed with this operation?
                                        </div>
                                        <div class="modal-footer py-2 d-flex justify-content-between">
                                            <button type="button" class="btn btn-sm btn-secondary"
                                                data-bs-dismiss="modal">Cancel</button>
                                            <a title="Proceed with Deletion" id="feature_it_button" href=""
                                                class="btn btn-sm btn-danger">Proceed</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="modal fade" id="status_change" data-bs-backdrop="static"
                                data-bs-keyboard="false" tabindex="-1" aria-labelledby="status_changeLabel"
                                aria-hidden="true">
                                <div class="modal-dialog modal-dialog-centered">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h1 class="modal-title fs-5" id="status_changeLabel">Change status</h1>
                                            <button type="button" class="btn-close" data-bs-dismiss="modal"
                                                aria-label="Close"></button>
                                        </div>
                                        <div class="modal-body">
                                            <form id="status_change_it_button" action="" method="post">
                                                {{form.csrf_token}}
                                                <div class="col-lg-4"></div>
                                                <div class="form-group">
                                                    {{form.status.label}}
                                                    {{form.status(class_="form-control")}}
                                                </div>
                                                {% for error in form.status.errors %}
                                                <small class='text-danger text-sm text-gradient'>{{ error }}</small>
                                                {% endfor %}
                                        </div>
                                        <div class="modal-footer py-2 d-flex justify-content-between">
                                            <button type="button" class="btn btn-sm btn-secondary"
                                                data-bs-dismiss="modal">Cancel</button>
                                            <button type="submit" title="Proceed with Deletion"
                                                form="status_change_it_button"
                                                class="btn btn-sm btn-danger">Proceed</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="modal fade" id="delete" data-bs-backdrop="static" data-bs-keyboard="false"
                                tabindex="-1" aria-labelledby="deleteLabel" aria-hidden="true">
                                <div class="modal-dialog modal-dialog-centered">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h1 class="modal-title fs-5" id="deleteLabel">Delete Item</h1>
                                            <button type="button" class="btn-close" data-bs-dismiss="modal"
                                                aria-label="Close"></button>
                                        </div>
                                        <div class="modal-body">
                                            Are you sure you want to proceed with this operation?
                                        </div>
                                        <div class="modal-footer py-2 d-flex justify-content-between">
                                            <button type="button" class="btn btn-sm btn-secondary"
                                                data-bs-dismiss="modal">Cancel</button>
                                            <a title="Proceed with Change" id="delete_it_button" href=""
                                                class="btn btn-sm btn-danger">Proceed</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="card-footer">
                                <div class="d-flex justify-content-center">
                                    {% set current_page = request.args.get('page', none) | int%}

                                    {% set pvis = '' %}
                                    {% set nvis = '' %}

                                    {% if last_page %}
                                    {% set nvis = 'disabled' %}
                                    {% endif %}

                                    {% if current_page == 0 or current_page == 1 %}
                                    {% set current_page = 1 %}
                                    {% set pvis = 'disabled' %}
                                    {% endif %}

                                    {% set next = current_page + 1%}
                                    {% set prev = current_page - 1%}

                                    {% set prev_url = url_for('all_listing', page=prev)%}
                                    {% set next_url = url_for('all_listing', page=next)%}


                                    <div class="pagination-area">
                                        <div aria-label="Page navigation example">
                                            <ul class="pagination my-3">
                                                <li class="page-item">
                                                    <a class="{{pvis}} page-link bg-info text-white border-black"
                                                        href="{{prev_url}}" aria-label="Previous">
                                                        <span aria-hidden="true"><i
                                                                class="fa text-white fa-angle-double-left"></i></span>
                                                    </a>
                                                </li>
                                                <li class="page-item">
                                                    <a class="{{nvis}}  page-link bg-info text-white border-black"
                                                        href="{{next_url}}" aria-label="Next">
                                                        <span aria-hidden="true"><i
                                                                class="fa text-white fa-angle-double-right"></i></span>
                                                    </a>
                                                </li>
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {% include 'admin/includes/part.footer.html' %}
        </div>
    </main>

    <!--   Core JS Files   -->
    {% include 'admin/includes/imports.bottom.html' %}
    <script>
        var win = navigator.platform.indexOf('Win') > -1;
        if (win && document.querySelector('#sidenav-scrollbar')) {
            var options = {
                damping: '0.5'
            }
            Scrollbar.init(document.querySelector('#sidenav-scrollbar'), options);
        }
        function MarkFeature(link) {
            $('#feature_it_button').attr('href', link);
        }
        function status_change(link) {
            $('#status_change_it_button').attr('action', link);
        }
    </script>
</body>

</html>