@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($answer ?? ''  && $answer->id)
            @method('PUT')
        @endif
        @CSRF

       <div class="form-group col-lg-6">
                <label>{{ meta('patients', getCurrentLocale()) ?? __('answers.patients') }} </label>
               
                     
                         <select  class="selectpicker form-control"  data-show-subtext="true" data-live-search="true" name="user_id">
                             <option selected disabled>{{__('answers.select')}}</option>
                    @foreach($patients as $row)
                    <option @if($answer ?? ''  && $answer->id) @if($row->id == $answer->user_id) selected @endif @endif value="{{$row->id}}"> {{$row->name}}</option>
                    @endforeach
                    </select>
              
              
                </div>
                  <div class="form-group col-lg-6">
            <label>{{ meta('date', getCurrentLocale()) ?? __('answers.date') }} </label>
            <input type="date" class="form-control {{ $errors->has('date') ? 'is-invalid' : '' }}" name="date" placeholder="{{ meta('date', getCurrentLocale()) ??__('answers.date') }}" value='{{ old("date", $answer->date ?? "") }}' >
        </div>
       <div class="form-group col-lg-6">
                <label>{{ meta('questions', getCurrentLocale()) ?? __('answers.questions') }} </label>
               
                     
                         <select onchange="question(this)" class="form-control" name="answer[0][question_id]">
                             <option selected disabled>{{__('answers.select')}}</option>
                    @foreach($questions as $row)
                    <option @if($answer ?? ''  && $answer->id) @if($row->id==$answer->question_id) selected @endif @endif value="{{$row->id}}"> {{$row->question}}</option>
                    @endforeach
                    </select>
              
              
                </div>
                         
       
      
                 <div class="form-group col-lg-6">
                <label>{{ meta('questions', getCurrentLocale()) ?? __('answers.answer') }} </label>
               
                     
                         <select  onchange="answered(this)" class="form-control" id="answer" name="answer[0][answer_id]">
                            @if($answer ?? ''  && $answer->id)
                            @foreach($answers as $row)
                            <option @if($answer ?? ''  && $answer->id) @if($row->id==$answer->answer_id) selected @endif @endif value="{{$row->id}}">{{$row->answer}}</option>
                            @endforeach
                            @else
                             <option selected disabled>{{__('answers.selectquestion')}}</option>
                             @endif
                            
                    </select>
              
              
                </div>
          <div class="col-lg-12">
              
                    <div class="row pricing-list-item border-item mb-4 form-group "  @if(!($answer ?? ''  && $answer->id)) style="display:none" @endif id="data" >
                        
                        @if($answer ?? ''  && $answer->id)
                @if(checkquestion($answer->id))
             <div class="form-group col-lg-6">
                <label> {{__('answers.question')}} </label>
               
                     
                         <select onchange="answersed(this)" class="form-control " name="answer[1][question_id]">
                            @foreach(questionsanswer($answer->answer_id) as $row)
                    <option @if($row->id == questionanswer($answer->id)) selected @endif  value="{{$row->id}}"> {{$row->question}}</option>
                    @endforeach
                    </select>
              
                </div>
                 <div class="form-group col-lg-6">
                <label>{{__('answers.answer')}} </label>
               
                                  <select class="form-control" id="answer" name="answer[1][answer_id]">
                         @foreach(answers(questionanswer($answer->id)) as $row)
                            <option  @if($row->id == answer($answer->id)) selected @endif value="{{$row->id}}">{{$row->answer}}</option>
                            @endforeach
              </select>
              </div>
               @endif
                   @endif
                
                </div>
            </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 question(elem){
       
        var num = $(elem).val();
        
        
        
         $.ajax({
          url : url_question+'/'+num,
          type : 'GET',
          data : 'nothing',
          dataType : 'json',
          success:function(data)
          {
             
            $('#answer').html(data);
              
          }
        })
    }
    function answered(elem){
       
        var num = $(elem).val();
        
         $.ajax({
          url : url_answer+'/'+num,
          type : 'GET',
          data : 'nothing',
          dataType : 'json',
          success:function(data)
          {
               if(data != 'no'){
            $("#data").css("display", "block");
            $('#data').html(data);
               }
               else{
                    $("#data").empty();
               }
          }
        })
    }
     function answersed(elem){
       
        var num = $(elem).val();
        
         $.ajax({
          url : url_question+'/'+num,
          type : 'GET',
          data : 'nothing',
          dataType : 'json',
          success:function(data)
          {
              
            $('#answers').html(data);
          }
        })
    }
    
    </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",
            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
