@extends('layout')
@section('title','Bon de livraison')
@section('content')
<br />
<div class="section-body">
    <div class="row">
        <div class="col-lg-10 offset-1 col-md-12 ">
            @if (session('status'))
            <div class="alert alert-warning" style="font-size: 15px; background-color: #328039; color: white">
                <i class="fa fa-check"></i> &nbsp; {{ session('status') }}
            </div>
            @endif

            @if (session('error'))
            <div class="alert alert-danger" style="font-size: 15px; background-color: #fb5757; color: white">
                <i class="ti-na"></i> {{ session('error') }}
            </div>
            @endif
            @if ($errors->any())
            <div class="alert alert-danger" style="font-size: 15px; background-color: #fb5757; color: white">
                <ul>
                    @foreach ($errors->all() as $error)
                    <li> <i class="ti-na"></i> {{ $error }}</li>
                    @endforeach
                </ul>
            </div>
            @endif
            <br />
            <div class="card">
                <div class="card-header">
                    <h4>Enregistrer un bon de livraison (<h7>Les champs en * sont obligatoires</h7>)</h4>
                </div>

                <div class="card-body">
                    <form action="" method="POST">
						{{csrf_field()}}
                        <div class="row" style="padding-top: 30px">
						<input title="" type="text" class="form-control"
                        value="{{$id}}" hidden>
                            <div class="col-lg-4 col-md-4">
                                <div class="form-group">
                                    <label>N° Commande<font color="red">*</font></label>
                                    <input title="" type="text" class="form-control"
                                        value="{{$vente->numero}}" readOnly>
										<input title="" type="text" class="form-control"
                                        value="{{$vente->id}}" name="vente_id" hidden>
                                </div>
                            </div>
                            <div class="col-lg-4 col-md-4">
                                <div class="form-group">
                                    <label>Client<font color="red">*</font></label>
                                    <input title="" type="text" class="form-control"
                                        value="{{DB::table('client_externe')->where('id', $vente->client_id)->first()->noms}}" readOnly>
									<input title="" type="text" class="form-control"
                                        value="{{$vente->client_id}}" name="client_id" hidden>
								</div>
                            </div>
							<div class="col-lg-4 col-md-4">
                                <div class="form-group">
                                    <label>Date commande<font color="red">*</font></label>
                                    <input title="" type="text" class="form-control"
                                        value="{{$vente->date}}" readOnly>
                                </div>
                            </div>
                        </div>
						@foreach($ligne as $lignes)
                        <div class="row">
                            <div class="col-lg-6 col-md-6">
                                <div class="form-group">
                                    <label>Produit <font color="red">*</font></label>
                                    <select title="sélectionner le produit" class="form-control select2"
                                        id="selectproduit-0" name="peps[0][produit][]" required>
                                        <option value="{{$lignes->produit_id}}">{{DB::table('produit_vendable')->where('id', $lignes->produit_id)->first()->desi}}/ {{DB::table('produit_vendable')->where('id', $lignes->produit_id)->first()->code}}</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-lg-6 col-md-6">
                                <div class="form-group">
                                    <label>Qte<font color="red">*</font></label>
                                    <input title="saisir la quantité" type="text" class="form-control"
                                        name="peps[0][qte][]" value="{{$lignes->reste}}" required>
                                </div>
                            </div>
                        </div>
						@endforeach
                        <div class="pull-left">

                            <button type="reset" class="btn btn-icon icon-left btn-danger"><i class="fas fa-times"></i>
                                Annuler</button>
                            <button class="btn btn-icon icon-left btn-primary" id="addliv"><i
                                    class="fas fa-check"></i> Enregistrer</button>
                        </div>

                        <br><br>

                    </form>

                </div>

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



</div>


<div class="section-body">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h4>Bons de livraison</h4>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-striped" id="table-1">
                            <thead>
                                <tr>
                                    <th class="text-center"> ID </th>
                                    <th class="text-center"> Réf. </th>
                                    <th class="text-center">N° Commande</th>
                                    <th class="text-center">Client</th>
                                    <th class="text-center">Date</th>
                                    <th class="text-center">Etat</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($livraison as $livraisons)
                                <tr>
							<td style="text-align:center;">{{$loop->iteration}}</td>
							<td style="text-align:center;">{{$livraisons->reference}}</td>
							<td style="text-align:center;">{{DB::table('vente_client')->where('id', $livraisons->vente_id)->first()->numero}}</td>
							<td style="text-align:center;">{{DB::table('client_externe')->where('id', $livraisons->client_id)->first()->noms}}</td>
							@php
							$dd = implode('/', array_reverse(explode('-', $livraisons->date)));
							@endphp
							<td style="text-align:center;">{{$dd}}</td>
							<td style="text-align:center;">
							@if( $livraisons->etat ==  "Non livré")
							<div class="badge badge-warning badge-shadow">{{$livraisons->etat}}</div>
							@else
							<div class="badge badge-success badge-shadow">{{$livraisons->etat}}</div>
							@endif
							</td>
							<td class="align-middle">
							<a type="button" href="{{route('detail-livraison',['id'=>$livraisons->id])}}" data-toggle="tooltip" data-placement="bottom" title="Détails" class="btn btn-icon btn-info"><i class="fas fa-info"></i></a>
							<a type="button" href="#" data-toggle="tooltip" data-id="{{$livraisons->id}}" data-placement="bottom" title="Supprimer" class="btn btn-icon btn-danger data-supprimer"><i class="fas fa-trash"></i></a>
							<a type="button" target="_blank" href="{{route('bordereau-livraison',['id'=>$livraisons->id])}}" class="btn btn-icon btn-warning"><i class="fas fa-print"></i></a>	
							</td>
                          </tr>
                                @endforeach 
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
</div>

<div class="modal fade" id="Validermodal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Validation</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                Voulez-vous valider cette livraison ?
            </div>
            <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="valider">

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

<div class="modal fade" id="Supprimermodal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Suppression</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                Voulez-vous supprimer ce bon de livraison ?
            </div>
            <div class="modal-footer bg-whitesmoke br" style="background-color:#ffffff" id="delete">

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

@push('scripts')

<script>
        $(".data-valider").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/es/Validerventeclient/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('ValiderventeclientPot')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.vente.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#valider').html('').append(html);
          })
          $('#Validermodal').modal();
        });
    </script>
	   
	      <script>
        $(".data-supprimer").on('click', function () {
                var id=$(this).data('id');
                $.get(location.origin+'/es/Livraison/'+id, function (data) {
                  var html = `
            <button type="button" class=" btn btn-danger" data-dismiss="modal">Non</button>
                      <form action="{{route('SupprimerlivraisonPost')}}" method="post" style="display: inline">
                        {{csrf_field()}}
                      <input type="hidden" value="`+data.livraison.id+`" name="id" >
                      <button type="submit" class="btn btn-success">Oui</button>

            </form>`
            ;
            $('#delete').html('').append(html);
          })
          $('#Supprimermodal').modal();
        });
    </script>

<script>
    $("#addliv").on('click', function (e) {
        e.preventDefault();
        var data = {};
        var isValide = true;
        data.peps = [];
		data.client_id = $("input[name=client_id]").val();
		data.vente_id = $("input[name=vente_id]").val();
        $("[name*=peps]").each(function (index, element) {
            if ($(element).val() != "") {
                if (!Number($(element).val())) {
                    isValide = false;
                    toastr.error('Veuillez ' + $(element)[0].attributes[0].textContent, 'Attention', { timeOut: 5000 });
                }
                console.log($(element).val());
                data.peps.push($(element).val());
            } else {
                toastr.error('Veuillez ' + $(element)[0].attributes[0].textContent, 'Attention', { timeOut: 5000 });
                console.log($(element));
                isValide = false;
                return false;
            }
        });
		
        data._token = "{{csrf_token()}}";

        if (data.peps.length && isValide) {
            $.post("{{route('livraisonPost')}}", data, function (result) {
                if (result.type == 'error') {
                    toastr.error(result.response, result.title, { timeOut: 5000 })
                } else {

                    toastr.success(result.response, result.title, { timeOut: 15000 })
                    location.reload();
                }

            })
        }
    });

</script>


@endpush

@endsection