<?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 	= 'hr' ;
$active_sub_menu 	= 'hr-payment-slip' ;
$active_menu 		= 'hr-payment-slip' ;

// check permission
if ( !permissionCheck($row_user, 'payment-slip') ){
	header('Location: index.php') ;
	exit ;
}

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

		// check query exsits
		$submit_type = 'new' ;
		$mysqli_page = $mysqli->query("SELECT * FROM staff_payment_slip
									   WHERE payment_slip_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' ;
		}

		// 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 ( $page == '' ){
				$mysqli->query("INSERT INTO staff_payment_slip (created_at) VALUES ('".TODAYDATE."')") ;
				$page = $mysqli->insert_id ;	
			}

			// resize image
	        // set image in variable
			$image 		  = $_FILES["image"]["name"] ;
			$image_query  = '' ;
			$remove_photo = $_POST['remove_photo'] ;
			if ($remove_photo == 1){
				$image = '' ;	
				$image_query = "payment_file_type = '',
								payment_file      = ''," ;
			}

			if ( $image != '' ){
				$get_image = pathinfo($image) ;
				if ( $get_image['extension'] == 'pdf' ){
					$file_name = $page.'-'.time().'.pdf' ;
					copy($_FILES["image"]["tmp_name"], 'uploads/PaymentSlip/'.$file_name) ;
					$image_query = "payment_file_type = 'pdf',
									payment_file      = '".$file_name."'," ;
				}else{
					$create_image = reCreateImage('PaymentSlip', $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	
						}
						$get_image = pathinfo($create_image['image']) ;
						$image_query = "payment_file_type = '".$get_image['extension']."',
										payment_file      = '".$create_image['image']."'," ;
					}
				}
			}
			
			// update database
			$mysqli->query("UPDATE staff_payment_slip SET
							".$image_query."
							staff_id 				= '".escapeString($_POST['staff_id'])."',
							payment_subject 		= '".$page_title."',
							updated_at 				= '".TODAYDATE."'
							WHERE payment_slip_id 	= '".$page."'") ;

			pushToUserCron( 'staff_payment_slip', $page, $_POST['staff_id'], 'Payment Slip', 'Your payment slip has been submitted.' ) ;
		
			// add system log
			$array_remark = array('old' => array('title' => $row_page['payment_subject']),
					  			  'new' => array('title' => $page_title)) ;
			// refresh page
			header("Location:hr-payment-slip.php?page_mode=edit&page=".$page."&success=1") ;
			$_SESSION['system_result'] = 'success-updated' ;
			exit ;
		}

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

		// start header here
		include 'requires/page_header.php';
		include 'requires/page_top.php';

		// get all requires 
		// get all staff
		$staff_list   = [] ;
		$mysqli_staff = $mysqli->query("SELECT staff_id, staff_name, staff_idno FROM staff
										WHERE deleted_at IS NULL ".$user_branch_permission_sql) ;
		if ( $mysqli_staff->num_rows > 0 ){
			while ( $row_staff = $mysqli_staff->fetch_assoc() ){
				$staff_list[$row_staff['staff_id']] = dataFilter($row_staff['staff_name']) . ' ( ' . dataFilter($row_staff['staff_idno']) . ' )' ;
			}
		}

		?>

		<style>

		</style>

		<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><?= $lang['payment-slip']?> <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 your payment slip 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="hr-payment-slip.php?page_mode=edit&page=<?= $page ?>&type=edit" novalidate="novalidate">

						<div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['Staff']?></div>
                            <div class="col-sm-9">
                            	<select name="staff_id" id="selectuser" class="chosen-select form-control tab_index ui-dropdown-select ui-search-input select2-basic-single" required>
                            		<option value=""><?= $lang['select']?></option>
									<?php
									if ( count($staff_list) > 0 ){
										foreach ( $staff_list as $k => $v ){
											echo '<option value="'.$k.'" '.( $k == $row_page['staff_id'] ? 'selected' : '').'>'.$v.'</option>' ;
										}
									}
									?>
								</select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['Subject']?></div>
                            <div class="col-sm-9">
                                <input type="text" name="title" class="form-control ui-search-input" value="<?= dataFilter($row_page['payment_subject']) ?>" placeholder="<?= $lang['Subject']?>" >
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['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['payment_file']) ?>" />
                                        <input type="hidden" name="remove_photo" value="">
                                        <input type="file" name="image" class="file_button control-label" />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php if ($row_page['payment_file'] != ''){ ?>
	                        <div class="form-group form-group-pdf">
	                            <div class="col-sm-2 control-label"><?= $lang['preview']?></div>
	                            <div class="col-sm-9">
	                            	<label class="remove_photo"><input type="checkbox" name="remove_photo" class="ui-checkbox tick" value="1">&nbsp;<?= $lang['Remove']?> <?= $lang['File']?></label>
	                            	<a href="<?= PATH.'uploads/PaymentSlip/'.dataFilter($row_page['payment_file']) ?>" target="_blank"><?= $lang['Download']?></a>
		                        </div>
	                        </div>
                        <?php }else{ ?>
                            <input type="hidden" name="remove_photo" value="">
                        <?php } ?>

                        <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>   
                    </form>
                </div>
            </div>
        </div>
		</div>
		<?php
		
	break ;
	
	// all hostel list
	case 'all' :
	default :
		
		// query type
		$search_query = '' ;
		
		// search query
		if ($search != ''){
			$search_query .= " AND (payment_subject LIKE '%".$search."%')" ;
		}
		
		// form submit
		if ($_POST['hide'] == '1' && $_POST['hide_status'] == 'action'){
			// trash item
			switch($_POST['page_action']){
				case 'trash':
					$mysqli_query = "UPDATE " . staff_payment_slip . " SET
									 deleted_at = '".TODAYDATE."'
									 WHERE payment_slip_id = " ;
					$trash_page = trashPage('payment_slip', $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 staff_payment_slip a
						 LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
						 WHERE a.deleted_at IS NULL " . $search_query.$user_branch_permission_sql_b ;
		$mysqli_page = $mysqli->query($mysqli_query." ORDER BY a.payment_slip_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><?= $lang['payment-slip']?> <small><?= $lang['list']?></small></h1>
					</div>
					<div class="pull-right col">
				<?php if ( permissionCheck($row_user, 'payment-slip-new') ){ ?>
					<a href="hr-payment-slip.php?page_mode=new" class="btn" style="color:white;margin:5px;background-color: #5e5bd0;margin-top: 5px;" target="_blank"><?= $lang['add_new']?></a>
				<?php } ?>
					</div>
				</div>
			</div>

            <form method="post">
                
                <?php if ( permissionCheck($row_user, 'payment-slip-update') ){ ?>
                <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'] ?>" value="<?= $lang['submit']?>" />
                    </div>
                </div>
                <?php } ?>

                <div class="panel panel-default">
                    <div class="panel-heading">listing</div>
                    <div class="panel-body">
						<table cellpadding="0" cellspacing="0" border="0" class="listing-table responsive table table-striped table-bordered" id="basic-datatable">
							<thead>
								<tr>
									<th><?= $lang['Action']?></th>
									<th><?= $lang['Staff']?></th>
									<th><?= $lang['title']?></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_array(MYSQLI_ASSOC)){

										// default variable
										$id = $row_page['payment_slip_id'] ;
		
										echo '
										 <tr class="odd gradeX">
										 	<td class="align_center">' ;
												if ( permissionCheck($row_user, 'payment-slip-update') ){
													echo '
										 			<a title="'.$lang['Edit Current'].$lang['payment-slip'].'" href="hr-payment-slip.php?page_mode=edit&page='.$id.'"><i class="fa fa-edit"></i></a>' ;
												}else{
													echo '-' ;
												}
											echo '
										 	</td>
											<td>'.dataFilter($row_page['staff_name']).' ( '.dataFilter($row_page['staff_idno']).' )</td>
											<td>'.dataFilter($row_page['payment_subject']).'</td>
											<td class="align_center">'.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>
										<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' ;
?>