<?php
include 'connect/cms-config.php' ;
include 'requires/function.php' ;
include 'requires/session.php' ;

// check permission
if ( !permissionCheck($row_user, 'staff-list-update') && !permissionCheck($row_user, 'foreign-only') ){
	echo '<script>alert("Sorry You Don\'t Have The Permission.")</script>';

	header('Location: index.php') ;
	exit ;
}

// keep parameter in value
$staff_id 		= escapeString($_GET['staff_id']) ;
$branch_transaction = escapeString($_GET['branch_transaction']) ;
$hide = escapeString($_GET['hide']) ;
$name = escapeString($_GET['name']) ;
$confirm = escapeString($_GET['confirm']) ;
// include the class
include 'requires/class_resize.php' ;

// get all branch
$branch     = [] ;
$get_branch = $mysqli->query("SELECT * FROM branch
							  WHERE deleted_at IS NULL") ;
if ( $get_branch->num_rows > 0 ){
	while ( $row_branch = $get_branch->fetch_assoc() ){
		$branch[$row_branch['branch_id']] = $row_branch['branch_name'] ;
	}
}

$mysqli_page = $mysqli->query("SELECT * FROM staff
							   WHERE staff_id = '".$staff_id."' LIMIT 1");
if ($mysqli_page->num_rows > 0){
	$row_page    = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;


	if( $confirm == 1 ){
		if ( $row_page['branch_id'] != $branch_transaction ){
			$mysqli->query("UPDATE staff SET
							branch_id 		= '".$branch_transaction."'
							WHERE staff_id 	= '".$staff_id."'") ;
			$mysqli->query("DELETE FROM staff_token WHERE staff_id 	= '".$staff_id."'") ;
		}

		header("Location:hr-staff-transaction.php?staff_id=".$staff_id) ;
		exit ;
	}
}

$active_main_menu = 'hr' ;
$active_sub_menu  = 'hr-staff' ;
$active_menu 	  = 'hr-staff-list' ;
// start header here
include 'requires/page_header.php' ;
include 'requires/page_top.php' ;


if($hide == 1){
echo '<script type="text/javascript"> ';  
echo '  if (confirm("Are you sure you want to open new URL")) {';  
echo '      window.location.replace("hr-staff-transaction.php?name='.$name.'&branch_transaction='.$branch_transaction.'&hide='.$hide.'&staff_id='.$staff_id.'&confirm=1");';  
echo '  }else{';  
echo '  	window.location.replace("hr-staff-transaction.php?staff_id='.$staff_id.'");';  
echo '  }';  
echo '</script>';  
}

?>
<!-- Header Ends -->
<div class="warper container-fluid">   

	<div class="page-header">
		<h1>Change Branch <small><?= $lang['edit']?></small></h1>
	</div>

	<form method="GET" class="form-horizontal">
		<div class="panel panel-default">
            <div class="panel-heading"></div>
            <div class="panel-body">
            	<div class="form-group">
            		<div class="col-sm-2 control-label">Staff Name</div>
			        <div class="col-sm-9">
			            <input class="form-control" name="name" type="text" value="<?= $row_page['staff_name'] ?>" placeholder="Staff Name" readonly/>
			        </div>
			    </div>
			    <div class="form-group">
            		<div class="col-sm-2 control-label">Branch</div>
			        <div class="col-sm-9">
                        <select class="form-control" disabled>
                            <?php
                            foreach ( $branch as $kk => $vv ){
                            	echo '<option value="'.$kk.'" '.( $row_page['branch_id'] == $kk ? 'selected' : '' ).' >'.$vv.'</option>' ;
                            }
                            ?>
                        </select>
			        </div>
			    </div>
			    <div class="form-group">
            		<div class="col-sm-2 control-label">Transaction To</div>
			        <div class="col-sm-9">
                        <select name="branch_transaction" class="form-control">
                            <?php
                            foreach ( $branch as $kk => $vv ){
                            	echo '<option value="'.$kk.'" '.( $row_page['branch_id'] == $kk ? 'selected' : '' ).' >'.$vv.'</option>' ;
                            }
                            ?>
                        </select>
			        </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" value="1" />
                        <input type="hidden" name="staff_id" value="<?= $staff_id ?>" />
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<?php
// footer
include 'requires/page_footer.php' ;
?>