<?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-notification' ;
$active_menu = 'setting-letterhead' ;

switch($page_mode){
	case 'new' :
	case 'edit' :
		$submit_type = 'new' ;
		$mysqli_page = $mysqli->query("SELECT * FROM setting_letterhead
									   WHERE letterhead_id = '".$page."' LIMIT 1");
		if( $mysqli_page->num_rows > 0 ){
			$row_page    = $mysqli_page->fetch_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_desc		 = escapeString($_POST['description']) ;
			$page_title      = ($page_title != '' ? $page_title : 'No Title') ;

			if ( $page == '' ){
				$mysqli->query("INSERT INTO setting_letterhead (created_at) VALUES ('".TODAYDATE."')") ;
				$page = $mysqli->insert_id ;	
			}
			
			// update database
			$mysqli->query("UPDATE setting_letterhead SET
							".$image_query."
							letterhead_type = '".escapeString($_POST['letterhead_type'])."',
							letterhead_title = '".$page_title."',
							letterhead_content = '".$page_desc."',
							updated_at = '".TODAYDATE."'
							WHERE letterhead_id = '".$page."'") ;
			
			// refresh page
			header("Location:setting-letterhead.php?page_mode=edit&page=".$page."&success=1") ;
			$_SESSION['system_result'] = 'success-updated' ;
			exit ;
		}

		if ( $page_mode == 'new' && !permissionCheck($row_user, 'user-letterhead-new') ){
			header('Location: app-association-category.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;">
				<div class="row">
                	<div class="pull-left col">
                		<h1><?= $lang['Letterhead'] ?> <small><?= $lang[$page_mode] ?></small></h1>
                	</div>
                </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>
            <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="setting-letterhead.php?page_mode=edit&page=<?= $page ?>&type=edit" novalidate="novalidate">

                        <div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['title']?></div>
                            <div class="col-sm-9">
                                <input type="text" name="title" autofocus="autofocus" class="form-control ui-search-input" value="<?= dataFilter($row_page['letterhead_title']) ?>" placeholder="<?= $lang['title']?>" >
                            </div>
                        </div>
                       <div class="form-group">
							<div class="col-sm-2 control-label"><?= $lang['type'] ?></div>
							<div class="col-sm-9">
								<select name="letterhead_type" class="form-control ui-search-input">
									<option value="offer" <?= ( $row_page['letterhead_type'] == 'offer' ? 'selected' : '' ) ?> >Offer</option>
									<option value="iea" <?= ( $row_page['letterhead_type'] == 'iea' ? 'selected' : '' ) ?> >IEA</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<div class="col-sm-2 control-label"><?= $lang['Description'] ?></div>
							<div class="col-sm-9">
                            	<textarea name="description" class="form-control ui-search-input ckeditor" required placeholder="<?= $lang['Description'] ?>"><?= dataFilter($row_page['letterhead_content']) ?></textarea>
							</div>
						</div>

                        <hr>
                        <?php if (  $page_mode == 'new'  || ( $page_mode == 'edit' && permissionCheck($row_user, 'user-letterhead-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;"><?= $lang['submit']?></button>
	                                <input type="hidden" name="hide" value="1">
	                                <input type="hidden" name="page_status" value="<?= $submit_type ?>">
	                            </div>
	                        </div>   
						<?php } ?>

                    </form>

                    <div style="font-size:12px;">
						<b>*IEA Form*</b><br />
						- [IEAASSIGNEDBY]<br />
						- [IEAWORKERNAME]<br />
						- [IEAWORKERIC]<br />
						- [IEAPOSITION]<br />
						- [IEAINCHARGEBY]<br />
						- [IEASALARY]<br />
						- [IEAALLOWANCE]<br />
						- [IEACOMISSION]<br />
					</div>

                </div>
            </div>
        </div>
		</div>
	<?php
	break ;
	case 'all' :
	default :

		// check permission
		if ( !permissionCheck($row_user, 'user-letterhead-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'])) : '' ) ;
		
		$search_query = '' ;
		
		if ($search != ''){
			$search_query .= " AND (letterhead_content LIKE '%".$search."%')" ;
		}
		if( $search_title != ''){
			$search_query .= " AND letterhead_content LIKE '%".$search_title."%'" ;
		}
		if ( $search_date != '' ){
		    $search_query .= " AND 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 " . setting_letterhead . " SET
									deleted_at = '".TODAYDATE."'
									WHERE letterhead_id = " ;
					$trash_page = trashPage( 'letterhead', $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 * FROM setting_letterhead
						 WHERE deleted_at IS NULL " . $search_query ;
		$mysqli_page = $mysqli->query($mysqli_query." ORDER BY letterhead_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='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><?= $lang['Letterhead'] ?> <small><?= $lang['list']?></small></h1>
					</div>
                	<div class="pull-right col">
						<?php if ( permissionCheck($row_user, 'user-letterhead-new') ){ ?>
							<a href="setting-letterhead.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['Subject'] ?></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, 'user-letterhead-trash') ){ ?>
	                <!-- 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>
	                <!-- end trash -->
                <?php } ?>
                <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><?= $lang['type'] ?></th>
									<th><?= $lang['Letterhead'] ?></th>
									<th><?= $lang['date'] ?></th>
                                    <th width="50"><?= $lang['trash'] ?></th>
								</tr>
							</thead>
							<tbody>
								<?php
								if( $mysqli_page->num_rows > 0 ){
									while( $row_page = $mysqli_page->fetch_assoc() ){
										$id = $row_page['letterhead_id'] ;
		
										echo '
										 <tr class="odd gradeX">
											<td class="text-center">'.ucwords( $row_page['letterhead_type'] ).'</td>
											<td><a href="setting-letterhead.php?page_mode=edit&page='.$id.'">'.dataFilter( $row_page['letterhead_title'] ).'</a></td>
											<td>'.resetDateFormat( $row_page['created_at'] ).'</td>
											<td>
												<div class="checkbox multiple_trash">
													<input type="checkbox" name="multiple_trash['.$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>
		<?php
	break ;
}
// footer
include 'requires/page_footer.php' ;
?>