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

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


    // select all department
    $department_list   = [] ;
    $select            = $mysqli->query( "SELECT a.department_id, b.department_desc FROM setting_department a
                                          LEFT JOIN setting_department_translation b ON ( a.department_id = b.department_id )
                                          WHERE a.deleted_at IS NULL AND b.lang = '".$array['lang']."'" ) ;
    if ( $select->num_rows > 0 ){
        while ( $row   = $select->fetch_assoc() ){
            $department_list[$row['department_id']] = $row['department_desc'] ;
        }
    }

	$search_query      = '' ;
	if ( $array['search'] != '' ){
        $search_query .= " AND b.staff_name LIKE '%".$array['search']."%'" ;
    }
    if ( $array['submission_type'] != '' ){
        $search_query .= " AND a.formnomination_type = '".$array['submission_type']."'" ;
    }
    

    $query             = "SELECT a.formnomination_id, a.staff_id, a.nominee_staff_id, a.status, a.created_at FROM formnomination a
                          WHERE a.deleted_at IS NULL AND a.branch_id = '".$array['branch_id']."' AND a.staff_id = '".$staff_info['staff_id']."' " . $search_query ;
    $mysqli_query      = $mysqli->query( $query . " ORDER BY a.created_at DESC LIMIT " . getLimit( $current ) ) ;

    if ( $mysqli_query->num_rows > 0 ){

        $status        = '200' ;

        $list               = [] ;
        $nominee_staff_list = [] ;
        $formnomination_id  = [] ;
        while ( $row   = $mysqli_query->fetch_assoc() ){
            $row['id']                  = dataFilter( $row['formnomination_id'] ) ;
            $row['nominee_name']        = '' ;
			$row['nominee_departments'] = '' ;
            $row['created_at']          = resetDateFormat( $row['created_at'] ) ;
            $list[]                     = $row ;

            $nominee_staff_list[$row['nominee_staff_id']] = $row['nominee_staff_id'] ; 
        }

        // select all staff
        $related_staff_list   = [] ;
        $select_related_staff = $mysqli->query( "SELECT a.staff_id, a.staff_name FROM staff a
                                                 WHERE a.deleted_at IS NULL AND a.staff_id IN ( ".implode( ',', $nominee_staff_list )." )" ) ;
        if ( $select_related_staff->num_rows > 0 ){
            while ( $row_related_staff = $select_related_staff->fetch_assoc() ){
                $related_staff_list[$row_related_staff['staff_id']] = [
                    'staff_name' => $row_related_staff['staff_name']
                ] ;
            }
        }

        // select all staff related deparment
        $related_list   = [] ;
        $select_related = $mysqli->query( "SELECT a.staff_id, a.department_id FROM staff_department a
                                           WHERE a.deleted_at IS NULL AND a.staff_id IN ( ".implode( ',', $nominee_staff_list )." )" ) ;
        if ( $select_related->num_rows > 0 ){
            while ( $row_related   = $select_related->fetch_assoc() ){
                if ( checkExists( $department_list[$row_related['department_id']] ) ){
                    $related_list[$row_related['staff_id']][] = $department_list[$row_related['department_id']] ;
                }
            }
        }

        foreach ( $list as $k => $v ){
            $selected_staff                  = ( checkExists( $related_staff_list[$v['nominee_staff_id']] ) ? $related_staff_list[$v['nominee_staff_id']]  : [] ) ;

            $list[$k]['nominee_name']        = ( checkExists( $selected_staff['staff_name'] ) ? checkExists( $selected_staff['staff_name'] ) : 'Unknown' ) ;
            $list[$k]['nominee_departments'] = ( checkExists( $related_list[$v['nominee_staff_id']] ) ? implode( ', ', $related_list[$v['nominee_staff_id']] ) : '' ) ;
        }

        $data['list']  = $list ;

    }
}

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