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

// include the class
include 'requires/class_resize.php' ;

// keep parameter in value
$page 				= escapeString($_GET['page']) ;
$page_mode 			= escapeString($_GET['page_mode']) ;
$type 				= escapeString($_GET['type']) ;
$search 			= escapeString($_GET['search']) ;

// active menu bar
$active_main_menu 	= 'service' ;
$active_sub_menu 	= 'ourinbox' ;
$active_menu 		= 'ourinbox-request-list' ;

// check permission
if ( !permissionCheck($row_user, 'our-category-main-stock') ){
	header('Location: index.php') ;
	exit ;
}

// query type
$search_query      = '' ;
$main_id           = ( $_GET['main_id'] != '' ? escapeString($_GET['main_id']) : '0' ) ;
$sub_id            = ( $_GET['sub_id'] != '' ? escapeString($_GET['sub_id']) : '0' ) ;
if ( $sub_id 	   > 0 ){
	$main_id   	   = '0' ;
}

if ( $_POST['hidden'] == 1 ){

	$error 		   = 'failed' ;

	$updatequantity= escapeString($_POST['updatequantity']) ;
	$updateremark  = escapeString($_POST['updateremark']) ;

	if ( ( $updatequantity < 0 || $updatequantity > 0) && $updatequantity != '' ){
		$before 	   = 0 ;

		// get last movment
		$mysqli_select = $mysqli->query( "SELECT balance FROM setting_request_movement 
										  WHERE deleted_at IS NULL AND main_id = '".$main_id."' AND sub_id = '".$sub_id."'
										  ORDER BY movement_id DESC
										  LIMIT 1" ) ;
		if ( $mysqli_select->num_rows > 0 ){
			$row_select = $mysqli_select->fetch_assoc() ;
			$before     = $row_select['balance'] ;
		}

		$quantity       = $updatequantity ;
		$balance 		= ( $before + $quantity ) ;

		$mysqli->query( "INSERT INTO setting_request_movement
						 ( main_id, sub_id, before_quantity, quantity, balance, remark ) VALUES
						 ( '".$main_id."', '".$sub_id."', '".$before."', '".$quantity."', '".$balance."', '".$updateremark."' )" ) ;
		$error 		    = 'success' ;
	}

	// refresh page
	$_SESSION['system_result'] = $error ;
	header("Refresh:0") ;
	exit ;
}


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

// set search url
$search_url 	 = 'search_name='.$search_name ;

// page query
$mysqli_query 	 = "SELECT * FROM setting_request_movement
				    WHERE deleted_at IS NULL AND main_id = '".$main_id."' AND sub_id = '".$sub_id."' " . $search_query ;
$mysqli_page 	 = $mysqli->query( $mysqli_query." ORDER BY movement_id DESC LIMIT $start_from, " . LIMIT ) ;

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

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

?>
<!-- Header Ends -->
<div class="warper container-fluid">   	
	
	<div class="page-header">
		<h1>Stock Movement <small><?= $lang['list']?></small></h1>

		<?php
        if ($_SESSION['system_result'] != ''){
            switch($_SESSION['system_result']){
            	case 'failed' :
                    echo '<div class="result_error">Sorry, please try again</div>' ;	
                break ;
                case 'success' :
                    echo '<div class="result_success">Thank you details has been updated</div>' ;	
                break ;
            }
            unset($_SESSION['system_result']) ;
        }
        ?>
	</div>
	
    <div class="panel panel-default" id="basic-table-title">
        <div class="panel-heading"></div>
        <div class="panel-body">
            <form method="POST" class="form-horizontal" style="max-width:600px;">
                <div class="form-group">
                    <label class="col-sm-2 control-label">Quantity</label>
                    <div class="col-sm-9">
                        <input type="text" name="updatequantity" class="form-control" required />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Remark</label>
                    <div class="col-sm-9">
                        <input type="text" name="updateremark" class="form-control" required />
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-11">
                        <input type="hidden" name="hidden" value="1" />
                        <button type="submit" class="btn btn-purple" style="float:right"><?= $lang['submit'] ?></button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <form method="post">

        <div class="panel panel-default">
            <div class="panel-heading"></div>
            <div class="panel-body">
				<table cellpadding="0" cellspacing="0" border="0" class="responsive table table-striped table-bordered" id="basic-datatable">
					<thead>
						<tr>
							<th>Date</th>
							<th>Before</th>
							<th>Quantity</th>
                            <th>Balance</th>
                            <th>Remark</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">
									<td class="text-center">'.resetDateTimeFormat($row_page['created_at']).'</td>
									<td class="text-center">'.dataFilter($row_page['before_quantity']).'</td>
									<td class="text-center">'.dataFilter($row_page['quantity']).'</td>
									<td class="text-center">'.dataFilter($row_page['balance']).'</td>
									<td class="text-center">'.dataFilter($row_page['remark']).'</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>

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