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

  <title>{{trans('admin.Edit_Inventory')}}</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_Inventory')}}   </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('PostEditInventory/'.$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_Inventory')}}  </i></span>
                                    </h2>
                                </div>
                                <div class="panel-container show">
                                  <span id="ex"> @include('admin.layouts.messages')</span>
                                    <div class="panel-content">
                                <form action="">
                                    <div class="form-row">
                                        <div class="form-group col-lg-4">
                                <label class="form-label" for="simpleinput"> {{trans('admin.Code')}}  </label>
                             <input type="text"  value="{{$item->Code}}" class="form-control " name="Old_Code" 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-4">
                        <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-3">
                                            <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-4">
                        <label class="form-label" for="">{{trans('admin.Price')}}</label>
             <select class="select2 form-control w-100" id="pricee">
                                                <option value="Cost"> {{trans('admin.Cost')}}</option>
                                                <option value="Sale"> {{trans('admin.Sale')}}</option>

                                            </select>
                                        </div>



                                                       <div class="form-group col-lg-3">
                                 <label class="form-label" for="simpleinput">{{trans('admin.Account_Dificit')}}</label>
                <select  class="js-data-example-ajax form-control w-100" name="Account_Dificit" id="AccountDificit" required>
            <option value="{{$item->Account_Dificit}}" selected>

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

                    </option>
                                        </select>
                                        </div>


                                            <div class="form-group col-lg-3">
                                 <label class="form-label" for="simpleinput">{{trans('admin.Account_Excess')}}</label>
                   <select  class="js-data-example-ajax form-control w-100" name="Account_Excess" id="AccountExcess" required>
                  <option value="{{$item->Account_Excess}}" selected>

                         {{app()->getLocale() == 'ar' ?$item->Account_Excess()->first()->Name :$item->Account_Excess()->first()->NameEn}}
                       </option>
                                                    </select>
                                        </div>

                                        <div class="form-group col-lg-8">
                                            <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="form-group col-lg-2 ">
                                       <select class="select2 form-control w-100" id="Brandd">
                                          <option value="">{{trans('admin.Brand')}}</option>
                                          @foreach($Brands as $brand)
                                          <option value="{{$brand->id}}">
                                      {{app()->getLocale() == 'ar' ?$brand->Name :$brand->NameEn}}
                                           </option>
                                          @endforeach
                                            </select>
                            </div>
                            <div class="form-group col-lg-2">
                                       <select class="select2 form-control w-100" id="Groupp">
                                          <option value="">{{trans('admin.Group')}}</option>
                                          @foreach($ItemsGroups as $group)
                 <option value="{{$group->id}}">
                                             {{app()->getLocale() == 'ar' ?$group->Name :$group->NameEn}}
                                           </option>
                                          @endforeach
                                       </select>
                            </div>

                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    </div>
                    <div class="col-xl-12">
                        <div id="panel-1" class="panel">

                            <div class="panel-container show">
                                <div class="panel-content">

                                    <!-- datatable start -->
                    <table id="dt-basic-example" class="table table-bordered table-hover table-striped w-100">
                                        <thead>
                                            <tr>
                                                <th> {{trans('admin.Name')}}</th>
                                                <th> {{trans('admin.Unit')}}</th>
                                                <th> {{trans('admin.Code')}}</th>
                                                <th> {{trans('admin.Price')}}</th>
                                                @if($show->Expire_Date == 1)
                                                    <th> {{trans('admin.Exp_Date')}}</th>
                                                @endif

                                                <th> {{trans('admin.Qty')}}</th>
                                                <th> {{trans('admin.Inventory')}}</th>
                                                <th> {{trans('admin.Dificit')}}</th>
                                                <th> {{trans('admin.Execess')}}</th>
                                                <th> {{trans('admin.Actions')}}</th>
                                            </tr>
                                        </thead>
                                        <tbody class="Data">

                                        </tbody>
                                    </table>
                                    <!-- datatable end -->
                                    <div class="row">
                                        <div class="col-md-12">
                              <table id="dt" class="table table-bordered table-hover table-striped w-100">
                                            <thead>
                                                <tr>
                                                <th> {{trans('admin.Name')}}</th>
                                                <th> {{trans('admin.Unit')}}</th>
                                                <th> {{trans('admin.Code')}}</th>
                                                <th> {{trans('admin.Price')}}</th>
                                                    @if($show->Expire_Date == 1)
                                                        <th> {{trans('admin.Exp_Date')}}</th>
                                                    @endif

                                                    <th> {{trans('admin.Qty')}}</th>
                                                <th> {{trans('admin.Inventory')}}</th>
                                                <th> {{trans('admin.Dificit')}}</th>
                                                <th> {{trans('admin.Execess')}}</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))
            ( {{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='{{app()->getLocale() == 'ar' ?$pro->V1()->first()->Name :$pro->V1()->first()->NameEn}}'>
        @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='{{app()->getLocale() == 'ar' ?$pro->V2()->first()->Name :$pro->V2()->first()->NameEn}} '>
                            @else
                                                 <input type='hidden' name='VTwo[]' 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}}'>

                            </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>
                            <td><input type='hidden' name='Price[]' value='{{$pro->Price}}'>{{$pro->Price}}</td>

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

                            <td><input type='hidden' name='Qty[]' value='{{$pro->Av_Qty}}'>{{$pro->Av_Qty}}</td>
                            <td>
              <input class='Inven' type='hidden' name='Inventory[]' value='{{$pro->Inventory}}'>{{$pro->Inventory}}
                            </td>
                            <td>
                            <input type='hidden' class='Dificit' name='Dificit[]' value='{{$pro->Deficit}}'>
                        <input type='hidden' class='TotDificit' name='TotalDificitP[]'  value='{{$pro->TotalDificitP}}'>
                            {{$pro->Deficit}}
                            </td>
                            <td>
                            <input type='hidden' class='Excess' name='Excess[]' value='{{$pro->Excess}}'>
                            <input type='hidden' class='TotExcess'  name='TotalExcessP[]'  value='{{$pro->TotalExcessP}}'>
                              {{$pro->Excess}}
                            </td>
                            <td>
                            <button id='DelAssem' type='button' class='btn btn-default'><i class='fal fa-trash'></i></button>

                    <input id='Product_IDInp{{$pro->Product}}' type='hidden' name='Product[]' value='{{$pro->Product}}'>
                            </td>
                            </tr>

                                    @endforeach



                                            </tbody>
                                        </table>
                                        </div>
                                    </div>
                                    <div class="form-row">
                                        <div class="form-group col-lg-3">
                                  <label class="form-label" for="simpleinput">{{trans('admin.Total_Dificit')}}</label>
                                            <input type="text" id="TotalDificit" disabled  class="form-control">
                                    <input type="hidden" id="TotalDificitHide" name="Total_Dificit"  class="form-control">
                                        </div>
                                        <div class="form-group col-lg-3">
                                     <label class="form-label" for="simpleinput"> {{trans('admin.Total_Execess')}}</label>
                                            <input type="text" id="TotalExecess" disabled  class="form-control">
                                    <input type="hidden" id="TotalExecessHide" name="Total_Excess"  class="form-control">

                                        </div>
                                        <div class="form-group col-lg-3">
                                     <label class="form-label" for="simpleinput"> {{trans('admin.Total_Dificit_Price')}} </label>
                                            <input type="text" id="TotalDificitPrice" disabled  class="form-control">
                                       <input type="hidden" id="TotalDificitPriceHide" name="Total_Dificit_Price" >
                                        </div>
                                     <div class="form-group col-lg-3">
                                     <label class="form-label" for="simpleinput"> {{trans('admin.Total_Execess_Price')}} </label>
                                            <input type="text" id="TotalExecessPrice" disabled  class="form-control">
                                            <input type="hidden" id="TotalExecessPriceHide"  name="Total_Excess_Price" >
                                        </div>

                                    </div>

                                   <div class="row">
                                    <div class="form-group col-md-12">
                                        <label class="form-label" for="simpleinput"> {{trans('admin.Notes')}}  </label>

                            <textarea class="js-summernote" id="saveToLocal" name="Note">
                                        {{$item->Note}}
                                        </textarea>
                                    </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(app()->getLocale() == 'ar' )
<input type="hidden" id="LANG" value="ar">
@else
<input type="hidden" id="LANG" value="en">
@endif


  <input type="hidden" id="SHOWEXP" value="{{$show->Expire_Date}}">

@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
              }


              $('#AccountDificit').select2({
                  placeholder: "select...",
                  ajax: {
                      type: "GET",
                      dataType: 'json',
                      url: 'AllSubAccounts',
                      processResults: function (data) {
                          return {
                              results: $.map(data, function(obj, index) {
                                  return { id: index, text: obj };
                              })
                          };

                          console.log(data);

                      },
                      data: function (params) {


                          var query = {
                              search: params.term,
                          };


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

                              success:function(data) {
                                  $('#AccountDificit').empty();
                                  $.each(data, function(key, value){

                                      $('#AccountDificit').append('<option value="'+ key +'">' + value + '</option>');

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



                      }
                  }
              });


              $('#AccountDificit').on('select2:select', function (e) {
                  console.log("select done", e.params.data);
              });


              $('#AccountExcess').select2({
                  placeholder: "select...",
                  ajax: {
                      type: "GET",
                      dataType: 'json',
                      url: 'AllSubAccounts',
                      processResults: function (data) {
                          return {
                              results: $.map(data, function(obj, index) {
                                  return { id: index, text: obj };
                              })
                          };

                          console.log(data);

                      },
                      data: function (params) {


                          var query = {
                              search: params.term,
                          };


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

                              success:function(data) {
                                  $('#AccountExcess').empty();
                                  $.each(data, function(key, value){

                                      $('#AccountExcess').append('<option value="'+ key +'">' + value + '</option>');

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



                      }
                  }
              });


              $('#AccountExcess').on('select2:select', function (e) {
                  console.log("select done", e.params.data);
              });


          });
      });



  </script>

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

          fetch_customer_data();

          function fetch_customer_data(search = '',store = '',Brandd='',Groupp='',pricee='')
          {
              $.ajax({
                  url:'1/InventoryFilter',
                  method:'GET',
                  data:{search:search,store:store,Brandd:Brandd,Groupp:Groupp,pricee:pricee},
                  dataType:'json',
                  success:function(data)
                  {
                      $('.Data').html(data.table_data);
                  }
              })
          }

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

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

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

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


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

          $(document).ready(function(){
              var Groupp = $("#Groupp").val();
              var search = $('#search').val();
              var Brandd = $('#Brandd').val();
              var store = $('#store').val();
              var pricee = $('#pricee').val();
              fetch_customer_data(search,store,Brandd,Groupp,pricee);



          });

      });
  </script>

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

          var countryId = $('#UnitStart'+x).val();
          var Pro = $('#Product'+x).val();
          var code = $('#CodeStart'+x).val();
          var PRORATE = $('#PRORATE'+x).val();
          var store = $('#store').val();

          if(countryId) {
              $.ajax({
                  url: 'UnitNameCodeInventoryFilter' ,
                  type:"GET",
                  data:{
                      id:countryId,
                      Pro:Pro,
                      code:code,
                      store:store,
                      PRORATE:PRORATE,
                  },
                  dataType:"json",
                  beforeSend: function(){
                      $('#loader').css("visibility", "visible");
                  },

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


                          $('#UnitStartName'+x).val(data.name);
                          $('#Price'+x).val(parseFloat(data.price).toFixed(2));
                          $('#Qty'+x).val(parseFloat(data.qty).toFixed(2));


                      });

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

                      var result = parseFloat(Qty) -   parseFloat(Inventory) ;

                      if(parseFloat(result) < 0){

                          $('#Dificit'+x).val(0);
                          $('#Excess'+x).val(result);

                          var res = parseFloat(-result) * parseFloat(Price) ;

                          $('#TotalDificit'+x).val(0);
                          $('#TotalExcess'+x).val(res);

                      }else{
                          $('#Dificit'+x).val(result);
                          $('#Excess'+x).val(0);


                          var res = parseFloat(result) * parseFloat(Price) ;


                          $('#TotalDificit'+x).val(res);
                          $('#TotalExcess'+x).val(0);


                      }



                      var Dificit = $('#Dificit'+x).val();
                      var Excess = $('#Excess'+x).val();
                      var TotalDificit = $('#TotalDificit'+x).val();
                      var TotalExcess = $('#TotalExcess'+x).val();
                      var UnitID = $('#UnitStart'+x).val();


                      if(Dificit == ''  || Excess == ''  ||  TotalDificit == '' ||  TotalExcess == '' || UnitID == '' || Inventory == '' ){

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


                      if(Dificit != ''  && Excess != ''  &&  TotalDificit != '' && TotalExcess != '' && UnitID != '' && Inventory != ''){

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





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

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

      }
  </script>

  <!-- Unit Code and Name  -->
  <script>
      function  UnitCodeStartt(x){
          var PRORATE = $('#PRORATE'+x).val();
          var countryId = $('#UnitStart'+x).val();
          var Pro = $('#Product'+x).val();
          var code = $('#CodeStart'+x).val();
          var store = $('#store').val();
          if(countryId) {
              $.ajax({
                  url: 'UnitNameCodeInventoryFilter' ,
                  type:"GET",
                  data:{
                      id:countryId,
                      Pro:Pro,
                      code:code,
                      store:store,
                      PRORATE:PRORATE,
                  },
                  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(parseFloat(data.price).toFixed(2));
                          $('#Qty'+x).val(parseFloat(data.qty).toFixed(2));


                      });

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

                      var result = parseFloat(Qty) -   parseFloat(Inventory) ;

                      if(parseFloat(result) < 0){

                          $('#Dificit'+x).val(0);
                          $('#Excess'+x).val(result);

                          var res = parseFloat(-result) * parseFloat(Price) ;

                          $('#TotalDificit'+x).val(0);
                          $('#TotalExcess'+x).val(res);

                      }else{
                          $('#Dificit'+x).val(result);
                          $('#Excess'+x).val(0);


                          var res = parseFloat(result) * parseFloat(Price) ;


                          $('#TotalDificit'+x).val(res);
                          $('#TotalExcess'+x).val(0);


                      }



                      var Dificit = $('#Dificit'+x).val();
                      var Excess = $('#Excess'+x).val();
                      var TotalDificit = $('#TotalDificit'+x).val();
                      var TotalExcess = $('#TotalExcess'+x).val();
                      var UnitID = $('#UnitStart'+x).val();


                      if(Dificit == ''  || Excess == ''  ||  TotalDificit == '' ||  TotalExcess == '' || UnitID == '' || Inventory == '' ){

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


                      if(Dificit != ''  && Excess != ''  &&  TotalDificit != '' && TotalExcess != '' && UnitID != '' && Inventory != ''){

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





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

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

      }
  </script>

  <!-- Inventory and Execess -->
  <script>
      function Inventory(r){

          var Price =$('#Price'+r).val();
          var Qty =$('#Qty'+r).val();
          var Inventory =$('#Inventory'+r).val();

          var result = parseFloat(Qty) -   parseFloat(Inventory) ;

          if(parseFloat(result) < 0){

              $('#Dificit'+r).val(0);
              $('#Excess'+r).val(-result);

              var res = parseFloat(-result) * parseFloat(Price) ;

              $('#TotalDificit'+r).val(0);
              $('#TotalExcess'+r).val(res);

          }else{
              $('#Dificit'+r).val(result);
              $('#Excess'+r).val(0);


              var res = parseFloat(result) * parseFloat(Price) ;


              $('#TotalDificit'+r).val(res);
              $('#TotalExcess'+r).val(0);


          }



          var Dificit = $('#Dificit'+r).val();
          var Excess = $('#Excess'+r).val();
          var TotalDificit = $('#TotalDificit'+r).val();
          var TotalExcess = $('#TotalExcess'+r).val();
          var UnitID = $('#UnitStart'+r).val();


          if(Dificit == ''  || Excess == ''  ||  TotalDificit == '' ||  TotalExcess == '' || UnitID == '' || Inventory == '' ){

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


          if(Dificit != ''  && Excess != ''  &&  TotalDificit != '' && TotalExcess != '' && UnitID != '' && Inventory != ''){

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




      }
  </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 Inventory = $("#Inventory"+r).val();
          var Dificit = $("#Dificit"+r).val();
          var TotalDificit = $("#TotalDificit"+r).val();
          var Excess = $("#Excess"+r).val();
          var TotalExcess = $("#TotalExcess"+r).val();
          var V_Name = $("#V_Name"+r).val();
          var VOne = $("#VOne"+r).val();
          var VV_Name = $("#VV_Name"+r).val();
          var VTwo = $("#VTwo"+r).val();
          var SHOWEXP = $("#SHOWEXP").val();

          var ExpDate = $("#ExpDate" + r).val();
          var ExpDateName = $("#ExpDate" + r).find(":selected").text();

          var Exp = '';


          if (parseFloat(SHOWEXP) == 1) {

              Exp = "<td><input type='hidden' name='Exp_Date[]' value='" + ExpDate + "'>" + ExpDateName + "</td>";

          } else {

              Exp = "<input type='hidden' name='Exp_Date[]' value='" + ExpDate + "'>";
          }


          var Viro = '';
          var VViro = '';

          if( V_Name != ''){

              Viro= '(' + V_Name +')';
          }

          if( VV_Name != ''){

              VViro= '(' + VV_Name +')';
          }

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

          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 + ""+ Viro +" "+ VViro +"</td><td><input type='hidden' name='Unit[]' value='"+UnitID+"'>" + UnitName + "</td><td><input type='hidden' name='P_Code[]' value='"+Barcode+"'>" + Barcode + "</td><td><input type='hidden' name='Price[]' value='"+Price+"'>" + Price + "</td>"+Exp+"<td><input type='hidden' name='Qty[]' value='"+Qty+"'>" + Qty + "</td><td><input class='Inven' type='hidden' name='Inventory[]' value='"+Inventory+"'>" + Inventory + "</td><td><input type='hidden' class='Dificit' name='Dificit[]' value='"+Dificit+"'><input type='hidden' class='TotDificit' name='TotalDificitP[]'   value='"+TotalDificit+"'>" + Dificit + "</td><td><input type='hidden' class='Excess' name='Excess[]' value='"+Excess+"'><input type='hidden' class='TotExcess' name='TotalExcessP[]'  value='"+TotalExcess+"'>" + Excess + "</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='"+VOne+"'><input type='hidden' name='VTwo[]' value='"+VTwo+"'><input type='hidden' name='V_Name[]' value='"+V_Name+"'><input type='hidden' name='VV_Name[]' value='"+VV_Name+"'></td></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 sumD = 0;
          $('.Dificit').each(function(){
              sumD += parseFloat($(this).val());
          });

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

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

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


          $('#TotalDificit').val(parseFloat(sumD));
          $('#TotalDificitHide').val(parseFloat(sumD));
          $('#TotalExecess').val(parseFloat(sumE));
          $('#TotalExecessHide').val(parseFloat(sumE));
          $('#TotalDificitPrice').val(parseFloat(sumTD));
          $('#TotalDificitPriceHide').val(parseFloat(sumTD));
          $('#TotalExecessPrice').val(parseFloat(sumTE));
          $('#TotalExecessPriceHide').val(parseFloat(sumTE));



          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 sumD = 0;
              $('.Dificit').each(function(){
                  sumD += parseFloat($(this).val());
              });

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

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

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


              $('#TotalDificit').val(parseFloat(sumD));
              $('#TotalDificitHide').val(parseFloat(sumD));
              $('#TotalExecess').val(parseFloat(sumE));
              $('#TotalExecessHide').val(parseFloat(sumE));
              $('#TotalDificitPrice').val(parseFloat(sumTD));
              $('#TotalDificitPriceHide').val(parseFloat(sumTD));
              $('#TotalExecessPrice').val(parseFloat(sumTE));
              $('#TotalExecessPriceHide').val(parseFloat(sumTE));



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

              }else{

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



          })


      }
  </script>

  <!-- Expire Date Qty -->
  <script>
      function ExpDateQty(r) {

          var countryId = $('#ExpDate' + r).val();


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

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


                          $('#Qty' + r).val(parseFloat(data.qty).toFixed(2));

                      });



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

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




      }


  </script>

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

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


      });
  </script>


  <script>

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

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

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

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

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

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


          $('#TotalDificit').val(parseFloat(sumD));
          $('#TotalDificitHide').val(parseFloat(sumD));
          $('#TotalExecess').val(parseFloat(sumE));
          $('#TotalExecessHide').val(parseFloat(sumE));
          $('#TotalDificitPrice').val(parseFloat(sumTD));
          $('#TotalDificitPriceHide').val(parseFloat(sumTD));
          $('#TotalExecessPrice').val(parseFloat(sumTE));
          $('#TotalExecessPriceHide').val(parseFloat(sumTE));


          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 sumD = 0;
          $('.Dificit').each(function(){
              sumD += parseFloat($(this).val());
          });

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

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

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


          $('#TotalDificit').val(parseFloat(sumD));
          $('#TotalDificitHide').val(parseFloat(sumD));
          $('#TotalExecess').val(parseFloat(sumE));
          $('#TotalExecessHide').val(parseFloat(sumE));
          $('#TotalDificitPrice').val(parseFloat(sumTD));
          $('#TotalDificitPriceHide').val(parseFloat(sumTD));
          $('#TotalExecessPrice').val(parseFloat(sumTE));
          $('#TotalExecessPriceHide').val(parseFloat(sumTE));



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

          }else{

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



      })



  </script>


@endpush
