@extends('admin.index')
@section('content')

  <title>{{trans('admin.Edit_Secretariat_Import_goods')}}</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.Secretariat')}} </a></li>
                        <li class="breadcrumb-item active">{{trans('admin.Edit_Secretariat_Import_goods')}} </li>
                        <li class="position-absolute pos-top pos-right d-none d-sm-block"><span
                                class="js-get-date"></span></li>
                    </ol>
        
    <form id="form" action="{{url('PostEditSecretariat_Import_goods')}}" method="post" enctype="multipart/form-data">
        {!! csrf_field() !!}
               @honeypot
        <input type="hidden" name="ID" value="{{$item->id}}">
        <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_Secretariat_Import_goods')}}  </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-3">
                                            <label class="form-label" for="simpleinput">{{trans('admin.Code')}} </label>
                                            <input type="text" value="{{$item->Code}}" class="form-control " disabled>
                                            <input type="hidden" name="Code" value="{{$item->Code}}" class="form-control">
                                        </div>
                                        <div class="form-group col-lg-3">
                                            <label class="form-label" for="simpleinput">{{trans('admin.Date')}}</label>
                           <input type="date" name="Date" value="{{$item->Date}}" class="form-control" required>
                                        </div>
                      
                                        
                                        <div class="form-group col-lg-4">
                                            <label class="form-label" for="">{{trans('admin.Account')}}</label>
                                      <select  class="select2 form-control w-100" id="vendor" name="Account" required>
                                          @foreach($Accounts as $account)
              <option value="{{$account->id}}" @if($item->Account == $account->id) selected @endif>       {{app()->getLocale() == 'ar' ?$account->Name :$account->NameEn}}</option>
                                          @endforeach
                                            </select> 
                                        </div>
                                        <div class="form-group col-lg-2">
                                 <label class="form-label" for="simpleinput"> {{trans('admin.Account_Credit')}} </label>
                             <input type="text" id="AccountCredit" value="0" class="form-control " disabled>
                                        </div>
                                        
                                                                      <div class="form-group col-lg-4">
                                            <label class="form-label" for="">{{trans('admin.Store')}}</label>
                                            <select class="select2 form-control w-100" id="store" name="Store" required>
                                            @foreach($Stores as $stor)    
                                                <option value="{{$stor->id}}" @if($item->Store == $stor->id) selected @endif>
                                             {{app()->getLocale() == 'ar' ?$stor->Arabic_Name :$stor->English_Name}}   
                                                </option>
                                            @endforeach    
                                            </select>
                                        </div>
                 
                                 <div class="form-group col-lg-1 col-4">
                  <button style="margin: 22px" type="button" class="btn btn-default" data-toggle="modal" data-target="#NewClient">
                                            <i class="fal fa-pen"></i>          
                                     </button> 
                                             
                                       
                               <!-- Modal New Product -->
            <div class="modal fade" id="NewClient" tabindex="-1" role="dialog" aria-hidden="true">
                        <div class="modal-dialog modal-dialog-centered modal-xl" role="document">              
                            <div class="modal-content">
                                <div class="modal-header">
                                   
                                    <h4 class="modal-body row">
                          
                      
                                             <div class="form-group col-md-6">
                                       <label class="form-label" for=""> 
                                       {{trans('admin.Product_Type')}}    
                                       </label><span class="strick">*</span>
                                       <select class="select2 form-control w-100" id="P_Type"  >
                                          <option value="Completed">{{trans('admin.Completed')}} </option>
                                          <option value="Raw">{{trans('admin.Raw')}} </option>
                                          <option value="Service">{{trans('admin.Service')}}</option>
                                          <option value="Subscribe">{{trans('admin.Subscribe')}}</option>
                                          <option value="Industrial">{{trans('admin.Industrial')}}</option>
                                          <option value="Serial">{{trans('admin.Serial')}}</option>
                                       </select>
                                    </div>
                                           <div class="form-group col-md-6">
                                       <label class="form-label" for="">  {{trans('admin.Brand')}} </label>
                                       <select class="select2 form-control w-100" id="Brand">
                                          @foreach($Brands as $brand)
                                          <option value="{{$brand->id}}">
                                               {{app()->getLocale() == 'ar' ?$brand->Name :$brand->NameEn}}      
                                           </option>
                                          @endforeach
                                       </select>
                                    </div>
                                <div class="form-group col-md-4">
                                       <label class="form-label" for="simpleinput"> {{trans('admin.Product_Ar_Name')}} 
                                       </label><span class="strick">*</span>
                <input type="text" id="P_Ar_Name"  placeholder="{{trans('admin.Product_Ar_Name')}} "  class="form-control" >
                                    </div>
                                    <div class="form-group col-md-4">
                                       <label class="form-label" for="simpleinput">  {{trans('admin.Product_En_Name')}}</label>
                                       <input type="text" id="P_En_Name" placeholder="{{trans('admin.Product_En_Name')}} "  class="form-control">
                                    </div>
                                   <div class="form-group col-md-4">
                                       <label class="form-label" for="">  {{trans('admin.Group')}}  </label><span class="strick">*</span>
                                       <select class="select2 form-control w-100" id="Group" >
                                          @foreach($ItemsGroups as $group)
                 <option value="{{$group->id}}">
                                           
                                                        {{app()->getLocale() == 'ar' ?$group->Name :$group->NameEn}}   
                                           </option>
                                          @endforeach
                                       </select>
                                    </div>
                                        
                                                     <div class="form-group col-md-4">
                                       <label class="form-label" for="">  {{trans('admin.Unit')}}  </label><span class="strick">*</span>
                          <select class="select2 form-control w-100" id="unit" >
                                                                  @foreach($Units as $uni)
                              <option value="{{$uni->id}}">
                              
                                    {{app()->getLocale() == 'ar' ?$uni->Name :$uni->NameEn}}  
                              </option>
                                                                  @endforeach
                                                               </select>
                                    </div>
                                        
                                          <div class="form-group col-md-4">
                                      <label class="form-label" for=""> 
                                       {{trans('admin.Rate')}}    
                                       </label><span class="strick">*</span>                  
                  <input type="number" step="any" id="Rate" class="form-control"  value="1" placeholder="{{trans('admin.Rate')}} ">
                                                            </div>
                                        
                                    <div class="form-group col-md-4" style="position:relative;">
                                  <label class="form-label" for=""> 
                                       {{trans('admin.Barcode')}}    
                                       </label><span class="strick">*</span>                
                                     <input type="text" id="Barcode" class="form-control" placeholder="باركود">
                                                          
                                                            </div>
                                                            <div class="form-group col-md-4">
                                                      <label class="form-label" for=""> 
                                 {{trans('admin.Price_One')}} 
                                       </label><span class="strick">*</span>                    
                  <input type="number" step="any" id="Price" class="form-control"placeholder="{{trans('admin.Price_One')}} " value="0" >
                                                            </div>
                                                             <div class="form-group col-md-4">
                                                                          <label class="form-label" for=""> 
                                 {{trans('admin.Price_Two')}} 
                                       </label><span class="strick">*</span>                    
            <input type="number" step="any" id="Price_Two" class="form-control" placeholder="{{trans('admin.Price_Two')}} " value="0" >
                                                            </div>
                                                            <div class="form-group col-md-4">
                                                                  <label class="form-label" for=""> 
                                 {{trans('admin.Price_Three')}} 
                                       </label><span class="strick">*</span>                           
              <input type="number" step="any" id="Price_Three" class="form-control" placeholder=" {{trans('admin.Price_Three')}} "  value="0">
                                                            </div>
      
        
                                    </h4>
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true"><i class="fal fa-times"></i></span>
                                    </button>
                                </div>
                                
                                <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-dismiss="modal"> {{trans('admin.No')}}</button>
                   <button type="button" onclick="NewProducts()"  class="btn btn-primary"> {{trans('admin.AddNew')}}</button>
                                </div>
                                
                            </div>
                        </div>
                    </div>
                                     
                                             
                                        </div>  
                                

                                        <div class="form-group col-lg-4">
                                            <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="row"> 
                            <div class="form-group col-lg-12">
                                <label class="form-label" for="simpleinput">  </label>
                                <div class="input-items" style="position:relative;">
                 <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>
                            <div class="panel-container show">
                                <div class="panel-content">
                                      <div id="mobile-overflow">
                                    <table 
                                    class="table table-bordered table-hover table-striped w-100 mobile-width-more">
                                    <thead>
                                        <tr>
                                            <th>{{trans('admin.Name')}}</th>
                                            <th>{{trans('admin.Unit')}}</th>
                                            <th>{{trans('admin.Code')}}</th>
                                            <th>{{trans('admin.Qty')}}</th>
                                            <th>{{trans('admin.Store')}}</th>
                                            <th>{{trans('admin.Actions')}}</th>
                                        </tr>
                                    </thead>
                                    <tbody class="Data" id="Data">
                                       
                                    </tbody>
                                    </table>
                                    </div>
                                    <!-- datatable start -->
                                      <div id="mobile-overflow">
                                    <table id="dt"
                                        class="table table-bordered table-hover table-striped w-100 mobile-width-more">
                                        <thead>
                                            <tr>
                                            <th>{{trans('admin.Name')}}</th>
                                            <th>{{trans('admin.Unit')}}</th>
                                            <th>{{trans('admin.Code')}}</th>
                                            <th>{{trans('admin.Qty')}}</th>    
                                            <th>{{trans('admin.Store')}}</th>
                                            <th>{{trans('admin.Actions')}}</th>
                                            </tr>
                                        </thead>
                                        <tbody id="data-dt">
                                         @foreach($Products as $pro)
                                                
                                <tr>
                        <td>
                            
                   {{app()->getLocale() == 'ar' ?$pro->Product()->first()->P_Ar_Name :$pro->Product()->first()->P_En_Name}}  
                        <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}}'>

                    @if(!empty($pro->V1))  
                ( {{app()->getLocale() == 'ar' ?$pro->V1()->first()->Name :$pro->V1()->first()->NameEn}} )
                            
                        <input type='hidden' name='VOne[]' value='{{$pro->V1}}'>
                        <input type='hidden' name='V_Name[]' value='{{$pro->V_Name}}'>          
                            
                    @else
                       <input type='hidden' name='VOne[]' value=''>
                        <input type='hidden' name='V_Name[]' value=''>      
                    @endif 
                            
                      @if(!empty($pro->V2))  
                                              ( {{app()->getLocale() == 'ar' ?$pro->V2()->first()->Name :$pro->V2()->first()->NameEn}} )
                        <input type='hidden' name='VTwo[]' value='{{$pro->V2}}'>
                        <input type='hidden' name='VV_Name[]' value='{{$pro->VV_Name}}'>       
                    @else
                         <input type='hidden' name='VTwo[]' value=''>
                      <input type='hidden' name='VV_Name[]' value=''>     
                    @endif            
     
                    </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->Product_Code}}'>{{$pro->Product_Code}}</td>
            <td><input class='Qun' type='hidden' name='Qty[]' value='{{$pro->Qty}}'>{{$pro->Qty}}</td>
        <td><input  type='hidden' name='StorePurch[]' value='{{$pro->Store}}'>{{app()->getLocale() == 'ar' ?$pro->Store()->first()->Arabic_Name :$pro->Store()->first()->English_Name}}
                                           <input type='hidden' name='Product[]' value='{{$pro->Product}}'>
                                    </td>
        <td>
        <button id='DelAssem' type='button' class='btn btn-default'><i class='fal fa-trash'></i></button>
        </td>
        </tr>            
               
                                        @endforeach    
                                        </tbody>

                                    </table>
                                    </div>
                                    <!-- datatable end -->
                                    <div class="form-row">
                                        <div class="form-group col-lg-6">
                                 <label class="form-label" for="simpleinput"> {{trans('admin.Product_Numbers')}} </label>
                      <input type="text" id="Product_Numbers" disabled  class="form-control">
                      <input type="hidden" id="Product_NumbersHide" name="Product_Numbers">
                                        </div>
                                        <div class="form-group col-lg-6">
                         <label class="form-label" for="simpleinput"> {{trans('admin.Total_Qty')}} </label>
                                           <input type="text" id="Total_Qty" disabled  class="form-control">
                                            <input type="hidden" id="Total_QtyHide" name="Total_Qty">
                                        </div>       
                                    </div>
                                    <div class="buttons mt-3" id="Submit" style="display: none">
                          <input type="hidden" id="sp" name="SP">                 
            <button type="button"  class="btn btn-primary" onclick="SPS()"> <i class="fal fa-folder"></i> {{trans('admin.SaveChanges')}} </button>
                                        

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



                    </div>

                </form> 
                </main>


@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>
    
     <script>
        var autoSave = $('#autoSave');
        var interval;
        var timer = function()
        {
            interval = setInterval(function()
            {
                //start slide...
                if (autoSave.prop('checked'))
                    saveToLocal();

                clearInterval(interval);
            }, 3000);
        };

        //save
        var saveToLocal = function()
        {
            localStorage.setItem('summernoteData', $('#saveToLocal').summernote("code"));
            console.log("saved");
        }

        //delete 
        var removeFromLocal = function()
        {
            localStorage.removeItem("summernoteData");
            $('#saveToLocal').summernote('reset');
        }

        $(document).ready(function()
        {
            //init default
            $('.js-summernote').summernote(
            {
                height: 200,
                tabsize: 2,
                placeholder: "Type here...",
                dialogsFade: true,
                toolbar: [
                    ['style', ['style']],
                    ['font', ['strikethrough', 'superscript', 'subscript']],
                    ['font', ['bold', 'italic', 'underline', 'clear']],
                    ['fontsize', ['fontsize']],
                    ['fontname', ['fontname']],
                    ['color', ['color']],
                    ['para', ['ul', 'ol', 'paragraph']],
                    ['height', ['height']]
                    ['table', ['table']],
                    ['insert', ['link', 'picture', 'video']],
                    ['view', ['fullscreen', 'codeview', 'help']]
                ],
                callbacks:
                {
                    //restore from localStorage
                    onInit: function(e)
                    {
                        $('.js-summernote').summernote("code", localStorage.getItem("summernoteData"));
                    },
                    onChange: function(contents, $editable)
                    {
                        clearInterval(interval);
                        timer();
                    }
                }
            });

            //load emojis
            $.ajax(
            {
                url: 'https://api.github.com/emojis',
                async: false
            }).then(function(data)
            {
                window.emojis = Object.keys(data);
                window.emojiUrls = data;
            });

            //init emoji example
            $(".js-hint2emoji").summernote(
            {
                height: 100,
                toolbar: false,
                placeholder: 'type starting with : and any alphabet',
                hint:
                {
                    match: /:([\-+\w]+)$/,
                    search: function(keyword, callback)
                    {
                        callback($.grep(emojis, function(item)
                        {
                            return item.indexOf(keyword) === 0;
                        }));
                    },
                    template: function(item)
                    {
                        var content = emojiUrls[item];
                        return '<img src="' + content + '" width="20" /> :' + item + ':';
                    },
                    content: function(item)
                    {
                        var url = emojiUrls[item];
                        if (url)
                        {
                            return $('<img />').attr('src', url).css('width', 20)[0];
                        }
                        return '';
                    }
                }
            });

            //init mentions example
            $(".js-hint2mention").summernote(
            {
                height: 100,
                toolbar: false,
                placeholder: "type starting with @",
                hint:
                {
                    mentions: ['jayden', 'sam', 'alvin', 'david'],
                    match: /\B@(\w*)$/,
                    search: function(keyword, callback)
                    {
                        callback($.grep(this.mentions, function(item)
                        {
                            return item.indexOf(keyword) == 0;
                        }));
                    },
                    content: function(item)
                    {
                        return '@' + item;
                    }
                }
            });

        });

    </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 V-->
<script>
   function  UnitCodePurch(r){
   
   var countryId = $('#UnitPurch'+r).val();
   var Pro = $('#Product'+r).val();
   var client = $('#client').val();
   var code = $('#CodePurch'+r).val();
        var Store = $('#StorePurch'+r).val();
                     if(countryId) {
                         $.ajax({
                             url: 'UnitSalesFilter/'+countryId+'/'+Pro+'/'+client+'/'+code+'/'+Store,,
                             type:"GET",
                             dataType:"json",
                             beforeSend: function(){
                                 $('#loader').css("visibility", "visible");
                             },
   
                             success:function(data) {
                                 $.each(data, function(key, value){
                                     
                     
                       $('#UnitPurchName'+r).val(data.name); 
                       $('#AvQty'+r).val(parseFloat(data.qty).toFixed(2)); 
    

       
   
                                 });
                                 
 
   
                             },
                             complete: function(){
                                 $('#loader').css("visibility", "hidden");
                             }
                         });
                     } else {
   
                         $('select[name="state"]').empty();
                     }
   
   }   
</script>  

<!-- Unit Code and Name -->
<script>
   function  UnitCodePurchh(r){
   
   var countryId = $('#UnitPurch'+r).val();
   var Pro = $('#Product'+r).val();
   var client = $('#client').val();
   var code = $('#CodePurch'+r).val();
        var Store = $('#StorePurch'+r).val();
                     if(countryId) {
                         $.ajax({
                             url: 'UnitSalesFilter/'+countryId+'/'+Pro+'/'+client+'/'+code+'/'+Store,,
                             type:"GET",
                             dataType:"json",
                             beforeSend: function(){
                                 $('#loader').css("visibility", "visible");
                             },
   
                             success:function(data) {
                                 $.each(data, function(key, value){
                                     
                       $('#CodePurch'+r).val(data.code); 
                       $('#UnitPurchName'+r).val(data.name); 
                    $('#AvQty'+r).val(parseFloat(data.qty).toFixed(2)); 


       
   
                                 });
                                 


     
        
   
                             },
                             complete: function(){
                                 $('#loader').css("visibility", "hidden");
                             }
                         });
                     } else {
   
                         $('select[name="state"]').empty();
                     }
   
   }   
</script>  


<!-- Store Name -->
<script>
   function  StoreNamePurch(r){
   
   var countryId = $('#StorePurch'+r).val();
   var Product = $('#Product'+r).val();
   var Code = $('#CodePurch'+r).val();
     var Unit = $('#UnitPurch'+r).val();    
                     if(countryId) {
                         $.ajax({
                             url: 'StoreNameSalesFilter/'+countryId+'/'+Product+'/'+Code+'/'+Unit,,
                             type:"GET",
                             dataType:"json",
                             beforeSend: function(){
                                 $('#loader').css("visibility", "visible");
                             },
   
                             success:function(data) {
                                 $.each(data, function(key, value){
                                     
                       $('#StorePurchName'+r).val(data.name); 
                       $('#AvQty'+r).val(data.AvQty); 

                                     
   
                                 });
                                 
  
        
   
                             },
                             complete: function(){
                                 $('#loader').css("visibility", "hidden");
                             }
                         });
                     } else {
   
                         $('select[name="state"]').empty();
                     }
   
   }   
</script>  



<!-- Account Balance -->
<script>
   $(document).ready(function() {
   
                  $('#vendor').on('change', function(){
                      var countryId = $(this).val();
                      if(countryId) {
                          $.ajax({
                              url: 'AccountBalanceFilter/'+countryId,
                              type:"GET",
                              dataType:"json",
                              beforeSend: function(){
                                  $('#loader').css("visibility", "visible");
                              },
   
                              success:function(data) {
                                  $.each(data, function(key, value){
   
        
                    $('#AccountCredit').val(key); 
                    $('#AccountCredit').val(value); 
                                  });
                              },
                              complete: function(){
                                  $('#loader').css("visibility", "hidden");
                              }
                          });
                      } else {
   
                          $('select[name="state"]').empty();
                      }
   
                  });
   
              });
    
        
     $(document).ready(function() { 
 
                      var countryId = $('#vendor').val();
                      if(countryId) {
                          $.ajax({
                              url: 'AccountBalanceFilter/'+countryId,
                              type:"GET",
                              dataType:"json",
                              beforeSend: function(){
                                  $('#loader').css("visibility", "visible");
                              },
   
                              success:function(data) {
                                  $.each(data, function(key, value){
   
        
                    $('#AccountCredit').val(key); 
                    $('#AccountCredit').val(value); 
                                  });
                              },
                              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 = '',store='')
    {  
     $.ajax({
      url:'ImportGoodsProductsFilter',
      method:'GET',
      data:{search:search,store:store},
      dataType:'json',
      success:function(data)
      {
       $('.Data').html(data.table_data);
                  $("#Data").show();
      }
     })
    }
    
   $(document).on('keyup', '#search', function(){
     var search = $(this).val();     
     var store = $('#store').val();     
     fetch_customer_data(search,store);
    });
       
       
    $(document).on('change', '#store', function(){
     var store = $(this).val();     
     var search = $('#search').val();     
     fetch_customer_data(search,store);
    });
       
               
   });
</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 = $("#UnitPurch"+r).val();
             var UnitName = $("#UnitPurchName"+r).val();
             var Qty = $("#Qty"+r).val();
             var Barcode = $("#CodePurch"+r).val();
             var StorePurch = $("#StorePurch"+r).val();
             var StorePurchName = $("#StorePurchName"+r).val();
                  
             document.getElementById("AddBtnPur"+r).style.display = "none";
             document.getElementById("Row"+r).style.display = "none";
           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><td><input class='Qun' type='hidden' name='Qty[]' value='"+Qty+"'>" + Qty + "</td><td><input  type='hidden' name='StorePurch[]' value='"+StorePurch+"'>" + StorePurchName + "</td><td><button id='DelAssem' type='button' class='btn btn-default'><i class='fal fa-trash'></i></button><input 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=''></tr>";
   
            
             $("#data-dt").append(markup);
       $("#Data").hide();
      $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };
   
   var rowctr = $('#dt').rowCount();     
                
    var sumQ = 0;        
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });   
            

   
   $('#Product_Numbers').val(parseFloat(rowctr));
   $('#Product_NumbersHide').val(parseFloat(rowctr));
       
   $('#Total_Qty').val(parseFloat(sumQ));
   $('#Total_QtyHide').val(parseFloat(sumQ));
       
  
       

       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(); 
            
   
      $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };
   
   var rowctr = $('#dt').rowCount();     
                
    var sumQ = 0;        
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });   
            
  
       
   
   $('#Product_Numbers').val(parseFloat(rowctr));
   $('#Product_NumbersHide').val(parseFloat(rowctr));
       
   $('#Total_Qty').val(parseFloat(sumQ));
   $('#Total_QtyHide').val(parseFloat(sumQ));
       
   

       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: 'ViraFilterPurchases/'+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 = $("#UnitPurch"+r).val();
             var UnitName = $("#UnitPurchName"+r).val();
             var Qty = $("#Qty"+r).val();
             var Barcode = Math.floor(Math.random() * 10000); 
             var StorePurch = $("#StorePurch"+r).val();
             var StorePurchName = $("#StorePurchName"+r).val();
           
             document.getElementById("AddBtnPur"+r).style.display = "none";
             document.getElementById("Row"+r).style.display = "none";              
              
                                                 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 + " ("+value+") </td><td><input type='hidden' name='Unit[]' value='"+UnitID+"'>" + UnitName + "</td><td><input type='text' name='P_Code[]' value='"+Barcode+"' class='form-control'></td><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='hidden' name='StorePurch[]' value='"+StorePurch+"'>" + StorePurchName + "</td><td><button id='DelAssem' type='button' class='btn btn-default'><i class='fal fa-trash'></i></button><input type='hidden' name='Product[]' value='"+Product+"'><input type='hidden' name='VOne[]' value='"+key+"'><input type='hidden' name='VTwo[]' value=''></td><input type='hidden' name='V_Name[]' value='"+value+"'><input type='hidden' name='VV_Name[]' value=''></tr>";
   
             $("#data-dt").append(markup);                            
                  $("#Data").hide(); 
  $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };
   
   var rowctr = $('#dt').rowCount();     
                
    var sumQ = 0;        
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });   
            

   
   $('#Product_Numbers').val(parseFloat(rowctr));
   $('#Product_NumbersHide').val(parseFloat(rowctr));
       
   $('#Total_Qty').val(parseFloat(sumQ));
   $('#Total_QtyHide').val(parseFloat(sumQ));
       


       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(); 
            
            
      $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };
   
   var rowctr = $('#dt').rowCount();     
                
    var sumQ = 0;        
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });   
    
       
  
   
   $('#Product_Numbers').val(parseFloat(rowctr));
   $('#Product_NumbersHide').val(parseFloat(rowctr));
       
   $('#Total_Qty').val(parseFloat(sumQ));
   $('#Total_QtyHide').val(parseFloat(sumQ));

       

       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: 'ViraNamePurchases/'+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 = $("#UnitPurch"+r).val();
             var UnitName = $("#UnitPurchName"+r).val();
             var Qty = $("#Qty"+r).val();
             var Barcode = Math.floor(Math.random() * 10000); 
             var StorePurch = $("#StorePurch"+r).val();
             var StorePurchName = $("#StorePurchName"+r).val();
  
                        
             document.getElementById("AddBtnPur"+r).style.display = "none";
             document.getElementById("Row"+r).style.display = "none";                     
            
                                                                                       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 + " ("+value+") </td><td><input type='hidden' name='Unit[]' value='"+UnitID+"'>" + UnitName + "</td><td><input type='text' name='P_Code[]' value='"+Barcode+"' class='form-control'></td><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='hidden' name='StorePurch[]' value='"+StorePurch+"'>" + StorePurchName + "</td><td><button id='DelAssem' type='button' class='btn btn-default'><i class='fal fa-trash'></i></button><input 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='"+value+"'><input type='hidden' name='VV_Name[]' value='"+value+"'></tr>";
         
             $("#data-dt").append(markup);                            

                      $("#Data").hide();                   
                                  });
                                  
                             $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };
   
   var rowctr = $('#dt').rowCount();     
                
    var sumQ = 0;        
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });   
            
  
       
   
   $('#Product_Numbers').val(parseFloat(rowctr));
   $('#Product_NumbersHide').val(parseFloat(rowctr));
       
   $('#Total_Qty').val(parseFloat(sumQ));
   $('#Total_QtyHide').val(parseFloat(sumQ));
       
 
       

       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(); 
            
   $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };
   
   var rowctr = $('#dt').rowCount();     
                
    var sumQ = 0;        
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });   
            
   
       
   
   $('#Product_Numbers').val(parseFloat(rowctr));
   $('#Product_NumbersHide').val(parseFloat(rowctr));
       
   $('#Total_Qty').val(parseFloat(sumQ));
   $('#Total_QtyHide').val(parseFloat(sumQ));
       
  
       if(rowctr == 0){
        document.getElementById("Submit").style.display = "none";    
           
       }else{
           
        document.getElementById("Submit").style.display = "block";    
       }
       
   
         
              
            
                    })  
       
       
     }    
</script> 


<!-- Scanner  problem -->         
<script>
   $(".form-control").keypress(function(event){
   if (event.which == '10' || event.which == '13') {
   event.preventDefault();
   }
   });             
   
   
</script>


<!-- Status -->
<script>
    function Statuss(){
        
 
  $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };
   
   var rowctr = $('#dt').rowCount();     
     

        
              if(rowctr == 0){
        document.getElementById("Submit").style.display = "none";    
           
       }else{
           
        document.getElementById("Submit").style.display = "block";    
       }
         
        
    }
    
    
     $(document).ready(function() {
      $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };
   
   var rowctr = $('#dt').rowCount();     
     

        
              if(rowctr == 0){
        document.getElementById("Submit").style.display = "none";    
           
       }else{
           
        document.getElementById("Submit").style.display = "block";    
       }
         
          });
</script> 

<!-- Submit Script -->
<script>

        function SPP(){
           $('#sp').val(1);  
           var x= $('#sp').val();   
        
        if(x == 1){
            
            document.getElementById("Submit").style.display = "none";     
      document.getElementById('form').submit();   
        }
    }
    
    
       function SPS(){
           $('#sp').val(0);  
           var x= $('#sp').val();   
        
        if(x == 0){
        document.getElementById("Submit").style.display = "none";         
      document.getElementById('form').submit();   
        }
    }

    
 
</script>

<!-- Add New Product -->
<script>
    function NewProducts(){
        

    var Name = $('#P_Ar_Name').val();  
    var EnName = $('#P_En_Name').val();  
    var P_Type = $('#P_Type').val();  
    var Brand = $('#Brand').val();  
    var Group = $('#Group').val();  
    var unit = $('#unit').val();  
    var Rate = $('#Rate').val();  
    var Barcode = $('#Barcode').val();  
    var Price = $('#Price').val();  
    var Price_Two = $('#Price_Two').val();  
    var Price_Three = $('#Price_Three').val();  

          if(Name) {
                         $.ajax({
                             url: 'AddNewProduct/'+Name+'/'+EnName+'/'+P_Type+'/'+Brand+'/'+Group+'/'+unit+'/'+Rate+'/'+Barcode+'/'+Price+'/'+Price_Two+'/'+Price_Three,
                             type:"GET",
                             dataType:"json",
                             beforeSend: function(){
                                 $('#loader').css("visibility", "visible");
                             },
   
                             success:function(data) {
                                 $.each(data, function(key, value){
                                     
                     alert('تم الاضافه بنجاح');
                                     
                                 });
                             },
                             complete: function(){
                                 $('#loader').css("visibility", "hidden");
                             }
                         });
                     }else{
                         
                        alert('حقل الاسم مطلوب'); 
                         
                     }
        
        
        
    }
</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());
   });   
            

   
   $('#Product_Numbers').val(parseFloat(rowctr));
   $('#Product_NumbersHide').val(parseFloat(rowctr));
       
   $('#Total_Qty').val(parseFloat(sumQ));
   $('#Total_QtyHide').val(parseFloat(sumQ));
        });
  
   $('#data-dt').on('click', '#DelAssem', function(e){
                $(this).closest('tr').remove(); 
            
   
      $.fn.rowCount = function() {
     return $('tr', $(this).find('tbody')).length;
   };
   
   var rowctr = $('#dt').rowCount();     
                
    var sumQ = 0;        
   $('.Qun').each(function(){
   sumQ += parseFloat($(this).val());
   });   
            
  
       
   
   $('#Product_Numbers').val(parseFloat(rowctr));
   $('#Product_NumbersHide').val(parseFloat(rowctr));
       
   $('#Total_Qty').val(parseFloat(sumQ));
   $('#Total_QtyHide').val(parseFloat(sumQ));
       
   

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

                    })  
</script>

@endpush