<?php
require_once("../layouts/members/head.php");
?>
<?php if(in_array($user_data['usertype'], ['Admin','White Label','Master Distributor','Distributor'])){ ?>

<?php
if(isset($_POST['usercreate']) && $user_data['user_id']>0){
$name = real_escape_string($_POST['name']);
$password = real_escape_string($_POST['password']);
$company = real_escape_string($_POST['company']);
$mobile = is_valid_mobile($_POST['mobile']);
$email = is_valid_email($_POST['email']);
$aadhaar = is_valid_aadhaar($_POST['aadhaar'] ?? '000000000000');
$pan = is_valid_pan($_POST['pan'] ?? '0000000000' );
$usertype = is_valid_usertype($_POST['usertype']);

if(!empty($name) && !empty($company) && !empty($mobile) && !empty($email) && !empty($aadhaar) && !empty($pan) && !empty($usertype)){

if(is_access_usertype($user_data,$usertype)){

$username = get_username($mobile);

$condition = " username='".$username."' || mobile='".$mobile."' ";
$old_member = db_fetch_all("members",$condition);
if(count($old_member)==0){

// $password = generateNumericOTP(6);
$insData = array();
$insData['site_id'] = $site_data['site_id'];
$insData['username'] = $username;
$insData['password'] = $password;
$insData['is_otp'] = "NO";
$insData['usertype'] = $usertype;
$insData['parent_id'] = $user_data['user_id'];
$insData['parent_uid'] = $user_data['username'];
$insData['name'] = $name;
$insData['company'] = $company;
$insData['mobile'] = $mobile;
$insData['email'] = $email;
$insData['aadhaar'] = $aadhaar;
$insData['pan'] = $pan;
$insData['balance'] = 0;
$insData['created_at'] = current_timestamp();
$insData['remark'] = "";
$insData['status'] = "Active";
if(db_insert("members", $insData)){
$message = "Dear {$insData['name']}, Your Login Username: {$insData['username']} and Password: {$insData['password']}, Login https://".baseurl(); 

$whamessage = "Dear *{$name}*,\n" .
"Registration has been successfully On *{$site_data['brand']}.*\n\n" .
"UserName: *{$insData['username']}*\n" .
"Password: *{$insData['password']}*\n\n" .
"Login Here: *https://".baseurl()."*\n\n" .
"Best Regards,\n" .
"_*{$site_data['brand']}.*_";
send_sms_credentials($insData['mobile'],$whamessage);
send_email($insData['email'],"Registration Successfull",$message);   

awal_alert_msg("Member Create Successfully!",'success','');  
}else{
awal_alert_msg("Server is Error!",'info');  
}

}else{
awal_alert_msg("Username OR Mobile Already Exists!",'error');
}

}else{
awal_alert_msg("UserType Not Access!",'error');
}

}else{
awal_alert_msg("Please Fill All Valid Details!",'error');
}

}
?>


<div class="row">

  <div class="col-md-12 mb-2">
    <div class="card text-danger">
      <div class="card-header bg-primary text-white py-2">
        <div class="d-flex align-items-center">
          <span class="mr-auto"><?=file_name()?></span>
        </div>
      </div>
      <div class="p-3">
      <form class="row text-dark" method="POST" action="">
        <div class="col-md-3 mb-3">
          <label>Name</label>
          <input type="text" class="form-control" placeholder="Enter Name" name="name" required>
        </div>
        <div class="col-md-3 mb-3">
          <label>Shop Name</label>
          <input type="text" class="form-control" placeholder="Enter Shop Name" name="company" required>
        </div>
        <div class="col-md-3 mb-3">
          <label>User Type</label>
          <select class="form-control" name="usertype" required>
                <option value="">Select</option>
                 <?php if(in_array($user_data['usertype'], ['Admin','White Label'])){ ?>
                <option value="Retailer">Retailer</option>
                <option value="Distributor">Distributor</option>
                <option value="Master Distributor">Master Distributor</option>
                 <?php } ?>   
                 <?php if(in_array($user_data['usertype'], ['Master Distributor'])){ ?>
                <option value="Retailer">Retailer</option>
                <option value="Distributor">Distributor</option>
                 <?php } ?>   
                 <?php if(in_array($user_data['usertype'], ['Distributor'])){ ?>
                <option value="Retailer">Retailer</option>
                 <?php } ?>            
            </select>
        </div>
        <div class="col-md-3 mb-3">
          <label>Mobile Number</label>
          <input type="number" class="form-control" placeholder="Enter Mobile Number" name="mobile" onkeypress="if(this.value.length==10) return false;" required>
        </div>
        <div class="col-md-3 mb-3">
          <label>Password</label>
          <input type="text" class="form-control" placeholder="Abcr@1234" name="password" onkeypress="if(this.value.length==25) return false;" required>
        </div>
        <div class="col-md-3 mb-3">
          <label>Email Address</label>
          <input type="emial" class="form-control" placeholder="Enter Email Address" name="email" required>
        </div>
        <!--<div class="col-md-3 mb-3">-->
        <!--  <label>Aadhaar Number</label>-->
        <!--  <input type="number" class="form-control" placeholder="Enter Aadhaar Number" name="aadhaar" onkeypress="if(this.value.length==12) return false;" required>-->
        <!--</div>-->
        <!--<div class="col-md-3 mb-3">-->
        <!--  <label>PAN Number</label>-->
        <!--  <input type="text" class="form-control" placeholder="Enter PAN Number" name="pan" onkeypress="if(this.value.length==10) return false;" required>-->
        <!--</div>-->
        <div class="col-md-4 mb-3">
          <label>Create Account</label>
          <button type="submit" class="btn btn-primary btn-block" name="usercreate">Submit</button>
        </div>
      </form>
     </div>
    </div>
  </div>

</div>
<?php 
}else{
require_once("404.php");
} 
?>
<?php
require_once("../layouts/members/foot.php");
?>