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

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




	$search_query          = '' ;
    if ( $array['search'] != '' ){
        $search_query     .= " AND a.title LIKE '%".$array['search']."%'" ;
    }
    if ( $array['searchstatus'] != '' ){
        // $search_query  .= " AND a.status = '".$array['searchstatus']."'" ;

        $search_status     = [] ;
        switch ( $array['searchstatus'] ){
            case 'approved' :
                $search_status = [ 'approved' ] ;
            break ;
            case 'rejected' :
                $search_status = [ 'rejected' ] ;
            break ;
            default :
                $search_status = [ 'pending', 'assigned', 'resubmit', 'progress', 'completed', 'confirmed' ] ;
        }
        $search_query     .= " AND a.status IN ( '".implode("', '", $search_status)."' )" ;
    }else{
        $search_query     .= " AND a.status != 'rejected'" ;
    }
    switch ( $array['searchincharge'] ){
        case 'create' :
            $search_query .= " AND a.created_by = '".$staff_info['staff_id']."'" ;
        break ;
        case 'assign' :
            $search_query .= " AND a.assigned_by = '".$staff_info['staff_id']."'" ;
        break ;
        case 'execute' :
            $search_query .= " AND EXISTS ( SELECT b.staff_id FROM task_joinstaff b WHERE a.task_id = b.task_id AND b.staff_id = '".$staff_info['staff_id']."' LIMIT 1 )" ;
        break ;
    }

    $query             = "SELECT a.task_id, a.task_type, a.title, a.department_id, a.created_by, a.assigned_by, a.difficulty, a.date_start, a.date_end, a.remark, a.todo_list, a.todo_done, a.status, a.created_at, a.updated_at FROM task a
                          WHERE a.deleted_at IS NULL AND ( a.created_branch_id = '".$array['branch_id']."' AND a.created_by = '".$staff_info['staff_id']."' OR a.assigned_by = '".$staff_info['staff_id']."' OR EXISTS ( SELECT b.staff_id FROM task_joinstaff b WHERE a.task_id = b.task_id AND b.staff_id = '".$staff_info['staff_id']."' LIMIT 1 ) ) " . $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          = [] ;
        $task_ids      = [] ;
        while ( $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'] ) ;
            $row['is_warning']        = ( $row['date_end'] == TODAYDAY ? 'yes' : 'no' ) ;

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

            $task_ids[]               = $row['task_id'] ;
        }

        // join staff
        $query_joinstaff  = $mysqli->query( "SELECT * FROM task_joinstaff
                                             WHERE task_id IN ( ".implode(', ', $task_ids)." )" ) ;
        $joinstaffs       = [] ;
        if ( $query_joinstaff->num_rows > 0 ){
            while ( $row_joinstaff = $query_joinstaff->fetch_assoc() ){
                $joinstaffs[$row_joinstaff['task_id']][] = $staff_list[ $row_joinstaff['staff_id'] ] ;
            }
        }

        // reset list
        $new_list         = [] ;
        foreach ( $list as $k => $v ){
            $assigned_by_name      = ( checkExists( $joinstaffs[$v['task_id']] ) ? implode( ', ', $joinstaffs[$v['task_id']] ) : $v['assigned_by_name'] ) ;
            $v['assigned_by_name'] = $assigned_by_name ;
            $new_list[]            = $v ;
        }

        $data['list']     = $new_list ;

    }
}

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