<?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']) ;
$point_id			= escapeString($_GET['point_id']) ;

// active menu bar
$active_main_menu   = 'setting' ;
$active_sub_menu    = 'setting-app' ;
$active_menu        = 'setting-app-point' ;

// check permission
if ( !permissionCheck($row_user, 'app-point-view') ){
	header('Location: index.php') ;
	exit ;
}

// mode type | all list | new | edit
switch( $page_mode ){
	
	// edit association category
	case 'new' :
	case 'edit' :

		// check query exsits
		$submit_type = 'new' ;
		$mysqli_page = $mysqli->query("SELECT * FROM setting_point
									   WHERE point_id = '".$point_id."' LIMIT 1");
		if ($mysqli_page->num_rows > 0){
			// keep query value in array
			$row_page    = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
			$submit_type = 'edit' ;
		}

		// update database
		if ( isset($type) && ( $type == 'new' || $type == 'edit' ) && $_POST['hide'] == 1 ){

			// keep value in variable
			$page_title      = escapeString($_POST['title']) ;
			$page_title      = ($page_title != '' ? $page_title : 'No Title') ;
			if ( $point_id == '' ){
				$mysqli->query( "INSERT INTO setting_point 
							    ( created_at ) VALUES 
							    ( '".TODAYDATE."' )" ) ;
				$point_id = $mysqli->insert_id ;	
			}

			// update database
			$mysqli->query( "UPDATE setting_point SET
							point_title        = '".escapeString($_POST['point_title'])."',
							point_description  = '".escapeString($_POST['point_description'])."',
							point_from   	   = '".escapeString($_POST['point_from'])."',
							point_type  	   = '".escapeString($_POST['point_type'])."',
							difficulty   	   = '".escapeString($_POST['difficulty'])."',
							point_value  	   = '".escapeString($_POST['point_value'])."'
							WHERE point_id     = '".$point_id."'" ) ;

			// refresh page
			header("Location:app-point.php?page_mode=edit&point_id=".$point_id."&success=1") ;
			$_SESSION['system_result'] = 'success-updated' ;
			exit ;
		}

		if ( ( $page_mode == 'new' && !permissionCheck($row_user, 'app-point-new') ) ||
			 ( $page_mode == 'edit' && !permissionCheck($row_user, 'app-point-edit') ) ){
			header('Location: app-point.php') ;
			exit ;
		}

		// start header here
		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>Point Adjustment <small><?= $lang[$page_mode] ?></small></h1></div>
			<?php
            if ($_SESSION['system_result'] != ''){
                switch($_SESSION['system_result']){
                    case 'success-updated' :
                        echo '<div class="result_success">'.$lang['Thank you details has been updated'].'</div>' ;	
                    break ;
                }
                unset($_SESSION['system_result']) ;
            }
            ?>
            <div class="panel panel-default">
                <div class="panel-heading"></div>
                <div class="panel-body">
                    <form method="post" class="form-horizontal" id="quotationForm" enctype="multipart/form-data" action="app-point.php?page_mode=edit&point_id=<?= $point_id ?>&type=edit" novalidate="novalidate">

                        <div class="form-group">
                            <div class="col-sm-2 control-label">Title</div>
                            <div class="col-sm-9">
                                <input type="text" name="point_title" class="form-control ui-search-input" value="<?= dataFilter($row_page['point_title']) ?>" placeholder="Title" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Description</div>
                            <div class="col-sm-9">
                                <input type="text" name="point_description" class="form-control ui-search-input" value="<?= dataFilter($row_page['point_description']) ?>" placeholder="Description" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">From</div>
                            <div class="col-sm-9">
                                <select name="point_from" class="form-control ui-search-input" >
                                	<option value="">Select</option>
                                	<option <?= ( $row_page['point_from'] == 'system' ? 'selected' : '' ) ?> value="system">System</option>
                                	<option <?= ( $row_page['point_from'] == 'task' ? 'selected' : '' ) ?> value="task">Task</option>
                                	<option <?= ( $row_page['point_from'] == 'redeem' ? 'selected' : '' ) ?> value="redeem">Redeem</option>
                                	<option <?= ( $row_page['point_from'] == 'adjustment' ? 'selected' : '' ) ?> value="adjustment">Adjustment</option>
                                	<option <?= ( $row_page['point_from'] == 'training' ? 'selected' : '' ) ?> value="training">Training</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Type</div>
                            <div class="col-sm-9">
                                <select name="point_type" class="form-control ui-search-input" >
                                	<option value="">Select</option>
                                	<option <?= ( $row_page['point_type'] == 'reset' ? 'selected' : '' ) ?>  value="reset">Reset</option>
                                	<option <?= ( $row_page['point_type'] == 'forward' ? 'selected' : '' ) ?>  value="forward">Forward</option>
                                	<option <?= ( $row_page['point_type'] == 'exchange' ? 'selected' : '' ) ?>  value="exchange">Exchange</option>
                                	<option <?= ( $row_page['point_type'] == 'exchange-refund' ? 'selected' : '' ) ?>  value="exchange-refund">Exchange Refund</option>
                                	<option <?= ( $row_page['point_type'] == 'plus' ? 'selected' : '' ) ?>  value="plus">Plus</option>
                                	<option <?= ( $row_page['point_type'] == 'minus' ? 'selected' : '' ) ?>  value="minus">Minus</option>
                                	<option <?= ( $row_page['point_type'] == '1time' ? 'selected' : '' ) ?>  value="1time">1 Time</option>
                                	<option <?= ( $row_page['point_type'] == 'daily' ? 'selected' : '' ) ?>  value="daily">Daily</option>
                                	<option <?= ( $row_page['point_type'] == 'weekly' ? 'selected' : '' ) ?>  value="weekly">Weekly</option>
                                	<option <?= ( $row_page['point_type'] == 'monthly' ? 'selected' : '' ) ?>  value="monthly">Monthly</option>
                                	<option <?= ( $row_page['point_type'] == 'yearly' ? 'selected' : '' ) ?>  value="yearly">Yearly</option>
                                	<option <?= ( $row_page['point_type'] == 'fair' ? 'selected' : '' ) ?>  value="fair">Fair</option>
                                	<option <?= ( $row_page['point_type'] == 'good' ? 'selected' : '' ) ?>  value="good">Good</option>
                                	<option <?= ( $row_page['point_type'] == 'excellent' ? 'selected' : '' ) ?>  value="excellent">Excellent</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Difficulty</div>
                            <div class="col-sm-9">
                                <select name="difficulty" class="form-control ui-search-input" >
                                	<option value="">Select</option>
                                	<option <?= ( $row_page['difficulty'] == 'normal' ? 'selected' : '' ) ?> value="normal">Normal</option>
                                	<option <?= ( $row_page['difficulty'] == 'easy' ? 'selected' : '' ) ?> value="easy">Easy</option>
                                	<option <?= ( $row_page['difficulty'] == 'medium' ? 'selected' : '' ) ?> value="medium">Medium</option>
                                	<option <?= ( $row_page['difficulty'] == 'hard' ? 'selected' : '' ) ?> value="hard">Hard</option>
                                	<option <?= ( $row_page['difficulty'] == 'extreme' ? 'selected' : '' ) ?> value="extreme">Extreme</option>
                                	<option <?= ( $row_page['difficulty'] == 'master' ? 'selected' : '' ) ?> value="master">Master</option>
                                	<option <?= ( $row_page['difficulty'] == 'workupdate' ? 'selected' : '' ) ?> value="workupdate">Workupdate</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Point</div>
                            <div class="col-sm-9">
                                <input type="text" name="point_value" class="form-control ui-search-input" value="<?= dataFilter($row_page['point_value']) ?>" placeholder="Point" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label"></div>
                            <div class="col-sm-9">
                                <button type="submit" class="btn" style="color:white;background-color: #5e5bd0;float:right; margin-top: 5px;width: 100px;">Submit</button>
                                <input type="hidden" name="hide" value="1">
                                <input type="hidden" name="page_status" value="<?= $submit_type ?>">
                            </div>
                        </div>   
                    </form>
                </div>
            </div>
        </div>
		</div>

		<?php
		
	break ;


	
	// all association list
	case 'all' :
	default :
		
		// query type
		$search_query      = '' ;
		
		$search_title      = escapeString($_GET['search_title']) ;
		$search_from       = escapeString($_GET['search_from']) ;
		$search_type       = escapeString($_GET['search_type']) ;
		$search_difficulty = escapeString($_GET['search_difficulty']) ;
		
		// search query
		if ( $search_title != ''){
			$search_query .= " AND ( point_title LIKE '%".$search_title."%' OR point_description LIKE '%".$search_title."%' )" ;
		}
		if ( $search_from != ''){
			$search_query .= " AND point_from = '".$search_from."'" ;
		}
		if ( $search_type != ''){
			$search_query .= " AND point_type = '".$search_type."'" ;
		}
		if ( $search_difficulty != ''){
			$search_query .= " AND difficulty = '".$search_difficulty."'" ;
		}
		
		// form submit
		if ( $_POST['hide'] == '1' && $_POST['hide_status'] == 'action' ){
			switch($_POST['page_action']){
				case 'trash':
					$mysqli_query = "UPDATE setting_point SET
									 deleted_at        = '".TODAYDATE."'
									 WHERE point_id = " ;
					$trash_page = trashPage('category', $mysqli, $mysqli_query, $_POST['multiple_trash']) ;
				break;
			}
		}
		
		// 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_title='.$search_title.'&search_from='.$search_from.'&search_type='.$search_type.'&search_difficulty='.$search_difficulty ;
		
		// page query
		$mysqli_query = "SELECT a.* FROM setting_point a
						 WHERE a.deleted_at IS NULL " . $search_query ;
		$mysqli_page = $mysqli->query( $mysqli_query." ORDER BY a.point_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='container' style="background-color: white; border-radius: 10px;">
			<div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;">
				<div class="row">
                	<div class="pull-left col">
						<h1>Point Adjustment <small><?= $lang['list']?></small></h1>
					</div>
                	<div class="pull-right col">
				<?php if ( permissionCheck($row_user, 'app-point-new') ){ ?>
					<a href="app-point.php?page_mode=new" class="btn" style="color:white;background-color: #5e5bd0;" target="_blank"><?= $lang['add_new']?></a>
				<?php } ?>
					</div>
				</div>
			</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"><?= $lang['title'] ?></label>
		                    <div class="col-sm-9">
		                        <input type="text" name="search_title" value="<?= $search_title ?>" class="form-control" />
		                    </div>
		                </div>
		                <div class="form-group">
		                    <label class="col-sm-2 control-label">From</label>
		                    <div class="col-sm-9">
		                        <select name="search_from" class="form-control ui-search-input" >
                                	<option value="">Select</option>
                                	<option <?= ( $search_from == 'system' ? 'selected' : '' ) ?> value="system">System</option>
                                	<option <?= ( $search_from == 'task' ? 'selected' : '' ) ?> value="task">Task</option>
                                	<option <?= ( $search_from == 'redeem' ? 'selected' : '' ) ?> value="redeem">Redeem</option>
                                	<option <?= ( $search_from == 'adjustment' ? 'selected' : '' ) ?> value="adjustment">Adjustment</option>
                                	<option <?= ( $search_from == 'training' ? 'selected' : '' ) ?> value="training">Training</option>
                                </select>
		                    </div>
		                </div>
		                <div class="form-group">
		                    <label class="col-sm-2 control-label">Type</label>
		                    <div class="col-sm-9">
		                        <select name="search_type" class="form-control ui-search-input" >
                                	<option value="">Select</option>
                                	<option <?= ( $search_type == 'reset' ? 'selected' : '' ) ?>  value="reset">Reset</option>
                                	<option <?= ( $search_type == 'forward' ? 'selected' : '' ) ?>  value="forward">Forward</option>
                                	<option <?= ( $search_type == 'exchange' ? 'selected' : '' ) ?>  value="exchange">Exchange</option>
                                	<option <?= ( $search_type == 'exchange-refund' ? 'selected' : '' ) ?>  value="exchange-refund">Exchange Refund</option>
                                	<option <?= ( $search_type == 'plus' ? 'selected' : '' ) ?>  value="plus">Plus</option>
                                	<option <?= ( $search_type == 'minus' ? 'selected' : '' ) ?>  value="minus">Minus</option>
                                	<option <?= ( $search_type == '1time' ? 'selected' : '' ) ?>  value="1time">1 Time</option>
                                	<option <?= ( $search_type == 'daily' ? 'selected' : '' ) ?>  value="daily">Daily</option> 
                                	<option <?= ( $search_type == 'weekly' ? 'selected' : '' ) ?>  value="weekly">Weekly</option>
                                	<option <?= ( $search_type == 'monthly' ? 'selected' : '' ) ?>  value="monthly">Monthly</option>
                                	<option <?= ( $search_type == 'yearly' ? 'selected' : '' ) ?>  value="yearly">Yearly</option>
                                	<option <?= ( $search_type == 'fair' ? 'selected' : '' ) ?>  value="fair">Fair</option>
                                	<option <?= ( $search_type == 'good' ? 'selected' : '' ) ?>  value="good">Good</option>
                                	<option <?= ( $search_type == 'excellent' ? 'selected' : '' ) ?>  value="excellent">Excellent</option>
                                </select>
		                    </div>
		                </div>
		                <div class="form-group">
		                    <label class="col-sm-2 control-label">Difficulty</label>
		                    <div class="col-sm-9">
		                        <select name="search_difficulty" class="form-control ui-search-input" >
                                	<option value="">Select</option>
                                	<option <?= ( $search_difficulty == 'normal' ? 'selected' : '' ) ?> value="normal">Normal</option>
                                	<option <?= ( $search_difficulty == 'easy' ? 'selected' : '' ) ?> value="easy">Easy</option>
                                	<option <?= ( $search_difficulty == 'medium' ? 'selected' : '' ) ?> value="medium">Medium</option>
                                	<option <?= ( $search_difficulty == 'hard' ? 'selected' : '' ) ?> value="hard">Hard</option>
                                	<option <?= ( $search_difficulty == 'extreme' ? 'selected' : '' ) ?> value="extreme">Extreme</option>
                                	<option <?= ( $search_difficulty == 'master' ? 'selected' : '' ) ?> value="master">Master</option>
                                	<option <?= ( $search_difficulty == 'workupdate' ? 'selected' : '' ) ?> value="workupdate">Workupdate</option>
                                </select>
		                    </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;background-color: #5e5bd0;float:right; 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">listing</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>Action</th>
									<th>Title</th>
									<th>Description</th>
									<th>From</th>
									<th>Type</th>
									<th>Difficulty</th>
									<th>Point</th>
									<th>Created Date</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="align_center">' ;
												if ( permissionCheck($row_user, 'app-point-edit') ){
													echo '
										 			<a title="Edit Current point" href="app-point.php?page_mode=edit&point_id='.$row_page['point_id'].'"><i class="fa fa-edit"></i></a>' ;
												}else{
													echo '-' ;
												}
											echo '
										 	</td>
											<td>'.dataFilter($row_page['point_title']).'</td>
											<td>'.dataFilter($row_page['point_description']).'</td>
											<td class="text-center">'.ucwords($row_page['point_from']).'</td>
											<td class="text-center">'.ucwords($row_page['point_type']).'</td>
											<td class="text-center">'.ucwords($row_page['difficulty']).'</td>
											<td class="text-center">'.dataFilter($row_page['point_value']).'</td>
											<td class="text-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>
										<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
	break ;

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