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

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


    // 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'] ;
        }
    }

    // select all staff
    $staff_list          = [] ;
    $select              = $mysqli->query( "SELECT a.staff_id, a.staff_name, a.staff_shortname, a.staff_idno, a.staff_tier FROM staff a
                                           WHERE a.deleted_at IS NULL" ) ;
    if ( $select->num_rows > 0 ){
        while ( $row     = $select->fetch_assoc() ){
            $get_staff_tier = $all_tier[$row['staff_tier']] ;
            
            $staff_list[$row['staff_id']] = $row['staff_shortname'] . ' ( '.$row['staff_idno'].' / '.strtoupper( $get_staff_tier['title'] ).')' ;
        }
    }


    // default parameter
    $isupdate            = 'no' ;
    $iscancel            = 'no' ;
    $isreject            = 'no' ;
    $isassigned          = 'no' ;

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

    $query               = "SELECT task_id, task_type, task_so, title, department_id, created_by, assigned_by, difficulty, date_start, date_end, remark, incentive, incentive2, extra, rating, todo_list, todo_done, status, created_at, updated_at FROM task
                           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() ;
        $task_type_name  = '' ;
        switch ( $row['task_type'] ){
            case '1time'   : $task_type_name = 'One Time Only' ; break ;
            case 'daily'   : $task_type_name = 'Daily Update' ; break ;
            case 'weekly'  : $task_type_name = 'Weekly Update' ; break ;
            case 'monthly' : $task_type_name = 'Monthly Update' ; break ;
            case 'yearly'  : $task_type_name = 'Yearly Update' ; break ;
        }   

        $row['title']              = dataFilter( $row['title'] ) ;
        $row['task_type_name']     = $task_type_name ;
        $row['expired']            = strtotime( $row['date_end'] . ' 23:59:59' ) ;
        $row['is_warning']         = ( $row['date_end'] == TODAYDAY ? 'yes' : 'no' ) ;

        $row['department_name']    = checkExists( $department_list[ $row['department_id'] ] ) ;
        $row['created_by_name']    = checkExists( $staff_list[ $row['created_by'] ] ) ;
        $row['assigned_by_name']   = checkExists( $staff_list[ $row['assigned_by'] ] ) ;
        $row['progress']           = ( $row['todo_done'] > $row['todo_list'] ? ( numberFormat( ( $row['todo_done'] / $row['todo_list'] * 100 ), 2 ) + 0 ) : 0 ) ;




        // get all joinstaff 
        $joinstaff_list            = [] ;
        $select_joinstaff          = $mysqli->query( "SELECT staff_id FROM task_joinstaff
                                                     WHERE task_id = '".$array['task_id']."'" ) ;
        if ( $select_joinstaff->num_rows > 0 ){
            while ( $row_joinstaff = $select_joinstaff->fetch_assoc() ){
                $joinstaff_list[]  = [
                    'id'           => $row_joinstaff['staff_id'],
                    'title'        => $staff_list[ $row_joinstaff['staff_id'] ]
                ] ;

                if ( $row_joinstaff['staff_id'] == $staff_info['staff_id'] ){
                    $isreject      = 'yes' ;
                }
            }
        }
        $row['joinstaffs']         = $joinstaff_list ;




        // get all todo remark
        $todo_remark_list          = [] ;
        $select_task_todo_remark   = $mysqli->query( "SELECT * FROM task_todo_remark
                                                      WHERE deleted_at IS NULL AND task_id = '".$array['task_id']."'" ) ;
        if ( $select_task_todo_remark->num_rows > 0 ){
            while ( $row_todo_remark = $select_task_todo_remark->fetch_assoc() ){
                $todo_remark_list[$row_todo_remark['todo_id']][]  = [
                    'id'           => $row_todo_remark['remark_id'],
                    'title'        => dataFilter( $row_todo_remark['title'] ),
                    'is_delete'    => 'no'
                ] ;
            }
        }

        // get all todo task
        $todo_list                 = [] ;
        $select_task_todo          = $mysqli->query( "SELECT * FROM task_todo
                                                      WHERE deleted_at IS NULL AND task_id = '".$array['task_id']."'
                                                      ORDER BY sortable ASC, todo_id ASC" ) ;
        if ( $select_task_todo->num_rows > 0 ){
            while ( $row_todo      = $select_task_todo->fetch_assoc() ){
                $todo_list[]       = [
                    'id'           => $row_todo['todo_id'],
                    'title'        => dataFilter( $row_todo['title'] ),
                    'is_done'      => $row_todo['is_done'],
                    'done_by'      => $row_todo['done_by'],
                    'done_name'    => $staff_list[ $row_todo['done_by'] ],
                    'done_at'      => $row_todo['done_at'],
                    'is_delete'    => 'no',
                    'remark'       => ( checkExists( $todo_remark_list[$row_todo['todo_id']] ) ? $todo_remark_list[$row_todo['todo_id']] : [] )
                ] ;
            }
        }
        $row['todo_list']          = $todo_list ;





        // get all reject 
        $rejected_list             = [] ;
        $select_rejected           = $mysqli->query( "SELECT * FROM task_rejected
                                                      WHERE deleted_at IS NULL AND task_id = '".$array['task_id']."'" ) ;
        if ( $select_rejected->num_rows > 0 ){
            while ( $row_rejected  = $select_rejected->fetch_assoc() ){
                $rejected_list[]   = [
                    'id'           => $row_rejected['reject_id'],
                    'title'        => dataFilter( $row_rejected['remark'] ),
                    'staff_name'   => $staff_list[ $row_rejected['staff_id'] ],
                    'created_at'   => $row_rejected['created_at']
                ] ;
            }
        }
        $row['rejected_list']      = $rejected_list ;





        // get all media
        $select_media              = $mysqli->query( "SELECT media_id, file, filetype FROM task_media
                                                      WHERE deleted_at IS NULL AND task_id = '".$array['task_id']."' AND todo_id = '".$array['todo_id']."'" ) ;
        $photos                    = [] ;
        $attachments               = [] ;
        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/Task/b/'.$row_media['file'] : '' )
                        ] ;
                    break ;
                    default     :
                        $attachments[]     = [
                            'media_id'     => $row_media['media_id'],
                            'type'         => 'online',
                            'filetype'     => $row_media['filetype'],
                            'file'         => ( $row_media['file'] != '' ? PATH.'uploads/Task/'.$row_media['file'] : '' )
                        ] ;
                }
            }
        }

        $row['photos']                     = $photos ;
        $row['attachments']                = $attachments ;



        // check update permission
        // check cancel permission
        switch ( $row['status'] ){
            case 'pending'   :
            case 'assigned'  :
            case 'resubmit'  :
            case 'progress'  :
            case 'completed' :
            case 'confirmed' :
                if ( $row['created_by']   == $staff_info['staff_id'] ){
                    $isupdate              = 'yes' ;
                    $iscancel              = 'yes' ;
                }
            break ;
        }
        $row['isupdate']                   = $isupdate ;
        $row['iscancel']                   = $iscancel ;

        // check reject permission
        switch ( $row['status'] ){
            case 'pending'   :
            case 'assigned'  :
            case 'resubmit'  :
            case 'progress'  :
            case 'completed' :
                if ( $row['assigned_by']  == $staff_info['staff_id'] ){
                    $isreject              = 'yes' ;
                }
            break ;
            case 'confirmed' :
                if ( $row['created_by']   == $staff_info['staff_id'] ){
                    $isreject              = 'yes' ;
                }
            break ;
        }
        $row['isreject']                   = $isreject ;

        // check assign permission
        if ( $staff_info['staff_tier_is_task_assigned'] == 'yes' ){
            $isassigned                    = 'yes' ;
        }
        $row['isassigned']                 = $isassigned ;

        $data                              = $row ;

    }
}

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