@extends('admin.layouts.container')

@section('content')
<div class="side-title">
    <h3>{{ $head ?? '' }}</h3>
</div>

<div class="border-item">
    <form class="form-row" method="post" action="{{ $action }}">
        @if($userfood ?? ''  && $userfood->id)
            @method('PUT')
        @endif
        @CSRF

             <div class="form-group col-lg-6">
            <label>{{ meta('patient', getCurrentLocale()) ?? __('ketos.patient') }} </label>
           <select class="selectpicker form-control {{ $errors->has('user_id') ? 'is-invalid' : '' }}" data-show-subtext="true" data-live-search="true"  name="user_id">
               <option selected disabled>{{__('ketos.select')}}</option>
               @foreach($patients as $row)
               <option @if($userfood ?? ''  && $userfood->id) @if($row->id == $userfood->id) selected @endif @endif value="{{$row->id}}">{{$row->name}}</option>
               @endforeach
           </select>
            </div>
              <div class="form-group col-lg-6">
                     @if($userfood ?? ''  && $userfood->id)
                     <input type="checkbox" class="checkbox_check" onclick="showdata(this)" style="
    margin-top: 3pc;
    margin-right: 4pc;
">عرض واخفاء 
                     @endif
                 </div>
                 
                 
                  <div class="row pricing-list-item border-item mb-4 col-12" @if($userfood ?? ''  && $userfood->id)   @else  style="display:none" @endif  id="foods">
                    @if($userfood ?? ''  && $userfood->id)
                    <div  class="row pricing-list-item border-item mb-4 col-12" style="display:none"  id="ketoshow" >
                    @foreach($userfoods as $data=>$row)
                   
                     <div class="row pricing-list-item border-item mb-4 col-12">
                      
        
 <div class="form-group col-lg-6">
            <label>{{ meta('system_food_id', getCurrentLocale()) ?? __('reservations.system_food_id') }} </label>
          <select onchange="foodcodes(this)" class="form-control {{ $errors->has('system_food_id') ? 'is-invalid' : '' }}" data-id="{{$data}}" name="food[{{$data}}][system_food_id]" id="system_food">
              <option selected  value="">{{__('reservations.select')}}</option>
                    @foreach($systemfoods as $rows)
                    <option  @if($rows->id == $row->system_food_id) selected  @endif  value="{{$rows->id}}">{{$rows->name}}</option>
                    @endforeach
                    </select>
           </div>
        <div class="form-group col-lg-5" >
             <label>{{ __('reservations.dating') }} </label>
             <input type="date" class="form-control" name="food[{{$data}}][dating]" value="{{$row->dating}}">
        </div>
        <div class="form-group col-lg-1" style="font-size: 30px;margin-top:30px;" onclick="remove(this)"><i class="fas fa-times-circle"></i></div>
   
      
       <div class="form-group col-lg-6" id="categories{{$data}}">
            <label>التصنيفات</label>
            <select onchange="foodcals(this)" class="form-control " data-id="{{$data}}"  name="food[{{$data}}][category_id]" id="system_food">
               
              <option selected  value="">{{__('reservations.select')}}</option>
                    @foreach(categoriesfood($row->system_food_id) as $rows)
                    <option @if($rows->id == $row->category_id) selected  @endif value="{{$rows->id}}">{{$rows->name}}</option>
                    @endforeach
                    </select>
       </div>
       <div class="form-group col-lg-6" id="calories{{$data}}">
            <label>السعرات</label>
          <select onchange="foodcoding(this)" class="form-control " data-id="{{$data}}" name="food[{{$data}}][calorie_id]" id="system_food">   
              <option selected  value="">{{__('reservations.select')}}</option>
                    @foreach(caloriesfood($row->category_id) as $rows)
                    <option @if($rows->id == $row->calorie_id) selected  @endif value="{{$rows->id}}">{{$rows->name}}</option>
                    @endforeach
                    </select>
       </div>
       
       <div class="form-group col-lg-12" id="codes{{$data}}">
           <label>الاكواد</label>
          <select onchange="fooddesc(this)" class="form-control" data-id="{{$data}}" name="food[{{$data}}][code_id]" id="system_food">
                  <option selected  value="">{{__('reservations.select')}}</option>
                    @foreach(codesfood($row->calorie_id) as $rows)
                    <option @if($rows->id == $row->code_id) selected  @endif value="{{$rows->id}}">{{$rows->name}}</option>
                    @endforeach
                    </select>
       </div>
        <div class="form-group col-lg-12" id="desc{{$data}}">
            <label>الوجبة </label>
                <textarea name="food[{{$data}}][description]" class="form-control my-editor">{{reservationmeal($row->id)}}</textarea>
       </div>
       
       
       </div>
                    @endforeach
                    </div>
                    @endif
                    
                </div>
<div class="col-lg-12">
       <a style="cursor: pointer;" @if($userfood ?? ''  && $userfood->id) data-id="{{count($userfoods)}}" @else   data-id="0" @endif onclick="addfood(this)" class="btn_1 gray "><i class="fa fa-fw fa-plus-circle"></i>{{__("ketos.add_keto")}}</a>
                   
</div>





           

        <div class="text-center mt-5 col-12">
            <button class="butn"><span>{{ __('dashboard.save') }}</span></button>
        </div>
    </form>
</div>

@endsection

@section('inner_js')
 <script src="{{ asset('/assets/admin/js/lib/tinymce.min.js') }}"></script>
  
<script>
    function addfood(elem){
        $("#foods").css("display", "block");
        var num = $(elem).attr('data-id');
       
       var newrow = parseInt(num)+1; 
        $(elem).attr('data-id', newrow);
        
         $.ajax({
          url : urls+'/'+num,
          type : 'GET',
          data : 'nothing',
          success:function(data)
          {
            $('#foods').append(data);
          }
        })
    }
    
    function foodcodes(elem){
        var num = $(elem).val();
        var dir = $(elem).attr('data-id');
        
         $.ajax({
          url : urlfoodcode +dir+','+num,
          type : 'GET',
          data : 'nothing',
          dataType : 'json',
          success:function(data)
          {
              
              $('#categories'+dir).html(data);
              
          }
        })
    }
    
    function foodcals(elem){
        var num = $(elem).val();
        var dir = $(elem).attr('data-id');
        
        
         $.ajax({
          url : urlfoodcal +dir+','+num,
          type : 'GET',
          data : 'nothing',
          dataType : 'json',
          success:function(data)
          {
                $('#calories'+dir).html(data);
          }
        })
    }
    function foodcoding(elem){
         var num = $(elem).val();
         var dir = $(elem).attr('data-id');
        
        
         $.ajax({
          url : urlfoodcoding +dir+','+num,
          type : 'GET',
          data : 'nothing',
          dataType : 'json',
          success:function(data)
          {
               $('#codes'+dir).html(data);
          }
        })
    }
    
     function fooddesc(elem){
         var num = $(elem).val();
         var dir = $(elem).attr('data-id');
         
         $.ajax({
          url : urlfooddesc +num,
          type : 'GET',
          data : 'nothing',
          dataType : 'json',
          success:function(data)
          {
               $('#desc'+dir).html(`
         
       
          <label>الوجبة </label>
                <textarea name="food[`+dir+`][description]" class="appended_text_area">`+data+`</textarea>
               
            `);
            tinymce.init({
                selector: '.appended_text_area',
                init_instance_callback: function (editor) {
                    editor.on('click', function (e) {
                        console.log('Element clicked:', e.target.nodeName);
                    });
                }
            });
              
          }
        })
    
     }
    
    
    function showdata(elem){
        let data = $(elem).val();
        if ($('input.checkbox_check').is(':checked')) {
            $('#ketoshow').css('display','block');
        }
        else{
            
            $('#ketoshow').css('display','none');
        }
    } 
     function remove(elem){
            $(elem).closest('.row').remove();
        }
        </script>
    <!-- Editor JS files -->
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            urls = "{{url('/')}}/admin/adduserfood",
            urlfoodcode = "{{url('admin/foodcodedata')}}/",
            urlfoodcal = "{{url('admin/foodcaloriesdata')}}/",
            urlfoodcoding = "{{url('admin/foodcodingdata')}}/",
            urlfooddesc = "{{url('admin/fooddescdata')}}/",
            url_question = "{{url('/')}}/admin/questiondata",
            imagePath = "{{asset('')}}";
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>
@endsection
