@extends('layout')
@section('title','Modifier BL ')
@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 />
			@php
			$dd = implode('/', array_reverse(explode('-', $vente->date)));
			@endphp
            <div class="card">
                <div class="card-header">
                    <h4>Modifier BL (<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">
                            <div class="col-lg-3 col-md-3" hidden>
                                <div class="form-group">
                                    <label>Type de vente <font color="red">*</font></label>
                                    <select class="form-control select2" name="typevente">
                                        <option value="Détail">Détail</option>
                                        <option value="Gros">Gros</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-lg-4 col-md-4">
                                <div class="form-group">
                                    <label>Client <font color="red">*</font></label>
                                    <select class="form-control select2" name="client">
										<option value="{{$vente->client_id}}">{{DB::table('client_externe')->where('id',$vente->client_id)->first()->noms}}</option>
                                        @foreach($client as $clients)
                                        <option value="{{$clients->id}}">{{$clients->noms}}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
				<div class="col-lg-4 col-md-4"> 
					  <div class="form-group">
                      <label>Date<font color="red">*</font></label>
                      <input type="text" class="form-control" name="datet" id="champ_date2" value="{{$dd}}" required>
					  <div id="calendarMain2"></div>
                    </div>
					</div>
				<div class="col-lg-4 col-md-4">
                                <div class="form-group">
                                    <label>Remise <font color="red">*</font></label>
                                    <input class="form-control" name="remise" value="{{$vente->remise}}"/>
                                </div>
                            </div>
                        </div>
						@foreach($ligne_vente as $ligne_ventes)
                        <div class="row venteProduits" style="margin-bottom:-25px">
                            <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"
                                       name="peps[0][produit][]" required>
										<option value="{{$ligne_ventes->produit_id}}">{{ DB::table('produit_vendable')->where('id',$ligne_ventes->produit_id)->first()->desi }}</option>
                                        @foreach($produit as $produits)
										<option value="{{$produits->id}}">{{$produits->desi}}</option>
										@endforeach
                                    </select>
                                </div>
                            </div>
                            <div class="col-lg-3 col-md-3">
                                <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="{{$ligne_ventes->qte}}" required>
                                </div>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1" style="padding: 30px">

                                <button data-toggle="tooltip" data-placement="bottom" title="Ajouter produit"
                                    type="button" class="btn btn-icon btn-primary" id="addNewblock"><i
                                        class="fas fa-plus"></i></button>
                            </div>
                        </div>
						@endforeach
                        <div class="addNouveau"></div>
						<hr>
						<div class="row">
							<div class="col-lg-3 col-md-3">
                                <div class="form-group">
                                    <label>Appliquer les taxes <font color="red">*</font></label>
                                    <select class="form-control select2" name="taxes">
                                        <option value="">Sélectionner</option>
                                        <option value="Non">Non</option>
                                        <option value="Oui">Oui</option>
                                    </select>
                                </div>
                            </div>
						</div>
                        <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-success" id="addvente"><i
                                    class="fas fa-check"></i> Enregistrer</button>
                        </div>

                        <br><br>

                    </form>

                </div>

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




@push('scripts')

<script>

    function removeBlock() {
                               $(".deleteblock").on("click", function () {
                                           $(this).parent().parent().remove()
                                       })
                              }

                              var i=0;
                              var j=1;

                               $("#addNewblock").on("click", function ()
                                {
                                   i++;
                                   j++;
                                   var html = `
                               

                         <div class="form-group row" style="margin-bottom:-25px">
                            
                            <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"
                                        name="peps[`+ i +`][produit][]" required>
                                        <option>Sélectionner le produit</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-lg-3 col-md-3">
                                <div class="form-group">
                                    <label>Qte<font color="red">*</font></label>
                                    <input title="saisir la quantité" type="text" class="form-control"
                                        name="peps[`+ i +`][qte][]" required>
                                </div>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1" style="padding: 30px">

                                <button data-toggle="tooltip" data-placement="bottom" title="Ajouter produit"
                                    type="button" class="btn btn-icon btn-danger deleteblock"><i
                                        class="fas fa-times"></i></button>

                            </div>

                        </div>`;
        $('.addNouveau').append(html);
				   $('.select2').select2();
                   removeBlock();

    });
</script>

<script>
    $("#addvente").on('click', function (e) {
        e.preventDefault();
        var data = {};
        var isValide = true;
        data.typevente = $("select[name=typevente]").val();
        data.client = $("select[name=client]").val();
	    data.remise = $("input[name=remise]").val();
		data.datet = $("input[name=datet]").val();
		data.taxes = $("select[name=taxes]").val();
        data.peps = [];

        $("[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;
            }
        });

        if (!data.typevente) {
            toastr.error('Veuillez sélectionner le type de vente ! ', 'Erreur !', { timeOut: 5000 })
            return false;
        }
		if (!data.remise) {
            toastr.error('Le champ remise ne doit pas être vide ! ', 'Erreur !', { timeOut: 5000 })
            return false;
        }
		if (!data.taxes) {
            toastr.error('Le champ taxe ne doit pas être vide ! ', 'Erreur !', { timeOut: 5000 })
            return false;
        }
        data._token = "{{csrf_token()}}";
		@php $id = $vente->id  @endphp
        if (data.peps.length && isValide) {
            $.post("{{route('modifventeclientPost', ['id' =>$id])}}", data, function (result) {
                if (result.type == 'error') {
                    toastr.error(result.response, result.title, { timeOut: 5000 })
                } else {

                    toastr.success(result.response, result.title, { timeOut: 15000 })
                    window.location.href = '/es/client_externe/vente';
                }

            })
        }
    });

</script>


@endpush

@endsection