<?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']) ;
$group_id	     	= escapeString($_GET['group_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 adjustment group
	case 'new' :
	case 'edit' :

		// check query exsits
		$submit_type = 'new' ;
		$mysqli_page = $mysqli->query("SELECT * FROM setting_adjustment_group
									   WHERE group_id = '".$group_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 ( $group_id  == '' ){
				$mysqli->query( "INSERT INTO setting_adjustment_group 
							    ( created_at ) VALUES 
							    ( '".TODAYDATE."' )" ) ;
				$group_id   = $mysqli->insert_id ;	
			}

			// update database
			$mysqli->query( "UPDATE setting_adjustment_group SET
							title          = '".escapeString($_POST['title'])."'
							WHERE group_id = '".$group_id."'" ) ;

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

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

		if ( ( $page_mode == 'new' && !permissionCheck($row_user, 'app-adjustment-group-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 Group <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-group.php?page_mode=edit&group_id=<?= $group_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="title" class="form-control ui-search-input" value="<?= dataFilter($row_page['title']) ?>" placeholder="Title" />
                            </div>
                        </div>
                        <?php if (  $page_mode == 'new'  || ( $page_mode == 'edit' && permissionCheck($row_user, 'app-adjustment-group-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-group-view') ){
			header('Location: index.php') ;
			exit ;
		}
		
		// query type
		$search_query = '' ;
		
		// search query
		if ($search != ''){
			$search_query .= " AND ( title LIKE '%".$search."%' )" ;
		}
		
		// form submit
		if ( $_POST['hide'] == '1' && $_POST['hide_status'] == 'action' ){
			switch($_POST['page_action']){
				case 'trash':
					$mysqli_query = "UPDATE setting_adjustment_group SET
									 deleted_at        = '".TODAYDATE."'
									 WHERE group_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 ;
		
		// page query
		$mysqli_query = "SELECT a.* FROM setting_adjustment_group a
						 WHERE a.deleted_at IS NULL " . $search_query ;
		$mysqli_page = $mysqli->query( $mysqli_query." ORDER BY a.group_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 Group <small><?= $lang['list']?></small></h1>
					</div>
                	<div class="pull-right col">
				<?php if ( permissionCheck($row_user, 'app-adjustment-group-new') ){ ?>
					<a href="app-adjustment-group.php?page_mode=new" class="btn" style="color:white;background-color: #5e5bd0;" target="_blank"><?= $lang['add_new']?></a>
				<?php } ?>
					</div>
				</div>
			</div>

            <form method="post">
                
                <?php if ( permissionCheck($row_user, 'app-adjustment-group-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 btn-purple" 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>Title</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-group.php?page_mode=edit&group_id='.$row_page['group_id'].'"><i class="fa fa-edit"></i></a>' ;
												}else{
													echo '-' ;
												}
											echo '
										 	</td>
											<td>'.dataFilter($row_page['title']).'</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['group_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>
									</tr>' ;
								}
								?>
							</tbody>
						</table>
						<?= $page_pagination['page_pagination'] ?>
                    </div>
                </div>
			</form>
		</div>
		</div>
		<?php
	break ;

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