<?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 title LIKE '%".$array['search']."%'" ;
    }

    $query             = "SELECT formresignation_id, staff_id, title, content, status_manager, status_hr, created_at FROM formresignation
                          WHERE deleted_at IS NULL AND branch_id = '".$array['branch_id']."' AND staff_id = '".$staff_info['staff_id']."' " . $search_query ;
    $mysqli_query      = $mysqli->query( $query . " ORDER BY created_at DESC LIMIT " . getLimit( $current ) ) ;

    if ( $mysqli_query->num_rows > 0 ){

        $status        = '200' ;

        $list          = [] ;
        $staff_list    = [] ;
        $formresignation_id = [] ;
        while ( $row   = $mysqli_query->fetch_assoc() ){
            $row['id']          = dataFilter( $row['formresignation_id'] ) ;
            $row['title']       = dataFilter( $row['title'] ) ;
            $row['content']     = dataFilter( $row['content'] ) ;
			$row['departments'] = '' ;
            $row['created_at']  = resetDateFormat( $row['created_at'] ) ;
            $list[]             = $row ;

            $staff_list[$row['staff_id']]         = $row['staff_id'] ; 
            $formresignation_id[$row['formresignation_id']] = $row['formresignation_id'] ;
        }

        // select related formresignation media
        $media_list     = [] ;
        $select_media   = $mysqli->query( "SELECT formresignation_id, file FROM formresignation_media a
                                           WHERE a.deleted_at IS NULL AND formresignation_id IN ( ".implode( ',', $formresignation_id )." ) AND filetype IN ( 'jpg', 'png', 'gif' )" ) ;
        if ( $select_media->num_rows > 0 ){
            while ( $row_media   = $select_media->fetch_assoc() ){
                $media_list[$row_media['formresignation_id']][] = ( $row_media['file'] != '' ? PATH.'uploads/FormResignation/b/'.$row_media['file'] : '' ) ;
            }
        }

        // 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( ',', $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 ){
            $list[$k]['departments'] = ( checkExists( $related_list[$v['staff_id']] ) ? implode( ', ', $related_list[$v['staff_id']] ) : '' ) ;
            $list[$k]['files']       = ( checkExists( $media_list[$v['formresignation_id']] ) ? $media_list[$v['formresignation_id']] : [] ) ;
        }

        $data['list']  = $list ;

    }
}

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