@extends("layouts.admin")
@section("title", "Add Product")
@section('content')

<div class="container-fluid">
    <!-- Page-Title -->
    <div class="row">
        <div class="col-sm-12">
            <div class="page-title-box">
                <div class="row">
                    <div class="col">
                        <h4 class="page-title">Products</h4>
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="{{ route('home') }}">Landnkitchen</a></li>
                            <li class="breadcrumb-item active">Product</li>
                        </ol>
                    </div><!--end col-->
                </div><!--end row-->
            </div><!--end page-title-box-->
        </div><!--end col-->
    </div><!--end row-->
    <!-- end page title end breadcrumb -->
    <div class="row">
        <div class="col-12">
            @if (Session::has('error'))
                <div class="alert alert-danger" role="alert">
                    {{ Session::get('error') }}
                </div>
            @endif
            @if (Session::has('success'))
                <div class="alert alert-success" role="alert">
                    {{ Session::get('success') }}
                </div>
            @endif
            <div class="table-responsive">
                <table id="datatable" class="table table-bordered dt-responsive nowrap" style="border-collapse: collapse; border-spacing: 0; width: 100%;">
                    <thead>
                    <tr>
                        <th>SN</th>
                        <th>Name</th>
                        <th>Category</th>
                        <th>Price</th>
                        <th>VAT</th>
                        <th>Action</th>
                    </tr>
                    </thead>
                    <tbody>
                        @php $sn = 1 @endphp
                        @foreach ( $products as $product )
                        @php
                            $category = App\Models\Category::find($product->category_id);
                        @endphp
                        <tr>
                            <td>{{ $sn }}</td>
                            <td>
                                <img src="assets/images/products/img-1.png" alt="" height="40">
                                <p class="d-inline-block align-middle mb-0">
                                    <a href="#" class="d-inline-block align-middle mb-0 product-name">{{ $product->name }}</a>
                                    <br>
                                    <span class="text-muted font-13">{{ $product->description }}</span>
                                </p>
                            </td>
                            <td>{{ $category->name }}</td>
                            <td>&#8358; {{ number_format($product->price, 2) }}</td>
                            <td>&#8358; {{ number_format($product->vat, 2) }}</td>
                            <td>
                                <a href="{{ route("edit.product", $product->id) }}" class="mr-2"><i class="las la-pen text-info font-18"></i></a>
                                <a href="javascript:void(0)" class="js_delete_product" data-toggle="modal" data-target="#deleteModal" data-id="{{ $product->id }}" class="mr-2"><i class="las la-trash-alt text-danger font-18"></i></a>
                            </td>
                        </tr>
                        @php $sn ++ @endphp
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div> <!-- end col -->
    </div> <!-- end row -->

</div><!-- container -->

  <!-- Delete Modal -->
  <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalTitle" aria-hidden="true">
    <form action="{{ route("delete.product") }}" method="POST">
        <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
            <h5 class="modal-title" id="deleteModalTitle">Delete Product</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            </div>
            <div class="modal-body" id="js_replace_delete_content">
            </div>
            <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
            <button type="submit" class="btn btn-primary">Yes</button>
            </div>
        </div>
        </div>
    </form>
  </div>

  <script src="{{ asset("js/jquery.min.js") }}"></script>
  <script>
        $(document).ready(function(){
            var loader = `<div class="text-center">
                            <i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
                        </div>`;

            const BASE_URL = "{{ url('/') }}";
            const REQUEST_URL = "<?=Request::url()?>";
            let CSRF = "{{ csrf_token() }}";

            $('body').on("click", '.js_delete_product', function(e){
                $('#js_replace_delete_content').html(loader);
                const id = $(this).attr("data-id");
                var data = new FormData();
                data.append('id', id);

                $.ajax({
                    url: BASE_URL + '/admin/delete-product/' + id,
                    method: "GET",
                    timeout: 5000,
                    data: data,
                    contentType: false,
                    cache: false,
                    processData: false,
                    headers: {'X-CSRF-TOKEN': CSRF},
                    success: function(response){
                        $('#js_replace_delete_content').html(response.html);
                    },
                    error: function(err){
                        alert("Oops, there was an error, try again later");
                    }
                })
            })
        });

  </script>
@endsection
