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

if ( $boolean_login ){
	$status   		               = '201' ;
    $all_tier                      = getAllTier( $array['lang'] ) ;
    $staff_settings                = $staff_info['staff_settings'] ;

	$search_query                  = '' ;
    $search_query                 .= " AND formresignation_id = '".$array['formresignation_id']."'" ;

    $query                         = "SELECT formresignation_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 " . $search_query ;
    $mysqli_query                  = $mysqli->query( $query . " LIMIT 1" ) ;

    if ( $mysqli_query->num_rows > 0 ){

        $status                    = '200' ;

        $list                      = [] ;
        $row                       = $mysqli_query->fetch_assoc() ;
        $row['title']              = dataFilter( $row['title'] ) ;
        $row['content']            = dataFilter( $row['content'] ) ;

        $manager_name              = '' ;
        $hr_name                   = '' ;

        // select manager / hr manager 
        $staff_ids                 = [ $row['staff_id_manager'] ]  ;
        if ( $row['staff_id_hr']   > 0 ){
            $staff_ids[]           = $row['staff_id_hr']  ;
        }
        $select_staff              = $mysqli->query( "SELECT staff_id, staff_idno, staff_name, staff_shortname, staff_tier FROM staff
                                                      WHERE staff_id IN ( ".implode(',', $staff_ids)." ) LIMIT 2" ) ;
        if ( $select_staff->num_rows > 0 ){
            while ( $row_staff     = $select_staff->fetch_assoc() ){
                $get_staff_tier    = $all_tier[$row_staff['staff_tier']] ;

                if ( $row_staff['staff_id'] == $row['staff_id_manager'] ){
                    $manager_name  = dataFilter( $row_staff['staff_shortname'] ) . ' ('.$row_staff['staff_idno'].' / '.strtoupper( $get_staff_tier['title'] ).')' ;
                }
                if ( $row_staff['staff_id'] == $row['staff_id_hr'] ){
                    $hr_name       = dataFilter( $row_staff['staff_shortname'] ) . ' ('.$row_staff['staff_idno'].' / '.strtoupper( $get_staff_tier['title'] ).')' ;
                }
            }
        }

        $is_manager_update         = 'no' ;
        if ( $staff_settings['ismanager'] == 'yes' ){
            if ( $row['status_manager'] == 'pending' && $row['status_hr'] == 'pending' ){
                if ( $staff_info['staff_id']  == $row['staff_id_manager'] ){
                    $is_manager_update = 'yes' ;
                }
            }
        }
        $row['is_manager_update']  = $is_manager_update ;
        $row['manager_name']       = $manager_name ;
        $row['comment_manager']    = ( $row['comment_manager'] != '' ? '<pre>'.dataFilter( $row['comment_manager'] ).'</pre>': '' ) ;

        $is_hr_update              = 'no' ;
        if ( $staff_settings['ishrmanager'] == 'yes' ){
            if ( $row['status_manager'] == 'confirmed' && $row['status_hr'] == 'pending' ){
                $is_hr_update      = 'yes' ;
            }
        }
        $row['is_hr_update']       = $is_hr_update ;
        $row['hr_name']            = $hr_name ;
        $row['comment_hr']         = ( $row['comment_hr'] != '' ? '<pre>'.dataFilter( $row['comment_hr'] ).'</pre>': '' ) ;

        // get all media
        $select_media              = $mysqli->query( "SELECT media_id, file, filetype FROM formresignation_media
                                                      WHERE deleted_at IS NULL AND formresignation_id = '".$array['formresignation_id']."'" ) ;
        $photos                    = [] ;
        if ( $select_media->num_rows > 0 ){
            while ( $row_media     = $select_media->fetch_assoc() ){
                
                switch ( $row_media['filetype'] ){
                    case 'jpg'  :
                    case 'jpeg' :
                    case 'png'  :  
                        $photos[]          = [
                            'media_id'     => $row_media['media_id'],
                            'type'         => 'online',
                            'filetype'     => $row_media['filetype'],
                            'file'         => ( $row_media['file'] != '' ? PATH.'uploads/FormResignation/b/'.$row_media['file'] : '' )
                        ] ;
                    break ;
                    default     :
                        $photos[]          = [
                            'media_id'     => $row_media['media_id'],
                            'type'         => 'online',
                            'filetype'     => $row_media['filetype'],
                            'file'         => ( $row_media['file'] != '' ? PATH.'uploads/FormResignation/'.$row_media['file'] : '' )
                        ] ;
                }



            }
        }

        $row['photos']             = $photos ;

        $data                      = $row ;

    }
}

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