<?php

/**
 * The public-facing functionality of the plugin.
 *
 * @link       http://ays-pro.com/
 * @since      1.0.0
 *
 * @package    Survey_Maker
 * @subpackage Survey_Maker/public
 */

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    Survey_Maker
 * @subpackage Survey_Maker/public
 * @author     AYS Pro LLC <info@ays-pro.com>
 */
class Survey_Maker_History_Page
{

    /**
     * The ID of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string $plugin_name The ID of this plugin.
     */
    protected $plugin_name;

    /**
     * The version of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string $version The current version of this plugin.
     */
    private $version;


    protected $settings;
    protected $html_class_prefix;

    /**
     * Initialize the class and set its properties.
     *
     * @since    1.0.0
     * @param      string $plugin_name The name of the plugin.
     * @param      string $version The version of this plugin.
     */
    public function __construct($plugin_name, $version){

        $this->plugin_name = $plugin_name;
        $this->version = $version;

        $this->html_class_prefix = 'ays-survey-';
        $this->settings = new Survey_Maker_Settings_Actions($this->plugin_name);

        add_shortcode('ays_survey_user_history', array($this, 'ays_generate_user_history_method'));

    }

    /**
     * Register the stylesheets for the public-facing side of the site.
     *
     * @since    1.0.0
     */
    public function enqueue_styles(){

        /**
         * This function is provided for demonstration purposes only.
         *
         * An instance of this class should be passed to the run() function
         * defined in Survey_Maker_Loader as all of the hooks are defined
         * in that particular class.
         *
         * The Survey_Maker_Loader will then create the relationship
         * between the defined hooks and the functions defined in this
         * class.
         */
		
		// wp_enqueue_style( $this->plugin_name . '-font-awesome', '//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css', array(), $this->version, 'all');
        // wp_enqueue_style( $this->plugin_name . '-font-awesome-icons', SURVEY_MAKER_ADMIN_URL . '/css/ays-font-awesome.css', array(), $this->version, 'all');
        wp_enqueue_style( $this->plugin_name . "-font-awesome", SURVEY_MAKER_PUBLIC_URL . '/css/survey-maker-font-awesome.min.css', array(), $this->version, 'all' );
		wp_enqueue_style( $this->plugin_name . '-user-history', SURVEY_MAKER_PUBLIC_URL . '/css/partials/survey-maker-user-history.css', array(), $this->version, 'all');
        wp_enqueue_style( $this->plugin_name . '-dataTable-min', SURVEY_MAKER_PUBLIC_URL . '/css/survey-maker-dataTables.min.css', array(), $this->version, 'all');
    }


    /**
     * Register the JavaScript for the public-facing side of the site.
     *
     * @since    1.0.0
     */
    public function enqueue_scripts() {

        /**
         * This function is provided for demonstration purposes only.
         *
         * An instance of this class should be passed to the run() function
         * defined in Survey_Maker_Loader as all of the hooks are defined
         * in that particular class.
         *
         * The Survey_Maker_Loader will then create the relationship
         * between the defined hooks and the functions defined in this
         * class.
         */

        wp_enqueue_script( $this->plugin_name . '-datatable-min', SURVEY_MAKER_PUBLIC_URL . '/js/survey-maker-datatable.min.js', array('jquery'), $this->version, true);
        wp_enqueue_script( $this->plugin_name . '-user-history-public', SURVEY_MAKER_PUBLIC_URL . '/js/partials/survey-maker-public-user-history.js', array('jquery'), $this->version, true);

        wp_localize_script( $this->plugin_name . '-user-history-public', 'aysSurveyMakerAjaxUserHistory', array(
            'ajaxUrl' => admin_url('admin-ajax.php'),
        ));

        wp_localize_script( $this->plugin_name . '-user-history-public', 'surveyLangDataTableObj', array(
            "sEmptyTable"           => __( "No data available in table", $this->plugin_name ),
            "sInfo"                 => __( "Showing _START_ to _END_ of _TOTAL_ entries", $this->plugin_name ),
            "sInfoEmpty"            => __( "Showing 0 to 0 of 0 entries", $this->plugin_name ),
            "sInfoFiltered"         => __( "(filtered from _MAX_ total entries)", $this->plugin_name ),
            // "sInfoPostFix":          => __( "", $this->plugin_name ),
            // "sInfoThousands":        => __( ",", $this->plugin_name ),
            "sLengthMenu"           => __( "Show _MENU_ entries", $this->plugin_name ),
            "sLoadingRecords"       => __( "Loading...", $this->plugin_name ),
            "sProcessing"           => __( "Processing...", $this->plugin_name ),
            "sSearch"               => __( "Search:", $this->plugin_name ),
            // "sUrl":                  => __( "", $this->plugin_name ),
            "sZeroRecords"          => __( "No matching records found", $this->plugin_name ),
            "sFirst"                => __( "First", $this->plugin_name ),
            "sLast"                 => __( "Last", $this->plugin_name ),
            "sNext"                 => __( "Next", $this->plugin_name ),
            "sPrevious"             => __( "Previous", $this->plugin_name ),
            "sSortAscending"        => __( ": activate to sort column ascending", $this->plugin_name ),
            "sSortDescending"       => __( ": activate to sort column descending", $this->plugin_name ),
            "all"                   => __( "All", $this->plugin_name ),
        ) );
    }

    /*
    ==========================================
        User History shortcode
    ==========================================
    */

    public function get_user_reports_info(){
        global $wpdb;

        $current_user = wp_get_current_user();
        $id = $current_user->ID;
        if($id == 0){
            return null;
        }

        $surveys_table     = esc_sql( $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "surveys" );
        $submissions_table = esc_sql( $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions" );

        $sql = "SELECT survey.title, s.survey_id, s.submission_date,s.id
                FROM $submissions_table AS s
                LEFT JOIN $surveys_table AS survey
                ON s.survey_id = survey.id
                WHERE s.user_id=$id
                ORDER BY s.id DESC";

        $results = $wpdb->get_results($sql, "ARRAY_A");

        return $results;

    }

    public function get_submission_by_id($submission_id){
        global $wpdb;

        $current_user = wp_get_current_user();
        $id = $current_user->ID;
        if($id == 0){
            return null;
        }

        $submissions_table = esc_sql( $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions" );

        $sql = "SELECT *
                FROM $submissions_table 
                WHERE user_id=" . esc_sql( absint( $id ) ) . " AND id=" . esc_sql( absint( $submission_id ) ).
                " ORDER BY id DESC";

        $results = $wpdb->get_row($sql, "ARRAY_A");

        return $results;

    }

    public function ays_survey_popup_submission(){
        $text_types = array(
            'text',
            'short_text',
            'number',
            'phone',
            'name',
            'hidden',
            'email',
            'date',
            'time',
            'date_time',
        );

        if(!isset($_POST) || empty($_POST)){
            echo json_encode(array('status' => false));
            wp_die();
        }
        
        $user_id = isset( $_POST['user_id'] ) && $_POST['user_id']  != "" ? esc_sql( absint($_POST['user_id']) ) : 0;
        
        if ($user_id <= 0 || $user_id != get_current_user_id()){
            echo json_encode(array('status' => false));
            wp_die();
        }
        $survey_id = isset( $_POST['survey_id'] ) && $_POST['survey_id']  != "" ? esc_sql( absint($_POST['survey_id']) ) : 0;
        $submission_id = isset( $_POST['submission_id'] ) && $_POST['submission_id']  != "" ? esc_sql( absint($_POST['submission_id']) ) : 0;

		$survey  = (array)Survey_Maker_Data::get_survey_by_id($survey_id); 
        $submission = $this->get_submission_by_id($submission_id);

		if( ($survey_id <= 0 || $submission_id <= 0) || (empty($survey) || empty($submission) ) ){
			echo json_encode(array('status' => false));
            wp_die();
		}
		
        $ays_survey_individual_questions = Survey_Maker_Data::ays_survey_individual_results_for_one_submission( $submission, $survey, false);
        $survey_options = isset( $survey['options'] ) && $survey['options'] != '' ? json_decode( $survey['options'], true ) : array();
        $survey_options[ 'survey_allow_html_in_section_description' ] = isset($survey_options[ 'survey_allow_html_in_section_description' ]) ? $survey_options[ 'survey_allow_html_in_section_description' ] : 'off';
        $survey_allow_html_in_section_description = (isset($survey_options[ 'survey_allow_html_in_section_description' ]) && $survey_options[ 'survey_allow_html_in_section_description' ] == 'on') ? true : false;
        
        $survey_for_charts = isset( $survey_options['survey_color'] ) && $survey_options['survey_color'] != '' ? esc_attr($survey_options['survey_color']) : "rgb(255, 87, 34)";
        if($survey_for_charts == 'rgba(0,0,0,0)'){
            $survey_for_charts = "rgba(0,0,0,1)";
        }
        
        $survey_options[ 'survey_allow_html_in_answers' ] = isset($survey_options[ 'survey_allow_html_in_answers' ]) ? $survey_options[ 'survey_allow_html_in_answers' ] : 'off';
        $allow_html_in_answers = (isset($survey_options[ 'survey_allow_html_in_answers' ]) && $survey_options[ 'survey_allow_html_in_answers' ] == 'on') ? true : false;

        ob_start();
        ?>
            <div class="question_result_container ays-survey-user-history-shortcode-user-details-main-box">
                <div class="ays_question_answer" style="position:relative;">
                    <div class="ays-survey-submission-sections">
						<?php
						$checked = '';
						$disabled = '';
						$selected = '';
						$color = '';
						$user_matrix_answer = array();
						$user_star_list_answer = array();
						$user_slider_list_answer = array();
						if( is_array( $ays_survey_individual_questions['sections'] ) ):
							foreach ($ays_survey_individual_questions['sections'] as $section_key => $section) {
								?>
                                <div class="ays-survey-submission-section">
                                    <div class="ays_survey_name" style="border-top-color: <?php echo $survey_for_charts; ?>;">
                                        <h3><?php echo stripslashes( $section['title'] ); ?></h3>
                                        <p><?php echo ($survey_allow_html_in_section_description) ? strip_tags(htmlspecialchars_decode($section['description'] )) : nl2br( $section['description'] ) ?></p>
                                    </div>
									<?php
									foreach ( $section['questions'] as $q_key => $question ) {
										?>
                                        <div class="ays_questions_answers" data-id="<?php echo $question['id']; ?>"  data-type="<?php echo $question['type']; ?>" style="border-left-color: <?php echo $survey_for_charts; ?>;">
                                            <div style="font-size: 18px; margin: 10px 0;"><?php echo stripslashes( nl2br( $question['question'] ) ); ?></div>
											<?php
											$question_type_content = '';
											$user_answer = isset( $ays_survey_individual_questions['questions'][ $question['id'] ] ) ? $ays_survey_individual_questions['questions'][ $question['id'] ] : '';

											$user_explanation = isset( $ays_survey_individual_questions['questions'][ $question['id'] ]['user_explanation'] ) ? $ays_survey_individual_questions['questions'][ $question['id'] ]['user_explanation'] : '';
											$user_explanation = stripslashes( $user_explanation );
											$enable_user_explanation = false;
											if(isset( $question['options'] )){
												$enable_user_explanation = isset( $question['options']['user_explanation'] ) && $question['options']['user_explanation'] == "on" ? true : false;
											}

											if($question['type'] == 'matrix_scale' || $question['type'] == 'matrix_scale_checkbox'){
												$user_matrix_answer = isset( $ays_survey_individual_questions['questions'][ $question['id'] ]['answer_ids'] ) ? $ays_survey_individual_questions['questions'][ $question['id'] ]['answer_ids'] : array();
												$matrix_column_ids = array();
												if( isset( $question['options'] ) ){
													if( is_array( $question['options']['matrix_columns'] ) ){
														$matrix_column_ids = $question['options']['matrix_columns'];
													}else{
														$matrix_column_ids = json_decode($question['options']['matrix_columns'] , true);
													}
												}
											}

											if($question['type'] == 'star_list'){
												$user_star_list_answer = isset( $ays_survey_individual_questions['questions'][ $question['id'] ]['star_list_answer_ids'] ) ? $ays_survey_individual_questions['questions'][ $question['id'] ]['star_list_answer_ids'] : array();
											}

											if($question['type'] == 'slider_list'){
												$user_slider_list_answer = isset( $ays_survey_individual_questions['questions'][ $question['id'] ]['slider_list_answer_ids'] ) ? $ays_survey_individual_questions['questions'][ $question['id'] ]['slider_list_answer_ids'] : array();
											}

											$other_answer = '';
											if( isset( $user_answer['otherAnswer'] ) ){
												$other_answer = $user_answer['otherAnswer'];
											}
											if( isset( $user_answer['answer'] ) ){
												$user_answer = $user_answer['answer'];
											}
											$question_type_content = '';
											if( $question['type'] == 'select' ){
												$question_type_content .= '<div class="ays_each_question_answer">
                                            <select class="ays-survey-submission-select" disabled>
                                                <option value="">' . __( "Choose", SURVEY_MAKER_NAME ) . '</option>';
											}

											if( in_array( $question['type'], $text_types ) ){
												if( !is_array($user_answer) ){
													$user_answer = $user_answer;
												}
												else{
													$user_answer = '';
												}
												$question_type_content .= '<div class="ays_each_question_answer">
                                            <p class="ays_text_answer">' . $user_answer . '</p>
                                        </div>';
											}

											if( $question['type'] == 'linear_scale' ){

												$linear_scale_label_1 = isset( $question['options']['linear_scale_1'] ) && $question['options']['linear_scale_1'] != '' ? $question['options']['linear_scale_1'] : '';
												$linear_scale_label_2 = isset( $question['options']['linear_scale_2'] ) && $question['options']['linear_scale_2'] != '' ? $question['options']['linear_scale_2'] : '';
												$linear_scale_length = isset( $question['options']['scale_length'] ) && $question['options']['scale_length'] != '' ? absint( $question['options']['scale_length'] ) : 5;

												$question_type_content .= '<div class="ays_each_question_answer">';

												$question_type_content .= '<div class="ays-survey-answer-linear-scale">
                                                <label class="ays-survey-answer-linear-scale-label">
                                                    <div class="ays-survey-answer-linear-scale-radio-label" dir="auto"></div>
                                                    <div class="ays-survey-answer-linear-scale-radio">' . stripslashes( $linear_scale_label_1 ) . '</div>
                                                </label>';

												for ($i=1; $i <= $linear_scale_length; $i++) {
													$checked = '';
													if( intval( $user_answer ) == $i ){
														$checked = 'checked';
													}

													$question_type_content .= '<label class="ays-survey-answer-label">
                                                        <div class="ays-survey-answer-linear-scale-radio-label" dir="auto">' . $i . '</div>
                                                        <div class="ays-survey-answer-linear-scale-radio">
                                                            <input type="radio" name="ays-survey-question-linear-scale-' . $question['id'] . '" disabled ' . $checked . ' value="'.$i.'" data-id="' . $i . '" >
                                                            <div class="ays-survey-answer-label-content">
                                                                <div class="ays-survey-answer-icon-content">
                                                                    <div class="ays-survey-answer-icon-ink"></div>
                                                                    <div class="ays-survey-answer-icon-content-1">
                                                                        <div class="ays-survey-answer-icon-content-2" style="border-color:'.$survey_for_charts.' !important;">
                                                                            <div class="ays-survey-answer-icon-content-3" style="border-color:'.$survey_for_charts.' !important;"></div>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </label>';
												}

												$question_type_content .= '<label class="ays-survey-answer-linear-scale-label">
                                                    <div class="ays-survey-answer-linear-scale-radio-label" dir="auto"></div>
                                                    <div class="ays-survey-answer-linear-scale-radio">' . stripslashes( $linear_scale_label_2 ) . '</div>
                                                </label>
                                            </div>
                                        </div>';
											}

											if( $question['type'] == 'star' ){

												$star_label_1 = isset( $question['options']['star_1'] ) && $question['options']['star_1'] != '' ? $question['options']['star_1'] : '';
												$star_label_2 = isset( $question['options']['star_2'] ) && $question['options']['star_2'] != '' ? $question['options']['star_2'] : '';
												$star_scale_length = isset( $question['options']['star_scale_length'] ) && $question['options']['star_scale_length'] != '' ? absint( $question['options']['star_scale_length'] ) : 5;

												$question_type_content .= '<div class="ays_each_question_answer">';

												$question_type_content .= '<div class="ays-survey-answer-star">
                                                <label class="ays-survey-answer-star-label">
                                                    <div class="ays-survey-answer-star-radio-label" dir="auto"></div>
                                                    <div class="ays-survey-answer-star-radio">' . stripslashes( $star_label_1 ) . '</div>
                                                </label>';

												for ($i=1; $i <= $star_scale_length; $i++) {
													$checked = '';
													$icon_class = 'ays_fa_star_o';
													if( intval( $user_answer ) >= $i ){
														$checked = 'checked';
														$icon_class = 'ays_fa_star';
													}

													$question_type_content .= '<label class="ays-survey-answer-label">
                                                        <div class="ays-survey-answer-star-radio-label" dir="auto">' . $i . '</div>
                                                        <div class="ays-survey-answer-star-radio">
                                                            <input type="radio" name="ays-survey-question-star-' . $question['id'] . '" disabled ' . $checked . ' value="'.$i.'" data-id="' . $i . '" >
                                                            <i class="ays_fa ' . $icon_class . ' ays-survey-star-icon"></i>
                                                        </div>
                                                    </label>';
												}

												$question_type_content .= '<label class="ays-survey-answer-star-label">
                                                    <div class="ays-survey-answer-star-radio-label" dir="auto"></div>
                                                    <div class="ays-survey-answer-star-radio">' . stripslashes( $star_label_2 ) . '</div>
                                                </label>
                                            </div>
                                        </div>';
											}
											if( $question['type'] == 'matrix_scale' || $question['type'] == 'matrix_scale_checkbox'){
												$question_type_content .= '<div class="ays-survey-answer-matrix-scale-main">
                                            <div class="ays-survey-answer-matrix-scale-container">';
											}
											if( $question['type'] == 'star_list' ){
												$star_list_stars_length = isset( $question['options']['star_list_stars_length'] ) && $question['options']['star_list_stars_length'] != '' ? absint( $question['options']['star_list_stars_length'] ) : 5;
												$question_type_content .= '<div class="ays-survey-answer-star-list-main">
                                            <div class="ays-survey-answer-star-list-container">';
											}
											if( $question['type'] == 'slider_list' ){


												$question_type_content .= '<div class="ays-survey-answer-slider-list-main">
                                            <div class="ays-survey-answer-slider-list-container">';
											}

											if( $question['type'] == 'range' ){

												$range_type_length = isset( $question['options']['range_length'] ) && $question['options']['range_length'] != '' ? esc_attr(intval($question['options']['range_length'])) : 100;
												$range_type_step_length = isset( $question['options']['range_step_length'] ) && $question['options']['range_step_length'] != '' ? esc_attr(intval($question['options']['range_step_length'])) : 1;
												$range_type_min_value     = (isset( $question['options']['range_min_value'] ) && $question['options']['range_min_value'] != '') ? esc_attr(intval($question['options']['range_min_value'])) : 0;
												$range_type_default_value = (isset( $question['options']['range_default_value'] ) && $question['options']['range_default_value'] != '') ? esc_attr(intval($question['options']['range_default_value'])) : 0;
												if($range_type_length == 0){
													$range_type_length = 100;
												}
												if($range_type_step_length == 0){
													$range_type_step_length = 1;
												}
												if($user_answer == ""){
													$user_answer = 0;
												}
												$user_range_answer = absint( $user_answer );
												$left = 0;

												$left = ( $user_range_answer - $range_type_min_value ) * 100 / ( $range_type_length - $range_type_min_value );
												// if(intval($user_range_answer) < ($range_type_length - $range_type_min_value) / 2){
												//     $left += 5;
												// }else{
												//     $left -= 5;
												// }
												// if($left < 0){
												//     $left = 0;
												// }

												$leftOffset = 'calc( ' .  $left . '% + ' . ( 9 - $left * 0.18 ) . 'px )';
												$question_type_content .= '<div class="ays_each_question_answer">';
												$question_type_content .= '<div class="ays-survey-answer-range-type-main">';
												$question_type_content .= '<div class="ays-survey-answer-range-type-min-max-val">' . __( 'Min', SURVEY_MAKER_NAME ) . ' ' . $range_type_min_value . '</div>';

												$question_type_content .= '<div class="ays-survey-answer-range-type-range">';
												$question_type_content .= '<span class="ays-survey-answer-range-type-info-text" style="left: '. $leftOffset .';">'.$user_range_answer.'</span>';
												$question_type_content .= '<input type="range" class="ays-survey-range-type-input" min="' . $range_type_min_value . '" max="'.$range_type_length.'" value="'.$user_range_answer.'" disabled>';
												$question_type_content .= '</div>';

												$question_type_content .= '<div class="ays-survey-answer-range-type-min-max-val">' . __( 'Max', SURVEY_MAKER_NAME ) . ' ' . $range_type_length . '</div>';
												$question_type_content .= '</div>';
												$question_type_content .= '</div>';
											}

											if( $question['type'] == 'upload' ){
												$file_name = "";
												$enable_upload_container = false;
												if($user_answer != '0'){
													$file_name = wp_basename($user_answer);
													$enable_upload_container = true;
												}
												$upload_container = $enable_upload_container ? "" : "display_none_not_important";
												$dont_show_box = $file_name ? "" : "display_none";
												$question_type_content .= '<div class="ays-survey-answer-upload-ready '.$upload_container.'" >';
												// if($file_name){
												$question_type_content .= '<a href="'.$user_answer.'" class="ays-survey-answer-upload-ready-link '.$dont_show_box.'" download="'.$file_name.'">'.$file_name.'</a>';
												// }
												$question_type_content .= '</div>';
											}



											$loop_iteration = 0;
											$width = 0;

											foreach ($question['answers'] as $key => $answer) {
												$checked = '';
												$selected = '';
												$disabled = 'disabled';
												$color = '#777';
												// $color = 'black';

												$answer_content = $allow_html_in_answers ? $answer['answer'] : htmlentities( $answer['answer'] );
												switch( $question['type'] ){
													case 'radio':
													case 'yesorno':
														if( intval( $user_answer ) == intval( $answer['id'] ) ){
															$checked = 'checked';
														}
														$question_type_content .= '<div class="ays_each_question_answer">
                                                    <label style="color:' . $color . '; margin: 7px 0;">
                                                        <input type="radio" ' . $checked . ' ' . $disabled . ' data-id="' . $answer['id'] . '"/>
                                                        <div class="ays-survey-answer-label-content">
                                                            <div class="ays-survey-answer-icon-content">
                                                                <div class="ays-survey-answer-icon-ink"></div>
                                                                <div class="ays-survey-answer-icon-content-1">
                                                                    <div class="ays-survey-answer-icon-content-2" style="border-color:'.$survey_for_charts.' !important;">
                                                                        <div class="ays-survey-answer-icon-content-3" style="border-color:'.$survey_for_charts.' !important;"></div>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                            <span style="font-size: 16px;">' . stripslashes( $answer_content ) . '</span> 
                                                        </div>
                                                    </label>
                                                </div>';
														break;
													case 'checkbox':
														if( is_array( $user_answer ) && !empty( $user_answer ) && in_array( $answer['id'], $user_answer ) ){
															$checked = 'checked';
														}elseif( intval( $user_answer ) == intval( $answer['id'] ) ){
															$checked = 'checked';
														}
														$question_type_content .= '<div class="ays_each_question_answer">
                                                    <label style="color:' . $color . '; margin: 7px 0;">
                                                        <input type="checkbox" ' . $checked . ' ' . $disabled . ' data-id="' . $answer['id'] . '"/>
                                                        <div class="ays-survey-answer-label-content">
                                                            <div class="ays-survey-answer-icon-content">
                                                                <div class="ays-survey-answer-icon-ink"></div>
                                                                <div class="ays-survey-answer-icon-content-1">
                                                                    <div class="ays-survey-answer-icon-content-2" style="border-color:'.$survey_for_charts.' !important;">
                                                                        <div class="ays-survey-answer-icon-content-3" style="border-color:'.$survey_for_charts.' !important;"></div>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                            <span style="font-size: 16px;">' . stripslashes( $answer_content ) . '</span> 
                                                        </div>
                                                    </label>
                                                </div>';
														break;
													case 'select':
														if( intval( $user_answer ) == intval( $answer['id'] ) ){
															$selected = 'selected';
														}
														$question_type_content .= '<option value=' . $answer['id'] . ' ' . $selected . '>' . stripslashes( $answer_content ) . '</option>';
														break;
													case 'matrix_scale':
													case 'matrix_scale_checkbox':
														$content = array();
														$row_spacer = '<div class="ays-survey-answer-matrix-scale-row-spacer"></div>';
														if($loop_iteration == 0){
															$content[] = '<div class="ays-survey-answer-matrix-scale-row">';
															$content[] = '<div class="ays-survey-answer-matrix-scale-column ays-survey-answer-matrix-scale-column-row-header"></div>';
															foreach($matrix_column_ids as $q_key => $q_value){
																$content[] = '<div class="ays-survey-answer-matrix-scale-column">' . $q_value . '</div>';
															}
															$content[] = "</div>";
															$content[] = $row_spacer;
														}

														$rows_content = array();
														$rows_content[] = '<div class="ays-survey-answer-matrix-scale-row">';
														$rows_content[] = '<div class="ays-survey-answer-matrix-scale-row-content">';
														$rows_content[] = '<div class="ays-survey-answer-matrix-scale-column ays-survey-answer-matrix-scale-column-row-header">' . stripslashes( $answer_content ) . '</div>';
														foreach($matrix_column_ids as $a_key => $a_value){
															$checked = '';
															$question_matrix_type = "radio";
															if($question['type'] == 'matrix_scale_checkbox'){
																$question_matrix_type = "checkbox";
																if(isset($user_matrix_answer[$answer['id']]) && in_array($a_key , $user_matrix_answer[$answer['id']])){
																	$checked = 'checked';
																}
															}
															else{
																if(isset($user_matrix_answer[$answer['id']]) && $user_matrix_answer[$answer['id']] == $a_key){
																	$checked = 'checked';
																}
															}
															$rows_content[] = '<div class="ays-survey-answer-matrix-scale-column ays_each_question_answer">';
															$rows_content[] = '<div class="ays-survey-answer-matrix-scale-column-content-wrap">';
															$rows_content[] = '<div class="ays-survey-answer-matrix-scale-column-content">';

															$rows_content[] = '<label class="ays-survey-answer-label ays-survey-answer-label-matrix-row">';
															$rows_content[] = '<input class="" type="'.$question_matrix_type.'" ' . $checked . ' ' . $disabled . ' data-id="' . $answer['id'] . '" data-col-id="'.$a_key.'">';
															$rows_content[] = '<div class="ays-survey-answer-label-content">';
															$rows_content[] = '<div class="ays-survey-answer-icon-content">';
															$rows_content[] = '<div class="ays-survey-answer-icon-ink"></div>';
															$rows_content[] = '<div class="ays-survey-answer-icon-content-1">';
															$rows_content[] = '<div class="ays-survey-answer-icon-content-2" style="border-color:'.$survey_for_charts.' !important;">';
															$rows_content[] = '<div class="ays-survey-answer-icon-content-3" style="border-color:'.$survey_for_charts.' !important;"></div>';
															$rows_content[] = '</div>';
															$rows_content[] = '</div>';
															$rows_content[] = '</div>';
															$rows_content[] = '</div>';
															$rows_content[] = '</label>';

															$rows_content[] = '</div>';
															$rows_content[] = '</div>';
															$rows_content[] = '</div>';
														}
														$rows_content[] = '</div>';
														$rows_content[] = '</div>';
														$rows_content[] = $row_spacer;

														$content[] = implode( '', $rows_content );

														$question_type_content .= implode( '', $content );
														break;
													case 'star_list':
														$content = array();
														$star_list_content = array();

														$row_spacer = '<div class="ays-survey-answer-star-list-row-spacer"></div>';
														if($loop_iteration == 0){
															$star_list_content[] = '<div class="ays-survey-answer-star-list-row">';
															$star_list_content[] = '<div class="ays-survey-answer-star-list-column ays-survey-answer-star-list-column-row-header"></div>';
															$star_list_content[] = '<div class="ays-survey-answer-star ays-survey-answer-stars-star-list">';
															for($i=1; $i <= $star_list_stars_length; $i++){
																$checked = '';
																$icon_class = 'ays_fa_star_o';
																if( isset( $user_star_list_answer[$answer['id']] ) && intval( $user_star_list_answer[$answer['id']] ) >= $i ){
																	$checked = 'checked';
																	$icon_class = 'ays_fa_star';
																}
																$star_list_content[] = '<label class="ays-survey-answer-label" style="margin:0">
                                                                        <div class="ays-survey-answer-star-radio-label" dir="auto">' . $i . '</div>
                                                                            
                                                                        </label>';
															}
															$star_list_content[] = "</div>";
															$star_list_content[] = "</div>";
															$star_list_content[] = $row_spacer;
														}
														$star_list_content[] = '<div class="ays-survey-answer-star-list-row">';
														$star_list_content[] = '<div class="ays-survey-answer-star-list-row-content">';

														$star_list_content[] = '<div class="ays-survey-answer-star-list-column ays-survey-answer-star-list-column-row-header" data-answer-id="'.$answer['id'].'">' . stripslashes( $answer_content ) . '</div>';
														$star_list_content[] = '<div class="ays-survey-answer-star ays-survey-answer-stars-star-list">';
														for ($i=1; $i <= $star_list_stars_length; $i++) {
															$checked = '';
															$icon_class = 'ays_fa_star_o';
															if( isset( $user_star_list_answer[$answer['id']] ) && intval( $user_star_list_answer[$answer['id']] ) >= $i ){
																$checked = 'checked';
																$icon_class = 'ays_fa_star';
															}
															$star_list_content[] = '<label class="ays-survey-answer-label" style="margin:0">
                                                                                            <div class="ays-survey-answer-star-radio">
                                                                                                <input type="radio" name="ays-survey-question-star-' . $question['id'] . '" disabled ' . $checked . ' value="'.$i.'" data-id="' . $i . '" class="ays-survey-star-list-radios">
                                                                                                <i class="ays_fa ' . $icon_class . ' ays-survey-star-icon"></i>
                                                                                            </div>
                                                                                        </label>';
														}
														$star_list_content[] = '</div>';
														$star_list_content[] = '</div>';
														$star_list_content[] = '</div>';
														$star_list_content[] = $row_spacer;

														$content[] = implode( '', $star_list_content );

														$question_type_content .= implode( '', $content );
														break;
													case 'slider_list':

														$range_type_length        = (isset( $question['options']['slider_list_range_length'] ) && $question['options']['slider_list_range_length'] != '') ? esc_attr(intval($question['options']['slider_list_range_length'])) : 100;
														$range_type_step_length   = (isset( $question['options']['slider_list_range_step_length'] ) && $question['options']['slider_list_range_step_length'] != '') ? esc_attr(intval($question['options']['slider_list_range_step_length'])) : 1;
														$range_type_min_value     = (isset( $question['options']['slider_list_range_min_value'] ) && $question['options']['slider_list_range_min_value'] != '') ? esc_attr(intval($question['options']['slider_list_range_min_value'])) : 0;
														$range_type_default_value = (isset( $question['options']['slider_list_range_default_value'] ) && $question['options']['slider_list_range_default_value'] != '') ? esc_attr(intval($question['options']['slider_list_range_default_value'])) : 0;
														if($range_type_length == 0){
															$range_type_length = 100;
														}
														if($range_type_step_length == 0){
															$range_type_step_length = 1;
														}
														if($user_answer == ""){
															$user_answer = 0;
														}
														$user_range_answer = isset($user_slider_list_answer[$answer['id']]) ? $user_slider_list_answer[$answer['id']] : 0;

														$left = 0;

														$left = ( $user_range_answer - $range_type_min_value ) * 100 / ( $range_type_length - $range_type_min_value );


														$leftOffset = 'calc( ' .  $left . '% + ' . ( 9 - $left * 0.18 ) . 'px )';


														$content = array();
														$slider_list_content = array();

														$row_spacer = '<div class="ays-survey-answer-slider-list-row-spacer"></div>';
														if($loop_iteration == 0){
															$slider_list_content[] = '<div class="ays-survey-answer-slider-list-row">';
															$slider_list_content[] = '<div class="ays-survey-answer-slider-list-column ays-survey-answer-slider-list-column-row-header"></div>';
															$slider_list_content[] = '<div class="ays-survey-answer-slider-list-column">';
															$slider_list_content[] = '<div class="ays-survey-answer-range-type-min-max-val">';
															$slider_list_content[] = __("Min ".$range_type_min_value , SURVEY_MAKER_NAME);
															$slider_list_content[] = " / ";
															$slider_list_content[] = __("Max ".$range_type_length , SURVEY_MAKER_NAME);
															$slider_list_content[] = "</div>";
															$slider_list_content[] = "</div>";
															$slider_list_content[] = "</div>";
															$slider_list_content[] = $row_spacer;
														}
														$slider_list_content[] = '<div class="ays-survey-answer-slider-list-row">';
														$slider_list_content[] = '<div class="ays-survey-answer-slider-list-row-content">';

														$slider_list_content[] = '<div class="ays-survey-answer-slider-list-column ays-survey-answer-slider-list-column-row-header ays-survey-answer-slider-list-column-row-header-only-slider" data-answer-id="'.$answer['id'].'" >' . stripslashes( $answer_content ) . '</div>';
														$slider_list_content[] = '<div class="ays-survey-answer-range-type-main">';
														$slider_list_content[] = '<div class="ays-survey-answer-range-type-range">';
														$slider_list_content[] = '<span class="ays-survey-answer-range-type-info-text" style="left: '. $leftOffset .';">'.$user_range_answer.'</span>';
														$slider_list_content[] = '<input type="range" class="ays-survey-range-type-input" min="' . $range_type_min_value . '" max="'.$range_type_length.'" value="'.$user_range_answer.'" disabled>';
														$slider_list_content[] = '</div>';
														$slider_list_content[] = '</div>';
														$slider_list_content[] = '</div>';
														$slider_list_content[] = '</div>';
														$slider_list_content[] = $row_spacer;

														$content[] = implode( '', $slider_list_content );

														$question_type_content .= implode( '', $content );
														break;
												}
												$loop_iteration++;
											}

											if( ( $question['type'] == 'radio' || $question['type'] == 'checkbox' || $question['type'] == 'yesorno' ) && $question['user_variant'] == 'on' ){
												$checked = '';
												if( $question['type'] == 'radio' && intval( $user_answer ) == 0 && $other_answer != "" ){
													$checked = 'checked';
												}

												if( $question['type'] == 'checkbox' && !empty( $user_answer ) && in_array( '0', $user_answer ) ){
													$checked = 'checked';
												}

												if( $question['type'] == 'yesorno' && intval( $user_answer ) == 0 && $other_answer != "" ){
													$checked = 'checked';
												}

												$input_type = $question['type'];
												if( $question['type'] == 'yesorno' ){
													$input_type = 'radio';
												}

												$question_type_content .= '<div class="ays_each_question_answer ays-survey-answer-label-other">
                                            <label style="color:' . $color . '">
                                                <input type="'. $input_type .'" ' . $checked . ' ' . $disabled . ' data-id="0"/>
                                                <div class="ays-survey-answer-label-content">
                                                    <div class="ays-survey-answer-icon-content">
                                                        <div class="ays-survey-answer-icon-ink"></div>
                                                        <div class="ays-survey-answer-icon-content-1">
                                                            <div class="ays-survey-answer-icon-content-2" style="border-color:'.$survey_for_charts.' !important;">
                                                                <div class="ays-survey-answer-icon-content-3" style="border-color:'.$survey_for_charts.' !important;"></div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <span style="font-size: 16px;">' . __( 'Other', SURVEY_MAKER_NAME ) . ':</span>
                                                </div>
                                            </label>
                                            <div class="ays-survey-answer-other-text">
                                                <input class="ays-survey-answer-other-input ays-survey-question-input ays-survey-input" disabled type="text" value="' . stripslashes( esc_attr( $other_answer ) ) . '" autocomplete="off" tabindex="0">
                                                <div class="ays-survey-input-underline" style="margin:0;"></div>
                                                <div class="ays-survey-input-underline-animation" style="margin:0;background-color: '.$survey_for_charts.';" ></div>
                                            </div>
                                        </div>';
											}

											if( $question['type'] == 'select' && $key == count( $question['answers'] ) - 1 ){
												$question_type_content .= '</select></div>';
											}

											if( $question['type'] == 'matrix_scale' || $question['type'] == 'slider_list' || $question['type'] == 'star_list' || $question['type'] == 'matrix_scale_checkbox'){
												$question_type_content .= '</div>
                                                            </div>';
											}

											if( $user_explanation != '' ){
												$question_type_content .= "<div class='ays-survey-individual-user-explanation'>
                                                                        <div>
                                                                            <span>" . __('User explanation', SURVEY_MAKER_NAME) . ":</span>
                                                                        </div>
                                                                        <div class='ays-survey-individual-user-explanation-text'>
                                                                            <span>
                                                                                ".$user_explanation."
                                                                            </span>
                                                                        </div>
                                                                   </div>";
											}

											echo $question_type_content;
											?>
                                        </div>
										<?php
									}
									?>
                                </div>
								<?php
							}
						endif;
						?>
                    </div>
                </div>
                <div class="ays_survey_preloader" style="display:none;">
                    <img src="<?php echo SURVEY_MAKER_ADMIN_URL ; ?>/images/loaders/tail-spin-result.svg" alt="" width="100">
                </div>
            </div>
			<?php
            
            $content = ob_get_clean();

            $survey_title = isset($survey['title']) ? $survey['title'] : "";
            echo json_encode(array('status' => true,'content' => $content, 'title' => $survey_title));

            wp_die();
    }

    public function ays_user_history_html(){

        $survey_settings = $this->settings;
        $survey_settings_options = ($survey_settings->ays_get_setting('options') === false) ? json_encode(array()) : $survey_settings->ays_get_setting('options');
        $survey_set_option = json_decode($survey_settings_options, true);
        
        $survey_set_option['ays_show_result_report'] = !isset($survey_set_option['ays_show_result_report']) ? 'on' : $survey_set_option['ays_show_result_report'];
        $show_result_report = isset($survey_set_option['ays_show_result_report']) && $survey_set_option['ays_show_result_report'] == 'on' ? true : false;

        $results = $this->get_user_reports_info();

        $default_user_history_columns = array(
            'survey_name'=> 'survey_name',
            'submission_date' => 'submission_date',
            'details_button' => 'details_button',
        );
        
        $user_history_columns = (isset( $survey_set_option['user_history_columns'] ) && !empty($survey_set_option['user_history_columns']) ) ? $survey_set_option['user_history_columns'] : $default_user_history_columns;
        $user_history_columns_order = (isset( $survey_set_option['user_history_columns_order'] ) && !empty($survey_set_option['user_history_columns_order']) ) ? $survey_set_option['user_history_columns_order'] : $default_user_history_columns;

        $ays_default_header_value = array(
            "survey_name"   => "<th style='width:20%;'>" . __( "Survey Name", $this->plugin_name ) . "</th>",
            "submission_date"    => "<th style='width:17%;'>" . __( "Submission Date", $this->plugin_name ) . "</th>",
            "details_button"    => "<th style='width:17%;'>" . __( "Details", $this->plugin_name ) . "</th>",
        );

        if($results === null){
            $user_history_html = "<p style='text-align: center;font-style:italic;'>" . __( "You must log in to see your results.", $this->plugin_name ) . "</p>";
            return $user_history_html;
        }
        
        $user_history_html = "<div class='ays-survey-user-history-container'>
        <table class='ays_survey_user_history ays-survey-data-table-responsive'>
        <thead>
        <tr>";
        
        foreach ($user_history_columns_order as $key => $value) {
            if (isset($user_history_columns[$value])) {
                $user_history_html .= $ays_default_header_value[$value];
            }
        }
        
        $user_history_html .= "</tr></thead>";


        foreach($results as $key => $result){
            $id         = isset($result['id']) ? $result['id'] : null;
            $user_id    = isset($result['user_id']) ? intval($result['user_id']) : 0;
            $survey_id    = isset($result['survey_id']) ? intval($result['survey_id']) : 0;
            $title      = isset($result['title']) ? $result['title'] : "";
            $submission_date = date_create($result['submission_date']);
            $start_date = date_format($submission_date, 'H:i:s M d, Y');

            $start_date_for_ordering = strtotime($result['submission_date']);

            $ays_default_html_order = array(
                "survey_name" => "<td>$title</td>",
                "submission_date" => "<td data-order='". $start_date_for_ordering ."'>$start_date</td>",
                "details_button" => "<td data-order='-'> <button class='" . $this->html_class_prefix . "show-submission-button' data-user-id='" . get_current_user_id() ."' data-survey-id='" . $survey_id . "' data-id='" . $id . "'>Details</button> </td>",
            );

            $user_history_html .= "<tr>";
            foreach ($user_history_columns_order as $key => $value) {
                if (isset($user_history_columns[$value])) {
                    $user_history_html .= $ays_default_html_order[$value];
                }
            }
            $user_history_html .= "</tr>";
        }

        $user_history_html .= "</table>
        </div>";

        $user_history_html .= '<div class="ays-modal" id="show-detailed-info">
            <div class="ays-modal-content ays-survey-user-history-modal-content">
                <div class="ays-preloader">
                    <img class="loader" src="' . SURVEY_MAKER_ADMIN_URL . '/images/loaders/tail-spin.svg">
                </div>
                <!-- Modal Header -->
                <div class="ays-modal-header">
                    <span class="ays-close">&times;</span>
                    <h2></h2>
                </div>
                <!-- Modal body -->
                    <div class="ays-modal-body">
                </div>
            </div>
        </div>
        ';
        
        return $user_history_html;
    }

    public function ays_generate_user_history_method(){
        $this->enqueue_styles();
        $this->enqueue_scripts();

        $user_history_html = $this->ays_user_history_html();

        return str_replace(array("\r\n", "\n", "\r"), '', Survey_Maker_Data::ays_survey_translate_content($user_history_html));
    }

}
