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

if ( $boolean_login ){

    $status                    = '303' ;

    if ( $staff_info['staff_settings']['ismanager'] == 'yes' ){
        $status                = '300' ;

        if ( $array['update_status'] != '' && $array['comment_manager'] != '' ){
            $status            = '201' ;

            $query             = "SELECT formresignation_id, formresignation_so, staff_id, title, content, staff_id_manager, comment_manager, comment_hr, status_manager, staff_id_hr, status_hr, created_at, updated_at FROM formresignation
                                  WHERE deleted_at IS NULL AND branch_id = '".$array['branch_id']."' AND formresignation_id = '".$array['formresignation_id']."' AND staff_id_manager = '".$staff_info['staff_id']."' AND status_manager = 'pending' AND status_hr = 'pending'" ;
            $mysqli_query      = $mysqli->query( $query . " LIMIT 1" ) ;

            if ( $mysqli_query->num_rows > 0 ){

                $row           = $mysqli_query->fetch_assoc() ;

                $status        = '308' ;

                $update_status = ( $array['update_status'] == 'confirmed' ? 'confirmed' : 'rejected' ) ;

                if ( $update_status != '' ){
                    $status    = '200' ;

                    $mysqli->query( "UPDATE formresignation SET
                                     status_manager           = '".$update_status."',
                                     comment_manager          = '".$array['comment_manager']."',
                                     date_manager             = '".TODAYDATE."'
                                     WHERE formresignation_id = '".$array['formresignation_id']."'" ) ;


                    // push notification to hr manager
                    $mailer                = new Mailer() ;
                    $mailer->from          = EMAILNOREPLY ;
                    if ( $update_status   == 'confirmed' ){

                        $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( 'formresignation', $array['formresignation_id'], $row_staff['staff_id'], 'Resignation', 'Resignation '.$row['formresignation_so'].' need your approval or rejection' ) ;

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

                        pushToUserCron( 'formresignation', $array['formresignation_id'], $row['staff_id'], 'Resignation', 'Resignation is confirmed by your Manager, we will send to HR Manager to do approval and rejection' ) ;
                    }else{
                        pushToUserCron( 'formresignation', $array['formresignation_id'], $row['staff_id'], 'Resignation', 'Resignation is rejected by your Manager' ) ;
                    }
                }

            }
        }
    }
}

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