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

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

// 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_adjustment
									   WHERE adjustment_id = '".$adjustment_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 ( $adjustment_id == '' ){
				$mysqli->query( "INSERT INTO setting_adjustment 
							    ( created_at ) VALUES 
							    ( '".TODAYDATE."' )" ) ;
				$adjustment_id = $mysqli->insert_id ;	
			}

			$set_tier          = [] ;
			foreach ( $_POST['set_tier'] as $k => $v ){
				$set_tier[]    = $v ;
			}

			// update database
			$mysqli->query( "UPDATE setting_adjustment SET
							adjustment_group    = '".escapeString($_POST['adjustment_group'])."',
							adjustment_site     = '".escapeString($_POST['adjustment_site'])."',
							adjustment_type     = '".escapeString($_POST['adjustment_type'])."',
							adjustment_scenario = '".escapeString($_POST['adjustment_scenario'])."',
							adjustment_times    = '".escapeString($_POST['adjustment_times'])."',
							set_tier   		    = '|".implode( '|', $set_tier )."|',
							adjustment_mode     = '".escapeString($_POST['adjustment_mode'])."',
							point  	            = '".escapeString($_POST['point'])."'
							WHERE adjustment_id = '".$adjustment_id."'" ) ;

			foreach ( $LANGS as $klang => $vlang ){
				$title        = escapeString( $_POST['title_'.$klang] ) ;

				checkLangUpdate( 'setting_adjustment_translation', 'adjustment_id', $adjustment_id, $klang, [ 
					'title' 	=> [ 'type' => 'input', 'value' => $title   ]
				] ) ;
			}

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

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

		// get all requires 
		// get all adjustment group
		$group_list         = [] ;
		$mysqli_group       = $mysqli->query("SELECT * FROM setting_adjustment_group
											 WHERE deleted_at IS NULL") ;
		if ( $mysqli_group->num_rows > 0 ){
			while ( $row_group = $mysqli_group->fetch_assoc() ){
				$group_list[]  = $row_group ;
			}
		}

		// get all tier
		$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 ;
			}
		}

		if ( ( $page_mode == 'new' && !permissionCheck($row_user, 'app-adjustment-new') ) ){
			header('Location: index.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>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-adjustment.php?page_mode=edit&adjustment_id=<?= $adjustment_id ?>&type=edit" novalidate="novalidate">

                    	<div class="form-group">
                            <div class="col-sm-2 control-label">Group</div>
                            <div class="col-sm-9">
                                <select name="adjustment_group" class="form-control ui-search-input" >
                                	<option value="">Select</option>
                                	<?php 
									foreach( $group_list as $k => $v ){ ?>
					        			<option value="<?= $v['group_id'] ?>" <?= ( $row_page['adjustment_group'] == $v['group_id'] ? 'selected' : '') ?> ><?= $v['title'] ?></option>
					        		<?php } ?>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Site</div>
                            <div class="col-sm-9">
                                <select name="adjustment_site" class="form-control ui-search-input" >
                                	<option value="">Select</option>
                                	<option <?= ( $row_page['adjustment_site'] == 'backend' ? 'selected' : '' ) ?> value="backend">Backend</option>
                                	<option <?= ( $row_page['adjustment_site'] == 'frontend' ? 'selected' : '' ) ?> value="frontend">Frontend</option>
                                </select>
                            </div>
                        </div>

                    	<?php 
                		echo showTabs( 'setting_adjustment_translation', 'adjustment_id', $adjustment_id, [ 
                			'title'   => [
                				'type'    => 'input', 
                				'title'   => $lang['title']
                			]
                		]) ; 
                    	?>

                        <div class="form-group">
                            <div class="col-sm-2 control-label">Type</div>
                            <div class="col-sm-9">
                                <select name="adjustment_type" class="form-control ui-search-input" >
                                	<option value="">Select</option>
                                	<option <?= ( $row_page['adjustment_type'] == 'plus' ? 'selected' : '' ) ?> value="plus">Plus</option>
                                	<option <?= ( $row_page['adjustment_type'] == 'minus' ? 'selected' : '' ) ?> value="minus">Minus</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Mode</div>
                            <div class="col-sm-9">
                                <select name="adjustment_mode" class="form-control ui-search-input" >
                                	<option value="">Select</option>
                                	<option <?= ( $row_page['adjustment_mode'] == 'fixed' ? 'selected' : '' ) ?> value="fixed">Fixed</option>
                                	<option <?= ( $row_page['adjustment_mode'] == 'custom' ? 'selected' : '' ) ?> value="custom">Custom</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Scenario</div>
                            <div class="col-sm-9">
                                <select name="adjustment_scenario" class="form-control ui-search-input" >
                                	<?php foreach ( [ 'none','daily','2daily','weekly','monthly','quaterly','annually' ] as $k => $v ){ ?>
                                		<option <?= ( $row_page['adjustment_scenario'] == $v ? 'selected' : '' ) ?> value="<?= $v ?>"><?= $v ?></option>
                                	<?php } ?>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Times</div>
                            <div class="col-sm-9">
                                <input type="text" name="adjustment_times" class="form-control ui-search-input" value="<?= dataFilter($row_page['adjustment_times']) ?>" placeholder="Times" />
                            </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" class="form-control ui-search-input" value="<?= dataFilter($row_page['point']) ?>" placeholder="Point" />
                            </div>
                        </div>


                        <div class="form-group">
                            <div class="col-sm-2 control-label">Tier</div>
                            <div class="col-sm-9">
                                <select name="set_tier[]" id="selectuser" class="chosen-select form-control tab_index ui-dropdown-select ui-search-input select2-basic-single" multiple >
									<?php 
									$set_tier = explode( '|', $row_page['set_tier'] ) ;
									foreach( $tier_list as $k => $v ){ ?>
					        			<option value="<?= $v['tier_id'] ?>" <?= ( in_array( $v['tier_id'], $set_tier ) ? 'selected' : '') ?> ><?= $v['title'] ?></option>
					        		<?php } ?>
								</select>
                            </div>
                        </div>

                        <?php if (  $page_mode == 'new'  || ( $page_mode == 'edit' && permissionCheck($row_user, 'app-adjustment-edit') ) ) { ?>
                        <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>   
                        <?php } ?>  
                    </form>
                </div>
            </div>
        </div>
		</div>

		<?php
		
	break ;


	
	// all association list
	case 'all' :
	default :

		// check permission
		if ( !permissionCheck($row_user, 'app-adjustment-view') ){
			header('Location: index.php') ;
			exit ;
		}
		
		$search_title 		= escapeString($_GET['search_title']) ;
		$search_date 		= ( $_GET['search_date']!= '' ? date('Y-m-d', strtotime($_GET['search_date'])) : '' ) ;
		
		// query type
		$search_query 		= '' ;
		
		// search query
		if( $search_title  != ''){
			$search_query  .= " AND b.title LIKE '%".$search_title."%'" ;
		}
		if ( $search_date  != '' ){
		    $search_query  .= " AND created_at like '%".$search_date."%' " ;
		}
		
		// form submit
		if ( $_POST['hide'] == '1' && $_POST['hide_status'] == 'action' ){
			switch($_POST['page_action']){
				case 'trash':
					$mysqli_query 	= "UPDATE setting_adjustment SET deleted_at = '".TODAYDATE."' WHERE adjustment_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_date='.$search_date.'&page_mode='.$page_mode ;
		
		// page query
		$mysqli_query 	= "SELECT a.adjustment_id, a.adjustment_site, a.adjustment_type, a.adjustment_mode, a.point, a.created_at, b.title FROM setting_adjustment a
						   LEFT JOIN setting_adjustment_translation b ON ( a.adjustment_id = b.adjustment_id )
						   WHERE a.deleted_at IS NULL AND b.lang = 'en' " . $search_query ;
		$mysqli_page 	= $mysqli->query( $mysqli_query." ORDER BY a.adjustment_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>Adjustment <small><?= $lang['list']?></small></h1>
					</div>
					<div class="pull-right col">
						<?php if ( permissionCheck($row_user, 'app-adjustment-new') ){ ?>
							<a href="app-adjustment.php?page_mode=new" class="btn" style="color:white;background-color: #5e5bd0;" target="_blank"><?= $lang['add_new']?></a>
						<?php } ?>
						<?php if ( permissionCheck($row_user, 'app-adjustment-group-view') ){ ?>
							<a href="app-adjustment-group.php?page_mode=all" class="btn" style="color:white;background-color: #5e5bd0;" target="_blank">Group</a>
						<?php } ?>
					</div>
				</div>
			</div>

		    <div class="panel panel-default" id="basic-table-title">
		        <div class="panel-heading"></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"><?= $lang['date'] ?></label>
		                    <div class="col-sm-9">
		                        <input class="form-control" name="search_date" type="date" value="<?= $search_date ?>" placeholder="Date Resigned">
		                    </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">
                
                <?php if ( permissionCheck($row_user, 'app-adjustment-trash') ){ ?>
                <div class="panel panel-default">
                    <div class="panel-body">
                        <select name="page_action" class="form-control selectpicker">
                            <option value=""><?= $lang['select']?></option>
                            <option value="trash"><?= $lang['move_to_trash']?></option>
                        </select>
                        <input type="hidden" name="hide" value="1" />
                        <input type="hidden" name="hide_status" value="action" />
                        <input type="submit" class="btn" style="color:white; background-color:#5e5bd0; width:100px;" value="<?= $lang['submit']?>" />
                    </div>
                </div>
                <?php } ?>

                <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>Action</th>
									<th>Site</th>
									<th>Title</th>
									<th>Type</th>
									<th>Mode</th>
									<th>Point</th>
									<th>Created Date</th>
                                    <th width="50"><?= $lang['trash']?></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, 'adjustment-update') ){
													echo '
										 			<a title="Edit Current adjustment" href="app-adjustment.php?page_mode=edit&adjustment_id='.$row_page['adjustment_id'].'"><i class="fa fa-edit"></i></a>' ;
												}else{
													echo '-' ;
												}
											echo '
										 	</td>
											<td class="text-center">'.ucwords($row_page['adjustment_site']).'</td>
											<td>'.dataFilter($row_page['title']).'</td>
											<td class="text-center">'.ucwords($row_page['adjustment_type']).'</td>
											<td class="text-center">'.ucwords($row_page['adjustment_mode']).'</td>
											<td class="text-center">'.dataFilter($row_page['point']).'</td>
											<td class="text-center">'.resetDateFormat($row_page['created_at']).'</td>
											<td>
												<div class="checkbox multiple_trash">
													<input type="checkbox" name="multiple_trash['.$row_page['adjustment_id'].']" class="trash_button" value="1">
													<label for="checkbox1"></label>
												</div>
											</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' ;
?>