(function(e){
"use strict";
var starsArr = [];
// var Arr = [];
var options = {
max_value: 5,
step_size: 1,
}
// function createAnswerField(){
// $.each(questions.questions, function(key,value){
// $('.container-fluid', function(){
// $('.q-list2').append('
'); $('.rating-'+key+'').rate(options);
// $(this).on('change','.rating-'+key+'', function(e, data){
// // $("#output-"+qst_id).val(data.to);
// var name = "Arr"+value.qst;
// name = [];
// starsArr.push(name = [
// value.qst_id,
// data.to
// ]);
// });
// });
// });
// }
function createAnswerField(){
$.each(questions.questions, function(key,value){
var count = key + 1;
var formattedQ = value.title.replace(/\\n/g,"
");
$('.container-fluid', function(){
$('.q-list2').append('
');
$('.rating-'+key+'').rate(options);
$(this).on('change','.rating-'+key+'', function(e, data){
// Find the index of the current question in the array
var index = starsArr.findIndex(function(element){
return element[0] == value.qst_id;
});
// Update the existing array element or create a new one
if(index == -1){
var name = "Arr"+value.qst;
name = [];
starsArr.push(name = [
value.qst_id,
data.to
]);
}
else{
starsArr[index][1] = data.to;
}
});
});
// var count = key + 1;
// var formattedQ = value.title.replace(/\\n/g, "
");
// var questionDiv = $('');
// var questionLabel = $('').attr("id", value.qst_id).html(count + ". " + formattedQ);
// var ratingDiv = $('').rate(options);
// questionDiv.append(questionLabel, '
', ratingDiv);
// $('.q-list2').append(questionDiv, '
');
// ratingDiv.on('change', function(e, data) {
// var index = starsArr.findIndex(function(element) {
// return element[0] == value.qst_id;
// });
// if (index == -1) {
// starsArr.push([value.qst_id, data.to]);
// } else {
// starsArr[index][1] = data.to;
// }
// });
});
}
function automaticLogout(){
$.ajax({
type: "POST",
url: "/customLogout",
dataType: 'json',
data: null,
processData: false,
contentType: false,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function(result){
window.location.href = "/thankyou";
},
error: function(error){
}
});
}
// function SubmitReview(){
// $('#submitReview').on('click', function(e){
// e.preventDefault();
// var formData = new FormData();
// formData.append('t_id',t_id);
// formData.append('s_id',s_id);
// formData.append('q_id',qst_id);
// formData.append('stars',JSON.stringify(starsArr));
// formData.append('comment',$('#comment').val());
// if(starsArr.length < questions.questions.length){
// Swal.fire({
// title: 'Error!',
// text: 'Please rate every question',
// });
// return;
// } else {
// Swal.fire({
// title: 'Loading...',
// allowOutsideClick: false,
// didOpen: () => {
// Swal.showLoading()
// // $('#submitReview').attr('disabled',true);
// }
// });
// $.ajax({
// type: "POST",
// url: "/response",
// dataType: 'json',
// data: formData,
// processData: false,
// contentType: false,
// headers: {
// 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
// },
// beforeSend: function() {
// console.log("loading");
// if (!Swal.isLoading()) {
// Swal.showLoading();
// }
// },
// success: function(success){
// Swal.close();
// Swal.fire({
// title: 'Success!',
// text: 'Thank you for your response',
// });
// automaticLogout();
// setTimeout(() => {
// window.location.href = "/thankyou";
// },3000);
// },
// error: function(error){
// Swal.close();
// Swal.fire({
// title: 'Error',
// text: error.message
// });
// }
// });
// }
// });
// }
// 2974
function SubmitReview() {
$('#submitReview').on('click', function(e) {
e.preventDefault();
var formData = new FormData();
formData.append('t_id', t_id);
formData.append('s_id', s_id);
formData.append('q_id', qst_id);
formData.append('stars', JSON.stringify(starsArr));
formData.append('comment', $('#comment').val());
var unfilledQuestions = [];
// Check if all questions have been answered
for (var i = 0; i < questions.questions.length; i++) {
if (!starsArr.some(a => a[0] === questions.questions[i].qst_id)) {
unfilledQuestions.push(questions.questions[i].qst_id);
}
}
if (unfilledQuestions.length > 0) {
var unfilledQuestionIds = "#" + unfilledQuestions.join(", #");
var firstUnfilledQuestionId = "#" + unfilledQuestions[0];
var offset = $(unfilledQuestionIds).first().offset().top;
$('html, body').animate({
scrollTop: offset - 50
}, 500, function() {
$(firstUnfilledQuestionId).focus();
});
toastRWithTime("Please rate the following questions:
' + unfilledQuestions.join(", ") + ''",'error');
return;
}
Swal.fire({
title: 'Loading...',
allowOutsideClick: false,
didOpen: () => {
Swal.showLoading()
// $('#submitReview').attr('disabled',true);
}
});
$.ajax({
type: "POST",
url: "/response",
dataType: 'json',
data: formData,
processData: false,
contentType: false,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
beforeSend: function() {
console.log("loading");
if (!Swal.isLoading()) {
Swal.showLoading();
}
},
success: function(success) {
Swal.close();
Swal.fire({
title: 'Success!',
text: 'Thank you for your response',
});
automaticLogout();
setTimeout(() => {
window.location.href = "/thankyou";
}, 3000);
},
error: function(error) {
Swal.close();
Swal.fire({
title: 'Error',
text: error.message
});
}
});
});
}
$(function(){
createAnswerField();
SubmitReview();
// $('#proceed_rating').on('click', (e) => {
// e.preventDefault();
// if($('#data-privacy').prop("checked")){
// $('.data-privacy').hide();
// $('.q-list').show();
// }else{
// toastRWithTime('Please agree to the terms and agreements first','error');
// }
// });
});
})();