<?php
include 'connect/cms-config.php' ;
include 'requires/function.php' ;

$is_app 	     	= escapeString($_GET['is_app']) ;
if ( $is_app       == 'yes' ){

}else{
	// check permission
	include 'requires/session.php' ;
	if ( !permissionCheck($row_user, 'task-report-view') ){
		header('Location: index.php') ;
		exit ;
	}
}

// active menu bar
$active_main_menu 	= 'task' ;
$active_sub_menu 	= 'task-report' ;
$active_menu 		= 'task-report' ;

// keep parameter in value
$page 				= escapeString($_GET['page']) ;
$page_mode 			= escapeString($_GET['page_mode']) ;
$type 				= escapeString($_GET['type']) ;
$search 			= escapeString($_GET['search']) ;
$search_name 		= escapeString( $_GET['search_name'] ) ;
$search_idno 		= escapeString( $_GET['search_idno'] ) ;
$search_branch    	= escapeString( $_GET['search_branch'] ) ;
$search_department  = $_GET['search_department'] ;
$search_department_url = '' ;
$search_tier 		= escapeString( $_GET['search_tier'] ) ;
$search_datefrom	= escapeString( $_GET['search_datefrom'] ) ;
$search_dateto		= escapeString( $_GET['search_dateto'] ) ;

$search_query 	    = '' ;

// pagination
if (isset($page) && !empty($page)) { $product_page = $page ; } else { $product_page = 1 ; } // next and prev page (5 thing need to change)
$start_from 		= ($product_page - 1) * LIMIT ; //end next and prev page

if ( $search_name  != '' ){
	$search_query  .= " AND a.staff_name LIKE '%".$search_name."%'" ;
}
if ( $search_idno  != '' ){
	$search_query  .= " AND a.staff_idno LIKE '%".$search_idno."%'" ;
}
if ( $search_branch != '' ){
	$search_query  .= " AND a.branch_id = '".$search_branch."'" ;
}
if ( $search_department != '' ){
	if ( arrayCheck( $search_department ) ){
		$search_query .= " AND EXISTS ( SELECT b.staff_id FROM staff_department b WHERE a.staff_id = b.staff_id AND b.department_id IN (".implode(',',$search_department).") LIMIT 1 )" ;
        foreach( $search_department as $k => $v ){
            $search_department_url .= '&search_department%5B'.$k.'%5D='.$v;
        }
    } 
}
if ( $search_tier  != '' ){
	$search_query  .= " AND a.staff_tier = '".$search_tier."'" ;
}
if ( $search_datefrom != '' ){
	if ( $search_dateto != '' ){
		$search_query  .= " AND a.created_at BETWEEN '".$search_datefrom." 00:00:00' AND '".$search_dateto." 23:59:59'" ;
	}else{
		$search_query  .= " AND a.created_at LIKE '%".$search_datefrom."%'" ;
	}
}

// set search url
$search_url 		= 'is_app='.$is_app.'&search='.$search.'&search_name='.$search_name.'&search_idno='.$search_idno.'&search_branch='.$search_branch.'&search_tier='.$search_tier . $search_department_url ;

// page query
$mysqli_query       = "SELECT a.staff_id, a.staff_name, a.staff_idno, a.staff_tier, 
				      	    ( select COUNT(task_id) from task where created_by = a.staff_id ) as total_created, 
				      	    ( select COUNT(task_id) from task where assigned_by = a.staff_id ) as total_assigned, 
				      	    ( select COUNT(task_id) from task_joinstaff where staff_id = a.staff_id ) as total_executed
			      	   FROM staff a
                       WHERE a.deleted_at IS NULL AND ( a.staff_date_resigned >= '".date("Y-m-d",time())."' OR a.staff_date_resigned = '0000-00-00' OR a.staff_date_resigned IS NULL )" . $search_query . $user_branch_permission_sql_a ;
$mysqli_page        = $mysqli->query( $mysqli_query." ORDER BY a.staff_tier ASC, a.staff_idno ASC LIMIT $start_from, " . LIMIT ) ;

// load pagination
$page_pagination    = nextPrevious($product_page, LIMIT, $search_url, $mysqli_query) ;

// all required parameter
// get all branch
$branch             = [] ;
$get_branch         = $mysqli->query("SELECT branch_id, branch_name FROM branch
							          WHERE deleted_at IS NULL") ;
if ( $get_branch->num_rows > 0 ){
	while ( $row_branch = $get_branch->fetch_assoc() ){
		$branch[$row_branch['branch_id']] = $row_branch['branch_name'] ;
	}
}

// department check list
$department         = [] ;
$get_department     = $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 = 'en'") ;
if ( $get_department->num_rows > 0 ){
    while ( $row_department = $get_department->fetch_assoc() ){
        $department[$row_department['department_id']] = $row_department['department_desc'] ;
    }
}

// get all requires 
$tier_list   = [] ;
$mysqli_tier = $mysqli->query("SELECT a.tier_id, b.title FROM profile_tier a
							   LEFT JOIN profile_tier_translation b ON ( a.tier_id = b.tier_id )
							   WHERE a.deleted_at IS NULL AND b.lang = 'en' ORDER BY a.sortable ASC") ;
if ( $mysqli_tier->num_rows > 0 ){
	while ( $row_tier = $mysqli_tier->fetch_assoc() ){
		$tier_list[]  = $row_tier ;
	}
}

// start header here
include 'requires/page_header.php' ;
include 'requires/page_top.php' ; 
?>

<?php if ( $is_app == 'yes' ){ ?>
	<style>
		header, aside.left-panel, .marquee, .page-header, footer{ display: none; }
		.content{ margin-left: 0; }
	</style>
<?php } ?>

<!-- Header Ends -->
<div class="warper container-fluid">
<div class='container' style="background-color: white; border-radius: 10px;">
    <div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;">
		<h1><?= $lang['Task']?> <small><?= $lang['report']?></small></h1>
	</div>

    <div class="panel panel-default" id="basic-table-title">
        <div class="panel-heading">search</div>
        <div class="panel-body">
            <form method="get" class="form-horizontal">
            	<div class="form-group">
                    <label class="col-sm-2 control-label">Name</label>
                    <div class="col-sm-9">
                        <input type="text" name="search_name" value="<?= $search_name ?>" class="form-control" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">ID No</label>
                    <div class="col-sm-9">
                        <input type="text" name="search_idno" value="<?= $search_idno ?>" class="form-control" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Branch</label>
                    <div class="col-sm-9">
                        <select name="search_branch" class="form-control">
                            <option value="">All</option>
                            <?php
                            foreach ( $branch as $kk => $vv ){
                            	echo '<option value="'.$kk.'" '.( $search_branch == $kk ? 'selected' : '' ).' >'.$vv.'</option>' ;
                            }
                            ?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Department</label>
                    <div class="col-sm-9">
                        <select name="search_department[]" class="chosen-select select2-basic-single form-control" multiple >
                            <?php
                            foreach ( $department as $kk => $vv ){
                                $selected = '';
                                
                                if(arrayCheck($_GET['search_department'])){
                                    if(in_array("$kk", $_GET['search_department'], TRUE)){
                                         $selected = 'selected';
                                    }
                                }
                                echo '<option value="'.$kk.'" '.$selected.'>'.$vv.'</option>' ;
                            }
                            ?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Tier</label>
                    <div class="col-sm-9">
                        <select name="search_tier" class="form-control">
                            <option value="">All Tier</option>
                            <?php foreach( $tier_list as $k => $v ){ ?>
                            	<option value="<?= $v['tier_id'] ?>" <?= ( $search_tier == $v['tier_id'] ? 'selected' : '' ) ?> ><?= $v['title'] ?></option>
                            <?php } ?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Date From</label>
                    <div class="col-sm-9">
                        <input type="date" name="search_datefrom" value="<?= $search_datefrom ?>" class="form-control" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Date To</label>
                    <div class="col-sm-9">
                        <input type="date" name="search_dateto" value="<?= $search_dateto ?>" class="form-control" />
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-11">
                        <input type="hidden" name="is_app" value="<?= $is_app ?>" />
                        <input type="hidden" name="search" value="<?= $search ?>" />
                        <button type="submit" class="btn" style="margin-top:5px;float:right;color:white;background-color: #5e5bd0;width: 100px;"><?= $lang['submit'] ?></button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <form method="post">

        <div class="panel panel-default">
            <div class="panel-heading clearfix">listing</div>
            <div class="panel-body">
				<table cellpadding="0" cellspacing="0" border="0" class="listing-table responsive table table-striped table-bordered" id="basic-datatable">
					<thead>
						<tr>
							<?php if ( $is_app != 'yes' ){ ?><th>Action</th><?php } ?>
							<th>Tier</th>
							<th>ID</th>
							<th>Name</th>
							<th>Total Created</th>
							<th>Total Assigned</th>
							<th>Total Executed</th>
						</tr>
					</thead>
					<tbody>
						<?php
						if ( $mysqli_page->num_rows > 0 ){
							while ( $row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ){

								echo '
								 <tr class="odd gradeX">' ;
								 	if ( $is_app != 'yes' ){
								 		echo '
									 	<td class="align_center">
									 		<a target="_blank" title="'.$lang['Edit Current'].$lang['Staff'].'" href="hr-staff.php?page_mode=edit&page='.$row_page['staff_id'].'"><i class="fa fa-edit"></i></a>
									 	</td>' ;
								 	}
								 	echo '
									<td class="align_center">'.dataFilter($row_page['staff_tier']).'</td>
									<td class="align_center">'.dataFilter($row_page['staff_idno']).'</td>
									<td>'.dataFilter($row_page['staff_name']).'</td>
									<td class="align_center">
										<a href="'.PATH.'task.php?is_app='.$is_app.'&search_staffid='.$row_page['staff_id'].'&search_mode=created" class="app_click_task_report" target="_blank">'.( $row_page['total_created'] > 0 ? $row_page['total_created'] : '' ).'</a>
									</td>
									<td class="align_center">
										<a href="'.PATH.'task.php?is_app='.$is_app.'&search_staffid='.$row_page['staff_id'].'&search_mode=assigned" class="app_click_task_report" target="_blank">'.( $row_page['total_assigned'] > 0 ? $row_page['total_assigned'] : '' ).'</a>
									</td>
									<td class="align_center">
										<a href="'.PATH.'task.php?is_app='.$is_app.'&search_staffid='.$row_page['staff_id'].'&search_mode=executed" class="app_click_task_report" target="_blank">'.( $row_page['total_executed'] > 0 ? $row_page['total_executed'] : '' ).'</a>
									</td>
								</tr>';
							}
						}else{
							echo '
							<tr class="odd gradeX">
								<td class="border_none">'.$lang['no_data'].'</td>
								'.( $is_app != 'yes' ? '<td class="border_none"></td>' : '' ).'
								<td class="border_none"></td>
								<td class="border_none"></td>
								<td class="border_none"></td>
								<td class="border_none"></td>
								<td class="border_none"></td>
							</tr>' ;
						}
						?>
					</tbody>
				</table>
				<?= $page_pagination['page_pagination'] ?>
            </div>
        </div>
	</form>
</div>
</div>

<?php
// footer
include 'requires/page_footer.php' ;
?>