<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
$_SESSION['cuPages']='prj_sgrouplist.php';   
$cuPage='prj_sgrouplist.php';    
$aid=$_SESSION['uid'];
$uty=$_SESSION['utype'];    
$brid=$_SESSION['abrid'];     
}else{
header('Location:../index.php');
exit;    
}
$mhead='project';
$menuh='Manage Project';
$phead='psglist';
$page='Project Sub-Group Edit';
$dtnow = date("Y-m-d h:i:s", time());
$today = strftime("%Y-%m-%d", time());
?>
<?php 
if(isset($_POST['update_subgroup'])){
    $sgrpid = remove_junk(escape($_POST['sgrpid']));
	$name = ucwords(remove_junk(escape($_POST['name'])));
    $pgid = remove_junk(escape($_POST['pgid']));
    $code = remove_junk(escape($_POST['code']));
    if(strlen($code)<1){$code='NULL';}else{$code="'".$code."'";}
    $description = remove_junk(escape($_POST['description']));
	
	if(isset($_POST['name'])){
	$ducode = mysqli_query($con,"SELECT * FROM tbl_prosubgroup WHERE name = '$name' AND pgid='$pgid' AND id!='$sgrpid'");
	}
	
	if($ducode->num_rows > 0) {
	save_msg('i','Sub-Group name alrady used under selected group! Plz try another');
	echo "<script>window.location='prj_sgrouplist.php'</script>";
	}else{    
    $sql="UPDATE tbl_prosubgroup SET pgid='$pgid',name='$name',code=$code,description='$description' WHERE id='$sgrpid'";    
	$result = mysqli_query($con,$sql) or die(mysqli_error($con));
    $efid=mysqli_affected_rows($con);      
    if($efid>0){
    $act =remove_junk(escape('Sub-Group name: '.$name));    
    write_activity($aid,'SGP','Sub-Group has been updated',$act);    
    save_msg('s','Data Successfully Updated!');
    }else{
    save_msg('w','Data Fail to Update!');    
    }
    echo "<script>window.location='prj_sgrouplist.php'</script>";  
	}  
}
?>
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
?>

<div class="content-wrapper">
<?php include('../layout/page_head.php'); ?>
<!-- Main content -->
<section class="content">
    
<div class="row">
<div class="col-md-8">
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">Create Sub-Group</h3>
</div>
<div class="box-body">
<?php echo display_msg();?> 
<?php 
if(isset($_POST['editsgrp'])){
$ids = $_POST['editsgrp'];
$sql="SELECT * FROM tbl_prosubgroup WHERE id='".$ids."' ORDER BY id ASC LIMIT 1";    
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);
?>    
<form action="prj_sgroupedit.php" onsubmit="return validate()" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<div class="col-md-12 popup_details_div">

<div class="row ">
<div class="col-md-12">
<div class="col-md-3"></div>
<div class="col-md-6">
<div class="row">
<div class="col-md-8">    
<div class="form-group">
<label>Sub-Group Name</label>
<input type="text" name="name" maxlength="45" value="<?php echo $adm['name'];?>" id="sname" class="form-control" placeholder="e. g. LGED" />
<input type="hidden" maxlength="11" value="<?php echo $adm['id'];?>" class="form-control" name="sgrpid" autocomplete="off" readonly> 
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Sub-Group Code</label>
<input type="text" name="code" maxlength="15" value="<?php echo $adm['code'];?>" class="form-control" placeholder="e.g 101020"  />
</div>    
</div>
</div>    
<div class="form-group">
<label>Select Group</label>
<select class="form-control select2" name="pgid" id="pgid">
<option value="">-Select-</option>
<?php									
$querys=mysqli_query($con,"SELECT * FROM tbl_progroup ORDER BY id ASC")or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<option<?php if($rows['id']==$adm['pgid']){echo ' selected';}?> value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php } ?>
</select>    
</div>    
<div class="form-group">
<label>Description</label>
<textarea class="form-control" maxlength="250" rows="6" name="description" placeholder="Description"><?php echo $adm['description'];?></textarea>
</div>   
</div>    
<div class="col-md-3"></div>    
</div>    
</div>    

</div>
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<input type="submit" name="update_subgroup" id="submit" class="btn btn-flat bg-purple btn-sm " value="Update"/> <a href="prj_sgrouplist.php" class="btn btn-flat bg-gray  ">Close</a>
</div> 
</div>     
</form>
<?php } ?>    
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-md-12">
<div class="box box-solid">
<div class="box-header">
<h3 class="box-title">History </h3>
</div>
<!-- /.box-header -->
<div class="box-body" >
<?php if($_SESSION['utype']=='1'){echo read_activity($aid,'SGP','A');}else{echo read_activity($aid,'SGP','U');}?>
</div>
</div>
</div>
</div>
</div>
</div>
    
<?php include('../layout/quick.php');?>    
</section>
<!-- /.main content -->    
</div>
<?php
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
$(document).ready(function () {  
var sname = new LiveValidation('sname');
sname.add(Validate.Presence);
var pgid = new LiveValidation('pgid');
pgid.add(Validate.Presence);    
}); 
</script>    
<!-- /page script -->
</html>    