<?php
	include("../includes/dbconfig.php");
	$pagetitle = "New Job Classfication";
	require('../includes/applicationtop.php');
	require('../includes/applicationmenu.php');
    $error = ''; $name = $cat = $amount= $focus= false;
    if(!empty($_POST['userfocus'])){ 
        $focus= $_POST['userfocus']; 
        $results = DB::query("SELECT * FROM repair_jobnames JOIN repair_jobtypes on repair_jobtypes.rptid=repair_jobnames.job_type");
        if(!empty($results)){
            foreach( $results as $user){

            $name =  $user['job_name']; $amount =  $user['job_amount']; $cat =  $user['rptid']; 
            }

        }
        
        



    }
    else 
    if ($_SERVER["REQUEST_METHOD"] == "POST"  ) {
		
        if(!empty($_POST["amount"]) and !empty($_POST["name"]) and !empty($_POST["category"]) ){
            $cat = $_POST["category"];
            $name = $_POST["name"];
            $amount = $_POST["amount"]; 
            DB::startTransaction();
					DB::query("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE");
					DB::update
					('repair_jobnames', 
						array
						(
							'job_type' => $cat, 
							'job_name' => $name, 
							'job_amount' => $amount
                        ),"job_id=%i", $_POST['id']
					);
					
					DB::commit();
                    
                    ?>
						<script>
							location.href="repairname.php";
						</script>
<?php
					
        }else  { $error =  'All fields are required';}


    }
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					<h1><?= $pagetitle  ?></h1>
					<ol class="breadcrumb">
					</ol>
				</section>

				<!-- Main content -->
				<section class="content">
					<!-- Default card -->
					<div class="card card-primary">
					 
						<div class="card-header with-border text-center">
							<h3 class="card-title"><?= $pagetitle  ?></h3>
						</div>
						<div class="card-body">
							<div id="servererrormessage">
							</div>
							<div class="card-body table-responsive no-padding">
								 
								
								<div class="col-md-12" style="margin-top: 25px"></div>

                                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
								 
							<div class="row">
								<div class="col-sm-12">
                                   <?php if(!empty($error)){ ?>
                                   <h6 class='alert alert-warning' align='center'>  <?= $error; ?> </h6>
                                   <?php } ?>

                                   <input type='text' name='id' value='<?=  $focus;?>'/>
                        
								   
									<div class="form-group  ">
									<label for="employee_role">Category</label>
									<select  class="form-control" name="category" id="employee_role" data-placeholder="Choose One">
											 
											<?php
												$employee_roledata = DB::query("SELECT * FROM repair_jobtypes");
												if(!empty($_POST['category']) ){ $cat = $_POST['category']; }
												foreach($employee_roledata as $useemployee_role)
												{
                                                    if($cat == $useemployee_role['rptid']   ){
											?>
											<option  value="<?php echo $useemployee_role['rptid']; ?>"><?php echo $useemployee_role['rpt_name']; ?></option>
											<?php
												}
                                            }

                                                foreach($employee_roledata as $useemployee_role)
												{
											?>
											<option  value="<?php echo $useemployee_role['rptid']; ?>"><?php echo $useemployee_role['rpt_name']; ?></option>
											<?php
												}
											?>
											</select>
										 </div><!-- form-group -->
										 
									<div class="form-group">
										<label for="firstname"> Name</label>
										<input type="text" name="name" id="name" maxlength="40" class="form-control" value="<?php if(!empty(($_POST['name']) )){ echo $_POST['name'];} else echo $name; ?>" >
									</div><!-- form-group -->
									
									<div class="form-group">
										<label for="lastname">Amount charged</label>
										<input type="text" name="amount" id="amount" maxlength="40" class="form-control" value="<?php if(!empty(($_POST['amount']) )){ echo $_POST['amount'];} else echo $amount; ?>" >
									</div><!-- form-group -->
									
									 
                                    <div class="btn-list">
								<div class="text-right">
									<a href="systemusers.php" class="btn btn-danger"><i class="glyphicon glyphicon-remove"></i> Cancel</a>
									
									<button type="submit" name="savedatabtn" id="savedatabtn" class="btn btn-success"><i class="glyphicon glyphicon-floppy-disk"></i> Save</button>
								</div>
							</div>
									
									 
									
																		
								</div>
							
								 
									</div><!-- form-group -->
									</div>
								</div>
							</div>
						</div><!-- /.card-body -->
						
						<div class="card-footer">
							
							
						</div><!-- /.card-footer-->
							</form>
										
							 
							</div>
						</div><!-- /.card-body -->
						<div class="card-footer">
							
						</div><!-- /.card-footer-->
				 
				 
					</div><!-- /.card -->
				</section><!-- /.content -->
			</div><!-- /.content-wrapper -->

			 
		</div><!-- ./wrapper -->
		
	 
		
	 
<?php include ('../includes/general_footer.php')?>
		<script src="../plugins/bert/storepos.js" type="text/javascript"></script>
		<script src="../plugins/bert/category.js" type="text/javascript"></script>
	</body>
</html>