<?php include "header.php";

if($_SESSION['usertype'] != "ADMIN"){
    ?>
    <script>
        window.location.href='index.php';
    </script>
    <?php
    die;
}

if (isset($_POST['reg']) && $_POST['reg'] == "ahkweb") {
  
  // Data Sanitize
  $name = mysqli_real_escape_string($conn, $_POST['name']);
  $userid = mysqli_real_escape_string($conn, $_POST['userid']); // Generated by JS
  $phone = mysqli_real_escape_string($conn, $_POST['phone']);
  $state =  mysqli_real_escape_string($conn, $_POST['state']);
  $city =  mysqli_real_escape_string($conn, $_POST['city']);
  $email = mysqli_real_escape_string($conn, $_POST['email']);
  $password = mysqli_real_escape_string($conn, $_POST['password']);
  $walletamount = mysqli_real_escape_string($conn, $_POST['walletamount']);
  $usertype = mysqli_real_escape_string($conn, $_POST['usertype']);
 
  // Date and Time
  $joineddate = date('Y-m-d');
  $joinedtime = date('H:i:s');

  if (!empty($password) && $phone != "ADMIN" && $email != "ADMIN") {
      
      $fpassword = password_hash($password, PASSWORD_DEFAULT);
      
      // Email or Phone Check
      $checksql = mysqli_query($conn, "SELECT * FROM usertable WHERE emailid='$email' OR phone='$phone'");
      
      if (!empty($name) && !empty($phone) && !empty($email)) {
        
        if (mysqli_num_rows($checksql) == 0) {
    
          // Yahan maine saare extra columns ko bhi handle kiya hai (default value 0 ya empty string)
          $sql = "INSERT INTO `usertable` 
          (`phone`, `emailid`, `name`, `psaid`, `cprize`, `password`, `city`, `state`, `userid`, `status`, `profilepic`, `createdby`, `joineddate`, `joinedtime`, `usertype`, `walletamount`, `otp`, `birth_fee`, `death_fee`, `b_fee`, `pdffee`, `pfee`, `fullnamefee`, `e_pdf_fee`, `half_name_fee`, `panwallet`) 
          VALUES 
          ('$phone', '$email', '$name', 'NOT ALOTED', '98', '$fpassword', '$city', '$state', '$userid', 'verified', 'profile/admin.jpg', 'ADMIN', '$joineddate', '$joinedtime', '$usertype', '$walletamount', '', '0', '0', '0', '0', '0', '0', '0', '0', '0')";

          $res = mysqli_query($conn, $sql);
          
          if($res){
            ?>
            <script>
              $(function() {
                Swal.fire('Success', 'User Registered Successfully!', 'success');
              });
              window.setTimeout(function() { window.location.href = "adduser.php"; }, 1500);
            </script>
            <?php
          } else {
            // Agar query fail ho toh error dekhne ke liye ye line kaam aayegi
            $db_error = mysqli_error($conn);
            ?>
            <script>
              $(function(){
                Swal.fire('Error!', 'Database Error: <?php echo addslashes($db_error); ?>', 'error');
              });
            </script>
            <?php
          }
        } else {
          ?>
          <script>
            $(function() { Swal.fire('Oops!', 'Email or Phone already exists!', 'error'); });
            window.setTimeout(function() { window.location.href = "userlist.php"; }, 1500);
          </script>
          <?php
        }
      } else {
        echo "<script>$(function() { Swal.fire('Oops!', 'Please Fill All Required Fields', 'error'); });</script>";
      }
  }
}
?>
  <!-- Content Wrapper. Contains page content -->
  
  <style>
.content-wrapper{
    background:#f4f6f9;
}

.card-default{
    border:none !important;
    border-radius:20px !important;
    overflow:hidden;
    background:#fff;
    box-shadow:0 10px 30px rgba(0,0,0,.08);
}

.card-default:before{
    content:'';
    display:block;
    height:6px;
    background:linear-gradient(90deg,#0d6efd,#6610f2,#20c997);
}

.card-body{
    padding:30px !important;
}

.form-group label{
    font-weight:600;
    color:#344767;
    margin-bottom:8px;
}

.form-control{
    border-radius:12px !important;
    border:1px solid #dce3ea !important;
    height:48px !important;
    box-shadow:none !important;
}

.form-control:focus{
    border-color:#0d6efd !important;
    box-shadow:0 0 0 0.15rem rgba(13,110,253,.15) !important;
}

select.form-control{
    height:48px !important;
}

.card-footer{
    background:#fff !important;
    border-top:1px solid #edf2f7 !important;
    text-align:center;
    padding:25px !important;
}

.btn-primary{
    background:linear-gradient(135deg,#0d6efd,#6610f2) !important;
    border:none !important;
    border-radius:12px !important;
    padding:12px 40px !important;
    font-weight:600 !important;
    box-shadow:0 5px 15px rgba(13,110,253,.25);
}

.btn-primary:hover{
    transform:translateY(-2px);
    transition:.3s;
}

.content-header{
    padding-bottom:15px;
}

.content-header:after{
    content:'Add New User';
    display:block;
    font-size:28px;
    font-weight:700;
    color:#0d6efd;
    margin-left:15px;
}

input[readonly]{
    background:#f8fafc !important;
    font-weight:600;
}

@media(max-width:768px){
    .card-body{
        padding:15px !important;
    }
}
</style>
  

    <!-- Content Header (Page header) -->
<div class="content-header">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-sm-6">
       <!------------- <h1 class="m-0">Add User</h1>---------------->
      </div><!-- /.col -->
    </div><!-- /.row -->
  </div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->

<!-- Main content -->
<section class="content">
	<div class="container-fluid">
		<div class="row">
			<!-- left column -->
			<div class="col-md-12">

				<!-- general form elements -->
				<div class="card card-default">

				  
				  <!-- form start -->
				  <form method="post" action="" autocomplete="off">
				  					    <div class="card-body row">

				      <div class="form-group col-md-3">
				        <label for="name">Name</label>
						<input type="hidden" name="reg" value="ahkweb">
				        <input type="text" class="form-control" maxlength="30" id="name" name="name" placeholder="Fill Name" value="" required>
				      </div>

              <div class="form-group col-md-3">
                                <label for="userid">Username or User ID</label>
                                <input type="text" class="form-control" id="userid" name="userid" placeholder="Username will be auto-generated" value="" readonly required>
                              </div>

				      <div class="form-group col-md-3">
				        <label for="email">Email</label>
				        <input type="email" class="form-control" maxlength="30" id="email" name="email" placeholder="Enter email" value="" required>
				      </div>

				      <div class="form-group col-md-3">
				        <label for="mobile">Mobile</label>
				        <input type="number" class="form-control" id="phone" name="phone" placeholder="Enter mobile" maxlength="10" value="" required>
				      </div>
				      
				        <div class="form-group col-md-3">
				        <label for="state">State</label>
				        <input type="state" class="form-control" maxlength="20" id="state" name="state" placeholder="State" value='State' readonly required>
				      </div>
				      
				        <div class="form-group col-md-3">
				        <label for="city">City</label>
				        <input type="city" class="form-control" maxlength="20" id="city" name="city" placeholder="Enter City" value="City" readonly required>
				      </div>

				      <div class="form-group col-md-3">
                                <label for="password">Password</label>
                                <input type="text" class="form-control" id="password" name="password" placeholder="Password will be auto-generated" value="" readonly required>
                              </div>
              <div class="form-group col-md-3">
				        <label for="walletamount">Balance</label>
				        <input type="number" class="form-control" maxlength="4" id="walletamount" name="walletamount" placeholder="Enter Amount" value="" required>
				      </div>

				      <div class="form-group col-md-3">
				        <label for="usertype">Role</label>
				        <select class="form-control" name="usertype" id="usertype" required>
						    <option value="">Select Role</option>
				        	<option value="OPERATOR">Operator</option>
				        	<option value="DISTRIBUTOR">Supervisor</option>
				        	<option value="SUPERDISTRIBUTOR">Super Distributor</option>
				        	<option value="ADMIN2">Master ADMIN</option>
			-		    	<option value="MOBILE">Mobile Operator</option>
							<option value="CHILD">Child Operator</option>
							<option value="DEMOGRAPHIC">Demographic Operator</option>
							<option value="PAN">PAN Operator</option>
              
				        </select>
				      </div>
              <div class="form-group col-md-3">
				        <label for="username">Created By</label>
				        <input type="text"disabled="disabled" class="form-control" id="username" name="username" placeholder="Enter email" value="<?php echo $udata['userid']?>" required>
				      </div>
				    </div>
				    <!-- /.card-body -->

				    <div class="card-footer">
				      <button type="submit" class="btn btn-primary btn-lg">Submit</button>
				    </div>
				  </form>
				</div>
				<!-- /.card -->
			</div>
    	</div>
	</div><!-- /.container-fluid -->
</section>
<!-- /.content -->


  </div>

</div>
<!-- ./wrapper -->

<!-- End of Footer -->

</div>
<!-- End of Content Wrapper -->

</div>
<!-- End of Page Wrapper -->

<!-- Scroll to Top Button-->
<a class="scroll-to-top rounded" href="#page-top">
<i class="fas fa-angle-up"></i>
</a>

<!-- Logout Modal-->


<!-- Bootstrap core JavaScript-->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

<!-- Core plugin JavaScript-->
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>

<!-- Custom scripts for all pages-->
<script src="js/sb-admin-2.min.js"></script>

<!-- Page level plugins -->
<script src="vendor/chart.js/Chart.min.js"></script>

<!-- Page level custom scripts -->
<script src="js/demo/chart-area-demo.js"></script>
<script src="js/demo/chart-pie-demo.js"></script>

<!-- jQuery -->
<script src="http://crsorgiup.co.in/admin-assets/plugins/jquery/jquery.min.js"></script>
<!-- jQuery UI 1.11.4 -->
<script src="http://crsorgiup.co.in/admin-assets/plugins/jquery-ui/jquery-ui.min.js"></script>
<!-- Resolve conflict in jQuery UI tooltip with Bootstrap tooltip -->
<script>
  $.widget.bridge('uibutton', $.ui.button)
</script>
<!-- Bootstrap 4 -->
<script src="http://crsorgiup.co.in/admin-assets/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>

<!-- Select2 -->
<script src="http://crsorgiup.co.in/admin-assets/plugins/select2/js/select2.full.min.js"></script>

<!-- SweetAlert -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/8.11.8/sweetalert2.all.min.js"></script>

<!-- DataTables  & Plugins -->
<script src="http://crsorgiup.co.in/admin-assets/plugins/datatables/jquery.dataTables.min.js"></script>
<script src="http://crsorgiup.co.in/admin-assets/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="http://crsorgiup.co.in/admin-assets/plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="http://crsorgiup.co.in/admin-assets/plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
<script src="http://crsorgiup.co.in/admin-assets/plugins/datatables-buttons/js/dataTables.buttons.min.js"></script>
<script src="http://crsorgiup.co.in/admin-assets/plugins/datatables-buttons/js/buttons.bootstrap4.min.js"></script>

<script src="http://crsorgiup.co.in/admin-assets/plugins/datatables-buttons/js/buttons.html5.min.js"></script>
<script src="http://crsorgiup.co.in/admin-assets/plugins/datatables-buttons/js/buttons.print.min.js"></script>
<script src="http://crsorgiup.co.in/admin-assets/plugins/datatables-buttons/js/buttons.colVis.min.js"></script>

<!-- ChartJS -->
<script src="http://crsorgiup.co.in/admin-assets/plugins/chart.js/Chart.min.js"></script>
<!-- Sparkline -->
<script src="http://crsorgiup.co.in/admin-assets/plugins/sparklines/sparkline.js"></script>
<!-- JQVMap -->
<script src="http://crsorgiup.co.in/admin-assets/plugins/jqvmap/jquery.vmap.min.js"></script>
<script src="http://crsorgiup.co.in/admin-assets/plugins/jqvmap/maps/jquery.vmap.usa.js"></script>
<!-- jQuery Knob Chart -->
<script src="http://crsorgiup.co.in/admin-assets/plugins/jquery-knob/jquery.knob.min.js"></script>
<!-- daterangepicker -->
<script src="http://crsorgiup.co.in/admin-assets/plugins/moment/moment.min.js"></script>
<script src="http://crsorgiup.co.in/admin-assets/plugins/daterangepicker/daterangepicker.js"></script>
<!-- Tempusdominus Bootstrap 4 -->
<script src="http://crsorgiup.co.in/admin-assets/plugins/tempusdominus-bootstrap-4/js/tempusdominus-bootstrap-4.min.js"></script>
<!-- Summernote -->
<script src="http://crsorgiup.co.in/admin-assets/plugins/summernote/summernote-bs4.min.js"></script>
<!-- overlayScrollbars -->
<script src="http://crsorgiup.co.in/admin-assets/plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
<!-- AdminLTE App -->
<script src="http://crsorgiup.co.in/admin-assets/dist/js/adminlte.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="http://crsorgiup.co.in/admin-assets/dist/js/demo.js"></script>
<!-- AdminLTE dashboard demo (This is only for demo purposes) -->
<script src="http://crsorgiup.co.in/admin-assets/dist/js/pages/dashboard.js"></script>

<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js" integrity="sha512-37T7leoNS06R80c8Ulq7cdCDU5MNQBwlYoy1TX/WUsLFC2eYNqtKlV0QjH7r8JpG/S0GUMZwebnVFLPd6SU5yg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-toast-plugin/1.3.2/jquery.toast.min.js" integrity="sha512-zlWWyZq71UMApAjih4WkaRpikgY9Bz1oXIW5G0fED4vk14JjGlQ1UmkGM392jEULP8jbNMiwLWdM8Z87Hu88Fw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script>
function alertMessage(type,message) {
  if (type=='error') {
    type = 'danger';
  }

  return "<div class='alert alert-"+type+" alert-dismissible'> <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a> "+message+" </div>";
}
</script>

<script>
  $(document).ready(function() {
    // When the mobile number is changed, automatically update the username and password
    $('#phone').on('input', function() {
      var phone = $(this).val();
      var lastFourDigits = phone.slice(-5); // Get the last 4 digits
      if (lastFourDigits.length == 5) { // Only update the username and password if there are at least 4 digits
        var username = 'UIDAI' + lastFourDigits; // Create the username
        var password = 'CSC@' + lastFourDigits; // Create the password
        $('#userid').val(username); // Set the value in the username field
        $('#password').val(password); // Set the value in the password field (read-only)
      }
    });
  });
</script>

