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

if ( $boolean_login ){ 
    $status                    = '300' ;
   
    $nominee_to                = $array['nominee_to'] ;
    $formnomination_id         = $array['formnomination_id'] ;
    $forms                     = $array['forms'] ;
   
    if ( $nominee_to != '' && $formnomination_id != '' && count($forms) > 0 ){
        $status                = '201' ;

        $where_question        = '' ;
        if ( $array['submission_type'] != '' ){
            $where_question   .= " AND a.nomination_type = '".$array['submission_type']."'" ;
        }

        $select_formnomination = $mysqli->query( "SELECT * FROM formnomination
                                                  WHERE deleted_at IS NULL AND formnomination_id = '".$formnomination_id."' AND status = 'pending' LIMIT 1" ) ;
        if ( $select_formnomination->num_rows > 0 ){
            $status            = '314' ;

            $row_formnomination = $select_formnomination->fetch_assoc() ;

            $form_list         = [] ;
            $select_form       = $mysqli->query( "SELECT a.question_id FROM formnomination_question a
                                                  WHERE a.deleted_at IS NULL AND a.question_type = 'form'" . $where_question ) ;

            $total_form        = $select_form->num_rows ;

            $total_remark      = 0 ;
            foreach ( $forms as $kform => $vform ){
                if ( $vform['remark'] != '' ){
                    $total_remark++ ;
                }
            }

            if ( count($forms)  == $total_form && $total_form == $total_remark ){
                $status         = '200' ;

                $mysqli->query( "UPDATE formnomination SET
                                 nominee_staff_id        = '".$nominee_to."',
                                 status                  = 'approved'
                                 WHERE formnomination_id = '".$formnomination_id."'" ) ;

                foreach ( $forms as $kform => $vform ){
                    $mysqli->query( "INSERT INTO formnomination_answer
                                  ( formnomination_id, question_id, question_type, checkbox, chosen, remark ) VALUES
                                  ( '".$formnomination_id."', '".$vform['id']."', 'form', '".$vform['checkbox']."', '".$vform['radio']."', '".$vform['remark']."' )" ) ;
                }

                $mailer            = new Mailer() ;
                $mailer->from          = EMAILNOREPLY ;
                $select_staff      = $mysqli->query( "SELECT a.staff_id, a.staff_name, a.staff_email FROM staff a
                                                      WHERE a.deleted_at IS NULL AND a.branch_id = '".$array['branch_id']."' AND ( a.staff_date_resigned >= '".date("Y-m-d",time())."' OR a.staff_date_resigned = '0000-00-00' OR a.staff_date_resigned IS NULL ) AND a.staff_settings LIKE '%\"ishrmanager\":\"yes\"%'" ) ;

                if ( $select_staff->num_rows > 0 ){
                    while ( $row_staff = $select_staff->fetch_assoc() ){
                        pushToUserCron( 'formnomination', $array['formnomination_id'], $row_staff['staff_id'], 'Nomination', 'Nomination '.$row_formnomination['formnomination_so'].' need you to review' ) ;

                        if ( $row_staff['staff_email'] != '' ){
                            $mailer->to       = [ $row_staff['staff_email'] ] ;
                            // $mailer->cc       = $EMAILCC ;
                            $mailer->subject  = 'Reminder for Nomination' ;
                            $mailer->body     = 'Dear HR Manager, '.dataFilter($staff_info['staff_name']).' has submitted an nomination request. Please log in to review.
                                                 <br /><br />
                                                 * This is an auto-generated message, please do not reply.' ;
                            $mailer->send() ;
                        }
                    }
                }

            }

        }
    }
}

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