$(document).ready(function(){
  console.log("in doc ready");
  $("#tc").click(function(e){
    //alert("The paragraph was clicked.");
	e.preventDefault();
	//console.log("tc clicked ", $("#sid").val(), $('#stuname').val(), $('#class').val(), $('#imgfile')[0].files[0])
        var formData = new FormData();

        formData.append('sid', $('#sid').val());
        formData.append('stuname', $('#stuname').val());
        formData.append('class', $('#class').val());
        formData.append('image', $('#imgfile')[0].files[0])
	//console.log("form data ", formData);

	/*for (var key of formData.entries()) {
        	console.log(key[0] + ', ' + key[1]);
    	}*/

        $.ajax({
            
            type: 'POST',
            url: 'tcupload.php',
            data: formData,
            processData: false, contentType: false,
            success: function(result) {
                console.log("Student record added successfully", result);
		let msg = "";
		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 (result == 1) {msg = "Fields can not be empty";toastr["warn"]( msg );}
		if (result == 2) {msg = "Student record already exist";toastr["warning"]( msg );}
		if (result == 3) {msg = "Student record added successfully";toastr["success"]( msg );}
		if (result == 4) {msg = "Failed to add student record";toastr["error"]( msg );}
                
		$('#sid').val('');
		$('#stuname').val('');
		$('#class').val('');
		$('#imgfile').val('');
            },
            
            error: function() {
                console.log("unsuccessful");
                //swal("Failed to register Student ","", "error");

		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"
                }
                toastr["error"]( "Something went wrong, Failed to add student");

            }
        });
	
    });

    var id = "";
    $(".delm").click(function(e){
       id = $(this).closest('tr').find("#id").context.id;
       $("#delmodal").modal();
     });


    function delTc(id) {
	$.ajax({
	    async: false,
            type: 'POST',
            url: 'deltc.php',
            data: {token: id},
            success: function(result) {
		let msg = "";
		if (result == 1) {msg = "ID can not be empty"}
		if (result == 2) {msg = "Student record deleted successfully"}
		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"
                }
                toastr["success"]( msg);
		
	        getall();
                
            },
            error: function() {
		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"
                }
                toastr["error"]( "Something went wrong, Failed to delete");
            }
	});

    }


    function getall() {               
	setTimeout(() => {  console.log("Reloading"); }, 2000);
	window.location.reload()
	//console.log("in get all");
    	/*$.ajax({
                type: 'GET',
                url: 'getall.php',
                success: function(result) {
			let res = JSON.parse(result);
                },
                error: function(error) {
			console.log("error ");
                }
    	 });*/
    }
    
	
    $(".delpopup").click(function(e) {
	  delTc(id);

    });
    


});

 /*qwest.post('deltc.php', {
       token: id,
 })
 .then(function(xhr, response) {
           console.log("Deleted successfully", response);
 })
.catch(function(e, xhr, response) {
       console.log("Failed to delete","", "error", response);
});*/
