@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($history ?? ''  && $history->id)
            @method('PUT')
        @endif
        @CSRF
        
        
           <div class="form-group col-lg-6">
                <label>{{ meta('user', getCurrentLocale()) ?? __('histories.user') }} </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 >{{__('doctors.select')}}</option>
                    @foreach($users as $row)
                    <option  @if($history ?? ''  && $history->id) @if($history->id == $row->id) selected @endif @endif value="{{$row->id}}">{{$row->name}}</option>
                    @endforeach
               </select>
                </div>
                 <div class="form-group col-lg-6">
                     @if($history ?? ''  && $history->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" id="histories"  @if($history ?? ''  && $history->id)   @else  style="display:none" @endif    >
@if($history ?? ''  && $history->id) 
     <div  class="row pricing-list-item border-item mb-4 col-12" style="display:none"  id="historyshow" >
    @foreach($histories as $data=>$row)
      <div class="row pricing-list-item border-item mb-4" id="history">
         <div class="form-group col-lg-6">
            <label>{{ meta('date', getCurrentLocale()) ?? __('histories.date') }} </label>
            <input type="date" class="form-control {{ $errors->has('date') ? 'is-invalid' : '' }}" name="history[{{$data}}][date]" value="{{$row->date}}" placeholder="{{ meta('date', getCurrentLocale()) ??__('histories.date') }}" value='{{ old("date", $history->date ?? "") }}' >
        </div>
      
       <div class="form-group col-lg-5">
            <label>{{ meta('weight', getCurrentLocale()) ?? __('histories.weight') }} </label>
            <input type="text" class="form-control {{ $errors->has('weight') ? 'is-invalid' : '' }}" name="history[{{$data}}][weight]"value="{{$row->weight}}" placeholder="{{ meta('weight', getCurrentLocale()) ??__('histories.weight') }}" value='{{ old("weight", $history->weight ?? "") }}' >
        </div>
       <div class="form-group col-lg-1" style="font-size: 30px;margin-top:30px;" onclick="removehistory(this)"><i class="fas fa-times-circle"></i></div>
   
            
       <div class="form-group col-lg-6">
            <label>{{ meta('fat', getCurrentLocale()) ?? __('histories.fat') }} </label>
            <input type="text" class="form-control {{ $errors->has('fat') ? 'is-invalid' : '' }}" name="history[{{$data}}][fat]" value="{{$row->fat}}" placeholder="{{ meta('fat', getCurrentLocale()) ??__('histories.fat') }}" value='{{ old("fat", $history->fat ?? "") }}' >
        </div>
       <div class="form-group col-lg-6">
            <label>{{ meta('muscle', getCurrentLocale()) ?? __('histories.muscle') }} </label>
            <input type="text" class="form-control {{ $errors->has('muscle') ? 'is-invalid' : '' }}" name="history[{{$data}}][muscle]" value="{{$row->muscle}}" placeholder="{{ meta('muscle', getCurrentLocale()) ??__('histories.muscle') }}" value='{{ old("muscle", $history->muscle ?? "") }}' >
        </div>
       <div class="form-group col-lg-6">
            <label>{{ meta('liquid', getCurrentLocale()) ?? __('histories.liquid') }} </label>
            <input type="text" class="form-control {{ $errors->has('liquid') ? 'is-invalid' : '' }}" name="history[{{$data}}][liquid]" value="{{$row->liquid}}" placeholder="{{ meta('liquid', getCurrentLocale()) ??__('histories.liquid') }}" value='{{ old("liquid", $history->liquid ?? "") }}' >
        </div>
       <div class="form-group col-lg-6">
            <label>{{ meta('bone', getCurrentLocale()) ?? __('histories.bone') }} </label>
            <input type="text" class="form-control {{ $errors->has('bone') ? 'is-invalid' : '' }}" name="history[{{$data}}][bone]" value="{{$row->bone}}" placeholder="{{ meta('bone', getCurrentLocale()) ??__('histories.bone') }}" value='{{ old("bone", $history->bone ?? "") }}' >
        </div>
      
       <div class="form-group col-lg-6">
            <label>{{ meta('trust', getCurrentLocale()) ?? __('histories.trust') }} </label>
            <input type="text" class="form-control {{ $errors->has('trust') ? 'is-invalid' : '' }}" name="history[{{$data}}][trust]" value="{{$row->trust}}" placeholder="{{ meta('trust', getCurrentLocale()) ??__('histories.trust') }}" value='{{ old("trust", $history->trust ?? "") }}' >
        </div>
       <div class="form-group col-lg-6">
            <label>{{ meta('sport', getCurrentLocale()) ?? __('histories.sport') }} </label>
            <input type="text" class="form-control {{ $errors->has('sport') ? 'is-invalid' : '' }}" name="history[{{$data}}][sport]" value="{{$row->sport}}" placeholder="{{ meta('sport', getCurrentLocale()) ??__('histories.sport') }}" value='{{ old("sport", $history->sport ?? "") }}' >
        </div>
       <div class="form-group col-lg-6">
            <label>{{ meta('recommend', getCurrentLocale()) ?? __('histories.recommend') }} </label>
            <input type="text" class="form-control {{ $errors->has('recommend') ? 'is-invalid' : '' }}" name="history[{{$data}}][recommend]" value="{{$row->recommend}}" placeholder="{{ meta('recommend', getCurrentLocale()) ??__('histories.recommend') }}" value='{{ old("recommend", $history->recommend ?? "") }}' >
        </div>
       <div class="form-group col-lg-6">
            <label>{{ meta('complain', getCurrentLocale()) ?? __('histories.complain') }} </label>
            <input type="text" class="form-control {{ $errors->has('complain') ? 'is-invalid' : '' }}" name="history[{{$data}}][complain]" value="{{$row->complain}}" placeholder="{{ meta('complain', getCurrentLocale()) ??__('histories.complain') }}" value='{{ old("complain", $history->complain ?? "") }}' >
        </div>
         <div class="form-group col-lg-6">
            <label>{{ meta('system', getCurrentLocale()) ?? __('histories.system') }} </label>
            <input type="text" class="form-control {{ $errors->has('system') ? 'is-invalid' : '' }}" name="history[{{$data}}][system]" value="{{$row->system}}" placeholder="{{ meta('system', getCurrentLocale()) ??__('histories.system') }}" value='{{ old("system", $history->system ?? "") }}' >
        </div>
         <div class="form-group col-lg-6">
            <label>{{ meta('note', getCurrentLocale()) ?? __('histories.note') }} </label>
            <textarea type="text" class="form-control {{ $errors->has('note') ? 'is-invalid' : '' }}" name="history[{{$data}}][note]" placeholder="{{ meta('note', getCurrentLocale()) ??__('histories.note') }}"  >{{ $row->note }}</textarea>
        </div>
      
       <div class="col-lg-12">
     <div class="col-lg-12" id="reads{{$data}}">
    @foreach(historyreads($row->id) as $his=>$read)
    <div class="row pricing-list-item border-item mb-4" style="background-color: rgba(21,97,203,0.1);padding-bottom:60px;">
        
            <div class="form-group col-lg-5">
           <label>{{ __('reads.device')}} </label>
           <select class="form-control" name="history[{{$data}}][read][{{$his}}][device_id]">
               @foreach($devices as $device)
               <option @if($read->device_id == $device->id) selected @endif value="{{$device->id}}">{{$device->name}}</option>
               @endforeach
               </select>
            </div>
            <div class="form-group col-lg-5">
           <label>{{ __('reads.number')}} </label>
           <input type="number" steps="2" class="form-control" name="history[{{$data}}][read][{{$his}}][number]" value="{{$read->number}}" placeholder="'. __('reads.number').'" />
            </div>
        <div class="form-group col-lg-2" style="font-size: 30px;margin-top:30px;" onclick="remove(this)"><i class="fas fa-times-circle" ></i></div>
   
            
           
            </div>
            
        @endforeach
        </div>
            </div>
           
<div class="col-lg-12">
       <a style="cursor: pointer;" history-id="{{$data}}" @if(!($history ?? ''  && $history->id)) data-id="0" @else data-id="{{count(historyreads($row->id))}}" @endif onclick="addread(this)" class="btn_1 gray "><i class="fa fa-fw fa-plus-circle"></i>اضافة قراءة عضو</a>
                   
</div>
</div>
    @endforeach
    </div>
    @endif
</div>
<div class="col-lg-12">
       <a style="cursor: pointer;" @if(!($history ?? ''  && $history->id)) data-id="0" @else data-id="{{count($histories)}}" @endif onclick="addhistory(this)" class="btn_1 gray "><i class="fa fa-fw fa-plus-circle"></i>{{__("reads.add_read")}}</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>

 function addhistory(elem){
        $("#histories").css("display", "block");
        var num = $(elem).attr('data-id');
        
        var newrow = parseInt(num)+1; 
        $(elem).attr('data-id', newrow);
        //alert(urls_his+'/'+num);
         $.ajax({
          url : urls_his+'/'+num,
          type : 'GET',
          data : 'nothing',
          success:function(data)
          {
            $('#histories').append(data);
          }
        })
    }
    function addread(elem){
        $("#reads").css("display", "block");
        var num = $(elem).attr('data-id');
        var his = $(elem).attr('history-id');
        
        var newrow = parseInt(num)+1; 
        $(elem).attr('data-id', newrow);
        
         $.ajax({
          url : urls+'/'+num+','+his,
          type : 'GET',
          data : 'nothing',
          dataType : 'json',
          success:function(data)
          {
            $('#reads'+his).append(data);
          }
        })
    }
    function remove(elem){
       $(elem).closest('.row').remove();
    }
    function removehistory(elem){
       $(elem).closest('.row').remove();
    }
      function showdata(elem){
        let data = $(elem).val();
        if ($('input.checkbox_check').is(':checked')) {
            $('#historyshow').css('display','block');
        }
        else{
            
            $('#historyshow').css('display','none');
        }
    } 
</script>
    <!-- Editor JS files -->
{{--    <script src="{{ asset('/assets/admin/js/lib/tinymce.min.js') }}"></script>--}}
            <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            url_answer = "{{url('/')}}/admin/answerdata",
            urls_his = "{{url('/')}}/admin/historydata",
            url_question = "{{url('/')}}/admin/questiondata",
            urls = "{{url('/')}}/admin/addread",
            imagePath = "{{asset('')}}";
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>
@endsection
