<?php
require_once('../database/header.php');
?>
<div class="main-panel">
<div class="content-wrapper">
<?php if($userdata['usertype']=="mainadmin") {?>	
<?php
if(isset($_POST['action'])){

if($_POST['action']=="Approved"){
$status = "approved";
$data = [
$status,
$_POST['usrid']
];

$sql = $conn->prepare("UPDATE loginusers SET status=? WHERE id=?");
if($sql->execute($data)){
// Fetch user details for SMS
$user_sql = $conn->prepare("SELECT mobile_no, owner_name FROM loginusers WHERE id=?");
$user_sql->execute([$_POST['usrid']]);
$user = $user_sql->fetch(PDO::FETCH_ASSOC);

// Prepare approval SMS
$sms_number = $user['mobile_no'];
$msg = "Dear *".$user['owner_name']."*,%0A%0A"
  ."Your account has been *APPROVED*.%0A%0A"
  ."You can now login and start using our services.%0A%0A"
  ."Regards,%0A"
  ."*Support Team Webupi*";

// Send SMS
require_once('../database/sendsms.php');

echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>已批准！</strong> 用户已成功批准并通过短信通知。
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>';
}else{
echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>错误！</strong> 批准失败！
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>';
}
} 
else if($_POST['action']=="Rejected"){
$status = "rejected";
$data = [
$status,
$_POST['usrid']
];

$sql = $conn->prepare("UPDATE loginusers SET status=? WHERE id=?");
if($sql->execute($data)){
// Fetch user details for SMS
$user_sql = $conn->prepare("SELECT mobile_no, owner_name FROM loginusers WHERE id=?");
$user_sql->execute([$_POST['usrid']]);
$user = $user_sql->fetch(PDO::FETCH_ASSOC);

// Prepare rejection SMS
$sms_number = $user['mobile_no'];
$msg = "Dear *".$user['owner_name']."*,%0A%0A"
  ."We regret to inform you that your account registration has been *REJECTED*.%0A%0A"
  ."For more details, please contact our Support Team Webupi.%0A%0A"
  ."Regards,%0A"
  ."*Support Team Webupi*";

// Send SMS
require_once('../database/sendsms.php');

echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>已拒绝！</strong> 用户已成功拒绝并通过短信通知。
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>';
}else{
echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>错误！</strong> 拒绝失败！
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>';
}
}
}
?>

<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3 bg-primary">
<h6 class="m-0 font-weight-bold text-white">待审核用户</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="order-listing" width="100%" cellspacing="0">
<thead>
<tr>
<th style='display:none;'>SL No.</th>
<th class='text-dark'>用户详情</th>
<th class='text-dark'>用户地址</th>
<th class='text-dark'>用户身份验证</th>
<th class='text-dark'>用户类型 / 日期</th>
<th class='text-dark'>操作</th>
</tr>
</thead>
<tbody>
<?php
$stmt = $conn->prepare("select * from loginusers WHERE status = ? ORDER BY `id` DESC");
$stmt->execute(['pending']);
$sl=1;
while($row=$stmt->fetch()) {

$State = $row['state'];

switch ($State) {
case "2":
$StateName= "Andhra Pradesh";
break;
case "3":
$StateName= "Arunachal Pradesh";
break;
case "4":
$StateName= "Assam";
break;
case "5":
$StateName= "Bihar";
break;
case "33":
$StateName= "Chhattisgarh";
break;
case "10":
$StateName= "Goa";
break;
case "11":
$StateName= "Gujarat";
break;
case "12":
$StateName= "Haryana";
break;
case "13":
$StateName= "Himachal";
break;
case "14":
$StateName= "Jammu";
break;
case "35":
$StateName= "Jharkhand";
break;
case "15":
$StateName= "Karnataka";
break;
case "16":
$StateName= "Kerala";
break;
case "18":
$StateName= "Madhya Pradesh";
break;
case "19":
$StateName= "Maharashtra";
break;
case "20":
$StateName= "Manipur";
break;
case "21":
$StateName= "Meghalaya";
break;
case "22":
$StateName= "Mizoram";
break;
case "24":
$StateName= "Orissa";
break;
case "26":
$StateName= "Punjab";
break;
case "27":
$StateName= "Rajasthan";
break;
case "28":
$StateName= "Sikkim";
break;
case "29":
$StateName= "Tamil Nadu";
break;
case "30":
$StateName= "Tripura";
break;
case "31":
$StateName= "Uttar Pradesh";
break;
case "34":
$StateName= "Uttarakhand";
break;
case "32":
$StateName= "West Bengal";
break;
case "9":
$StateName= "Delhi";
break;
case "6":
$StateName= "Chandigarh";
break;
case "36":
$StateName= "Telangana";
break;
default:
$StateName= "";
break;
}	
echo "<tr>
<td style='display:none;'>".$sl."</td>
<td style='font-size:13px' class='text-dark'>".strtoupper($row['username'])."<br>".strtoupper($row['owner_name'])."<br>".strtoupper($row['mobile_no'])."<br>".strtoupper($row['email_id'])."</td>
<td><i style='font-size:13px' class='text-dark'>".strtoupper($row['address'])."<br>".strtoupper($StateName)."<br>".strtoupper($row['pin_code'])."<i></td>
<td style='font-size:13px' class='text-dark'>".strtoupper($row['uid_no'])."<br>".strtoupper($row['pan_no'])."</td>
<td class='text-dark'>".strtoupper($row['usertype'])."<br>".$row['date_time']."</td>

<td class='text-dark'>
<form action='' method='post'>
<input type='hidden' name='usrid' value='".$row['id']."'>
<input type='submit' name='action' class='btn btn-success btn-sm mb-2' value='Approved' onclick=\"return confirm('您确定批准吗？')\"><br>
<input type='submit' name='action' class='btn btn-danger btn-sm' value='Rejected' onclick=\"return confirm('您确定拒绝吗？')\">
</form>
</td>
</tr>";
$sl++;}							
?>					
</tbody>
</table>
</div>
</div>
</div>
<?php

}else{
?>
<img class="img-fluid" src="../bootstrap/img/cloud.png">
<?php
}
?>
</div>
<!-- /.container-fluid -->
<!-- End of Main Content -->
<?php
require_once('../database/footer.php');
?>