{% extends 'back.html.twig' %}

{% block title %} Invoices{% endblock %}

{% block stylesheets %}
    <style>
        .paid {
            color:green;
            font-weight:bold;
        }
        .unpaid, .not-sent {
            color:red;
            font-weight:bold;
        }
        .pending-payment {
            color:#e9692c;
            font-weight:bold;
        }
    </style>

{% endblock %}

{% block body %}

    <div class="app-content-header">
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm-6"><h3 class="mb-0">Invoices</h3></div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-end">
                        <li class="breadcrumb-item"><a href="{{ path('app_admin_dashboard') }}">Home</a></li>
                        <li class="breadcrumb-item active">Invoices</li>
                    </ol>
                </div>
            </div>
        </div>
    </div>

    <div class="app-content">
        <div class="container-fluid">
            <div class="row g-4">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-header with-border">
                                <h3 class="card-title" title="All Invoices">Invoices</h3>

                                <div style="float:right; margin-right: 10px;float:right" data-toggle="tooltip" data-placement="top" title="Filter Invoices">
                                    <button data-bs-toggle="modal" data-bs-target="#filterInvoicesModel" type="button" class="btn btn-primary filter_invoices_btn" >
                                        <i class="bi bi-search"></i> Search
                                    </button>
                                </div>

                            </div>
                            <div class="card-body" style="overflow-x: auto;">
                                <div class="col-md-12 mb-5">
                                    <a href="{{ path('admin_invoice_index_unpaid_invoices') }}"><button type="button" class="btn btn-warning btn-lg {% if selectedPaymentType == "UNPAID" %}border-active{% endif %}">Unpaid Invoices</button></a>
                                    <a href="{{ path('admin_invoice_index_paid_invoices') }}"><button type="button" class="btn btn-success btn-lg {% if selectedPaymentType == "PAID" %}border-active{% endif %}">Paid Invoices</button></a>
                                    <a href="{{ path('admin_invoice_index') }}"><button type="button" class="btn btn-primary btn-lg {% if selectedPaymentType == "ALL" %}border-active{% endif %}">All Invoices</button></a>
                                </div>
                                <table id="invoices_table" class="table table-bordered table-striped">
                                    <thead>
                                    <tr>
                                        <th>Invoice No</th>
                                        <th>Job Date</th>
                                        <th>Payment Status</th>
                                        <th>Invoice Status</th>
                                        <th>Discount %</th>
                                        <th>{{ 'Supervisor'|trans }}</th>
                                        <th>Total</th>
                                        <th>Job Address</th>
                                        <th>Actions</th>
                                    </tr>
                                    </thead>
                                    <tbody></tbody>
                                </table>
                            </div>
                        </div>
                    </div>


            {% include '@Invoice/invoices/partials/delete-invoice-modal.twig' %}
            {% include '@Invoice/invoices/partials/invoice-filter-modal.twig' %}
                </div>
        </div>
    </div>
{% endblock %}

{% block javascripts %}
<script type="text/javascript">

    $('.datepicker_fields').datepicker({dateFormat: 'dd/mm/yy',todayHighlight: true});

    $('#invoices_table').DataTable({
        "processing": true,
        "serverSide": true,
        "searching": false,
        "ajax": {
            url : '/api/invoice/filter',
            type : 'post',
            data : function(data){
                data.jobId = $("#job_id").val(),
                data.invoiceNo = $("#invoice_no").val(),
                data.address = $("#job_address").val(),
                data.builder = $("#job_builder").val(),
                data.supervisor = $("#job_supervisor").val(),
                data.sentDate = $("#invoice_sent_date").val(),
                data.paymentDate = $("#invoice_payment_date").val(),
                data.createdDate = $("#invoice_create_date").val(),
                data.paymentStatus = $("#invoice_payment_status").val(),
                data.customer = $("#job_customer").val()
            }
        },
        "columns": [
            {
                "data": "invoiceNo",
                "render": function ( data, type, full, meta ) {
                    var output =  "<div class='col-md-12'><a target='_blank' href='/admin/invoice/edit/"+ full.id +"'>"+ full.invoiceNo +"</a></div>";

                    return output;
                }
            },
            {
                "data": "jobDate",
                "render": function ( data, type, full, meta ) {
                    return full.jobDate;
                }
            },
            {
                "data": "paymentStatus",
                "render": function ( data, type, full, meta ) {
                    var output = "<span class='unpaid'>UNPAID</span>";

                    if (full.isPaid){
                        output = "<span class='paid'>PAID</span>";;
                    }

                    return output;
                }
            },
            {
                "data": "invoiceStatus",
                "render": function ( data, type, full, meta ) {
                    var output = "";

                    if (!full.sentDate) {
                        output = "<span class='not-sent'>NOT SENT</span>";
                    }
                    else if(!full.paymentDate){
                        output = "<span class='pending-payment'>PENDING PAYMENT</span>";
                    }
                    else if(full.paymentDate){
                        output = "<span class='paid'>PAID</span>";
                    }

                    return output;
                }
            },
            {
                "data": "discount",
                "render": function ( data, type, full, meta ) {
                    var output = full.discount;

                    if (full.discount == null){
                        output = 0;
                    }

                    return output;
                }
            },
            {
                "data": "supervisor",
                "render": function ( data, type, full, meta ) {
                    var output = full.supervisorName + " -> <i>"+ full.customerName +"</i>";

                    return output;
                }
            },
            {
                "data": "total",
                "render": function ( data, type, full, meta ) {

                    return "$"+full.total;
                }
            },
            {
                "data": "jobAddress",
                "render": function ( data, type, full, meta ) {

                    return full.jobAddress;
                }
            },
            {
                "data": "actions",
                "orderable": false,
                "render" : function (data, type, full, meta) {
                    var output = '';
                    output += '<div class="dropdown">';
                    output += '<button class="btn btn-dark dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">';
                    output += '<i class="bi bi-chevron-down"></i> Actions';
                    output += '</button>';
                    output += '<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">';
                    output += '<a class="dropdown-item" href="/admin/invoice/edit/'+ full.id +'" target="_blank"><i class="bi bi-pencil-square"></i> &nbsp;&nbsp; Edit Invoice</a>';
                    output +=  '<a class="dropdown-item action_btn" href="/admin/pdf/invoice/view/'+ full.id +'" target="_blank"><i class="bi bi-currency-dollar"></i> &nbsp;&nbsp; View Invoice</a>';
                    output +=  '<a class="dropdown-item action_btn" href="/admin/job/edit/'+ full.jobId +'" target="_blank"><i class="bi bi-layers"></i> &nbsp;&nbsp; View Job</a>';
                    output +=  '</div>';
                    output +=  '</div>';

                    return output;

                    return output;
                }
            }
        ],
        error: function(xhr, status, error) {
            new Noty({
                theme: 'metroui',
                type: 'error',
                text: "<strong>Error!</strong> Failed to Retrieve Invoice Data!",
                timeout: 2000
            }).show();
        }
    });

</script>
{% endblock %}
