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

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

    $search_query            = '' ;
    $search_query           .= " AND a.view_id = '".$array['id']."'" ;

    $query                   = "SELECT a.view_id, a.inbox_id, a.is_read, b.from_table, b.from_id, b.title, b.description, b.content, b.view_format, b.file, b.file_type, b.video_url, b.created_at FROM staff_inbox_view a
                                LEFT JOIN inbox b ON ( a.inbox_id = b.inbox_id ) 
                                WHERE a.deleted_at IS NULL AND a.staff_id = '".$staff_info['staff_id']."' " . $search_query ;
    $mysqli_query            = $mysqli->query( $query ) ;

    if ( $mysqli_query->num_rows > 0 ){

        $status               = '200' ;
        $row                  = $mysqli_query->fetch_assoc() ;

        $file                = '' ;
        if ( $row['file']   != '' ){
            if ( $row['file_type'] == 'pdf' ){
                $file        = PATH.'uploads/Inbox/'.$row['file'] ;
            }else{
                $file        = PATH.'uploads/Inbox/b/'.$row['file'] ;
            }
        }

        $row['title']         = dataFilter( $row['title'] ) ;
        $row['description']   = dataFilter( $row['description'] ) ;
        $row['content']       = dataFilter( $row['content'] ) ;
        $row['file']          = $file ;
        $row['created_at']    = resetDateFormat( $row['created_at'] ) ;

        $go_page              = '' ;
        $go_param             = '' ;

        $is_updateread        = true ;

        switch ( $row['from_table'] ){
            case 'task'               :
                $go_page      = 'TaskView' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'] ] ;
            break ;
            case 'redeem'       :
                $go_page      = 'RedeemDetails' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'], 'view_id' => '' ] ;
            break ;
            case 'staff_redeem'       :
                $select_view  = $mysqli->query( "SELECT redeem_id FROM staff_redeem WHERE view_id = '".$row['from_id']."' LIMIT 1" ) ;
                $main_id      = '' ;
                if ( $select_view->num_rows > 0 ){
                    $row_view = $select_view->fetch_assoc() ;
                    $main_id  = $row_view['redeem_id'] ;

                    $go_page  = 'RedeemView' ;
                    $go_param = [ 'refresh' => '', 'type' => 'update', 'id' => $main_id, 'view_id' => $row['from_id'] ] ;
                }
            break ;
            case 'association'        :
                $go_page      = 'AssociationDetails' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'], 'view_id' => '' ] ;
            break ;
            case 'staff_association'  :
                $select_view  = $mysqli->query( "SELECT association_id FROM staff_association WHERE view_id = '".$row['from_id']."' LIMIT 1" ) ;
                $main_id      = '' ;
                if ( $select_view->num_rows > 0 ){
                    $row_view = $select_view->fetch_assoc() ;
                    $main_id  = $row_view['association_id'] ;

                    $go_page  = 'AssociationView' ;
                    $go_param = [ 'refresh' => '', 'type' => 'update', 'id' => $main_id, 'view_id' => $row['from_id'] ] ;
                }
            break ;
            case 'training'           :
                $go_page      = 'TrainingDetails' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'], 'view_id' => '' ] ;
            break ;
            case 'staff_training' :
                $select_view  = $mysqli->query( "SELECT training_id FROM staff_training WHERE view_id = '".$row['from_id']."' LIMIT 1" ) ;
                $main_id      = '' ;
                if ( $select_view->num_rows > 0 ){
                    $row_view = $select_view->fetch_assoc() ;
                    $main_id  = $row_view['training_id'] ;

                    $go_page  = 'TrainingView' ;
                    $go_param = [ 'refresh' => '', 'type' => 'update', 'id' => $main_id, 'view_id' => $row['from_id'] ] ;
                }
            break ;
            case 'suggestion'         :
                $go_page      = 'SuggestionUpdate' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'] ] ;
            break ;
            case 'grievance'          :
                $go_page      = 'GrievanceUpdate' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'] ] ;
            break ;
            case 'request'            :
                $go_page      = 'RequestUpdate' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'] ] ;
            break ;
            case 'staff_adjustment' :
                $go_page      = 'AdjustmentDetails' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'] ] ;
            break ;
            case 'handbook'           :
                $go_page      = 'HandbookDetails' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'] ] ;
            break ;
            case 'staff_leave'        :
                $go_page      = 'LeaveDetails' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'] ] ;
            break ;
            case 'staff_payment_slip':
                $go_page      = 'PaymentSlipUpdate' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'] ] ;
            break ; 
            case 'staff_advance'      :
                $go_page      = 'AdvanceUpdate' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'] ] ;
            break ;
            case 'staff_health'       :
                $go_page      = 'HealthUpdate' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'] ] ;
            break ;
            case 'holiday'            :
                $go_page      = 'Holiday' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => '' ] ;
            break ;
            case 'announcement'       :
                $select_announcement = $mysqli->query( "SELECT is_showagree FROM announcement
                                                        WHERE deleted_at IS NULL AND status = 'active' AND announcement_id = '".$row['from_id']."' LIMIT 1" ) ;
                if ( $select_announcement->num_rows > 0 ){
                    $row_announcement = $select_announcement->fetch_assoc() ;
                    if ( $row_announcement['is_showagree'] == 'yes' ){
                        $select_agree = $mysqli->query( "SELECT * FROM staff_announcement 
                                                         WHERE deleted_at IS NULL AND announcement_id = '".$row['from_id']."' AND staff_id = '".$staff_info['staff_id']."' LIMIT 1" ) ;
                        if ( $select_agree->num_rows == 0 ){
                            $is_updateread = false ;
                        }
                    }
                }

                $go_page      = 'AnnouncementDetails' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'] ] ;
            break ;
            case 'outstanding-employee' :
            case 'lateness-board'       :
                $go_page      = 'Home' ;
                $go_param     = [ 'refresh' => '', 'type' => '', 'id' => '' ] ;
            break ;
            case 'association_gallery' :
            case 'training_gallery'    :
            case 'request_gallery'     :
                $select_view  = $mysqli->query( "SELECT file FROM ".$row['from_table']." WHERE gallery_id = '".$row['from_id']."' LIMIT 1" ) ;
                $main_id      = '' ;
                if ( $select_view->num_rows > 0 ){
                    $row_view = $select_view->fetch_assoc() ;

                    $path     = '' ;
                    switch ( $row['from_table'] ){
                        case 'association_gallery' :
                            $path = 'AssociationGallery' ;
                        break ;
                        case 'training_gallery'    :
                            $path = 'TrainingGallery' ;
                        break ;
                        case 'request_gallery'     :
                            $path = 'RequestGallery' ;
                        break ;
                    }

                    $files    = ( $row_view['file'] != '' ? [ PATH.'uploads/'.$path.'/b/'.$row_view['file'] ] : [] ) ;

                    $go_page  = 'ImageZoom' ;
                    $go_param = [ 'refresh' => '', 'files' => $files ] ;
                }
            break ;
            case 'form'            :
                $go_page      = 'FormDetails' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'] ] ;
            break ;
            case 'formresignation' :
                $go_page      = 'FormResignationUpdate' ;
                $go_param     = [ 'refresh' => '', 'type' => 'update', 'id' => $row['from_id'] ] ;
            break ;
        }

        $row['go_page']     = $go_page ;
        $row['go_param']    = $go_param ;

        $data['list']       = $row ;

        // update is read
        if ( $row['is_read'] == '0' && $is_updateread ) {
            $mysqli->query( "UPDATE staff_inbox_view SET
                             is_read       = '1' 
                             WHERE view_id = '".$row['view_id']."'" ) ;
        }
    }
}

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