<?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']) ;

// mode type | all list | new | edit
switch($page_mode){
	
	// new area
	case 'new' :
		
		// active menu bar
		$active_main_menu = 'setting' ;
		$active_sub_menu = 'setting-user' ;
		$active_menu = 'area-new' ;
		
		// start header here
		include 'requires/page_header.php';
		include 'requires/page_top.php';
		?>
		<div class="warper container-fluid">   	
			<div class="page-header"><h1>Area <small>new</small></h1></div>
            <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="page-area.php?page_mode=edit&page=<?= $last_id ?>&type=edit" novalidate="novalidate">
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Area</div>
                            <div class="col-sm-9">
                                <input type="text" name="title" class="form-control ui-search-input" placeholder="Area" >
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Photo</div>
                            <div class="col-sm-9">
                                <div class="file_upload">
                                    <div class="file_form">
                                        <input type="hidden" name="hide_image" value="" />
                                        <input type="hidden" name="remove_photo" value="">
                                        <input type="file" name="image" class="file_button control-label" />
                                    </div>
                                </div>
                            </div>
                        </div> 
                        <div class="form-group">
                            <div class="col-sm-2 control-label"></div>
                            <div class="col-sm-9">
                                <button type="submit" class="btn btn-purple" style="float:right">Submit</button>
                                <input type="hidden" name="hide_status" />
                                <input type="hidden" name="hide" value="1">
                                <input type="hidden" name="page_status" value="new">
                            </div>
                        </div>   
                    </form>
                </div>
            </div>
		</div>
		<?php
		
	break ;
	// edit area
	case 'edit' :

		// active menu bar
		$active_main_menu = 'setting' ;
		$active_sub_menu = 'setting-user' ;
		$active_menu = 'area' ;
		
		if ($_POST['page_status'] == 'new'){
			$mysqli->query("INSERT INTO system_post (post_type, post_categories, post_date, post_modified) VALUES ('area', 'area', '".TODAYDATE."', '".TODAYDATE."')") ;
			$page = $mysqli->insert_id ;	
		}
		
		// check query exsits
		$mysqli_page = $mysqli->query("SELECT * FROM system_post
										WHERE post_id = '".$page."' AND post_type = 'area' LIMIT 1");
		if ($mysqli_page->num_rows > 0){
			// update database
			if (isset($type) && $type == 'edit' && $_POST['hide'] == 1){
				// keep value in variable
				$page_title = escapeString($_POST['title']) ;
				$page_title = ($page_title != '' ? $page_title : 'No Title') ;
				// check if name already exists.
				$check_status = checkTitle($page_title, $page) ;
				$title = $check_status['title'] ;
				$status = $check_status['status'] ;
				// set image in variable
				$image = $_FILES["image"]["name"] ;
				// remove photo
				$remove_photo = $_POST['remove_photo'] ;
				if ($remove_photo == 1){
					$image = '' ;	
					$image_query = "post_file = ''," ;
				}
				// update database
				$mysqli->query("UPDATE system_post SET
								post_title = '".$page_title."',
								post_status = '".$status."',
								post_link = '".$title."',
								".$image_query."
								post_modified = '".TODAYDATE."',
								post_trash = '0'
								WHERE post_id = '".$page."'") ;
				// resize image
				$create_image = reCreateImage('Branch', $page, $page, '', $image, $_FILES["image"]["type"], $_FILES['image']['tmp_name']) ;
				// Image uploads when exists
				if ($create_image['result'] && is_array($create_image['crop']) && count($create_image['result']) > 0){
					$resizeObj = new resize($create_image['original']) ; // Initialise load image
					foreach($create_image['crop'] as $value){
						// Resize image (options: exact, portrait, landscape, auto, crop)
						$resizeObj -> resizeImage($value['width'], $value['height'], $value['type']) ;
						$resizeObj -> saveImage($value['source'], 70) ; // Save image	
					}
					// update database
					$mysqli->query("UPDATE system_post SET
									post_file = '".$create_image['image']."'
									WHERE post_id = '".$page."'");
				}
				// add system log
				$array_remark = array('old' => array('title' => $row_page['post_title']),
						  			  'new' => array('title' => $page_title)) ;
				// refresh page
				header("Location:page-area.php?page_mode=edit&page=".$page."&success=1") ;
				$_SESSION['system_result'] = 'success-updated' ;
				exit ;
			}
			// start header here
			include 'requires/page_header.php';
			include 'requires/page_top.php';
			// keep query value in array
			$row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
			?>
			<div class="warper container-fluid">   	
				<div class="page-header"><h1>Area <small>edit</small></h1></div>
				<?php
                if ($_SESSION['system_result'] != ''){
                    switch($_SESSION['system_result']){
                        case 'success-updated' :
                            echo '
                            <div class="result_success">
                                Thank you, your area 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="page-area.php?page_mode=edit&page=<?= $page ?>&type=edit" novalidate="novalidate">
                            <div class="form-group">
                                <div class="col-sm-2 control-label">Area</div>
                                <div class="col-sm-9">
                                    <input type="text" name="title" class="form-control ui-search-input" value="<?= dataFilter($row_page['post_title']) ?>" placeholder="Area" >
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-2 control-label">Photo</div>
                                <div class="col-sm-9">
                                    <div class="file_upload">
                                        <div class="file_form">
                                            <input type="hidden" name="hide_image" value="<?= dataFilter($row_page['post_file']) ?>" />
                                            <input type="hidden" name="remove_photo" value="">
                                            <input type="file" name="image" class="file_button control-label" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-2 control-label">Preview</div>
                                <div class="col-sm-9">
                                    <?php
                                    if ($row_page['post_file'] != ''){
                                        echo '
                                        <label class="remove_photo"><input type="checkbox" name="remove_photo" class="ui-checkbox tick" value="1">&nbsp;Remove photo</label>
                                        <img src="'.PATH.'uploads/Branch/'.dataFilter($row_page['post_file']).'" width="180" />' ;
                                    }else{
                                        echo '
                                        <input type="hidden" name="remove_photo" value="">
                                        <input type="text" class="form-control ui-search-input" value="No image." disabled />' ;	
                                    }
                                    ?>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-2 control-label"></div>
                                <div class="col-sm-9">
                                    <button type="submit" class="btn btn-purple" style="float:right">Submit</button>
                                    <input type="hidden" name="hide_status" value="<?= $row_page['post_status'] ?>" />
                                    <input type="hidden" name="hide" value="1">
                                    <input type="hidden" name="page_status" value="edit">
                                </div>
                            </div>   
                        </form>
                    </div>
                </div>
			</div>
			<?php
		}else{
			header("Location: page-area.php?page_mode=all") ;
			exit ;	
		}
	break ;
	
	// all area list
	case 'all' :
	default :
		
		// query type
		$search_query = '' ;
		
		// search query
		if ($search != ''){
			$search_query .= " AND (post_title LIKE '%".$search."%')" ;
		}
		
		// active menu bar
		$active_main_menu = 'setting' ;
		$active_sub_menu = 'setting-user' ;
		$active_menu = 'area' ;
		
		// form submit
		if ($_POST['hide'] == '1' && $_POST['hide_status'] == 'action'){
			// trash item
			switch($_POST['page_action']){
				case 'sortable' :
					// sortable first
					$sortable = $_POST['sortable'] ;
					if (arrayCheck($sortable)){
						foreach($sortable as $key => $value){
							$mysqli->query("UPDATE system_post SET
											post_order = '".$value."'
											WHERE post_id = '".$key."'") ;
							
							// add system log
							$old_order = $_POST['hide_sortable'][$key] ;
							if ($old_order != $value){
							}
						}
					}
				break ;
				case 'trash':
					$mysqli_query = "UPDATE " . system_post . " SET
									post_trash = '1'
									WHERE post_id = " ;
					$trash_page = trashPage('post', $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 * FROM system_post
						 WHERE post_title != '' AND post_type = 'area' AND post_trash = '0'".$search_query ;
		$mysqli_page = $mysqli->query($mysqli_query." ORDER BY post_order 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>Area <small>list</small></h1></div>
            <form method="post">
                <!-- trash -->
                <div class="panel panel-default">
                    <div class="panel-body">
                        <select name="page_action" class="form-control selectpicker">
                            <option value="">Select</option>
                            <option value="sortable">Sortable</option>
                            <option value="trash">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="Submit" />
                    </div>
                </div>
                <!-- end trash -->
                <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>Title</th>
									<th>Date</th>
                                    <th width="100">Sortable</th>
                                    <th width="50">Trash</th>
								</tr>
							</thead>
							<tbody>
								<?php
								if ($mysqli_page->num_rows > 0){
									while ($row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC)){
										// default variable
										$id = $row_page['post_id'] ;
										$title = dataFilter($row_page['post_title']) ;
										$sortable = dataFilter($row_page['post_order']) ;
		
										echo '
										 <tr class="odd gradeX">
											<td><a href="page-area.php?page_mode=edit&page='.$row_page['post_id'].'">'.$title.'</a></td>
											<td>'.resetDateFormat($row_page['post_date']).'</td>
											<td>
												<input type="text" name="sortable['.$id.']" value="'.$sortable.'" class="responsive_width text_center" />
												<input type="hidden" name="hide_sortable['.$id.']" value="'.$sortable.'" />
												<input type="hidden" name="hide_title['.$id.']" value="'.$title.'" />
											</td>
											<td>
												<div class="checkbox multiple_trash">
													<input type="checkbox" name="multiple_trash['.$row_page['post_id'].']" class="trash_button" value="1">
													<label for="checkbox1"></label>
												</div>
											</td>
										</tr>';
									}
								}else{
									echo '
									<tr class="odd gradeX">
										<td class="border_none">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>
		<?php
	break ;
}
// footer
include 'requires/page_footer.php' ;
?>