<form method="POST" action="{{route('ajax-post', ['name'=>'edit_register_teacher_save'])}}" class="edit_register_pending_teacher_form">   
    <input type="hidden" name="_token" value="{{ csrf_token() }}" />
    <input type="hidden" name="edit_id" value="{{$edit_data->id}}">
    <input type="hidden" name="action" value="edit"> 
    <div class="panel-body" style="padding:10px; background-color: #4b4c6824; border: 1px solid #e9e5e5; border-radius: 5px;">
    
        <div class="row">
            <div class="col-md-4">
                <div class="mb-2">
                    <label class="form-label config">First Name</label>
                    <div class="form-icon position-relative">
                        <input type="text" class="form-control" placeholder="First Name" id="first_name" name="first_name" value="{{$edit_data->first_name}}" required="" style="font-size: 13px;">
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="mb-2">
                    <label class="form-label config">Last Name</label>
                    <div class="form-icon position-relative">
                        <input type="text" class="form-control" placeholder="Last Name" id="last_name" name="last_name" value="{{$edit_data->last_name}}" required="" style="font-size: 13px;">
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="mb-2">
                    <label class="form-label config">Designation</label>
                    <div class="form-icon position-relative">
                        <input type="text" class="form-control" placeholder="Designation" id="designation" name="designation" value="{{$edit_data->designation}}" required="" style="font-size: 13px;">
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="mb-2">
                    <label class="form-label config">Department</label>
                    <div class="form-icon position-relative"> 
                        <select name="department" class="form-control" required="" style="font-size: 13px;">
                            <option selected disabled>Select Department</option>
                            @foreach($departments as $row)
                              <option value="{{$row->id}}" @if($row->id==$edit_data->department) selected @endif>{{$row->name}}</option>
                            @endforeach 
                        </select>
                    </div>
                </div>
            </div>

            <div class="col-md-4">
                <div class="mb-2">
                    <label class="form-label config">Destination</label>
                    <div class="form-icon position-relative"> 
                        <select name="destination" id="destination_id" class="form-control" required="" style="font-size: 13px;">
                            <option selected disabled>Select Destination</option>
                            @foreach($market_countries as $row)
                                <option value="{{$row->country_id}}" @if($row->country_id==$edit_data->nationality) selected @endif>{{$row->name}}</option>
                            @endforeach 
                        </select>
                    </div>
                </div>
            </div>
            
            <div class="col-md-4">
                <div class="mb-2">
                    <label class="form-label config">University</label>
                    <div class="form-icon position-relative"> 
                        <input type="hidden" id="edit_university_id" value="{{$edit_data->university_id}}">
                        <select name="university" id="university_id" class="form-control" required="" style="font-size: 13px;">
                            <option selected disabled>Select University</option>
                            {{-- @foreach($universities as $row)
                                <option value="{{$row->id}}" @if($row->id==$edit_data->university_id) selected @endif>{{$row->name}}</option>
                            @endforeach  --}}
                        </select>
                    </div>
                </div>
            </div> 
             
        </div>  
        <div class="row">
            <div class="col-md-12">
                <div class="text-end">
                    <button type="button" class="btn cancel_button mr-1 btn-cancel close_form" style="background-color: #ffbd59 !important; border: 2px solid #ffbd59; color: white;border-radius: 18px; padding: 2px 6px;">
                        <i aria-hidden="true" class="fa fa-refresh"></i>
                    </button>
                    <button class="btn btn2">Update</button>                                                                                                                
                </div>
            </div> 
        </div>  
    </div>
      
</form>

<script>
    $(function(){

        
      
         
        $('#destination_id').change(function(){  
            var self=$(this); 
            var id = $(this).val();
            var url="{{ route('destination-wise-university')}}";   

            if(id){

            $.get(url, {country_id:id}).done(function(res){   
                $("#university_id").empty();
                $("#university_id").append('<option value="">Select One..</option>');
                res.data.universities.forEach(function(row){  
                     $("#university_id").append('<option value="'+row.id+'">'+row.name+'</option>'); 
                }) 
            }) 
                
            }else{
                $("#university_id").empty(); 
            }   
        });

        var destination_id =$("#destination_id").val();
        var edit_university_id =$("#edit_university_id").val();

        if (destination_id) { 
            var self=$(this);  
            var url="{{ route('destination-wise-university')}}";  
            $.get(url, {country_id:destination_id}).done(function(res){   
                $("#university_id").empty(); 
                var selected = 'selected'
                res.data.universities.forEach(function(row){
                    if(row.id ==edit_university_id){ 
                        $("#university_id").append('<option selected="'+selected+'" value="'+row.id+'">'+row.name+'</option>');
                    }

                    if(row.id !=edit_university_id){ 
                        $("#university_id").append('<option value="'+row.id+'">'+row.name+'</option>');
                    } 

                }) 
            });
        
		}

        $('.edit_register_pending_teacher_form').on('submit', function(event){
            event.preventDefault();
            var self = $(this); 
            ajax_form(self, function(self){ 
                fetch_sub_content(
                    '#pending_teacher_load',
                    "{{ route('sub-content', ['name'=>'register_pending_teacher_load']) }}"
                ); 
            });
        });
    });

    $('.btn-cancel').on('click', function(){ 
        $('.edit_data').addClass('d-none')
    });
</script>