<?php
require_once("../config/config.php");
$docid = base64_decode($_REQUEST[id]);
$target_sql = "SELECT * FROM sf_target_master WHERE id = '".$docid."'";
$target_res = mysqli_query($link1,$target_sql)or die("er 1".mysqli_error($link1));
$target_row = mysqli_fetch_assoc($target_res);

@extract($_POST);
if($_POST['submit']=="Update"){
	mysqli_autocommit($link1, false);
	$flag = true;
	
	// update status in target master table //
	$sql_master = "UPDATE sf_target_master SET status = '".$status."', update_date  = '".$today."', update_by  = '".$_SESSION['userid']."' WHERE target_no = '".$target_row['target_no']."' ";
	
	$res_master =  mysqli_query($link1,$sql_master)or die("ER 1".mysqli_error($link1));
	/// check if query is execute or not//
	if(!$res_master){
		$flag = false;
		$err_msg = "Error 1". mysqli_error($link1) . ".";
	}

	///// check all query are successfully executed
	if ($flag) {
		////// insert in activity table////
		dailyActivity($_SESSION['userid'],$target_row['target_no'],"TARGET","UPDATE",$ip,$link1,"");
		
        mysqli_commit($link1);
        $msg = "Target is successfully updated.";
		///// move to parent page
		header("location:target_list.php?msg=".$msg."&sts=success".$pagenav);
		exit;
    } else {
		mysqli_rollback($link1);
		$msg = "Request could not be processed. Please try again.";
		///// move to parent page
		header("location:target_list.php?msg=".$msg."&sts=fail".$pagenav);
		exit;
	} 
    mysqli_close($link1);
}

?>
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title><?=siteTitle?></title>
 <script src="../js/jquery.min.js"></script>
 <link href="../css/font-awesome.min.css" rel="stylesheet">
 <link href="../css/abc.css" rel="stylesheet">
 <script src="../js/bootstrap.min.js"></script>
 <link href="../css/abc2.css" rel="stylesheet">
 <link rel="stylesheet" href="../css/bootstrap.min.css">
 
</head>
<body>
<div class="container-fluid">
 <div class="row content">
	<?php 
    include("../includes/leftnav2.php");
    ?>
   <div class="col-sm-9">
      <h2 align="center"><i class="fa fa-bullseye"></i> View Target </h2><br/>
   <form  name="frm1" id="frm1" class="form-horizontal" action="" method="post">   
   <div class="panel-group">
    <div class="panel panel-default table-responsive">
        <div class="panel-heading heading1">Target Information</div>
        <div class="panel-body">
         <table class="table table-bordered" width="100%">
            <tbody>
              <tr>
                <td width="20%"><label class="control-label">Target No.</label></td>
                <td width="30%"><?php echo $target_row['target_no']; ?></td>
                <td width="20%"><label class="control-label">Target Type</label></td>
                <td width="30%"><?php echo $target_row['target_type']; ?></td>
              </tr>
              <tr>
                <td><label class="control-label">From Date</label></td>
                <td><?php echo dt_format($target_row['from_date']); ?></td>
                <td><label class="control-label">To Date</label></td>
                <td><?php echo dt_format($target_row['to_date']); ?></td>
              </tr>
               <tr>
                <td><label class="control-label">Employee Name</label></td>
                <td><?php echo getAdminDetails($target_row['emp_id'],"name",$link1)." | ".$target_row['emp_id'];?></td>
                <td><label class="control-label">Period Type</label></td>
                <td><?php echo $target_row['period_type']; ?></td>
              </tr>
              <tr>
                <td><label class="control-label">Entry Type</label></td>
                <td><?php echo $target_row['entry_screen'];?></td>
                <td><label class="control-label">Status</label></td>
                <td><?php echo $target_row['status']; ?></td>
              </tr>
			  <tr>
                <td><label class="control-label">Create By</label></td>
                <td><?php echo getAdminDetails($target_row['create_by'],"name",$link1)." | ".$target_row['create_by'];?></td>
                <td><label class="control-label">Create Date</label></td>
                <td><?php echo dt_format($target_row['create_date']); ?></td>
              </tr>
			  <tr>
			  	<td><label class="control-label">Remark</label></td>
				<td colspan="3"><?=$target_row['remark'];?></td>
			  </tr>
			  <?php if($target_row['target_type'] == "Value"){ ?>
			  <tr style="background-color:#FBF7BB;">
			  	<td><label class="control-label">Target Value</label></td>
				<td colspan="3"><?=$target_row['target_val'];?></td>
			  </tr>
			  <?php } ?>
            </tbody>
          </table>
      </div><!--close panel body-->
    </div><!--close panel-->
    <br><br>
	<?php if($target_row['target_type'] != "Value"){ ?>
    <div class="panel panel-default table-responsive">
      <div class="panel-heading heading1">Items Information</div>
      <div class="panel-body">
       <table class="table table-bordered" width="100%">
            <thead>
              <tr class="<?=$tableheadcolor?>" >
                <th style="text-align:center" width="5%">#</th>
				<th style="text-align:center" width="15%">Target No.</th>
                <th style="text-align:center" width="30%">Product</th>
                <th style="text-align:center" width="15%">Qty</th>
              </tr>
            </thead>
            <tbody>
            <?php
			$i=1;
			$targetdata_sql = "SELECT * FROM sf_target_data WHERE target_no = '".$target_row['target_no']."'";
			$targetdata_res = mysqli_query($link1,$targetdata_sql);
			while($targetdata_row = mysqli_fetch_assoc($targetdata_res)){
				$proddet = explode("~",getProductDetails($targetdata_row['prod_code'],"productname,productcolor,productcode",$link1));
			?>
              <tr>
                <td style="text-align:center"><?=$i?></td>
				<td style="text-align:center"><?=$targetdata_row['target_no']?></td>
                <td><?=$proddet[0]." | ".$proddet[1]." | ".$proddet[2]?></td>
                <td style="text-align:center"><?=round($targetdata_row['qty'])?></td>
              </tr>
            <?php
			$i++;
			}
			?>
            </tbody>
          </table>
      </div><!--close panel body-->
    </div><!--close panel-->
	<?php } ?>
	<br><br>
	<div class="panel panel-default table-responsive">
        <div class="panel-heading heading1">Action</div>
        <div class="panel-body">
         <table class="table table-bordered" width="100%">
            <tbody>
              <tr>
			  	<td width="20%"><label class="control-label">Change Status : </label></td>
                <td width="80%">
					<select class="form-control" name="status" id="status">
						<option value="Active" <?php if($target_row['status'] == "Active"){ echo "selected"; } ?> >Active</option>
						<option value="Deactive" <?php if($target_row['status'] == "Deactive"){ echo "selected"; } ?> >Deactive</option>
					</select>
				</td>
              </tr>
            </tbody>
          </table>
      </div><!--close panel body-->
    </div><!--close panel-->
	</div>
	<br><br>
	<div class="form-group">
	  <div class="col-md-12" style="text-align:center;" > 
	  	  <button class="btn <?=$btncolor?>" type="submit" name="submit" id="submit" value="Update"> Update </button>  
		  <input title="Back" type="button" class="btn  <?=$btncolor?>" value="Back" onClick="window.location.href='target_list.php?<?=$pagenav?>'">
	  </div>  
	</div>
	<br><br>
  </form>
 </div><!--close col-sm-9-->
</div><!--close row content-->
</div><!--close container-fluid-->
<?php
include("../includes/footer.php");
include("../includes/connection_close.php");
?>
</body>
</html>