<?php
require_once('../database/header.php');
?>

<!-- Begin Page Content -->

<div class="main-panel">
<div class="content-wrapper">

<?php if($userdata['usertype']=="mainadmin"){?> 

<?php
require_once('../database/config.php');

if(isset($_POST['register']) AND 
!empty($_POST['owner_name'])  AND 
!empty($_POST['address'])  AND 
!empty($_POST['pin_code'])  AND 
!empty($_POST['mobile_no'])  AND 
!empty($_POST['email_id'])  AND 
!empty($_POST['state'])  AND 
!empty($_POST['shop_name'])  AND 
!empty($_POST['uid_no'])  AND 
!empty($_POST['pan_no'])  AND 
!empty($_POST['usertype']) ){

$ussql = $conn->prepare("select count(*) from loginusers WHERE username = ?");
$ussql->execute([$_POST['username']]);
$msql = $conn->prepare("select count(*) from loginusers WHERE mobile_no = ?");
$msql->execute([$_POST['mobile_no']]);
$esql = $conn->prepare("select count(*) from loginusers WHERE email_id = ?");
$esql->execute([$_POST['email_id']]);
$psql = $conn->prepare("select count(*) from loginusers WHERE pan_no = ?");
$psql->execute([$_POST['pan_no']]);
$usql = $conn->prepare("select count(*) from loginusers WHERE uid_no = ?");
$usql->execute([$_POST['uid_no']]);

$hash = password_hash($_POST['username'], PASSWORD_DEFAULT);

if($userdata['usertype']=="distributor"){
if($_POST['usertype']=="retailer"){
$usertype = $_POST['usertype'];
} else {
$usertype = "";
}
} 

if($userdata['usertype']=="supdistributor"){
if($_POST['usertype']=="retailer" || $_POST['usertype']=="distributor" ){
$usertype = $_POST['usertype'];
} else {
$usertype = "";
}
} 

if($userdata['usertype']=="wluser"){
if($_POST['usertype']=="retailer" || $_POST['usertype']=="distributor" || $_POST['usertype']=="supdistributor"  || $_POST['usertype']=="apipartner" ){
$usertype = $_POST['usertype'];
} else {
$usertype = "";
}
}	

if($userdata['usertype']=="mainadmin"){
if($_POST['usertype']=="retailer" || $_POST['usertype']=="distributor" || $_POST['usertype']=="supdistributor"  || $_POST['usertype']=="apipartner"  || $_POST['usertype']=="wluser" ){
$usertype = $_POST['usertype'];
} else {
$usertype = "";
}
}	

if($ussql->fetchColumn()==0 AND $msql->fetchColumn()==0 AND $esql->fetchColumn()==0 AND $psql->fetchColumn()==0 AND $usql->fetchColumn()==0 AND $usertype!="" ){

$sql = "INSERT INTO `loginusers`(`username`, `password`,`owner_name`, `shop_name`, `mobile_no`, `email_id`, `pan_no`, `uid_no`, `address`, `state`, `pin_code`, `usertype`, `createby`, `date_time`, `status`) 
VALUES (:username, :password, :owner_name, :shop_name, :mobile_no, :email_id, :pan_no, :uid_no, :address, :state, :pin_code, :usertype, :createby, :date_time, :status)";

$status = "approved";	
$stmt = $conn->prepare($sql);
$stmt->bindParam(":username", $_POST['username']);
$stmt->bindParam(":password", $hash);
$stmt->bindParam(":owner_name", $_POST['owner_name']);
$stmt->bindParam(":shop_name", $_POST['shop_name']);
$stmt->bindParam(":mobile_no", $_POST['mobile_no']);
$stmt->bindParam(":email_id", $_POST['email_id']);
$stmt->bindParam(":pan_no", $_POST['pan_no']);
$stmt->bindParam(":uid_no", $_POST['uid_no']);
$stmt->bindParam(":address", $_POST['address']);
$stmt->bindParam(":state", $_POST['state']);
$stmt->bindParam(":pin_code", $_POST['pin_code']);
$stmt->bindParam(":usertype", $usertype);
$stmt->bindParam(":createby", $userdata['username']);
$stmt->bindParam(":date_time", $date_time);
$stmt->bindParam(":status", $status);

if($stmt->execute()){
$lastId = $conn->lastInsertId();
$status	= 'active';
$sql = "INSERT INTO settings (webname, copyright, webemail, webmob1, address1, users, status, weburl) 
VALUES (:webname, :copyright, :webemail, :webmob1, :address1, :users, :status, :weburl)";

$stmt = $conn->prepare($sql);	
$stmt->bindParam(":webname", $_POST['shop_name']);	
$stmt->bindParam(":copyright", $_POST['shop_name']);
$stmt->bindParam(":webemail", $_POST['email_id']);
$stmt->bindParam(":webmob1", $_POST['mobile_no']);	
$stmt->bindParam(":address1", $_POST['address']);	
$stmt->bindParam(":users", $lastId);	
$stmt->bindParam(":status", $status);	
$stmt->bindParam(":weburl", $_POST['weburl']);	
$stmt->execute();
$success = 1;		
}

} else {
$invalid = 1;
}

} else {
$invalid = 0;
$success = 0;
}
?> 

<!-- 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">
  
<?php if($success==1){ echo '<div class="alert alert-success" role="alert"><strong>'.strtoupper($usertype).'!</strong> 创建成功！</div>';} ?>
<?php if($invalid==1){ echo '<div class="alert alert-danger" role="alert">无效或数据已存在！</div>';} ?>
<?php if($coupon==1){ echo '<div class="alert alert-danger" role="alert">无效的优惠券费率！</div>';} ?>

<form class="user" action="" method="POST">

<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">全名</h6> 
<input required="required" type="text" class="form-control"  placeholder="姓名" name="owner_name" value="" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();">
</div>

<div class="col-sm-6">
<btn-dark6 class="m-0 font-weight-bold text-dark">用户名（白标登录 ID）</h6> 
<input required="required" type="text" class="form-control" name="username" placeholder="自动生成" id="username" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();" value="">
</div>
</div>

<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">地址</h6> 
<input required="required" type="text" name="address" class="form-control"  placeholder="完整地址" value="" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();">
</div>
<div class="col-sm-6">
<btn-dark6 class="m-0 font-weight-bold text-dark">PIN 码</h6> 
<input required="required" type="text" class="form-control" placeholder="PIN" name="pin_code" value="" maxlength="6">
</div>
</div>

<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">手机号码</h6> 
<input required="required" type="text" class="form-control" onkeyup="myUSER(this.value)" placeholder="手机号码（必须唯一）" name="mobile_no" value="" maxlength="10">
</div>
<div class="col-sm-6">
<btn-dark6 class="m-0 font-weight-bold text-dark">电子邮件 ID</h6> 
<input required="required" type="email" class="form-control" placeholder="电子邮件 ID（必须唯一）" name="email_id" value="" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();">
</div>
</div>
   
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">州/省</h6> 
<select class="form-control select" placeholder="State" name="state" value="" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();" required >
<option value="">--选择州/省--</option>
<option value="1">ANDAMAN AND NICOBAR ISLANDS</option>
<option value="2">ANDHRA PRADESH</option>
<option value="3">ARUNACHAL PRADESH</option>
<option value="4">ASSAM</option>
<option value="5">BIHAR</option>
<option value="6">CHANDIGARH</option>
<option value="33">CHHATTISGARH</option>
<option value="7">DADRA AND NAGAR HAVELI</option>
<option value="8">DAMAN AND DIU</option>
<option value="9">DELHI</option>
<option value="10">GOA</option>
<option value="11">GUJARAT</option>
<option value="12">HARYANA</option>
<option value="13">HIMACHAL PRADESH</option>
<option value="14">JAMMU AND KASHMIR</option>
<option value="35">JHARKHAND</option>
<option value="15">KARNATAKA</option>
<option value="16">KERALA</option>
<option value="17">LAKSHADWEEP</option>
<option value="18">MADHYA PRADESH</option>
<option value="19">MAHARASHTRA</option>
<option value="20">MANIPUR</option>
<option value="21">MEGHALAYA</option>
<option value="22">MIZORAM</option>
<option value="23">NAGALAND</option>
<option value="24">ODISHA</option>
<option value="99">其他</option>
<option value="25">PONDICHERRY</option>
<option value="26">PUNJAB</option>
<option value="27">RAJASTHAN</option>
<option value="28">SIKKIM</option>
<option value="29">TAMILNADU</option>
<option value="36">TELANGANA</option>
<option value="30">TRIPURA</option>
<option value="31">UTTAR PRADESH</option>
<option value="34">UTTARAKHAND</option>
<option value="32">WEST BENGAL</option>

</select>

</div>
<div class="col-sm-6">
<div class="form-group">
<btn-dark6 class="m-0 font-weight-bold text-dark">公司名称</h6> 
<input required="required" type="text" class="form-control"  placeholder="公司或商店名称" name="shop_name" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();" >
</div>

</div>
</div>
 
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">Aadhar (身份证)</h6> 
<input required="required" type="text" class="form-control" placeholder="UID" name="uid_no"  maxlength="12">
</div>
<div class="col-sm-6">
<btn-dark6 class="m-0 font-weight-bold text-dark">PAN (税务账号)</h6> 
<input required="required" type="text" class="form-control"  placeholder="PAN 号码（必须唯一）"  name="pan_no" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();" maxlength="10">
</div>
</div>

<div class="form-group row">
<div class="col-sm-3">
<btn-dark6 class="m-0 font-weight-bold text-dark">账户类型 </h6> 
<select class="form-control select" name="usertype" required="required">
<?php if($userdata['usertype']=="mainadmin"){ ?>		
<option value="wluser">白标</option>
<?php }?>	
		
</select>
	
</div>
<div class="col-sm-6">
<btn-dark6 class="m-0 font-weight-bold text-dark">网站 URL  ( example.com ) </h6> 
<input required="required" type="text" class="form-control"  placeholder="网站 URL example.com"  name="weburl">
</div>
<div class="col-sm-2">
<btn-dark6 class="m-0 font-weight-bold text-dark">创建用户 </h6> 
<button class="btn btn-info bg-info text-white btn-block" name="register" type="submit">提交</button>
</div>
</div>

 </form>


<script>
function myUSER(value) {
document.getElementById("username").value = "WL" + value;
}			  
</script>			  
			  
</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');
?>