@extends('admin.index')
@section('content')
@php
use App\Models\SubVirables;
@endphp



  <title>{{trans('admin.Edit_Start_Period')}}</title>

     <main id="js-page-content" role="main" class="page-content">
                    <ol class="breadcrumb page-breadcrumb">
                 <li class="breadcrumb-item"><a href="javascript:void(0);">{{trans('admin.Stores')}}</a></li>
                 <li class="breadcrumb-item active">{{trans('admin.Edit_Start_Period')}}</li>
                        <li class="position-absolute pos-top pos-right d-none d-sm-block"><span
                                class="js-get-date"></span></li>
                    </ol>
<form action="{{url('PostEditStartPeriod/'.$item->id)}}" method="post">
    {!! csrf_field() !!}
     @honeypot
    <input type="hidden" name="User" value="{{$item->User}}">
                    <div class="row">
                        <div class="col-lg-12">
                            <div id="panel-2" class="panel">
                                <div class="panel-hdr">
                                    <h2>
                                        <span class="fw-300"><i>   {{trans('admin.Edit_Start_Period')}} </i></span>
                                    </h2>

                                </div>
                                <div class="panel-container show">
                                          <span id="ex"> @include('admin.layouts.messages')</span>
                                    <div class="panel-content">
                                    <div class="form-row">
                                        <div class="form-group col-lg-2">
                          <label class="form-label" for="simpleinput"> {{trans('admin.Code')}}  </label>
                             <input type="text" name="Old_Code"  value="{{$item->Code}}" class="form-control " readonly>
                             <input type="hidden" name="Code"  value="{{$Code}}">
                                        </div>
                                          @if(auth()->guard('admin')->user()->emp == 0)
                                        <div class="form-group col-lg-2">
                                            <label class="form-label" for="simpleinput">{{trans('admin.Date')}}</label>
                         <input type="date" name="Date" value="{{$item->Date}}" class="form-control" required>
                                        </div>
                                        @else

                                         @if(auth()->guard('admin')->user()->Date == 1)
                                                     <div class="form-group col-lg-2">
                                            <label class="form-label" for="simpleinput">{{trans('admin.Date')}}</label>
                         <input type="date" name="Date" value="{{$item->Date}}" class="form-control" required>
                                        </div>
                                        @else
                                                     <div class="form-group col-lg-2">
                                            <label class="form-label" for="simpleinput">{{trans('admin.Date')}}</label>
                         <input type="date" name="Date" value="{{$item->Date}}" class="form-control" required readonly>
                                        </div>
                                        @endif

                                        @endif
                                        <div class="form-group col-lg-3">
                                            <label class="form-label" for="">{{trans('admin.Store')}}</label>
                                            <select class="select2 form-control w-100" id="store" name="Store" required>
                                                <option value=""> {{trans('admin.Store')}}</option>
                                            @foreach($Stores as $store)
     <option value="{{$store->id}}" @if($item->Store == $store->id) selected  @endif>         {{app()->getLocale() == 'ar' ?$store->Name :$store->NameEn}} </option>
                                            @endforeach
                                            </select>
                                        </div>
                                        <div class="form-group col-lg-3">
                                            <label class="form-label" for="">{{trans('admin.Coin')}}</label>
                                            <select class="select2 form-control w-100" name="Coin" required>
                                                 <option value=""> {{trans('admin.Coin')}}</option>
                                            @foreach($Coins as $coin)
    <option value="{{$coin->id}}" @if($item->Coin == $coin->id) selected  @endif>    {{app()->getLocale() == 'ar' ?$coin->Arabic_Name :$coin->English_Name}}  </option>
                                            @endforeach
                                            </select>
                                        </div>
                                        <div class="form-group col-lg-2">
                                            <label class="form-label" for="simpleinput">{{trans('admin.Draw')}}</label>
                        <input type="number" step="any" value="{{$item->Draw}}" name="Draw" class="form-control" required>
                                        </div>
                                        <div class="form-group col-lg-12">
                                            <label class="form-label" for="simpleinput">{{trans('admin.Notes')}}</label>
                              <input type="text" name="Note" value="{{$item->Note}}" class="form-control">
                                        </div>
                                    </div>

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

                    <div class="col-xl-12">
                        <div id="panel-1" class="panel">
                            <div class="form-group col-lg-12">
                                <label class="form-label" for="simpleinput">  </label>
                                <div class="input-items">
           <input type="text" id="search" class="form-control" placeholder="{{trans('admin.Search_For_Products')}}">
                                 @if(app()->getLocale() == 'ar' )
                     <i class="fal fa-barcode-alt" style="position:absolute ;top:5px;left:3px;"></i>
                      @else
                            <i class="fal fa-barcode-alt" style="position:absolute ;top:5px;right: 0;left:auto;"></i>
                      @endif
                                </div>
                             </div>
                            <div class="panel-container show">
                                <div class="panel-content">
                    <table  class="table table-bordered table-hover table-striped w-100">
                                        <thead>
                                            <tr>
                                           <th>{{trans('admin.Name')}}</th>
                                            <th>{{trans('admin.Units')}}</th>
                                            <th>{{trans('admin.Barcode')}}</th>
                                            <th>{{trans('admin.Qty')}} </th>
                                            <th>{{trans('admin.Price')}}</th>
                                            <th>{{trans('admin.Total')}}</th>
                                            <th>{{trans('admin.Expire_Date')}}</th>
                                            <th>{{trans('admin.Actions')}}</th>
                                            </tr>
                                        </thead>
                                        <tbody class="Data">

                                        </tbody>
                                    </table>
                                    <!-- datatable start -->
                 <table id="dt" class="table table-bordered table-hover table-striped w-100">
                                        <thead>
                                            <tr>
                                           <th>{{trans('admin.Name')}}</th>
                                            <th>{{trans('admin.Units')}}</th>
                                            <th>{{trans('admin.Barcode')}}</th>
                                                @if($show->Patch_Number == 1)
                                                    <th>{{trans('admin.Patch_Number')}}</th>
                                                @endif
                                            <th>{{trans('admin.Qty')}} </th>
                                            <th>{{trans('admin.Price')}}</th>
                                            <th>{{trans('admin.Total')}}</th>
                                            <th>{{trans('admin.Expire_Date')}}</th>
                                            <th>{{trans('admin.Actions')}}</th>
                                            </tr>
                                        </thead>
                                        <tbody id="data-dt">
                                    @foreach($Pros as $pro)
                                <tr>
                    <td>

                 {{app()->getLocale() == 'ar' ?$pro->Product()->first()->P_Ar_Name :$pro->Product()->first()->P_En_Name}}

                  @if(!empty($pro->V1) and empty($pro->V2))

                          {{app()->getLocale() == 'ar' ?$pro->V1()->first()->Name :$pro->V1()->first()->NameEn}}

                          <input type='hidden' name='V_Name[]' value='    {{app()->getLocale() == 'ar' ?$pro->V1()->first()->Name :$pro->V1()->first()->NameEn}}  '>
                        @elseif(!empty($pro->V1) and !empty($pro->V2))
                     (    {{app()->getLocale() == 'ar' ?$pro->V1()->first()->Name :$pro->V1()->first()->NameEn}}  )  (    {{app()->getLocale() == 'ar' ?$pro->V2()->first()->Name :$pro->V2()->first()->NameEn}}  )
              <input type='hidden' name='V_Name[]' value='    {{app()->getLocale() == 'ar' ?$pro->V1()->first()->Name :$pro->V1()->first()->NameEn}}   "-"     {{app()->getLocale() == 'ar' ?$pro->V2()->first()->Name :$pro->V2()->first()->NameEn}}  '>
            <input type='hidden' name='VV_Name[]' value='    {{app()->getLocale() == 'ar' ?$pro->V1()->first()->Name :$pro->V1()->first()->NameEn}}   -     {{app()->getLocale() == 'ar' ?$pro->V2()->first()->Name :$pro->V2()->first()->NameEn}}  '>
                        @else
                        <input type='hidden' name='V_Name[]' value=''>
                         <input type='hidden' name='VV_Name[]' value=''>
                        @endif

                    <input type='hidden' name='P_Ar_Name[]' value='{{$pro->Product()->first()->P_Ar_Name}}'>
                    <input type='hidden' name='P_En_Name[]' value='{{$pro->Product()->first()->P_En_Name}}'>
             <input type='hidden' name='VOne[]' value='{{$pro->V1}}'>
            <input type='hidden' name='VTwo[]' value='{{$pro->V2}}'>

                    </td>
            <td><input type='hidden' name='Unit[]' value='{{$pro->Unit}}'>
                            {{app()->getLocale() == 'ar' ?$pro->Unit()->first()->Name :$pro->Unit()->first()->NameEn}}
                                    </td>
            <td><input type='hidden' name='P_Code[]' value='{{$pro->P_Code}}'>{{$pro->P_Code}}</td>

                                    @if($show->Patch_Number == 1)
                                        <td><input type='hidden' name='Patch_Number[]' value='{{$pro->Patch_Number}}'>{{$pro->Patch_Number}}</td>
                                    @else
                                        <input type='hidden' name='Patch_Number[]' value='{{$pro->Patch_Number}}'>
                                    @endif

            <td><input class='Qun form-control' type='number' step="any" onkeyup="Change({{$pro->id}})" onclick="Change({{$pro->id}})" name='Qty[]' id="Qty{{$pro->id}}" value='{{$pro->Qty}}' readonly></td>

            <td><input type='number' class="form-control" step="any" id="Price{{$pro->id}}" name='Price[]' value='{{$pro->Price}}' onkeyup="Change({{$pro->id}})" onclick="Change({{$pro->id}})" readonly></td>
            <td>
                <input class='Tot form-control' type='text' disabled  id="Total{{$pro->id}}" value='{{$pro->Total}}'>
                    <input type="hidden" name='Total[]' id="TotalHide{{$pro->id}}" value='{{$pro->Total}}' >
            </td>
            <td><input type='hidden' name='Exp_Date[]' value='{{$pro->Exp_Date}}'>{{$pro->Exp_Date}}</td>
            <td>
            <button id='DelAssem' type='button' class='btn btn-default'><i class='fal fa-trash'></i></button>
            <input id='Product_IDInp{{$pro->id}}' type='hidden' name='Product[]' value='{{$pro->Product}}'>
            <input id='Product_IDInp{{$pro->id}}' type='hidden' name='Product[]' value='{{$pro->Product}}'>
             <input type='hidden' name='Price_Sale[]' value='"{{$pro->Price_Sale}}"'>
            </td>

                                    </tr>

                                    @endforeach
                                        </tbody>
                                    </table>
                                    <!-- datatable end -->
                                    <div class="form-row">
                                        <div class="form-group col-lg-4">
                                            <label class="form-label" for="simpleinput">{{trans('admin.Total_Products')}}</label>
                                <input type="text" id="TotalPrroducts" disabled  class="form-control">
                                   <input type="hidden" id="TotalProductseHide" name="TotalProducts">
                                        </div>
                                        <div class="form-group col-lg-4">
                                            <label class="form-label" for="simpleinput">{{trans('admin.Total_Qty')}} </label>
                                 <input type="text" id="TotalQty" disabled  class="form-control">
                                   <input type="hidden" id="TotalQtyHide" name="TotalQty">
                                        </div>
                                        <div class="form-group col-lg-4">
                                 <label class="form-label" for="simpleinput">{{trans('admin.Total_Price')}}</label>
                                   <input type="text" id="TotalPrice" disabled  class="form-control">
                                   <input type="hidden" id="TotalPriceHide" name="TotalPrice">
                                        </div>
                                    </div>
                                    <div class="buttons mt-3">
   <button type="submit" id="Submit" class="btn btn-primary" style="display: none"> <i class="fal fa-folder"></i> {{trans('admin.Save')}} </button>
                                      </div>
                                </div>
                            </div>
                        </div>
                    </div>



                    </div>


         </form>
                </main>

      @if($show->Patch_Number == 1)
    <input type="hidden" id="Patch" value="1">
    @else
     <input type="hidden" id="Patch" value="0">
     @endif


    @if(app()->getLocale() == 'ar' )
<input type="hidden" id="LANG" value="ar">

@else
<input type="hidden" id="LANG" value="en">
@endif

@endsection

@push('js')

    <link rel="stylesheet" media="screen, print" href="{{asset('Admin/css/datagrid/datatables/datatables.bundle.css')}}">
    <link rel="stylesheet" media="screen, print" href="{{asset('Admin/css/formplugins/summernote/summernote.css')}}">
    <link rel="stylesheet" media="screen, print" href="{{asset('Admin/css/formplugins/select2/select2.bundle.css')}}">
    <script src="{{asset('Admin/js/datagrid/datatables/datatables.bundle.js')}}"></script>
    <script src="{{asset('Admin/js/datagrid/datatables/datatables.export.js')}}"></script>
    <script src="{{asset('Admin/js/formplugins/summernote/summernote.js')}}"></script>
    <script src="{{asset('Admin/js/formplugins/select2/select2.bundle.js')}}"></script>

    <!-- Search Selecet -->
<script>
    $(document).ready(function () {
        $(function () {
            $(".select2").select2();

            $(".select2-placeholder-multiple").select2({
                placeholder: "Select State",
            });
            $(".js-hide-search").select2({
                minimumResultsForSearch: 1 / 0,
            });
            $(".js-max-length").select2({
                maximumSelectionLength: 2,
                placeholder: "Select maximum 2 items",
            });
            $(".select2-placeholder").select2({
                placeholder: "Select a state",
                allowClear: true,
            });

            $(".js-select2-icons").select2({
                minimumResultsForSearch: 1 / 0,
                templateResult: icon,
                templateSelection: icon,
                escapeMarkup: function (elm) {
                    return elm;
                },
            });

            function icon(elm) {
                elm.element;
                return elm.id ? "<i class='" + $(elm.element).data("icon") + " mr-2'></i>" + elm.text : elm.text;
            }




        });
    });
</script>


    <!-- Unit Code and Name -->
    <script>
        function  UnitCodeStart(x){

            var countryId = $('#UnitStart'+x).val();
            var Store = $('#store').val();
            var Pro = $('#Product'+x).val();
            if(countryId) {
                $.ajax({
                    url: 'UnitNameCodeFilter/'+countryId+'/'+Pro,
                    type:"GET",
                    data:{
                        Store:Store,
                    },
                    dataType:"json",
                    beforeSend: function(){
                        $('#loader').css("visibility", "visible");
                    },

                    success:function(data) {
                        $.each(data, function(key, value){

                            $('#CodeStart'+x).val(data.code);
                            $('#UnitStartName'+x).val(data.name);
                            $('#Price'+x).val(data.price);


                        });


                        var Qty = $("#Qty"+x).val();
                        var Price = $("#Price"+x).val();

                        var result = parseFloat(Qty) *  parseFloat(Price) ;
                        $("#Total"+x).val(result);

                        var Total = $("#Total"+x).val();
                        var UnitID = $("#UnitStart"+x).val();


                        if(Qty == ''  || Price == ''  ||  Total == '' || UnitID == ''){

                            document.getElementById("AddBtnPur"+x).style.display = "none";
                        }


                        if(Qty != ''  && Price != ''  &&  Total != '' && UnitID != ''){

                            document.getElementById("AddBtnPur"+x).style.display = "block";
                        }





                    },
                    complete: function(){
                        $('#loader').css("visibility", "hidden");
                    }
                });
            } else {

                $('select[name="state"]').empty();
            }

        }
    </script>

<!--  Filter Products -->
<script>
   $(document).ready(function(){

    fetch_customer_data();

    function fetch_customer_data(search = '',Brand='',Group='')
    {
     var Store = $('#store').val();
     $.ajax({
      url:'StartProductsFilter/'+Store,
      method:'GET',
      data:{search:search,Brand:Brand,Group:Group},
      dataType:'json',
      success:function(data)
      {
       $('.Data').html(data.table_data);
      }
     })
    }

   $(document).on('keyup', '#search', function(){
     var search = $(this).val();
     var Brand = $('#Brandd').val();
     var Group = $('#Groupp').val();
     fetch_customer_data(search,Brand,Group);
    });

    $(document).on('change', '#store', function(){
     var search = $('#search').val();
          var Brand = $('#Brandd').val();
     var Group = $('#Groupp').val();
     fetch_customer_data(search,Brand,Group);
    });

    $(document).on('change', '#Brandd', function(){
     var Brand = $(this).val();
     var search = $('#search').val();
     var Group = $('#Groupp').val();
     fetch_customer_data(search,Brand,Group);
    });

    $(document).on('change', '#Groupp', function(){
     var Group = $(this).val();
     var Brand = $('#Brandd').val();
     var search = $('#search').val();
     fetch_customer_data(search,Brand,Group);
    });

   });
</script>
<!-- Add Products -->
<script>
   function Fun(r) {

             var P_Ar_Name = $("#P_Ar_Name"+r).val();
             var P_En_Name= $("#P_En_Name"+r).val();
             var Product = $("#Product"+r).val();
             var UnitID = $("#UnitStart"+r).val();
             var UnitName = $("#UnitStartName"+r).val();
             var Qty = $("#Qty"+r).val();
             var Barcode = $("#CodeStart"+r).val();
             var Price = $("#Price"+r).val();
             var Total = $("#Total"+r).val();
             var ExpDate = $("#ExpDate"+r).val();
             var Patch = $("#Patch").val();
    var Price_Sale = $("#Price_Sale"+r).val();

             document.getElementById("AddBtnPur"+r).style.display = "none";
             document.getElementById("Row"+r).style.display = "none";


       if(parseFloat(Patch) == 1){

           var pn="<td><input type='number' step='any' class='form-control' name='Patch_Number[]'></td>";
       }else{
           var pn="<input type='hidden'  name='Patch_Number[]'>";

       }

               var LANG = $("#LANG").val();
   if(LANG == 'ar' ){
          var Nemo = P_Ar_Name ;
          }else{
             var Nemo = P_En_Name ;
          }


             var markup = "<tr><td><input type='hidden' name='P_Ar_Name[]' value='"+P_Ar_Name+"'><input type='hidden' name='P_En_Name[]' value='"+P_En_Name+"'>" + Nemo + "</td><td><input type='hidden' name='Unit[]' value='"+UnitID+"'>" + UnitName + "</td><td><input type='hidden' name='P_Code[]' value='"+Barcode+"'>" + Barcode + "</td>"+pn+"<td><input class='Qun' type='hidden' name='Qty[]' value='"+Qty+"'>" + Qty + "</td><td><input type='hidden' name='Price[]' value='"+Price+"'>" + Price + "</td><td><input class='Tot' type='hidden' name='Total[]' value='"+Total+"'>" + Total + "</td><td><input type='hidden' name='Exp_Date[]' value='"+ExpDate+"'>" + ExpDate + "</td><td><button id='DelAssem' type='button' class='btn btn-default'><i class='fal fa-trash'></i></button><input id='Product_IDInp"+r+"' type='hidden' name='Product[]' value='"+Product+"'><input type='hidden' name='VOne[]' value=''><input type='hidden' name='VTwo[]' value=''></td><input type='hidden' name='V_Name[]' value=''><input type='hidden' name='VV_Name[]' value=''><input type='hidden' name='Price_Sale[]' value='"+Price_Sale+"'></tr>";


            var  Product_IDInp =$("#Product_IDInp"+r).val();

         if(Product != Product_IDInp){
             $("#data-dt").append(markup);
         }

      $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };

   var rowctr = $('#dt').rowCount();

    var sumQ = 0;
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });

    var sumT = 0;
   $('.Tot').each(function(){
   sumT += parseFloat($(this).val());
   });

   $('#TotalPrroducts').val(parseFloat(rowctr));
   $('#TotalProductseHide').val(parseFloat(rowctr));

   $('#TotalQty').val(parseFloat(sumQ));
   $('#TotalQtyHide').val(parseFloat(sumQ));

   $('#TotalPrice').val(parseFloat(sumT));
   $('#TotalPriceHide').val(parseFloat(sumT));


       if(rowctr == 0){
        document.getElementById("Submit").style.display = "none";

       }else{

        document.getElementById("Submit").style.display = "block";
       }



        $('#data-dt').on('click', '#DelAssem', function(e){
                $(this).closest('tr').remove();


    var sumQ = 0;
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });

    var sumT = 0;
   $('.Tot').each(function(){
   sumT += parseFloat($(this).val());
   });

     $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };

   var rowctr = $('#dt').rowCount();


   $('#TotalPrroducts').val(parseFloat(rowctr));
   $('#TotalProductseHide').val(parseFloat(rowctr));

   $('#TotalQty').val(parseFloat(sumQ));
   $('#TotalQtyHide').val(parseFloat(sumQ));

   $('#TotalPrice').val(parseFloat(sumT));
   $('#TotalPriceHide').val(parseFloat(sumT));



         if(rowctr == 0){
        document.getElementById("Submit").style.display = "none";

       }else{

        document.getElementById("Submit").style.display = "block";
       }

                    })


     }
</script>

<!-- Add Vira Two -->
<script>
   function FunVV(r) {
                      $.ajax({
                              url: 'ViraName/'+r,
                              type:"GET",
                              dataType:"json",
                              beforeSend: function(){
                                  $('#loader').css("visibility", "visible");
                              },

                              success:function(data) {
                                  $.each(data, function(key,value){


            var P_Ar_Name = $("#P_Ar_Name"+r).val();
             var P_En_Name= $("#P_En_Name"+r).val();
             var Product = $("#Product"+r).val();
             var UnitID = $("#UnitStart"+r).val();
             var UnitName = $("#UnitStartName"+r).val();
             var Barcode = Math.floor(Math.random() * 10000);
             var Price = $("#Price"+r).val();
             var Total = $("#Total"+r).val();
             var ExpDate = $("#ExpDate"+r).val();
              var Qty = $("#Qty"+r).val();
                         var Patch = $("#Patch").val();
                                       var Price_Sale = $("#Price_Sale"+r).val();




    if(parseFloat(Patch) == 1){

           var pn="<td><input type='hidden'  name='Patch_Number[]'></td>";
       }else{
           var pn="<input type='hidden'  name='Patch_Number[]'>";

       }

                  var LANG = $("#LANG").val();
   if(LANG == 'ar' ){
          var Nemo = P_Ar_Name ;
           var PNNN='سعر البيع';
          }else{
             var Nemo = P_En_Name ;
                        var PNNN='Sales Price';
          }


                  document.getElementById("AddBtnPur"+r).style.display = "none";
             document.getElementById("Row"+r).style.display = "none";

         var markup = "<tr><td><input type='hidden' name='P_Ar_Name[]' value='"+P_Ar_Name+"'><input type='hidden' name='P_En_Name[]' value='"+P_En_Name+"'>" + Nemo + "("+value+")</td><td><input type='hidden' name='Unit[]' value='"+UnitID+"'>" + UnitName + "<br><label>"+PNNN+"</label><input type='number' name='Price_Sale[]' value='"+Price_Sale+"'></td><td><input type='text' name='P_Code[]' value='"+Barcode+"' class='form-control'></td>"+pn+"<td><input class='Qun  form-control' type='number' id='QuntityVV"+key+"' value='"+Qty+"' step='any' name='Qty[]' onkeyup='TotVV("+key+")' onclick='TotVV("+key+")'></td><td><input type='number' name='Price[]' value='"+Price+"' onkeyup='TotVV("+key+")' onclick='TotVV("+key+")'  id='PriceVV"+key+"' step='any' class='form-control' ></td><td><input class='Tot form-control' type='text' id='TotalVV"+key+"' value='"+Total+"'  disabled><input type='hidden' id='TotalVVHide"+key+"' name='Total[]' value='"+Total+"'></td><td><input type='hidden' name='Exp_Date[]' value='"+ExpDate+"'>" + ExpDate + "</td><td><button id='DelAssem' type='button' class='btn btn-default'><i class='fal fa-trash'></i></button><input id='Product_IDInp"+r+"' type='hidden' name='Product[]' value='"+Product+"'><input type='hidden' name='VOne[]'><input type='hidden'  name='VTwo[]' ><input type='hidden' name='V_Name[]' value='"+value+"'><input type='hidden' name='VV_Name[]' value='"+value+"'></td></tr>";


             $("#data-dt").append(markup);


         $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };

   var rowctr = $('#dt').rowCount();

    var sumQ = 0;
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });

    var sumT = 0;
   $('.Tot').each(function(){
   sumT += parseFloat($(this).val());
   });

   $('#TotalPrroducts').val(parseFloat(rowctr));
   $('#TotalProductseHide').val(parseFloat(rowctr));

   $('#TotalQty').val(parseFloat(sumQ));
   $('#TotalQtyHide').val(parseFloat(sumQ));

   $('#TotalPrice').val(parseFloat(sumT));
   $('#TotalPriceHide').val(parseFloat(sumT));


       if(rowctr == 0){
        document.getElementById("Submit").style.display = "none";

       }else{

        document.getElementById("Submit").style.display = "block";
       }

                                  });
                              },
                              complete: function(){
                                  $('#loader').css("visibility", "hidden");
                              }
                          });


        $('#data-dt').on('click', '#DelAssem', function(e){
                $(this).closest('tr').remove();


    var sumQ = 0;
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });

    var sumT = 0;
   $('.Tot').each(function(){
   sumT += parseFloat($(this).val());
   });

     $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };

   var rowctr = $('#dt').rowCount();


   $('#TotalPrroducts').val(parseFloat(rowctr));
   $('#TotalProductseHide').val(parseFloat(rowctr));

   $('#TotalQty').val(parseFloat(sumQ));
   $('#TotalQtyHide').val(parseFloat(sumQ));

   $('#TotalPrice').val(parseFloat(sumT));
   $('#TotalPriceHide').val(parseFloat(sumT));



         if(rowctr == 0){
        document.getElementById("Submit").style.display = "none";

       }else{

        document.getElementById("Submit").style.display = "block";
       }

                    })


     }
</script>
<!-- Total Vira Two -->
<script>
   function TotVV(r) {
   var Qty = $("#QuntityVV"+r).val();
   var Price = $("#PriceVV"+r).val();

   var result = parseFloat(Qty) *  parseFloat(Price) ;
     $("#TotalVV"+r).val(result);
     $("#TotalVVHide"+r).val(result);


           $.fn.rowCount = function() {
   return $('tr', $(this).find('tbody')).length;
   };

   var rowctr = $('#dt').rowCount();

   var sumQ = 0;
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });

   var sumT = 0;
   $('.Tot').each(function(){
   sumT += parseFloat($(this).val());
   });

   $('#TotalPrroducts').val(parseFloat(rowctr));
   $('#TotalProductseHide').val(parseFloat(rowctr));

   $('#TotalQty').val(parseFloat(sumQ));
   $('#TotalQtyHide').val(parseFloat(sumQ));

   $('#TotalPrice').val(parseFloat(sumT));
   $('#TotalPriceHide').val(parseFloat(sumT));


     if(rowctr == 0){
      document.getElementById("Submit").style.display = "none";

     }else{

      document.getElementById("Submit").style.display = "block";
     }






   }


</script>
<!-- Add Vira One -->
<script>
   function FunV(r) {

                      $.ajax({
                              url: 'ViraFilter/'+r,
                              type:"GET",
                              dataType:"json",
                              beforeSend: function(){
                                  $('#loader').css("visibility", "visible");
                              },

                              success:function(data) {
                                  $.each(data, function(key, value){


             var P_Ar_Name = $("#P_Ar_Name"+r).val();
             var P_En_Name= $("#P_En_Name"+r).val();
             var Product = $("#Product"+r).val();
             var UnitID = $("#UnitStart"+r).val();
             var UnitName = $("#UnitStartName"+r).val();
             var Barcode = Math.floor(Math.random() * 10000);
             var Price = $("#Price"+r).val();
             var Total = $("#Total"+r).val();
             var ExpDate = $("#ExpDate"+r).val();
              var Qty = $("#Qty"+r).val();
                                       var Price_Sale = $("#Price_Sale"+r).val();




                                                 var Patch = $("#Patch").val();
    if(parseFloat(Patch) == 1){

           var pn="<td><input type='hidden'  name='Patch_Number[]'></td>";
       }else{
           var pn="<input type='hidden'  name='Patch_Number[]'>";

       }

                             var LANG = $("#LANG").val();
   if(LANG == 'ar' ){
          var Nemo = P_Ar_Name ;
       var PNNN='سعر البيع';
          }else{
             var Nemo = P_En_Name ;
               var PNNN='Sales Price';
          }

                  document.getElementById("AddBtnPur"+r).style.display = "none";
             document.getElementById("Row"+r).style.display = "none";

         var markup = "<tr><td><input type='hidden' name='P_Ar_Name[]' value='"+P_Ar_Name+"'><input type='hidden' name='P_En_Name[]' value='"+P_En_Name+"'>" + Nemo + "("+value+")</td><td><input type='hidden' name='Unit[]' value='"+UnitID+"'>" + UnitName + "<br><label>"+PNNN+"</label><input type='number' name='Price_Sale[]' value='"+Price_Sale+"'></td><td><input type='text' name='P_Code[]' value='"+Barcode+"' class='form-control'></td>"+pn+"<td><input class='Qun  form-control' type='number' id='QuntityV"+key+"' value='"+Qty+"' step='any' name='Qty[]' onkeyup='TotV("+key+")' onclick='TotV("+key+")'></td><td><input type='number' name='Price[]' value='"+Price+"' onkeyup='TotV("+key+")' onclick='TotV("+key+")'  id='PriceV"+key+"' step='any' class='form-control' ></td><td><input class='Tot form-control' type='text' id='TotalV"+key+"' value='"+Total+"'  disabled><input type='hidden' id='TotalVHide"+key+"' name='Total[]' value='"+Total+"'></td><td><input type='hidden' name='Exp_Date[]' value='"+ExpDate+"'>" + ExpDate + "</td><td><button id='DelAssem' type='button' class='btn btn-default'><i class='fal fa-trash'></i></button><input id='Product_IDInp"+r+"' type='hidden' name='Product[]' value='"+Product+"'><input type='hidden' name='VOne[]' value='"+key+"'><input type='hidden' name='VTwo[]' value=''><input type='hidden' name='V_Name[]' value='"+value+"'><input type='hidden' name='VV_Name[]' value=''></td></tr>";

             $("#data-dt").append(markup);



         $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };

   var rowctr = $('#dt').rowCount();

    var sumQ = 0;
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });

    var sumT = 0;
   $('.Tot').each(function(){
   sumT += parseFloat($(this).val());
   });

   $('#TotalPrroducts').val(parseFloat(rowctr));
   $('#TotalProductseHide').val(parseFloat(rowctr));

   $('#TotalQty').val(parseFloat(sumQ));
   $('#TotalQtyHide').val(parseFloat(sumQ));

   $('#TotalPrice').val(parseFloat(sumT));
   $('#TotalPriceHide').val(parseFloat(sumT));


       if(rowctr == 0){
        document.getElementById("Submit").style.display = "none";

       }else{

        document.getElementById("Submit").style.display = "block";
       }



                                  });
                              },
                              complete: function(){
                                  $('#loader').css("visibility", "hidden");
                              }
                          });


        $('#data-dt').on('click', '#DelAssem', function(e){
                $(this).closest('tr').remove();


    var sumQ = 0;
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });

    var sumT = 0;
   $('.Tot').each(function(){
   sumT += parseFloat($(this).val());
   });

     $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };

   var rowctr = $('#dt').rowCount();


   $('#TotalPrroducts').val(parseFloat(rowctr));
   $('#TotalProductseHide').val(parseFloat(rowctr));

   $('#TotalQty').val(parseFloat(sumQ));
   $('#TotalQtyHide').val(parseFloat(sumQ));

   $('#TotalPrice').val(parseFloat(sumT));
   $('#TotalPriceHide').val(parseFloat(sumT));



         if(rowctr == 0){
        document.getElementById("Submit").style.display = "none";

       }else{

        document.getElementById("Submit").style.display = "block";
       }

                    })


     }
</script>
<!-- Total Vira One -->
<script>
   function TotV(r) {
   var Qty = $("#QuntityV"+r).val();
   var Price = $("#PriceV"+r).val();

   var result = parseFloat(Qty) *  parseFloat(Price) ;
     $("#TotalV"+r).val(result);
     $("#TotalVHide"+r).val(result);


           $.fn.rowCount = function() {
   return $('tr', $(this).find('tbody')).length;
   };

   var rowctr = $('#dt').rowCount();

   var sumQ = 0;
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });

   var sumT = 0;
   $('.Tot').each(function(){
   sumT += parseFloat($(this).val());
   });

   $('#TotalPrroducts').val(parseFloat(rowctr));
   $('#TotalProductseHide').val(parseFloat(rowctr));

   $('#TotalQty').val(parseFloat(sumQ));
   $('#TotalQtyHide').val(parseFloat(sumQ));

   $('#TotalPrice').val(parseFloat(sumT));
   $('#TotalPriceHide').val(parseFloat(sumT));


     if(rowctr == 0){
      document.getElementById("Submit").style.display = "none";

     }else{

      document.getElementById("Submit").style.display = "block";
     }






   }


</script>
<!-- Total Products -->
<script>
   function StartTotal(r) {
   var Qty = $("#Qty"+r).val();
   var Price = $("#Price"+r).val();

    var result = parseFloat(Qty) *  parseFloat(Price) ;
       $("#Total"+r).val(result);


        var Total = $("#Total"+r).val();
        var UnitID = $("#UnitStart"+r).val();






       if(Qty == ''  || Price == ''  ||  Total == '' || UnitID == ''){

        document.getElementById("AddBtnPur"+r).style.display = "none";
     }


       if(Qty != ''  && Price != ''  &&  Total != '' && UnitID != ''){

               if( Price == 0 ){

        document.getElementById("AddBtnPur"+r).style.display = "none";
     }else{

        document.getElementById("AddBtnPur"+r).style.display = "block";
     }
     }





   }
</script>
<script>
   $(document).on('change', '#store', function(){

       $("#dt").find("tr:gt(0)").remove();


     });
</script>

<!-- Add Serial -->
<script>
   function FunSerial(r) {

             var P_Ar_Name = $("#P_Ar_Name"+r).val();
             var P_En_Name= $("#P_En_Name"+r).val();
             var Product = $("#Product"+r).val();
             var UnitID = $("#UnitStart"+r).val();
             var UnitName = $("#UnitStartName"+r).val();
             var Qty = $("#Qty"+r).val();
             var Barcode = $("#CodeStart"+r).val();
             var Price = $("#Price"+r).val();
             var Total = $("#Total"+r).val();
             var ExpDate = $("#ExpDate"+r).val();
       var Price_Sale = $("#Price_Sale"+r).val();

              var i=0;
             document.getElementById("AddBtnPur"+r).style.display = "none";
             document.getElementById("Row"+r).style.display = "none";

              var Patch = $("#Patch").val();
    if(parseFloat(Patch) == 1){

           var pn="<td></td>";
       }else{
           var pn="";

       }
                           var LANG = $("#LANG").val();
   if(LANG == 'ar' ){
          var Nemo = P_Ar_Name ;
          }else{
             var Nemo = P_En_Name ;
          }


                   var markup = "<tr><td>" + Nemo + "("+Qty+")</td><td>" + UnitName + "</td><td><div class='row'>";

            for(i ; i < Qty ; i++){
       markup += "<div class='col-md-12'><input type='text' onkeyup='SERIAL("+Qty+")' onclick='SERIAL("+Qty+","+Product+")' onkeypress='SERIAL("+Qty+","+Product+")' id='SRCODE"+i+Product+"' name='P_Code[]'  class='form-control inputs'><input type='hidden' name='P_Ar_Name[]' value='"+P_Ar_Name+"'><input type='hidden' name='P_En_Name[]' value='"+P_En_Name+"'><input type='hidden' name='Unit[]' value='"+UnitID+"'><input class='Qun' type='hidden' name='Qty[]' value='1'><input type='hidden' name='Price[]' value='"+Price+"'><input class='Tot' type='hidden' name='Total[]' value='"+Price+"'><input type='hidden' name='Exp_Date[]' value='"+ExpDate+"'><input id='Product_IDInp"+r+"' type='hidden' name='Product[]' value='"+Product+"'><input type='hidden' name='VOne[]' value=''><input type='hidden' name='VTwo[]' value=''><input type='hidden' name='V_Name[]' value=''><input type='hidden' name='VV_Name[]' value=''><input type='hidden'  name='Patch_Number[]'><input type='hidden' name='Price_Sale[]' value='"+Price_Sale+"'></div>";
               }

            markup += ""+pn+"</div><td>1</td><td>" + Price + "</td><td>" + Price + "</td><td>" + ExpDate + "</td><td><button id='DelAssem' type='button' class='btn btn-default'><i class='fal fa-trash'></i></button></td></tr>";

             $("#data-dt").append(markup);



      $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };

   var rowctr = $('#dt').rowCount();

    var sumQ = 0;
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });

    var sumT = 0;
   $('.Tot').each(function(){
   sumT += parseFloat($(this).val());
   });

   $('#TotalPrroducts').val(parseFloat(rowctr));
   $('#TotalProductseHide').val(parseFloat(rowctr));

   $('#TotalQty').val(parseFloat(sumQ));
   $('#TotalQtyHide').val(parseFloat(sumQ));

   $('#TotalPrice').val(parseFloat(sumT));
   $('#TotalPriceHide').val(parseFloat(sumT));


       if(rowctr == 0){
        document.getElementById("Submit").style.display = "none";

       }else{

        document.getElementById("Submit").style.display = "block";
       }



        $('#data-dt').on('click', '#DelAssem', function(e){
                $(this).closest('tr').remove();


    var sumQ = 0;
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });

    var sumT = 0;
   $('.Tot').each(function(){
   sumT += parseFloat($(this).val());
   });

     $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };

   var rowctr = $('#dt').rowCount();


   $('#TotalPrroducts').val(parseFloat(rowctr));
   $('#TotalProductseHide').val(parseFloat(rowctr));

   $('#TotalQty').val(parseFloat(sumQ));
   $('#TotalQtyHide').val(parseFloat(sumQ));

   $('#TotalPrice').val(parseFloat(sumT));
   $('#TotalPriceHide').val(parseFloat(sumT));



         if(rowctr == 0){
        document.getElementById("Submit").style.display = "none";

       }else{

        document.getElementById("Submit").style.display = "block";
       }

                    })


     }
</script>


<!-- Duplicate Serial -->
<script>
 function SERIAL(r,p){

          var i=0;
          var arr=[];

for(i ; i < r ; i++){
    var val= $('#SRCODE'+i+p).val();
    document.getElementById('SRCODE'+i+p).addEventListener('keypress', function(event) {
        if (event.keyCode == 13) {
            event.preventDefault();
        }
    });


        arr.push(val);
 }

     var encounteredIndices = {};
     for(var i = 0; i < arr.length; i++)
  if (encounteredIndices[arr[i]]){
    console.log(i);
     document.getElementById('SRCODE'+i+p).style.border="none";
         document.getElementById("Submit").style.display = "none";
  }else{
     encounteredIndices[arr[i]] = 1;
       document.getElementById('SRCODE'+i).style.border="none";
         document.getElementById("Submit").style.display = "block";
  }

 }
</script>
<!-- Enter Next Input -->
<script>
$(function () {
    $(document).on('keyup', '.inputs', function (e) {
        if (e.which == 13) {
            var $this = $(this);
            var $td = $this.closest('.col-md-12'); // Current TD
            var $row = $td.closest('.row'); // Current TR
            var $rows = $row.parent(); // Current TABLE or TBODY - parent of all rows
            var column = $td.index(); // Current column of TD

            // Search on a row basis in current column, then try next column
            // repeat until we run out of cells
            while ($td.length) {
                // get next row
                $row = $row.next('.row');
                // If we were on last row
                if ($row.length == 0) {
                    // Go back to first row
                    $row = $rows.children().first();
                    // And use next column
                    column++;
                }
                // get the position in the row column - if it exists
                $td = $row.children().eq(column);
                var $input = $td.find('.inputs');
                if ($input.length) {
                    $input.focus();
                    break;
                }
            }
        }
    });
});
</script>

<!-- Scanner  problem -->
<script>

      $(".form-control").keypress(function(event){
   if (event.which == '13') {
   event.preventDefault();
   }
   });

</script>


     <script>


         $(document).ready(function () {
             $.fn.rowCount = function () {
                 return $('tr', $(this).find('tbody')).length;
             };

             var rowctr = $('#dt').rowCount();

             var sumQ = 0;
             $('.Qun').each(function () {
                 sumQ += parseFloat($(this).val());
             });

             var sumT = 0;
             $('.Tot').each(function () {
                 sumT += parseFloat($(this).val());
             });

             $('#TotalPrroducts').val(parseFloat(rowctr));
             $('#TotalProductseHide').val(parseFloat(rowctr));

             $('#TotalQty').val(parseFloat(sumQ));
             $('#TotalQtyHide').val(parseFloat(sumQ));

             $('#TotalPrice').val(parseFloat(sumT));
             $('#TotalPriceHide').val(parseFloat(sumT));


             if (rowctr == 0) {
                 document.getElementById("Submit").style.display = "none";

             } else {

                 document.getElementById("Submit").style.display = "block";
             }

         });

         $('#data-dt').on('click', '#DelAssem', function(e){
                $(this).closest('tr').remove();


    var sumQ = 0;
$('.Qun').each(function(){
  sumQ += parseFloat($(this).val());
});

    var sumT = 0;
$('.Tot').each(function(){
  sumT += parseFloat($(this).val());
});

     $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };

  var rowctr = $('#dt').rowCount();


 $('#TotalPrroducts').val(parseFloat(rowctr));
 $('#TotalProductseHide').val(parseFloat(rowctr));

 $('#TotalQty').val(parseFloat(sumQ));
 $('#TotalQtyHide').val(parseFloat(sumQ));

 $('#TotalPrice').val(parseFloat(sumT));
 $('#TotalPriceHide').val(parseFloat(sumT));



         if(rowctr == 0){
        document.getElementById("Submit").style.display = "none";

       }else{

        document.getElementById("Submit").style.display = "block";
       }

                    })
</script>

@endpush

