<?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   = 'setting' ;
$active_sub_menu    = 'setting-app' ;
$active_menu 		= 'setting-app-password' ;

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

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

		// check permission
		if ( ( $submit_type == 'new' || $submit_type == 'edit' ) && !permissionCheck($row_user, 'app-password-edit') ){
			header('Location: index.php') ;
			exit ;
		}

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

			if ( $page == '' ){
				$mysqli->query("INSERT INTO app_password ( created_at ) VALUES ( '".TODAYDATE."' )") ;
				$page = $mysqli->insert_id ;	
			}
			
			// update database
			$mysqli->query("UPDATE app_password SET
							content        	= '".escapeString($_POST['password'])."',
							password_type   = '".escapeString($_POST['password_type'])."',
							updated_at      = '".TODAYDATE."'
							WHERE password_id  = '".$page."'") ;

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

				checkLangUpdate( 'app_password_translation', 'password_id', $page, $klang, [ 
					'title' 	=> [ 'type' => 'input', 'value' => $title   ]
				] ) ;
			}
			
			// refresh page
			header("Location:app-password.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';

		?>

		<div class="warper container-fluid">   	
			<div class="page-header"><h1>Password <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-password.php?page_mode=edit&page=<?= $page ?>&type=edit" novalidate="novalidate">
       
                        <?php 
                		echo showTabs( 'app_password_translation', 'password_id', $page, [ 
                			'title'   => [
                				'type'    => 'input', 
                				'title'   => $lang['title']
                			]
                		]) ; 
                    	?>

                        <div class="form-group">
                            <div class="col-sm-2 control-label">Password</div>
                            <div class="col-sm-9">
                            	<span id="ck_pass" style="position:absolute; right: 30px; font-size:20px;"><i class="fa fa-eye" aria-hidden="true" title="Click and Hold to Check Password"></i></span>
                                <input type="password" name="password" id="password" class="form-control ui-search-input" value="<?= dataFilter($row_page['content']) ?>" placeholder="Password" >
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label">Password Type</div>
                            <div class="col-sm-9">
                                <select name="password_type" class="form-control ui-search-input">
                                	<option value="">Select</option>
                                	<option value="nomination" <?= ($row_page['password_type'] == 'nomination'? 'selected' : '' )?> >Nomination</option>
                                </select>
                            </div>
                        </div>
                        <?php if (  $page_mode == 'new'  || ( $page_mode == 'edit' && permissionCheck($row_user, 'app-password-edit') ) ) { ?>
                        <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"><?= $lang['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>
		<script>
			$("#ck_pass").mouseleave(function(){
				if ($("#password").prop("type") == 'text') {
					$("#password").prop("type", "password");
				}
			});
			$("#ck_pass").on('mousedown', function(){
				$("#password").prop("type", "text");
			});

			$("#ck_pass").on('mouseup', function(){
				$("#password").prop("type", "password");
			});
		</script>
		<?php
		
	break ;
	
	// all list
	case 'all' :
	default :

		// check permission
		if ( !permissionCheck($row_user, 'app-password-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 a.created_at like '%".$search_date."%' " ;
		}
		
		// form submit
		if ( $_POST['hide'] == '1' && $_POST['hide_status'] == 'action' ){
			// trash item
			switch($_POST['page_action']){
				case 'trash':
					$mysqli_query = "UPDATE app_password SET deleted_at = '".TODAYDATE."' WHERE password_id = " ;
					$trash_page = trashPage('password', $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.password_id, a.password_type, a.created_at, b.title FROM app_password a
						   LEFT JOIN app_password_translation b ON ( a.password_id = b.password_id )
						   WHERE a.deleted_at IS NULL AND b.lang = 'en' " . $search_query ;
		$mysqli_page 	= $mysqli->query($mysqli_query." ORDER BY a.password_id 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>Password <small><?= $lang['list'] ?></small></h1>
			</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" style="max-width:600px;">
		                <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 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><?= $lang['Action'] ?></th>
									<th><?= $lang['title'] ?></th>
									<th>Type</th>
									<th><?= $lang['date'] ?></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['password_id'] ;
										$title = dataFilter($row_page['title']) ;
		
										echo '
										 <tr class="odd gradeX">
										 	<td class="align_center">
										 		<a title="'.$lang['Edit'].'" href="app-password.php?page_mode=edit&page='.$id.'"><i class="fa fa-edit"></i></a>
										 	</td>
											<td><a href="app-password.php?page_mode=edit&page='.$id.'">'.$title.'</a></td>
											<td>'.ucfirst(dataFilter($row_page['password_type'])).'</td>
											<td>'.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>
									</tr>' ;
								}
								?>
							</tbody>
						</table>
						<?= $page_pagination['page_pagination'] ?>
                    </div>
                </div>
			</form>
		</div>
		<?php
	break ;
}
// footer
include 'requires/page_footer.php' ;
?>