<?php include 'inc_classes.php';
$action = $_POST['action'];  
if ($action == 'submit_feedback') {
    $added_date = date('Y-m-d');
    $sel_branchs1 = "SELECT cm_id FROM site_setting WHERE admin_id='".$_SESSION['admin_id']."'";
    $ptr_branchs1 = mysql_query($sel_branchs1);
    $branch_datas1 = mysql_fetch_array($ptr_branchs1);

    $feedbackArray = json_decode($_POST['array_value'], true);
    $feedbackIds = mysql_real_escape_string($_POST['feedbackIds']);
    $comment = mysql_real_escape_string($_POST['comment']);
    $totQue = (int)$_POST['totQue'];

    $feedbackValue = 0;
    foreach ($feedbackArray as $feedback) 
    {
        $questionId = mysql_real_escape_string($feedback['questionId']);
        $value = (int)$feedback['value'];

        $query = "INSERT INTO emp_feedback (feedback_id, question_id, feedback, admin_id, cm_id, added_date) VALUES ('$feedbackIds', '$questionId', '$value', '{$_SESSION['admin_id']}', '{$branch_datas1['cm_id']}', '$added_date')";
        $ptr_query = mysql_query($query);
        $feedbackValue += $value;
    }

    $percentage = ($feedbackValue / ($totQue * 100)) * 100;

    $Percquery = "INSERT INTO emp_feedback_percentage (feedback_id, percentage, comment, admin_id, cm_id, added_date) VALUES ('$feedbackIds', '$percentage', '$comment', '{$_SESSION['admin_id']}', '{$branch_datas1['cm_id']}', '$added_date')";
    $ptr_Percquery = mysql_query($Percquery);

    if ($ptr_query && $ptr_Percquery) {
        echo "Feedback submitted successfully!";
    } else {
        echo "Error in submitting feedback.";
    }
}
?>