@extends('layouts.app')
@section('title','Course Details')
@section('pages')

<style type="text/css">
    .breadcrumb-title {
      border-right: none;
    }
    .dataTables_filter{
        margin-bottom: 6px;
    }
</style>
<link rel="stylesheet/less" type="text/css" href="{{ asset('admin/plugins/datepicker/less/datepicker.less') }}">
<link href="{{ asset('admin/plugins/datepicker/css/datepicker.css')}}" rel="stylesheet" />
  <!--breadcrumb-->
  <div class="page-breadcrumb row align-items-center justify-content-between mb-3 px-3 px-md-0">
    <div class="breadcrumb-title col-md-6"> Course Details
    </div>
    <div class="col-md-6">
      <nav aria-label="breadcrumb">
        <ol class="breadcrumb justify-content-md-end mb-0">
          <li class="breadcrumb-item"><i class="bi bi-info-circle-fill text-info show-help-support" data-id="42" data-bs-toggle="modal" data-bs-target="#commonModal"></i></li>
          <li class="breadcrumb-item" aria-current="page">Course</li>
            <li class="breadcrumb-item active" aria-current="page"><i class="bi bi-dot"></i>  Course Details </li>
        </ol>
      </nav>
    </div>
  </div>
  <!--end breadcrumb-->
  <div class="card style_table shadow-none radius-5 min-height my-auto">
    <div class="card-header py-3">
      @include('layouts.partials.nav-menu')
      <div class="row gx-2">
        <div class="col-md-4">
            <h5><b>{{$course->course_title}}</b></h5>
        </div>
        <div class="col-lg-8 col-md-8">
          <div class="ms-auto float-md-end">

            

            <div class="btn-group">
            @if(in_array('course-category-edit', $authRolePermissions))
                <a href="{{route('courses.edit',$course->id)}}" class="btn btn-warning" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Edit">
                    <i class="bi bi-pencil-square text-white"></i> <span class="text-white"> Edit Course</span>
                </a>
              @endif
            </div>
            <div class="btn-group">
                <a href="{{route('courses.index')}}" class="btn btn-outline-secondary me-1"><i class="bx bx-arrow-back ms-0 me-1"></i>Back</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="row auto">
      {{--<div class="col-xl-12 mx-auto">
          <div class="shadow-none radius-5 min-height">
              <div class="row">
                  <div class="col-md-10">
                      <div class="card">
                          <div class="card-body g-3">
                              <div class="table-responsive">
                                  <div class="d-flex justify-content-between">
                                      <h5 class="mb-0">Course Details</h5>
                                  </div>
                                  <hr class="mb-2">
                                  <table class="table table-bordered align-middle">
                                    <tr>
                                        <td style="text-align:left;">Course Name </td>
                                        <td>{{$course->course_title}}</td>
                                    </tr>
                                    <tr>
                                        <td style="text-align:left;">course Duration </td>
                                        <td>{{$course->course_duration}} {!! $course->duration_type??'' !!}</td>
                                    </tr>
                                    <tr>
                                        <td style="text-align:left;">course Type</td>
                                        <td>{{$course->course_type??''}}</td>
                                    </tr>
                                    <tr>
                                        <td style="text-align:left;">course Category</td>
                                        <td>{{$course->category_name??''}}</td>
                                    </tr>
                                    <tr>
                                        <td style="text-align:left;">Class</td>
                                        <td>{{$course->class_name??''}}</td>
                                    </tr>
                                    <tr>
                                        <td style="text-align:left;">Subject</td>
                                        <td>{{$course->subject_name??''}}</td>
                                    </tr>
                                    <tr>
                                        <td style="text-align:left;">Price</td>
                                        <td>{{isset($course->course_price) && $course->course_price!='' ? $course->course_price : 'Free'}}</td>
                                    </tr>
                                    <tr>
                                        <td style="text-align:left;">Short Description</td>
                                        <td>{!! $course->short_description??'' !!}</td>
                                    </tr>
                                  </table>
                              </div>
                          </div>
                      </div>
                  </div>
                  <div class="col-md-2">
                      <div class="card">
                          <div class="card-body g-3 row">
                              <div class="table-responsive" style="margin-bottom: 18px;">
                                  @if($course->thumbnail != '')
                                      <img src="{!! getTempUrl($course->thumbnail) !!}" height="auto" id="createImage" data-bs-toggle="modal" data-bs-target="#commonModal" style="cursor: pointer;max-width: 120px;width: 120px;">
                                  @else
                                      <img src="{!! asset('admin/images/avtaar.png') !!}" width="100%" height="auto" id="createImage" data-bs-toggle="modal" data-bs-target="#commonModal" style="cursor: pointer;max-width: 120px;width: 120px;">
                                  @endif
                                  <p class="pt-3" style="cursor: pointer;"><strong>Thumbnail Image</strong></p>
                              </div>
                          </div>
                      </div>
                  </div>
              </div>
          </div>
      </div>--}}
      <div class="col-xl-12 mx-auto">
          <div class="shadow-none radius-5 min-height">
              <div class="row">
                  <div class="col-md-12">
                      <div class="card">
                          <div class="card-body g-3" id="material_types_tables">
                          </div>
                      </div>
                  </div>
              </div>
          </div>
      </div>

      <div class="col-xl-12 mx-auto">
          <div class="shadow-none radius-5 min-height">
              <div class="row">
                  <div class="col-md-12">
                      <div class="card">
                          <div class="card-body g-3" id="notices_tables">
                          </div>
                      </div>
                  </div>
              </div>
          </div>
      </div>
  </div>

    <!--end row-->
@endsection
@section('js')
<script type="text/javascript" src="{{ asset('admin/plugins/datepicker/js/bootstrap-datepicker.js')}}"></script>
<script type="text/javascript">
    $(document).on('click', '.save_button', function(event){
        event.preventDefault();
        var error = 0;

        if ($('#commonModal').find('form').hasClass('create_semesters')) {

            if (error == 0) {
                let formdata = new FormData($('#commonModal').find('form')[0]);

                $.ajax({
                    url: $('#commonModal').find('form').attr('action'),
                    data: formdata,
                    processData: false,
                    contentType: false,
                    type: 'POST',
                    success: function (response) {
                        if (response.success === true) {
                          toastr.success(response.message, 'Success.');

                          // hide modal
                          $('#commonModal').modal('hide');
                          fetch_study_material_type();
                        } else {
                            toastr.error(response.message, 'Error!');
                        }
                    }
                })
                .fail(function(jqXHR, textStatus, errorThrown) {
                    console.log('AJAX request failed:', textStatus, errorThrown);
                    toastr.error('An error occurred during the request.', 'Error!');

                    // hide modal
                    $('#commonModal').modal('hide');
                    $('#commonModal').find('.modal-body').html('');
                });
            }
        }

        if ($('#commonModal').find('form').hasClass('image')) {
            $('.error_file').hide();
            $('.error_title').hide();

            if ($('#commonModal').find('form').find('[name="file"]').val().trim() == '') {
              $('.error_file').show();
              error = 1;
            }

            var account_id = $('#commonModal').find('form').find('input[name="account_id"]').data('id');
            $('#commonModal').find('form').find('input[name="account_id"]').val(account_id);

            if (error == 0) {
              $('#commonModal').find('form').submit();
            }
        }

        if ($('#commonModal').find('form').hasClass('create_study_materials')) {

            if (error == 0) {
                let formdata = new FormData($('#commonModal').find('form')[0]);

                $.ajax({
                    url: $('#commonModal').find('form').attr('action'),
                    data: formdata,
                    processData: false,
                    contentType: false,
                    type: 'POST',
                    success: function (response) {
                        if (response.success === true) {
                            fetch_study_material();
                            toastr.success(response.message, 'Success.');

                            // hide modal
                            $('#commonModal').modal('hide');
                        } else {
                            toastr.error(response.message, 'Error!');
                        }
                    }
                })
                .fail(function(jqXHR, textStatus, errorThrown) {
                    console.log('AJAX request failed:', textStatus, errorThrown);
                    toastr.error('An error occurred during the request.', 'Error!');

                    // hide modal
                    $('#commonModal').modal('hide');
                    $('#commonModal').find('.modal-body').html('');
                });
              }
        }

        if ($('#commonModal').find('form').hasClass('create_study_material_types')) {
            if (error == 0) {
                let formdata = new FormData($('#commonModal').find('form')[0]);

                $.ajax({
                    url: $('#commonModal').find('form').attr('action'),
                    data: formdata,
                    processData: false,
                    contentType: false,
                    type: 'POST',
                    success: function (response) {
                        if (response.success === true) {
                            fetch_study_material_type();
                            toastr.success(response.message, 'Success.');

                            // hide modal
                            $('#commonModal').modal('hide');
                        } else {
                            toastr.error(response.message, 'Error!');
                        }
                    }
                })
                .fail(function(jqXHR, textStatus, errorThrown) {
                    console.log('AJAX request failed:', textStatus, errorThrown);
                    toastr.error('An error occurred during the request.', 'Error!');

                    // hide modal
                    $('#commonModal').modal('hide');
                    $('#commonModal').find('.modal-body').html('');
                });
            }
        }

        if ($('#commonModal').find('form').hasClass('edit_study_material_types')) {
            if (error == 0) {
                let formdata = new FormData($('#commonModal').find('form')[0]);

                $.ajax({
                    url: $('#commonModal').find('form').attr('action'),
                    data: formdata,
                    processData: false,
                    contentType: false,
                    type: 'POST',
                    success: function (response) {
                        if (response.success === true) {
                            fetch_study_material_type();
                            toastr.success(response.message, 'Success.');

                            // hide modal
                            $('#commonModal').modal('hide');
                        } else {
                            toastr.error(response.message, 'Error!');
                        }
                    }
                })
                .fail(function(jqXHR, textStatus, errorThrown) {
                    console.log('AJAX request failed:', textStatus, errorThrown);
                    toastr.error('An error occurred during the request.', 'Error!');

                    // hide modal
                    $('#commonModal').modal('hide');
                    $('#commonModal').find('.modal-body').html('');
                });
            }
        }
        error = 1;
    })
    $( function() {
        $('.datepicker').datepicker({
            "dateFormat" : "dd-mm-yy",
            "maxDate": '0',
            "changeMonth": true,
            "changeYear": true
        }).datepicker("setDate",'now')
        .on('changeDate', function (ev) {
             $(this).datepicker('hide');
        })

    });
    $(document).ready(function(){
        fetch_study_material_type();
        fetch_notices();
        
    });
    function fetch_study_material_type(){
      $.ajax({
        url:"{!! route('study-material-types.all',['course_id'=>$course->id]) !!}",
        success:function(data){
            $('#material_types_tables').html('');
            $('#material_types_tables').html(data);
            $.switcher('#material_types_tables .changeStatusCustom');
        }
      })
    }

    function fetch_notices(){
      $.ajax({
        url:"{!! route('course.notice',['course_id'=>$course->id]) !!}",
        success:function(data){
            $('#notices_tables').html('');
            $('#notices_tables').html(data);
            $.switcher('#notices_tables .changeStatusCustom');
        }
      })
    }
   
    
// Create Semesters Modal
$(document).on('click', '#create_semesters', function() {
    $('#commonModalLabel').text('New Semesters');
    $('.save_button_span').text('Save Semesters');
    $.ajax({
        url: "{{ route('semesters-create') }}",
        success: function(data) {
            $('#commonModal').find('.modal-body').html('');
            $('#commonModal').find('.modal-body').html(data);
        }
    })
});
// Edit Semesters Modal 
$(document).on('click', '#edit_semesters', function() {
    var id = $(this).data('id');
     $('#commonModalLabel').text('Edit Semesters');
        $('.save_button_span').text('Update Semesters');
        var url = `{!! route('semesters.edit', ':id') !!}`.replace(':id', id);

        $.ajax({
            url: url,
            success: function(data) {
                $('#commonModal').find('.modal-body').html('');
                $('#commonModal').find('.modal-body').html(data);
            }
        });
});
// Create Study Materials Modal
$(document).on('click', '#create_study_materials', function() {
    $('#commonModalLabel').text('New Study Materials');
    $('.save_button_span').text('Save Study Materials');
    $.ajax({
        url: "{{ route('study-materials-create') }}",
        success: function(data) {
            $('#commonModal').find('.modal-body').html('');
            $('#commonModal').find('.modal-body').html(data);
        }
    })
});
// Edit Study Materials Modal 
$(document).on('click', '#edit_study_materials', function() {
    var id = $(this).data('id');
    $('#commonModalLabel').text('Edit Study Materials');
    $('.save_button_span').text('Update Study Materials');
    var url = `{!! route('study-materials.edit', ':id') !!}`.replace(':id', id);

    $.ajax({
        url: url,
        success: function(data) {
            $('#commonModal').find('.modal-body').html('');
            $('#commonModal').find('.modal-body').html(data);
        }
    });
});

$(document).on('click', '.edit_study_material_types', function() {
    var id = $(this).data('id');
     $('#commonModalLabel').text('Edit Study Material Types');
        $('.save_button_span').text('Update Study Material Types');
        var url = `{!! route('study-material-types.edit', ':id') !!}`.replace(':id', id);

        $.ajax({
            url: url,
            success: function(data) {
                $('#commonModal').find('.modal-body').html('');
                $('#commonModal').find('.modal-body').html(data);
            }
        });
});


// Create Study Material Types Modal
$(document).on('click', '#create_study_material_types', function() {
    $('#commonModalLabel').text('New Study Material Types');
    $('.save_button_span').text('Save Study Material Types');
    $.ajax({
        url: "{{ route('study-material-types-create') }}",
        success: function(data) {
            $('#commonModal').find('.modal-body').html('');
            $('#commonModal').find('.modal-body').html(data);
        }
    })
});
    
$(document).on('click', '#createImage', function(){
    $('#commonModalLabel').text('Upload New Image');
    $('.save_button_span').text('Upload Image');
    $.ajax({
        url:"{!! route('account-images.create_images', $course->id) !!}",
        success:function(data){
        $('#commonModal').find('.modal-body').html('');
        $('#commonModal').find('.modal-body').html(data);
        }
    })
});


    /*********change status**********/
    $("body").on("click",'.changeStatusCustom', function(event){
        dataString       = {"id":$(this).data('id'),"status":$(this).data('status'),"account": "{!! $course->id !!}"};
        var UrlValue     = $(this).data('url');
        var status       = $(this).data('status');
        var changeStatus = $(this);

        Swal.fire({
            title: 'Are you sure you want to change status?',
            icon: 'warning',
            showDenyButton: true,
            showCancelButton: false,
            confirmButtonText: 'Yes',
            denyButtonText: 'No',
        }).then((result) => {
            /* Read more about isConfirmed, isDenied below */
            if (result.isConfirmed) {
                $.ajax({
                    url     : UrlValue,
                    method  : 'post',
                    data    :dataString,
                    headers:
                    {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    beforeSend: function( xhr ) {
                        // xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
                    },
                    success : function(response){
                        if(response.success){
                            var ErroMsg = $(this).printErrorMsg(response.message);
                            $(this).Toastshow('success',ErroMsg);

                            if($(this).hasClass('semesters')){
                                fetch_study_material_type();
                            }
                            else if ($(this).hasClass('study_materials')) {
                                fetch_study_material();
                            }else if($(this).hasClass('notices')){
                                fetch_notices();
                            }

                        } else {
                            var ErroMsg = $(this).printErrorMsg(response.message);
                            if (ErroMsg === '') {
                                ErroMsg = "Something went wrong!";
                            }
                            $(this).Toastshow('error',ErroMsg);

                            if (status == 1) {
                                changeStatus.parent().find('.ui-switcher').attr('aria-checked', false);
                            } else {
                                changeStatus.parent().find('.ui-switcher').attr('aria-checked', true);
                            }
                        }
                    },
                    error: function (data) {
                        console.log("error ",data);

                        if (status == 1) {
                            changeStatus.parent().find('.ui-switcher').attr('aria-checked', false);
                        } else {
                            changeStatus.parent().find('.ui-switcher').attr('aria-checked', true);
                        }
                    }
                });
            }
            else if (result.isDenied) {
                if (status == 1) {
                    changeStatus.parent().find('.ui-switcher').attr('aria-checked', false);
                } else {
                    changeStatus.parent().find('.ui-switcher').attr('aria-checked', true);
                }

                if($(this).hasClass('semesters')){
                    fetch_study_material_type();
                }
                else if ($(this).hasClass('study_materials')) {
                    fetch_study_material();
                } 
                else if($(this).hasClass('notices')){
                    fetch_notices();
                }
            }
            //return false;
        });
    //return false;
    });

    /*********delete data**********/
    $("body").on("click",'.deleteDataCustom', function(event){
        dataString    = {"id":$(this).data('id')};
        var UrlValue  = $(this).data('url');
        var btn = $(this);

        Swal.fire({
            title: 'Are you sure you want to delete this?',
            icon: 'warning',
            showDenyButton: true,
            showCancelButton: false,
            confirmButtonText: 'Yes',
            denyButtonText: 'No',
        }).then((result) => {
            /* Read more about isConfirmed, isDenied below */
            if (result.isConfirmed) {
                $.ajax({
                url     : UrlValue,
                method  : 'post',
                data    :{
                        "_token": $('meta[name="csrf-token"]').attr('content'),
                        "id": $(this).data('id'),
                        "account": "{!! $course->id !!}"
                },
                headers:
                {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                beforeSend: function( xhr ) {
                    // xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
                },
                success : function(response){
                    console.log(response.data);
                    if(response.success){
                        var ErroMsg = $(this).printErrorMsg(response.message);

                        $(this).Toastshow('success',ErroMsg);

                        if(btn.hasClass('semesters')){
                            fetch_study_material_type();
                        }
                        else if (btn.hasClass('study_materials')) {
                            fetch_study_metarial();
                        } else if(btn.hasClass('notices')){
                            fetch_notices();
                        }
                        console.log('s');
                    }else{
                        var ErroMsg = $(this).printErrorMsg(response.message);
                        $(this).Toastshow('error',ErroMsg);
                    }
                },
                error: function (data) {
                  console.log("error ",data);
                }
            });
            }
        });
    });
</script>
@endsection
