@extends('admin.sub_contents.base')  
@section('main') 
@foreach($departments as $row)
<div class="mt-2 single_edit_item" style="border-bottom: 1px solid #e8eef1;">
<div style="display: flex; margin-top:2px;">
    <div class="left" style="width: 80%;">
        <span class="title" style="font-size:11px;">{{$row->name}}</span>
    </div>
    <div class="right" style="width: 20%;">
        <span class="title" style="font-size:11px; font-weight: 600;"> 
            <button type="button" class="btn btn2 edit_department" data-id="{{$row->id}}" style="padding: 2px 5px !important; font-size: 11px;"><i class="fa fa-pencil"></i></button>
           
        </span>
        <span class="title" style="font-size:11px; font-weight: 600;">
            <button type="button" class="btn btn2 delete_department" data-id="{{$row->id}}" style="padding: 2px 5px !important; font-size: 11px;"><i class="fa fa-trash"></i></button>
        </span>
    </div>
</div>      
</div>      
@endforeach  
 
@endsection

<script>
    $(function(){
        
        var form_row_added=false;

        $('.edit_department').on('click', function(){ 
            if(form_row_added){ 
                $('.open_department_info_div').remove();
                form_row_added=false;
            } 
            var self=$(this);  
            var self_tr=self.closest('.single_edit_item');

            var dep_id=self.data('id');

            form_row_added=true;

            self_tr.after( 
             "<div class='open_department_info_div'><div class='open_department_edit_info_div_inner'></div></div>"
            );

            fetch_sub_content(
                '.open_department_edit_info_div_inner',
                "{{ route('sub-content', ['name'=>'edit_department_form']) }}?dep_id="+dep_id
            ); 
             
        });

        $('.delete_department').on('click', function(){ 
            var id = $(this).attr("data-id"); 
            var form_data={
                _token: "{{ csrf_token() }}",  
                delete_id: id,
            }  
            swal({
                title: "Are you sure?",
                text: "You will not be able to recover this data!",
                type: "warning",
                showCancelButton: true,
                confirmButtonClass: "btn-danger",
                cancelButtonClass: "btn-info",
                confirmButtonText: "Yes, delete!",
                cancelButtonText: "No, cancel!",

            }, function(isConfirm){

                if(isConfirm){  
                    $.post("{{ route('ajax-post', ['name'=>'delete_department_info']) }}",form_data
                    ).done(function(res){

                        pop_up_msg(res.msg);
                        
                        $('#load_department').empty();
                        fetch_sub_content(
                            '#load_department',
                            "{{ route('sub-content', ['name'=>'load_department']) }}"
                        );

                    }).fail(function(err){ 
                        pop_up_msg(err_msg(err), 'error'); 
                    }); 
                } 
            }); 
                        
        });
    });
</script>