@extends('layout')
@section('title','Modifier facture vente ')
@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>Modifier vente (<h7>Les champs en * sont obligatoires</h7>)</h4>
                </div>

                <div class="card-body">
                    <form action="" method="POST">
						{{csrf_field()}}
						<input type="hidden" value="{{$produit}}" id="produits">
                        <div class="row">
                            
                            <div class="col-lg-3 col-md-3">
                                <div class="form-group">
                                    <label>Nom du Client </label>
                                    <input title="" type="text" class="form-control" name="nomcli" value="{{$vente->nomcli}}" required>
                                </div>
                            </div>
							<div class="col-lg-3 col-md-3">
                                <div class="form-group">
                                    <label>Contacts info </label>
                                    <input title="" type="text" class="form-control" name="contact" value="{{$vente->contact}}" required>
                                </div>
                            </div>
                            @php
							$dd=date('d/m/Y');
							@endphp
							<div class="col-lg-3 col-md-3"> 
							  <div class="form-group">
							  <label>Date<font color="red">*</font></label>
							  <input type="text" class="form-control" name="datet" value="{{$dd}}" readOnly>
							  <div id="calendarMain2"></div>
							</div>
							</div>
							<div class="col-lg-3 col-md-3">
                                <div class="form-group">
                                    <label>Remise <font color="red">*</font></label>
                                    <input title="" type="text" class="form-control" name="remise" value="{{$vente->remise}}" required>
                                </div>
                            </div>
                        </div>
						@foreach($ligne_vente as $ligne_ventes)
                        <div class="row venteProduits" style="margin-bottom:-25px">
                            <div class="col-lg-4 col-md-4">
                                <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 le montant" type="text" class="form-control"
                                        name="peps[0][qte][]" value="{{$ligne_ventes->montligne}}" 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" style="height:30px;width:15px;"><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>
			<a type="button" href="{{route('vente')}}" data-toggle="tooltip"
			data-placement="bottom" title="Retour"
			class="btn btn-icon btn-warning" style="margin-left:15px;">Retourner sur la liste des ventes</a>
        </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-4 col-md-4">
                                <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>
										@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>Montant achat<font color="red">*</font></label>
                                    <input title="saisir le montant" type="text" class="form-control"
                                        name="peps[`+ i +`][montant][]" 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" style="height:30px;width:15px;"
                                    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.remise = $("input[name=remise]").val();
		data.nomcli = $("input[name=nomcli]").val();
        data.contact = $("input[name=contact]").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.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('modifventePost', ['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/vente';
                }

            })
        }
    });

</script>


@endpush

@endsection