@extends('backend.layouts.master')
@section('title','E-SHOP || All Invoices')
@section('main-content')
 <!-- DataTales Example -->
 <div class="card shadow mb-4">
     <div class="row">
         <div class="col-md-12">
            @include('backend.layouts.notification')
         </div>
     </div>
    <div class="card-header py-3">
      <h6 class="m-0 font-weight-bold text-primary float-left">Invoice List</h6>
      <a href="{{route('invoice.create')}}" class="btn btn-primary btn-sm float-right" data-toggle="tooltip" data-placement="bottom" title="Add nft"><i class="fas fa-plus"></i> Add Invoice</a>
    </div>
    <div class="card-body">
      <div class="table-responsive">
        @if(count($invoices)>0)
        <table class="table table-bordered" id="banner-dataTable" width="100%" cellspacing="0">
          <thead>
            <tr>
              <th>Action</th>
              <th>S.N.</th>
              <th>Name</th>
              <th>Email</th>
              <th>Phone</th>
              <th>Payment Status</th>
              <th>Amount</th>
              <th>Currency</th>
              <th>Created At</th>
              <th>Created By</th>
            </tr>
          </thead>
          <tfoot>
            <tr>
              <th>Action</th>
              <th>S.N.</th>
              <th>Name</th>
              <th>Email</th>
              <th>Phone</th>
              <th>Payment Status</th>
              <th>Amount</th>
              <th>Currency</th>
              <th>Created At</th>
              <th>Created By</th>
            </tr>
          </tfoot>
          <tbody>
            @foreach($invoices as $invoice)
                <tr>
                    <td>
                        <a href="{{route('invoice.show',$invoice->id)}}" class="btn btn-warning btn-sm float-left mr-1" style="height:30px; width:30px;border-radius:50%" data-toggle="tooltip" title="view" data-placement="bottom"><i class="fas fa-eye"></i></a>
                        <a href="{{route('invoice.payment',$invoice->slug)}}" target="_blank" class="btn btn-success btn-sm float-left mr-1" style="height:30px; width:30px;border-radius:50%" data-toggle="tooltip" title="view" data-placement="bottom"><i class="fas fa-money-bill"></i></a>
                        @if($invoice->is_paid == 'unpaid')
                            <a href="{{route('invoice.edit',$invoice->id)}}" class="btn btn-primary btn-sm float-left mr-1" style="height:30px; width:30px;border-radius:50%" data-toggle="tooltip" title="edit" data-placement="bottom"><i class="fas fa-edit"></i></a>
                        @endif
                        @if(auth()->user()->role == 'admin' && $invoice->is_paid == 'paid')
                            <a href="{{route('invoice.pdf',$invoice->slug)}}" target="_blank" class="btn btn-primary btn-sm float-left mr-1" style="height:30px; width:30px;border-radius:50%" data-toggle="tooltip" title="PDF" data-placement="bottom"><i class="fas fa-upload"></i></a>
                        @endif
                        @if(auth()->user()->role == 'admin')
                            <form method="POST" action="{{route('invoice.destroy',[$invoice->id])}}">
                                @csrf
                                @method('delete')
                                <button class="btn btn-danger btn-sm dltBtn" data-id={{$invoice->id}} style="height:30px; width:30px;border-radius:50%" data-toggle="tooltip" data-placement="bottom" title="Delete"><i class="fas fa-trash-alt"></i></button>
                            </form>
                        @endif
                    </td>
                    <td>{{$invoice->id}}</td>
                    <td>{{$invoice->name}}</td>
                    <td>{{$invoice->email}}</td>
                    <td>{{$invoice->phone}}</td>
                    <td>
                        @if($invoice->is_paid=='paid')
                            <span class="badge badge-success">{{$invoice->is_paid}}</span>
                        @else
                            <span class="badge badge-danger">{{$invoice->is_paid}}</span>
                        @endif
                    </td>
                    <td>{{$invoice->invoiceItems->sum('price')}}</td>
                    <td>{{$invoice->Currency->name}}</td>
                    <td>{{ date('d M Y', strtotime($invoice->created_at)) }}</td>
                    <td>{{ isset($invoice->createdBy->name) ? $invoice->createdBy->name : '' }}</td> 
                </tr>
            @endforeach
          </tbody>
        </table>

        @else
          <h6 class="text-center">No Invoices found!!!</h6>
        @endif
      </div>
    </div>
</div>
@endsection

@push('styles')
  <link href="{{asset('backend/vendor/datatables/dataTables.bootstrap4.min.css')}}" rel="stylesheet">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" />
@endpush

@push('scripts')

  <!-- Page level plugins -->
  <script src="{{asset('backend/vendor/datatables/jquery.dataTables.min.js')}}"></script>
  <script src="{{asset('backend/vendor/datatables/dataTables.bootstrap4.min.js')}}"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>

  <!-- Page level custom scripts -->
  <script src="{{asset('backend/js/demo/datatables-demo.js')}}"></script>
  <script>
      $('#banner-dataTable').DataTable({
      "order": [[0, "desc"]] // Set the first column in descending order
    });
  </script>
  <script>
      $(document).ready(function(){
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
          $('.dltBtn').click(function(e){
            var form=$(this).closest('form');
              var dataID=$(this).data('id');
              // alert(dataID);
              e.preventDefault();
              swal({
                    title: "Are you sure?",
                    text: "Once deleted, you will not be able to recover this data!",
                    icon: "warning",
                    buttons: true,
                    dangerMode: true,
                })
                .then((willDelete) => {
                    if (willDelete) {
                       form.submit();
                    } else {
                        swal("Your data is safe!");
                    }
                });
          })
      })
  </script>
@endpush
