@extends('template.layout')
@section('style')
@endsection

@section('content')
    <div class="container">



        @if (session('error'))
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <ul class="list-unstyled m-0 p-0">
                    {{ session('error') }}
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                </ul>
            </div>
        @endif





        @if (session('success'))
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <ul class="list-unstyled m-0 p-0">
                    <strong>{{ session('success') }}</strong>
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                </ul>
            </div>
        @endif




        <div class="card">
            <div class="card-header"><strong>Invoice Return Summery</strong></div>
            <div class="card-body">

                <a class="btn btn btn-outline-info  mb-3" href="{{ url()->previous() }}"><i
                        class="bi bi-skip-backward-circle pr-1"></i> Back</a>
                {{-- <a class="btn btn-success mb-3" href="{{ url('grn/create') }}"><i class="bi bi-plus-circle pr-1"></i>Create</a> --}}


                <div class="row">




                    <div class="col-md-6 col-lg-6"></div>
                </div>





                <table class="table table-hover table-responsive fs-7">
                    <thead class="thead-light">
                        <tr>
                            <th scope="col">#</th>
                            <th scope="col">Code</th>
                            <th scope="col">Date</th>
                            <th scope="col">Customer</th>
                            <th scope="col">NIC</th>
                            <th scope="col">Contact</th>
                            <th scope="col">Branch</th>
                            <th scope="col">Salesman</th>
                            <th scope="col">Item</th>
                            <th scope="col">Bill Type</th>
                            <th scope="col">Item Amount</th>
                            <th scope="col">Discount</th>
                            <th scope="col">Bill Amount</th>
                            <th scope="col">Paid</th>
                            <th scope="col">Not Paid</th>
                            <th scope="col">Status</th>
                            <th scope="col">Return Date</th>
                            <th scope="col">Return Amount</th>

                            <th scope="col">Action</th>

                        </tr>
                    </thead>
                    <tbody>

                        @php
                            $total_invoice = 0;
                            $total_invoice_amount = 0;
                            $total_paid_amount = 0;
                            $total_not_paid_amount = 0;
                            $total_return_amount=0;
                        @endphp

                        @if (isset($invoiceHasReturns))
                            @foreach ($invoiceHasReturns as $invoiceHasReturn)
                                @php
                                    $total_payment = 0;
                                @endphp

                                <tr>
                                    <td>{{ $loop->iteration }}</td>
                                    <td>{{ $invoiceHasReturn->invoice->code }}</td>
                                    <td>{{ $invoiceHasReturn->invoice->date }}</td>
                                    <td>{{ $invoiceHasReturn->invoice->customer_name }}</td>
                                    <td>{{ $invoiceHasReturn->invoice->customer_nic }}</td>
                                    <td>{{ $invoiceHasReturn->invoice->customer_mobile . ' / ' . $invoiceHasReturn->invoice->customer_land }}
                                    </td>
                                    <td>{{ $invoiceHasReturn->invoice->branch->name }}</td>
                                    <td>{{ $invoiceHasReturn->invoice->salesman->name }}</td>
                                    <td>{{ $invoiceHasReturn->invoice->item->name }}</td>
                                    <td>{{ $invoiceHasReturn->invoice->type }}</td>
                                    <td>{{ number_format($invoiceHasReturn->invoice->amount, 2) }}</td>
                                    <td>{{ number_format($invoiceHasReturn->invoice->discount, 2) }}</td>

                                    
                                    <td>{{ number_format($invoiceHasReturn->invoice->amount - $invoiceHasReturn->invoice->discount, 2) }}
                                    </td>
                                    @foreach ($invoiceHasReturn->invoice->payment as $payment)
                                        @php
                                            $total_payment += $payment->amount; 
                                            $total_return_amount+=$payment->amount;
                                        @endphp
                                    @endforeach
                                    <td>{{ number_format($total_payment, 2) }}</td>
                                    <td>{{ number_format($invoiceHasReturn->invoice->amount - $invoiceHasReturn->invoice->discount - $total_payment, 2) }}
                                    </td>
                                    {{-- <td>{{ $invoice->vehicle_number }}</td> --}}
                                    <td><span
                                            class="badge rounded-pill {{ $invoiceHasReturn->invoice->status == 'Processing' ? 'bg-success' : 'bg-warning' }}  ">{{ $invoiceHasReturn->invoice->status }}</span>
                                    </td>

                                    <td>{{ $invoiceHasReturn->date }}</td>
                                    <td>{{ number_format($invoiceHasReturn->amount,2) }}</td>

                                    <td><a href="{{ url('invoice/view/' . $invoiceHasReturn->invoice->id) }}"
                                            class="btn btn-sm btn-primary"><i class="bi bi-eye pr-1"></i>View</td>

                                    @php
                                        $total_invoice++;
                                        $total_invoice_amount +=
                                            $invoiceHasReturn->invoice->amount - $invoiceHasReturn->invoice->discount;
                                        $total_paid_amount = $invoiceHasReturn->invoice->payment_sum_amount;

                                    @endphp



                                </tr>
                            @endforeach
                        @endif



                    </tbody>
                </table>

                <div class="paginate">
                    {{ $invoiceHasReturns->links() }}
                </div>

                <div class="row">
                    <div class="col-md-4">







                    </div>
                    <div class="col-md-4">
                        <div class="row g-3 d-flex justify-content-end">
                            <div class="col-auto">
                                <label for="inputPassword6" class="col-form-label col-form-label-sm">Total
                                    Invoice</label>
                            </div>
                            <div class="col-auto">
                                <input type="text" name="total" class="form-control form-control-sm text-end" readonly
                                    value="{{ $total_invoice }}" style="width: 120px;">
                            </div>

                        </div>


                        <div class="row g-3 d-flex justify-content-end pt-1">
                            <div class="col-auto">
                                <label for="inputPassword6" class="col-form-label col-form-label-sm">Total
                                    Invoice Amount</label>
                            </div>
                            <div class="col-auto">
                                <input type="text" name="total" class="form-control form-control-sm text-end" readonly
                                    value="{{ number_format($total_invoice_amount, 2) }}" style="width: 120px;">
                            </div>

                        </div>

                    </div>
                    <div class="col-md-4">
                        <div class="row g-3 d-flex justify-content-end">
                            <div class="col-auto">
                                <label for="inputPassword6" class="col-form-label col-form-label-sm">Total
                                    Paid Amount</label>
                            </div>
                            <div class="col-auto">
                                <input type="text" name="total" class="form-control form-control-sm text-end" readonly
                                    value="{{ number_format($total_paid_amount, 2) }}" style="width: 120px;">
                            </div>

                        </div>


                        <div class="row g-3 d-flex justify-content-end pt-1">
                            <div class="col-auto">
                                <label for="inputPassword6" class="col-form-label col-form-label-sm">Total
                                    Not Paid Amount</label>
                            </div>
                            <div class="col-auto">
                                <input type="text" name="total" class="form-control form-control-sm text-end" readonly
                                    value="{{ number_format($total_invoice_amount - $total_paid_amount, 2) }}"
                                    style="width: 120px;">
                            </div>

                        </div>

                         <div class="row g-3 d-flex justify-content-end pt-1">
                            <div class="col-auto">
                                <label for="inputPassword6" class="col-form-label col-form-label-sm">Total
                                    Return Amount</label>
                            </div>
                            <div class="col-auto">
                                <input type="text" name="total" class="form-control form-control-sm text-end" readonly
                                    value="{{ number_format($total_return_amount, 2) }}"
                                    style="width: 120px;">
                            </div>

                        </div>
                    </div>
                </div>

            </div>
        </div>





    </div>



    </div>
@endsection

@section('script')
    <script>
        //delete confirmation message
        $('.btn-delete').click(function(e) {
            e.preventDefault();
            var form = $(this).parents('form');
            //alert('aaa');
            Swal.fire({
                title: "Are you sure to delete item data ?",
                text: "You won't be able to revert this!",
                icon: "warning",
                showCancelButton: true,
                confirmButtonColor: "#3085d6",
                cancelButtonColor: "#d33",
                confirmButtonText: "Yes, delete it!"
            }).then((result) => {
                if (result.isConfirmed) {
                    // Swal.fire({
                    //   title: "Deleted!",
                    //   text: "Your file has been deleted.",
                    //   icon: "success"
                    // });
                    form.submit();
                }
            });

        });
    </script>
@endsection
