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

if ( $boolean_login ){
    $status             = '300' ;

    $formresignation_id = $array['formresignation_id'] ;
    $photos             = $array['photos'] ;

    if ( $array['staff_id_manager'] != '' && $array['staff_id_manager'] > 0 && $array['title'] != '' && $array['content'] != '' && $array['photos'] != '' && count( $photos ) > 0 ){
        $status         = '203' ;

        if ( $mysqli->query( "INSERT INTO formresignation
                           ( `branch_id`, `staff_id`, `staff_id_manager`, `title`, `content`, `status_manager`, `status_hr` ) VALUES
                           ( '".$array['branch_id']."', '".$staff_info['staff_id']."', '".$array['staff_id_manager']."', '".$array['title']."', '".$array['content']."', 'pending', 'pending' )" ) ){
            $status         = '200' ;

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

            $formresignation_so = 'FR'.strPad( 6, $formresignation_id ) ;
            $mysqli->query( "UPDATE formresignation SET
                             formresignation_so       = '".$formresignation_so."'
                             WHERE formresignation_id = '".$formresignation_id."'" ) ;

            if ( checkExists($photos) ){
                foreach ( $photos as $k => $v ){
                    if ( $v['type'] == 'local' ){
                        $file_upload = ( $v['file'] ) ;
                        $upload      = uploadImage( 'FormResignation', $formresignation_id.'-'.$formresignation_id, $file_upload ) ;
                        if ( $upload['status'] != '200' ){
                            $count_upload++ ;
                        }else{
                            $mysqli->query( "INSERT INTO formresignation_media
                                             ( formresignation_id, file, filetype ) VALUES
                                             ( '".$formresignation_id."', '".$upload['data']['file_name']."', '".$upload['data']['file_type']."' )" ) ;
                            $status = '200' ;
                        }
                    }
                }
            }
            
            pushToUserCron( 'formresignation', $formresignation_id, $array['staff_id_manager'], 'Resignation', 'Resignation '.$formresignation_so.' need your approval or rejection' ) ;

        }
    }
}

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