<?php
	$active_menu 	  = 'hr-staff-point-history' ;

	$search_query = '';

	$staff_all = [];

	$mysqli_staff = $mysqli->query("SELECT staff_idno, staff_id FROM staff
									   WHERE staff_id != ''");
	if ($mysqli_staff->num_rows > 0){
		while($row_staff = $mysqli_staff->fetch_array(MYSQLI_ASSOC)){
			$staff_all[$row_staff['staff_idno']] = $row_staff['staff_id'];
		}
	}

	$search_name = $_GET['search_name'];
	$search_date = $_GET['search_date'];

	if ( $search_date != '' ){
	    $search_query .= " AND ( a.created_at LIKE '%".$search_date."%' )" ;
	}
	if ( $search_name != '' ){
	    $search_query .= " AND ( b.staff_name LIKE '%".$search_name."%' )" ;
	}


	// page query
	$mysqli_query = "SELECT a.created_at, a.balance, a.before_amount, a.amount, b.staff_name FROM staff_point_movement a
					 LEFT JOIN staff b ON (a.staff_id = b.staff_id)
					 WHERE a.deleted_at IS NULL " . $search_query .$user_branch_permission_sql_b ;

	// 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

	// set search url
	$search_url = 'search='.$search.'&search_name='.$search_name.'&search_date='.$search_date.'&page_mode=staff-point-history' ;

	$mysqli_page = $mysqli->query($mysqli_query."ORDER by a.staff_id, a.movement_id DESC LIMIT $start_from, " . LIMIT) ;

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

	include 'requires/page_header.php';
	include 'requires/page_top.php';
?>
<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['Point']?><small><?=$lang['Movement']?></small></h1>
		<?= $display_error ?>
	</div>

    <div class="panel panel-default" id="basic-table-title">
        <div class="panel-heading"><?=$lang['search']?></div>
        <div class="panel-body">
            <form method="get" class="form-horizontal">
                <div class="form-group">
                    <label class="col-sm-2 control-label"><?= $lang['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"><?= $lang['date'] ?></label>
                    <div class="col-sm-9">
                        <input class="form-control" name="search_date" type="date" value="<?= ($search_date!= '' ? $search_date : '' ) ?>" placeholder="Date">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-11">
                        <input type="hidden" name="page_mode" value="<?= $page_mode ?>" />
                        <input type="hidden" name="search" value="<?= $search ?>" />
                        <button type="submit" class="btn" style="color:white;float:right;background-color: #5e5bd0;margin-top: 5px;width: 100px;"><?= $lang['submit'] ?></button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <form method="post">
        <div class="panel panel-default">
            <div class="panel-heading"><?=$lang['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>
							<th><?= $lang['Staff'] ?></th>
							<th><?= $lang['before'] ?></th>
							<th><?= $lang['amount'] ?></th>
							<th><?= $lang['Total'] ?></th>
                            <th width="50"><?= $lang['date'] ?></th>
						</tr>
					</thead>
					<tbody>
						<?php
						if( $mysqli_page->num_rows > 0 ){
							while( $row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ){

								if($row_page['before_amount'] >= $row_page['balance']){
									$movement_action = 'minus';
								}else{
									$movement_action = 'add';
								}

								echo '
								 <tr class="odd gradeX">
									<td class="align_center">'.dataFilter($row_page['staff_name']).'</td>
									<td class="align_center">'.dataFilter($row_page['before_amount']).'</td>
									<td class="align_center">'.($movement_action == 'add' ? '+' : '').dataFilter($row_page['amount']).'</td>
									<td class="align_center">'.dataFilter($row_page['balance']).'</td>
									<td class="align_center">'.resetDateFormat($row_page['created_at']).'</td>
								</tr>';
							}
						}else{
							echo '
							<tr class="odd gradeX">
								<td class="border_none">'.$lang['no_data'].'</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>