<?php

if (session_status() === PHP_SESSION_NONE) session_start();
if (!isset($_SESSION['reid'])) header('location:index.php');

require_once("../dashboard/conn.php");

require_once '../dashboard/dbconfig4.php';

if(isset($_SESSION['reid'])){

	$user_qury=mysqli_query($conn,"SELECT * FROM ormsregister WHERE reid='$_SESSION[reid]'");

	$user_resalt=mysqli_fetch_array($user_qury);

	$customer = $user_resalt['reid'];
	
	$addbycustomer = $user_resalt['userid'];
	
	$userbalance = number_format((float)$user_resalt['balance'],2);
	
	$sellertype = $user_resalt['sellertype'];
	
	$sellerpackage = $user_resalt['sellerpackage'];
	
	if($user_resalt['image']==""){

		$image_path="../dashboard/images/empty.jpg";

	}

	else{

		$image_path="../dashboard/images/sellers/".$user_resalt['image'];

	}

}
 

$msg5 = '';	

$errMSG = array();

if(isset($_POST['btn_add_pkgs'])) {
	$name = $_POST['name'];
	$userid = $_SESSION['reid'];
	$maxc = $_GET['id'];
	
		for ($i=1; $i <= $_POST['count']; $i++) {
			
		$sid = $i;
		$scomission = $_POST['scomission'.$i];
		$permission = $_POST['permission'.$i];
		
		if(empty($name)){		
		    array_push($errMSG, "Please Enter Package Name.");
			
		}else{
		
		// if no error occured, continue ....
		if(empty($errMSG))
		{
			
			//UPDATE mtareq_subject SET status='$pay', couponcode='$_POST[couponcode]', discount='$reqdis' WHERE sub_req_sub_id='$select_payment[1]' and sub_req_reg_no='$_POST[contact]'
				
			$sql = "UPDATE pkgs SET name='$name', scomission='$scomission', permission='$permission' WHERE pkgcount='$maxc' and sid='$sid'";

			mysqli_query($conn, $sql);
			
			echo mysqli_error($conn);
			/*$stmt = $DB_con->prepare('INSERT INTO pkgs(name,sid,scomission,permission,status,userid) VALUES(:name,:sid,:scomission,:permission,"1",:userid)');
			$stmt->bindParam(':name',$name);						
			$stmt->bindParam(':sid',$sid);						
			$stmt->bindParam(':scomission',$scomission);
			$stmt->bindParam(':permission',$permission);
			$stmt->bindParam(':userid',$userid);

			if($stmt->execute())

			{
				$successMSG = "Packages $name with ($scomission) and ($permission) Successfully Added.";
				array_push($successMSG, "Packages $name with ($scomission) and ($permission) Successfully Added.");

			}

			else

			{
				array_push($errMSG, "error while inserting feild $sid");
			}*/
		
		}
	}
}//for loop ending

header("refresh:2;packages.php?msg=updated"); // redirects image view page after 5 seconds.
}

// $q=mysqli_query($conn,"SELECT * FROM ormsservices where category='".$list_resalt['cid']."' ORDER BY sid DESC");
// $data=mysqli_fetch_array($q);
// $countServices = count($data); 
// $i=0;
// $sid = $i."service";
// echo '<pre>'; print_r($sid); echo '</pre>';

//if(isset($_GET['getPkgs'])){

	//$list_resalt2=mysqli_query($conn,"SELECT * FROM `pkgs` WHERE `name` ='".$_GET['getPkgs']."' ");

	//while($list_resalt2=mysqli_fetch_array($list_resalt2)){

    //$list_resalt2['sid'] = 2;

	//}

	// for ($i=1; $i <= $_POST['countServices']; $i++) { 

	// 	// echo $i;
	// 	$sid = $i."service";
	// 	$user_qury=mysqli_query($conn,"INSERT INTO `pkgs` (`name`, `sid`, `scomission`, `add_date`, `status`, `userid`) VALUES 
	// 		('".$_POST['name']."', '".$sid."', '".$_POST[$sid]."', '', '1', '4');");
	// }

//}

//if(isset($_POST['btn_add_pkgs'])){

	//for ($i=1; $i <= $_POST['countServices']; $i++) { 
	
		//$sid = $i;
		//$name = $_POST['name'];
		//$comission = $_POST['comission'];
		//$permission = $_POST['permission'];
		//$userid = $_SESSION['admin_user_id'];
		
		//$user_qury=mysqli_query($conn,"INSERT INTO `pkgs` (`name`, `sid`, `scomission`, `permission`, `status`, `userid`) VALUES 
		//('".$name."', '".$sid."', '".$comission."', '".$permission."', '1', '".$userid."');");
	//}

//}

//$countServices= 2;

?>

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Add Packages | Recharge and Billing System | ORMS | Dashbaord</title>
	
	<?php
	require_once 'headercss.php';
	?>

</head>

<body>

        <!--**********************************
        Main wrapper start
    ***********************************-->
    <div id="main-wrapper">
	
<?php
require_once 'header.php';
?>

        <!--**********************************
            Sidebar start
        ***********************************-->
        <div class="deznav">
            <div class="deznav-scroll">
<?php

require_once 'sidebarmenu.php';

?>
            </div>
        </div>
        <!--**********************************
            Sidebar end
        ***********************************-->
		
        <!--**********************************
            Content body start
        ***********************************-->
        <div class="content-body">
            <!-- row -->
            <div class="container-fluid">
				<div class="row">
				<div class="col-xl-12 col-xxl-12 col-sm-12">
				<div class="alert alert-danger bg-danger text-white" role="alert">
				<strong>Alert!</strong> ඔබගේ ගිනුම තහවුරු කිරීම අසම්පුර්නයි.<a href="identity_verify.php" class="btn btn-success float-right" style="padding: 0px 3px;">සම්පුර්න කරන්න</a>
				</div>
				</div>
				</div>    
                <div class="row page-titles mx-0">
                    <div class="col-sm-6 p-md-0">
                        <div class="welcome-text">
                            <h4>Edit Packages</h4>
                        </div>
                    </div>
                    <div class="col-sm-6 p-md-0 justify-content-sm-end mt-2 mt-sm-0 d-flex">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Packages</a></li>
                            <li class="breadcrumb-item active"><a href="javascript:void(0);">Edit Packages</a></li>
                        </ol>
                    </div>
                </div>
				
				<div class="row">
					<div class="col-lg-12">
						<div class="card">
      <h3 class="text-center mt-3">Edit Packages</h3>
      <hr>
      <div class="card-body pl-4 pr-4 pb-4">
        <form action="" method="post">
          <div class="form-group form-row">
			  <?php $lqury1=mysqli_query($conn,"SELECT * FROM pkgs WHERE pkgcount='$_GET[id]' and userid='".$_SESSION['reid']."'");
					$lresalt1=mysqli_fetch_array($lqury1); ?>
            <label class="col-md-3" for="name">Package Name:</label>
            <input type="text" class="form-control col-md-9" id="name" name="name" placeholder="Enter Package Name" required="" value="<?php echo $lresalt1['name'];?>">
          </div>
          <hr>
          <h4 class="text-center mb-3">Providers List</h4>
<?php
			
$count = 0;
$list_qury=mysqli_query($conn,"SELECT * FROM ormscategory ORDER BY cid ASC");

while($list_resalt=mysqli_fetch_array($list_qury)){

// $count++;

?>
		  <h4 class="text-center mb-3">------------<?php echo $list_resalt['catename']; ?>------------</h4>
          <div class="form-row">
<?php

// $count2=0;
$list_qury2=mysqli_query($conn,"SELECT * FROM ormsservices where category='".$list_resalt['cid']."' ORDER BY sid ASC");
while($list_resalt2=mysqli_fetch_array($list_qury2)){
$count++;

	$lqury=mysqli_query($conn,"SELECT * FROM pkgs WHERE pkgcount='$_GET[id]' and sid='$count'");
	$lresalt=mysqli_fetch_array($lqury);
	
	$lqury2=mysqli_query($conn,"SELECT * FROM pkgs WHERE pkgcount='".$sellerpackage."' and sid='$count'");
	$lresalt2=mysqli_fetch_array($lqury2);
?>
                <div class="form-group col-md-6">
				  <div class="row">
				  <div class="col-md-3">
				  <select class="form-control" name="permission<?=$count?>">
				  	<option value="1" <?php if($lresalt['permission'] == 1){ echo 'selected';} ?>>Enabled</option>
				  	<option value="2" <?php if($lresalt['permission'] == 2){ echo 'selected';} ?>>Disabled</option>
				  </select>
				  </div>
				  <div class="col-md-9">
				  <label for="lbl">  <?php echo $list_resalt2['sname']; ?>-<?php echo $list_resalt2['scode']; ?> (<?php echo $lresalt2['scomission']; ?>%)</label>
				  </div>
                  </div>
				  <input type="hidden" class="form-control" name="sid<?=$count?>" value="<?php echo $list_resalt2['sid']; ?>">
				  <input type="hidden" class="form-control" name="sid<?=$count?>" value="<?php echo $list_resalt2['scode']; ?>">
                  <input type="text" class="form-control" name="scomission<?=$count?>" placeholder="Commision Percentage" value="<?php echo $lresalt['scomission']; ?>">
                </div>
<?php 

}
?>
                        </div>
<?php 
} //echo $count;
?>
	<input type="hidden" class="form-control" name="count" value="<?php echo $count; ?>">
          <div class="form-row mt-3">
            <div class="col-md-12 text-center">
              <a href="" class="btn btn-secondary col-md-3">CANCEL</a>
              <button type="submit" name="btn_add_pkgs" id="btnsave" class="btn btn-success col-md-5">UPDATE CHANGES</button>
            </div>
          </div>
        </form>
      </div>
    </div>
					</div>
				</div>
				
            </div>
        </div>
        <!--**********************************
            Content body end
        ***********************************-->

		<?php
		require_once 'footer.php';
		?>

		<!--**********************************
           Support ticket button start
        ***********************************-->

        <!--**********************************
           Support ticket button end
        ***********************************-->

    </div>
    <!--**********************************
        Main wrapper end
    ***********************************-->

    <?php
	require_once 'footerjs.php';
	?>
    
</body>
</html>