<?php
$must_login                    = true ;
$require_path                  = '../../../' ;
$require_sub                   = '../../' ;
require( $require_sub.'header.php' ) ;

if ( $boolean_login ){ 
    $status                    = '300' ;
   
    $questions                 = $array['questions'] ;

    if ( count($questions)       > 0 ){
        $status                = '314' ;

        $where_question        = '' ;
        if ( $array['submission_type'] != '' ){
            $where_question   .= " AND a.nomination_type = '".$array['submission_type']."'" ;
        }
   
        $question_list         = [] ;
        $select_question       = $mysqli->query( "SELECT a.question_id FROM formnomination_question a
                                                  WHERE a.deleted_at IS NULL AND a.question_type = 'question'" . $where_question ) ;
        $total_question        = $select_question->num_rows ;

        $point_peryes          = ( 100 / $total_question ) ;
        $point_questionyes     = 0 ;
        $total_yesno           = 0 ;
        foreach ( $questions as $kquestion => $vquestion ){
            if ( $vquestion['checkbox'] == 'yes' ){
                $point_questionyes += $point_peryes ;
            }

            if ( $vquestion['checkbox'] != '' ){
                $total_yesno++ ;
            }
        }

        if ( count($questions)  == $total_question && $total_yesno == $total_question ){
            $status            = '99' ;

            $update_status     = 'cancelled' ;
            if ( $point_questionyes >= NOMINATIONPOINT ){
                $status        = '98' ;

                $update_status = 'pending' ;
            }

            if ( $mysqli->query( "INSERT INTO formnomination
                               ( `formnomination_type`, `branch_id`, `staff_id`, `status` ) VALUES
                               ( '".$array['submission_type']."', '".$array['branch_id']."', '".$staff_info['staff_id']."', '".$update_status."' )" ) ){

                $boolean_submit = true ;
                $formnomination_id = $mysqli->insert_id ;

                $formnomination_so = 'FN'.strPad( 6, $formnomination_id ) ;
                $mysqli->query( "UPDATE formnomination SET
                                 formnomination_so       = '".$formnomination_so."'
                                 WHERE formnomination_id = '".$formnomination_id."'" ) ;

                foreach ( $questions as $kquestion => $vquestion ){
                    $mysqli->query( "INSERT INTO formnomination_answer
                                  ( formnomination_id, question_id, question_type, checkbox, chosen, remark ) VALUES
                                  ( '".$formnomination_id."', '".$vquestion['id']."', 'question', '".$vquestion['checkbox']."', '".$vquestion['radio']."', '".$vquestion['remark']."' )" ) ;
                }

                $data = [
                    'formnomination_id' => $formnomination_id,
                    'update_status'     => $update_status
                ] ;

            }else{
                $status         = '203' ;
            }

        }

    }
}

require( $require_sub.'footer.php' ) ;
?>