<?php

session_start();
include_once "../config/database.php";
include_once '../security/session_config.php';
include_once '../security/csrf.php';
// include objects
include_once "../resources/survey.php";
$csrf_token = $_POST['csrf_token'];
validate_csrf_request($csrf_token);
$conn = DBConnection();
$survey = new Survey();
$conn->beginTransaction();

    if (isset($_SESSION['uid'])) {
        $id = $_SESSION['uid'];
    }
    $survey_id= $_POST['survey_id'];
    $allanswers = $_POST['allanswers'];

    $survey->survey_id = $survey_id;
    $survey->user_id = $id;
    
    $json = json_decode($allanswers);
//   print_r($json);
    
    foreach ($json as $item)
    {
        echo $item->qid;
        $survey->question_id = $item->qid;
        $question_type = $survey->getSurvey_question_typeid();        
        foreach ($item->option as $opt)
        {
            if ($question_type == "2" || $question_type == "3" || $question_type == "4") {
                if($opt->s_optid !=""){
                    if($opt->comment === ""){ 
                        $opt->comment = NULL;
                    }
                    $survey->comment = $opt->comment;
                    $survey->user_response = $opt->s_optid;
                    $survey->last_updated_ts = $date_time;
                    if ($survey->insert_survey_response()) {

                    } else {
                        $conn->rollBack();
                        echo 'error';
                        return -1;
                    }
                }
            }
            if ($question_type == "1" || $question_type == "5" ) {
                if ($opt->comment != "") {
                    $survey->comment = $opt->comment;
                    $survey->user_response = '0';
                    $survey->last_updated_ts = $date_time;
                    if ($survey->insert_survey_response()) {

                    } else {
                        $conn->rollBack();
                        echo 'error';
                        return -1;
                    }
                }
                
            }
            if ($question_type == "6" || $question_type == "7" ) {             
                    $survey->comment = $opt->s_optid;
                    $survey->user_response = $opt->optid;
                    $survey->last_updated_ts = $date_time;
                    if ($survey->insert_survey_response()) {

                    } else {
                        $conn->rollBack();
                        echo 'error';
                        return -1;
                    }               
                
            }
            if ($question_type == "8") {    
                if($opt->s_optid === ""){ 
                        $opt->s_optid = NULL;
                    }
                    $survey->comment =  $opt->s_optid;
                    $survey->user_response = $opt->optid;
                    $survey->last_updated_ts = $date_time;
                    if ($survey->insert_survey_response()) {

                    } else {
                        $conn->rollBack();
                        echo 'error';
                        return -1;
                    }               
                
            }

        }
    }
     $conn->commit();
    

?>