<?php
require '../../conn/codeblocs.php';

// EXIT;
// preprint($_GET);
$get = $_GET;
$whsid = 1; $whparams = [];
$sid = $get['sid'] ?? '';
$mode = $get['mode'] ?? 'labels'; //or numbers
if($PRIVILEGE == 'techno'){
	$vid = $get['vid'] ?? '';
}else{
    $vid = array_keys($V_ARR_TRIM)[0];
}
// preprint($get);


$v_arr_pholder = 'staff_id = :sid';
$v_arr_params = ['sid'=>$STAFF_ID];
if($vid){
	$v_arr_pholder .= " AND survey_id IN(SELECT survey_id FROM survey_vendee_bridge WHERE vendee_id = :vid)";

	$v_arr_params['vid'] = $vid;
}

$S_ARR = doPDOQ("SELECT * FROM survey_instance WHERE survey_id IN(SELECT survey_id FROM survey_vendee_bridge WHERE vendee_id IN(SELECT vendee_id FROM staff_vendee_bridge WHERE {$v_arr_pholder}))", $v_arr_params);
// preprint($S_ARR);

$S_ARR_TRIM = array_column($S_ARR ?? [], 'survey_name', 'survey_id');
if($sid && array_key_exists($sid, $S_ARR_TRIM)){
    $whsid .= " AND ".($sid ? "b.survey_id = :sid" : "1"); 
    $whparams['sid'] = $sid;
    $sid_exists = 1;
    
}else{
    $sid_exists = 0;
    // return;
}




$survey_instance = SW('survey_id, survey_name', 'survey_instance', "survey_id = :sid", 1, 1, ['sid'=>$sid]);
// preprint($survey_instance);
$display_modes = array('labels'=>'Labels', 'numbers'=>'Numbers');


if($vid){
    $survey_vendee = SW('vendee_id, vendee_name', 'survey_vendee', "vendee_id = :vid", 1, 1, ['vid'=>$vid]);
    // preprint($survey_vendee);

    $whsid .= " AND ".($vid ? "b.vendee_id = :vid" : "1"); 
    $whparams['vid'] = $vid;
}

// $survey_questions = SAW("question_id, question_name, rating_scale_id", "survey_questions", 1, 1);
$survey_questions = doPDOQ("SELECT question_id, question_name, question_code, rating_scale_id FROM survey_questions q LEFT JOIN survey_instance_section s USING(section_id) WHERE s.survey_id = :sid", ['sid'=>$sid]);
// preprint($survey_questions);

if($mode == 'labels'){
    $response_data = 'IF(q.rating_scale_id IS NOT NULL, (SELECT scale_option FROM rating_scale_options WHERE rating_scale_id = q.rating_scale_id AND scale_option_rank = r.response_data), r.response_data)';
}else{
    $response_data = 'r.response_data';
}

// echo 'whsid:'.$whsid;
// preprint($whparams);
$survey_batches = doPDOQ("SELECT b.response_batch_id, b.survey_id, b.vendee_id, b.response_token, v.short_vendee, v.vendee_name, GROUP_CONCAT(r.question_id) qids, GROUP_CONCAT(r.response_data) responses, b.created_at
FROM survey_response_batch b 
LEFT JOIN survey_responses r USING(response_batch_id) 
LEFT JOIN survey_questions q USING(question_id)
LEFT JOIN survey_vendee v USING(vendee_id) 

WHERE {$whsid}
GROUP BY b.response_batch_id
", $whparams);
// preprint($survey_batches);
// EXIT;

?>
        			
        <div class="container-table100">
            
         <div class="wrap-table100">

            <?php if($vid):?>
            <form method="get" id="sid-form" style="display:inline-block;float:right">
                <?php doDirectFormSelect('sid', $S_ARR_TRIM, $sid??'', [''=>'...'])?>
                <?php doDirectFormSelect('mode', $display_modes, $mode??'', [''=>'...'])?>

                <input type="hidden" name="vid" value="<?=$vid?>"/>
            </form>

            <h4><?=$survey_vendee['vendee_name'] ?? ''?></h4>
            <?php else:?>
                <span style="color:#666">Select Institution </span>
            <?php endif?>

            <h5><?=$survey_instance['survey_name']?></h5>
               
                <?php if(!$sid_exists):?>
                    <div style="margin-top:2em; padding:4em;text-align:center; background:#eeeeee4f; color:#999">No Survey Chosen</div>

                <?php else:?>
            
                <div class="table100">
                    <?php if( !count($survey_batches) ):?>
                        <div style="margin-top:2em; padding:4em;text-align:center; background:#cccccc4f; color:orangered">No submissions found</div>
                   
                     <?php else:?>
                  
                      <table id="main-summary-table" data-te-filename="main-summary-vendee.<?=$vid?>-survey.<?=$sid?>-mode.<?=$mode?>">
                        <thead>
                            <tr class="table100-head">
                                    <th class="first-column">Date</th>
                                    <th>Vendee</th>
                                    <th>S</th>
                                <?php foreach($survey_questions as $r1):?>
                                    <th title="<?=$r1['question_name']?>"><?php //preprint($r1)?><?=$r1['question_code']?></th>
                                <?php endforeach?>
                               
                                
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($survey_batches as $survey_batch):?>
                           
                            <tr>
                                <td class="">
                                    <?php
                                    //$response_arr = json_decode($survey_batch['response_json'], true);
                                    // preprint($response_arr);

                                    $response_arr = SAW("r.question_id, {$response_data} response_data",  "survey_responses r LEFT JOIN survey_questions q USING(question_id)",
                                    "response_batch_id = '{$survey_batch['response_batch_id']}'", 1, 1);

                                    // preprint($response_arr);
                                    ?>
                                        <?=$survey_batch['created_at']?>
                                </td>
                                <td><?=$survey_batch['short_vendee']?></td>
                                <td class=""><a href="pages/response-details.inc.php?rbid=<?=$survey_batch['response_batch_id']?>" class="d-anchor"><?=$survey_batch['response_batch_id']?></a></td>
                            

                                <?php foreach($survey_questions as $r2):?>
                                    <td><?=$response_arr[ $r2['question_id'] ]?></td>
                                <?php endforeach?>
                                
                            </tr>
                            <?php endforeach?>

                        
                        </tbody>
                    </table>
                    <?php endif?>
                </div>
                <?php endif?>

            </div>
        </div>