$(document).ready(function() {
    
    var emma_campus = $("#header-campusid").val();

    $.ajax({
        type:'POST',
        url:'../../controller/scripts/student/policy/emma_get_institution.php',
        data:{emma_get_campus:emma_campus},
        success:function(result){ 
            $(".emmaloadinstitutionforpolicy").html(result);

            var emmagetthisinstitutionforpolicy = $(".emmaloadinputforinstitutionorcampus").val();
            $(".keepinstitution").val(emmagetthisinstitutionforpolicy);

            var user_type = $("#user_type_holder").val();
            var emmaacceptancestatus = 1;

            if(user_type == 'admin')
            {
                var emma_institution = $(".keepinstitution").val();
                var emma_user_id = $("#user_id_admin").val();
            }
            else if(user_type == 'teacher')
            {
                var emma_institution = $(".keepinstitution").val();
                var emma_user_id = $("#user_id_teacher").val();
            }
            else if(user_type == 'schoolhead')
            {
                var emma_institution = $(".keepinstitution").val();
                var emma_user_id = $("#user_id").val();
            }else if(user_type == 'parent' || user_type == 'student')
            {
                var emma_institution = $(".keepinstitution").val();
                var emma_user_id = $("#user_id").val();
            }else{
            
            }
            var datastring = "emmasendinstitutionid=" + emma_institution + "&emmausertype=" + user_type + "&emmauserid=" + emma_user_id + "&emmaacceptancestatus=" + emmaacceptancestatus;
        
            $.ajax({
                type:'POST',
                url:'../../controller/scripts/student/policy/emma_select_for_policy.php',
                data:datastring,
                success:function(output){
                    if(output == 1){
                        $("#staticBackdropforteacherprivacypolicy").modal('hide');
                    } else {
                        $("#staticBackdropforteacherprivacypolicy").modal('show');
                    }
                }
            });
        }
    });
});


$('#staticBackdropforteacherprivacypolicy').on('shown.bs.modal', function(){
    var emma_institution = $(".keepinstitution").val();

    $(".emmapolicycard").html('<div align="center p-5"><div class="spinner-border text-primary" role="status"><span class="visually-hidden">Loading...</span></div></div>');

    $.ajax({
        type:'POST',
        url:'../../controller/scripts/student/policy/emma_get_card_for_policy.php',
        data:{emma_send_institution:emma_institution},
        success:function(result){
            if(result == 'NULL'){
                $("#staticBackdropforteacherprivacypolicy").modal('hide');
            }else{
                $(".emmapolicycard").html(result);
            }
        }
    });
});

$("body").on('click','.emma_accept_policy_button',function(){
    
    var user_type = $("#user_type_holder").val();
    var emmaacceptancestatus = 1;

    if(user_type == 'admin'){
        var emma_institution = $(".keepinstitution").val();
        var emma_user_id = $("#user_id_admin").val();
    }
    else if(user_type == 'teacher')
    {
        var emma_institution = $(".keepinstitution").val();
        var emma_user_id = $("#user_id_teacher").val();
    }
    else if(user_type == 'schoolhead')
    {
        var emma_institution = $(".keepinstitution").val();
        var emma_user_id = $("#user_id").val();
    }else if(user_type == 'parent' || user_type == 'student')
    {
        var emma_institution = $(".keepinstitution").val();
        var emma_user_id = $("#user_id").val();
    }else{
    
    }
    var datastring = "emmasendinstitutionidforpolicy=" + emma_institution + "&emmausertypeforpolicy=" + user_type + "&emmauseridforpolicy=" + emma_user_id + "&emmaacceptancestatusforpolicy=" + emmaacceptancestatus;

    $.ajax({
        type:'POST',
        url:'../../controller/scripts/student/policy/emma_insert_for_accepted_policy.php',
        data:datastring,
        success:function(output){

            if(output == 1){
                $("#staticBackdropforteacherprivacypolicy").modal('hide');
            } else {

            }
        }
    });
});

// function emma_add_section_for_edit() {
//     var emma_add_new_session_for_edit =
//     `   <div class="accordion" id="accordionExampleforedit">
//             <i class="fas fa-times text-danger float-end emma_remove_section_for_edit" style="cursor:pointer;"></i>

//                 <div class="accordion-item accordion_item_edit mt-3">
//                     <h6 class="accordion-header" id="headingOne" id="heading${$('.accordion-item_edit').length + 1}>
                    

//                     <button class="accordion-button collapsed " type="button" data-bs-toggle="collapse" data-bs-target="#accordion-item_edit" aria-expanded="false" aria-controls="collapseOne">
//                     <i class="fas fa-plus-circle text-success p-3"> Add Section(optional)</i>
//                     </button>

//                     </h6>
//                     <div id="accordion-item_edit" class="accordion-collapse collapse " aria-labelledby="headingOne" data-bs-parent="#accordionExampleforedit">
//                         <div class="accordion-body">
//                             <div class="border">
//                                 <div class="p-3">
//                                     <div class="mt-1 add-new-icon">
//                                         <div class="form-group abba_local-forms mt-3">
//                                             <input class="form-control emma_section_for_policy_for_edit" id="floatingTextareaforsection" placeholder="Section"></input>
//                                             <label for="floatingTextareaforsection_for_edit${$('.accordion-item_edit').length + 1}" class="fw-bolder">Section</label>
//                                     </div>

//                                     <div class="form-group abba_local-forms">
//                                         <textarea class="form-control mymce floatingTextareaforsection_description_edit" placeholder="Description" id="floatingTextareafordescriptionsedit${$('.accordion-item_edit').length + 1}></textarea>
//                                         <label for="floatingTextareafordescriptionsedit${$('.accordion-item_edit').length + 1}" class="fw-bolder">Description</label>
//                                     </div>
//                                 </div>
//                             </div>
//                         </div>
//                     </div>
//                 </div>
//             </div>
//         </div>`;

//     // Append the new section for edit
//     var $newSectionforedit = $(document.createElement('div')).append(emma_add_new_session_for_edit);
//     $('#emmaloadcontentnewforedit').append($newSectionforedit);

//     $('.mymce').summernote();
// }


// $("body").on("click",".emmacollapsesectionforaddnewedit",function(){
//     emma_add_section_for_edit();
// });

// $("body").on("click", ".emma_remove_section_for_edit", function () {
//     $(this).parent().remove(); // Remove the section
// });

// $("body").on("click", ".emma_edit_icon_for_policy", function () {

//     $('#emmaloaddescriptionforedit').html('');

//     var emma_data_id_for_edit = $(this).data('emmapolicy_edit_dataid');
//     var emma_input_policy_edit_title = $(this).data('emmapolicy_edit_title');
//     var emma_input_policy_edit_description = $(this).data('emma_edit_policy_description');
//     var emma_input_policy_edit_section = $(this).data('emma_policy_edit_section');
//     var emma_input_policy_edit_section_description = $(this).data('emma_edit_section_description');

//     var sectionArray = [];
//     var descriptionArray = [];

 

//     if (
//         emma_input_policy_edit_section != '' && 
//         emma_input_policy_edit_section != 'NULL' || 
//         emma_input_policy_edit_section_description != '' && 
//         emma_input_policy_edit_section_description != 'NULL'
//     ) {
//         var sectionArray = emma_input_policy_edit_section.split('###');
//         var descriptionArray = emma_input_policy_edit_section_description.split('###');

  
//         var emmacombinedContentforedit = ''; 

    
//         for (var i = 0; i < sectionArray.length; i++) {
//             var section = sectionArray[i];
//             var description = descriptionArray[i];
    
//             emmacombinedContentforedit += `
//                 <div class="form-group abba_local-forms mt-3">
//                     <input class="form-control emma_section_for_policy_for_edit" id="floatingTextareaforsection_for_edit" value="${section}" placeholder="Section"></input>
//                     <label for="floatingTextarea" class="fw-bolder"> Section </label>
//                     <br>
//                     <textarea class="form-control mymce floatingTextareaforsection_description_edit mt-4" placeholder="Description">${description}</textarea>
//                     <label for="floatingTextarea" class="fw-bolder"> Description </label>
//                 </div>`;
//         }
        
     
//         $('#emmaloaddescriptionforedit').append(emmacombinedContentforedit);
//         $('.mymce').summernote();

//     }

//     $(".emmadataidforedit").val(emma_data_id_for_edit);
//     $(".emma_edit_policy_title").val(emma_input_policy_edit_title);
//     $(".emma_edit_policy_description").val(emma_input_policy_edit_description);
// });



// $("body").on("click",".emma_edit_policy_button",function(){

//     $(".emma_edit_policy_button").html('<div class="spinner-border text-white spinner-border-sm" role="status"><span class="visually-hidden">Loading...</span></div>');
//        var emma_get_edit_data_id = $(".emmadataidforedit").val();
//        var emma_get_edit_title = $(".emma_edit_policy_title").val();
//        var emma_get_edit_description = $(".emma_edit_policy_description").val();

//        var emmapolicysections_for_edit = [];
//        var emmapolicysections_descriptions_for_edit = [];


//         $.each($(".emma_section_for_policy_for_edit"), function () {

//             var emma_policy_for_section_edit = $(this).val();

//                 if (emma_policy_for_section_edit == 'NULL' || emma_policy_for_section_edit == '') {
//                     emmapolicysections_for_edit.push('');
//                     $("#accordionExampleforedit").hide();
//                 } else {
//                     emmapolicysections_for_edit.push($(this).val()+'###');
//                 }
//         });
        
//         $.each($(".floatingTextareaforsection_description_edit"), function () {

//             var emma_policy_for_section_description_edit = $(this).val();
        
//                 if (emma_policy_for_section_description_edit == 'NULL' || emma_policy_for_section_description_edit == '') {
//                     emmapolicysections_descriptions_for_edit.push('');
//                     $("#accordionExampleforedit").hide();
//                 } else {
//                     emmapolicysections_descriptions_for_edit.push($(this).val()+'###');
//                 }
//         });

//         var datastring = "emma_edit_id=" + emma_get_edit_data_id + "&emma_edit_title=" + emma_get_edit_title +
//         "&emma_edit_description=" + emma_get_edit_description + "&emma_edit_section=" + emmapolicysections_for_edit +
//         "&emma_edit_section_description=" + emmapolicysections_descriptions_for_edit;
     

//         emmapolicysections_for_edit = emmapolicysections_for_edit.toString();
//         emmapolicysections_descriptions_for_edit = emmapolicysections_descriptions_for_edit.toString();

//        $.ajax({
//            type:'POST',
//            url:'../../controller/scripts/student/policy/emma_edit_for_policy.php',
//            data:datastring,
//            success:function(outcome){
//                $(".emma_edit_policy_button").html('<i class="fas fa-edit text-white"> Edit Policy</i>');
//             if(outcome == 1){
//                 $.wnoty({
//                     type: 'success',
//                     message: "Policy Edited successfully.",
//                     autohideDelay: 5000
//                 });
//             }else{
//                 $.wnoty({
//                     type: 'warning',
//                     message: "Policy Not Edited.",
//                     autohideDelay: 500031
//                 });
//             }
//            }
//        });
// });

// $("body").on("click",".emma_delete_icon_for_policy",function(){
//     var emma_institution_for_delete = $(".abba-change-institution").val();
    
//     $.ajax({
//         type:'POST',
//         url:'../../controller/scripts/student/policy/emma_display_delete_image.php',
//         data:{institution:emma_institution_for_delete},
//         success:function(result){
//             $(".emma_load_delete_container_for_policy").html(result);
//         }
//     })
// });

// $("body").on("click",".emma_delete_icon_for_policy",function(){
//     var data_id_for_delete = $(this).data('idforpolicydelete');

//     $("#deletedataidforpolicy").val(data_id_for_delete);
// });

// $("body").on("click",".emma_delete_for_policy_button",function(){
//     var emma_institution_for_delete_policy = $(".abba-change-institution").val();
//     var data_id_for_delete = $("#deletedataidforpolicy").val();

//     var datastring = "emma_institution_for_delete_policy=" + emma_institution_for_delete_policy + "&deletedata_id=" + data_id_for_delete;
  
//     $.ajax({
//         type:'POST',
//         url:'../../controller/scripts/student/policy/emma_delete_policy.php',
//         data:datastring,
//         success:function(results){
//             if(results == 1){
//                 $.wnoty({
//                     type: 'success',
//                     message: "Policy Deleted successfully.",
//                     autohideDelay: 5000
//                 });
//             }else{
//                 $.wnoty({
//                     type: 'warning',
//                     message: "Policy Not Deleted.",
//                     autohideDelay: 5000
//                 });
//             }
//         }
//     });
// });

// $("body").on("click",".emma_view_icon_for_policy",function(){
//     var data_id_for_view = $(this).data('idforpolicyview');
//     var emma_institution_for_view = $(".abba-change-institution").val();

//     var datastring = "emma_data_id_for_view=" + data_id_for_view + "&emma_institution_for_view=" + emma_institution_for_view;
    
//     $.ajax({
//         type:'POST',
//         url:'../../controller/scripts/student/policy/emma_view_for_policy.php',
//         data:datastring,
//         success:function(outcome){
//             $(".emmaloadviewdetails").html(outcome);
//         }
//     });
// });

// $('#publishbutton').on('click', function(){
//     if ($(this).hasClass('btn-success')) {
  
//         $(this).removeClass('btn-success').addClass('btn-danger').text('Unpublish');
//     } else if ($(this).hasClass('btn-danger')) {
    
//         $(this).removeClass('btn-danger').addClass('btn-success').text('Publish');
//     }
// });

// $("body").on("click",".emma_view_icon_for_policy",function(){
//     var emma_data_id_for_publish = $(this).data('idforpolicyview');
//     var emma_publishstatus = $(this).data('publishstatus');

//     $(".emma_data_id_for_publishing_status").val(emma_data_id_for_publish);
//     $(".emma_data_status_for_publishing_status").val(emma_publishstatus);
// });

// $("body").on("click", "#publishbutton", function(){
//     var emma_publish_data_id = $(".emma_data_id_for_publishing_status").val();
//     var emma_publish_status = $(".emma_data_status_for_publishing_status").val();
//     var emma_institution_for_status = $(".abba-change-institution").val();

//     var datastring = "view_publish_id=" + emma_publish_data_id + "&view_publish_status=" + emma_publish_status + "&emma_publish_institute=" + emma_institution_for_status;

//     $.ajax({
//         type: 'POST',
//         url: "../../controller/scripts/student/policy/emma_publish_for_policy.php",
//         data: datastring,
//         success: function(outcome) {
//             var publishButton = $("#publishbutton");
//             if(outcome == 1){
                
//                 $(".emma_data_status_for_publishing_status").val(2);
//                 publishButton.text("Publish").css("background-color", "#2EA243");
//                 $.wnoty({
//                     type: 'success',
//                     message: "Policy Unpublished.",
//                     autohideDelay: 5000
//                 });
                
//             }else if(outcome == 2){
//                 $(".emma_data_status_for_publishing_status").val(1);
//                 publishButton.text("Unpublish").css("background-color", "#DE0303");
//                 $.wnoty({
//                     type: 'success',
//                     message: "Policy Published.",
//                     autohideDelay: 5000
//                 });
//             }else{

//             }
//         }
//     });
// });


// $("body").on("click",".emma_view_icon_for_policy",function(){
//      var emma_get_publish_status = $(this).val();

//     if(emma_get_publish_status == 1){
//             $("body").on("click",".#publishbutton",function(){

//         $('#publishbutton').on('click', function(){
//         //     $(this).removeClass('btn-danger').addClass('btn-success').text('Publish');
//         }
//     }else{
//         // $('#publishbutton').on('click', function(){
//         //     $(this).removeClass('btn-success').addClass('btn-danger').text('Unpublish');
//         // }
//     }
// });

// $("body").on("click", "#myCheckboxforview", function() {
//     if ($(this).is(":checked")) {
//         $("#continueButton").prop("disabled", false);
//     } else {
//         $("#continueButton").prop("disabled", true);
//     }
// });



// $("body").on("click","#continueButton", function() {
//     if (!$(this).prop("disabled")) {
//         $("#emma_modal_for_view").modal('hide');
//     } else {
//         $.wnoty({
//             type: 'warning',
//             message: "Accept terms to continue",
//             autohideDelay: 5000
//         });
//     }
// });