$(document).ready(function(){
  $("#addsub").submit(function(e){
	e.preventDefault();
	var form = $(this);

    	$.ajax({
           type: "POST",
           url: 'bcreate-subject.php',
           data: form.serialize(), // serializes the form's elements.
           success: function(res)
           {
		resPars = JSON.parse(res);
		toastr.options = {                   	
                  "closeButton": true,
                  "debug": false,
                  "newestOnTop": false,
                  "progressBar": false,
                  "positionClass": "toast-top-right",
                  "preventDuplicates": false,
                  "onclick": null,
                  "showDuration": "300",
                  "hideDuration": "1000",
                  "timeOut": "5000",
                  "extendedTimeOut": "1000",
                  "showEasing": "swing",
                  "hideEasing": "linear",
                  "showMethod": "fadeIn",
                  "hideMethod": "fadeOut"
                }
                if (resPars.warning) {toastr["warning"]( resPars.warning );}
                if (resPars.success) {toastr["success"]( resPars.success );}
                if (resPars.error) {toastr["error"]( resPars.error );}

           },
           error: function() {
                console.log("unsuccessful");

	   }
        });
  })

  $("#editsub").submit(function(e){
        e.preventDefault();
        var form = $(this);
	let queryString = window.location.search;
                                          
    	$.ajax({
           type: "POST",
           url: 'bsub-edit.php?subjectid='+  queryString.substring(1).split("=")[1],
           data: form.serialize(), // serializes the form's elements.
           success: function(res)
           {
        	resPars = JSON.parse(res);
        	toastr.options = {                   	
                  "closeButton": true,
                  "debug": false,
                  "newestOnTop": false,
                  "progressBar": false,
                  "positionClass": "toast-top-right",
                  "preventDuplicates": false,
                  "onclick": null,
                  "showDuration": "300",
                  "hideDuration": "1000",
                  "timeOut": "5000",
                  "extendedTimeOut": "1000",
                  "showEasing": "swing",
                  "hideEasing": "linear",
                  "showMethod": "fadeIn",
                  "hideMethod": "fadeOut"
                }
                if (resPars.warning) {toastr["warning"]( resPars.warning );}
                if (resPars.success) {toastr["success"]( resPars.success );}
                if (resPars.error) {toastr["error"]( resPars.error );}
                                                                             
           },
           error: function() {
                console.log("unsuccessful");
                                                                             
           }
        });
  })


  var id = "";                                          
  $(".delpopup").click(function(e) {
      delSub(id);
  
  });

  $(".delc").click(function(e){
     console.log("del clicked");
     id = $(this).closest('tr').find("#id").context.id;
     $("#delmodal").modal();
   });

   function delSub(id) {
       $.ajax({
           async: false,
           type: 'POST',
           url: 'delsub.php',
           data: {token: id},
           success: function(result) {
               getall();
               
           },
           error: function(err) {
		console.log("error ", err);
           }
       });
   }


    function getall() {               
	setTimeout(() => {  console.log("Reloading"); }, 2000);
	window.location.reload()
    }

});
