<?php 
session_start();
date_default_timezone_set('Asia/Manila');
include('connect.php');

if(isset($_SESSION['supplier_id'])) {
  header('location:dashboard/loading.php');
}

$datetoday = date('Y-m-d h:i A'); 
$referralid = $_GET['refid'];


$message = '';
if (isset($_POST['register'])) {
  $username = $_POST['username'];
  $password = $_POST['password'];

  $firstname =  utf8_decode($_POST['firstname']);
  $lastname =  utf8_decode($_POST['lastname']);

  $mobile_number =  $_POST['mobile_number'];
  $referred_by =  $_POST['referral_code'];

  function generateRandomString($length = 6) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
      $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
  }

  $youruniqueid_num = 'FBSP'.generateRandomString();

  $sqlRegistered = "INSERT INTO tbl_accounts_affiliate(username, password, unique_id, firstname, lastname, mobile_number, unique_referral, date_registered,account_status) VALUES ('$username', '$password', '$youruniqueid_num', '$firstname','$lastname', '$mobile_number', '$referred_by', '$datetoday','0')";
  if (mysqli_query($conn, $sqlRegistered)) {
    header('location: index.php');
    $_SESSION['success'] = '<p class="text-success mb-0">Awesome! Try to login now and Verify Your Account!</p>';
  }
  
}

if ($_GET['refid'] == "") {
  $referral = "none";
}else{
  $referral = $_GET['refid'];
}

?>

<!DOCTYPE html>
<html>
<head>
<title>Fastsabuy Affiliate Marketer Registration</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes"/>
<link rel="apple-touch-icon" href="images/supplier-icon512.png">
<?php include_once('linkStyle.php'); ?>
<link rel="stylesheet" type="text/css" href="css/login.css">
</head>
<body class="hold-transition layout-top-nav layout-footer-fixed layout-fixed">
<div id="preloader" class="loading" style="display: none;"></div>
<div class="wrapper">
<br>
	<form class="pl-3 pr-3 pt-4" method="POST" id="quickForm">
	  <center>
	    <img src="dashboard/images/FS-Big.png" width="130">
	    <h5 class="title-text mb-0"><b>Join our Seller Affiliate Program today!</b></h5>
	    <?php echo $message; ?>
	  </center>

    <fieldset id="personal_information">
      <div class="form-group mt-4 pr-2 pl-2">
      <label> Firstname: </label>
        <input type="text" name="firstname" id="firstname" class="form-control" placeholder="First Name" autocomplete="off" required>
      </div>

      <div class="form-group pr-2 pl-2">
      <label> Lastname: </label>
        <input type="text" name="lastname" id="lastname" class="form-control" placeholder="Last Name" autocomplete="off" required>
      </div>

      <div class="form-group pr-2 pl-2">
      <label> Active Mobile Number: </label>
        <input type="number" name="mobile_number" id="mobile_number" class="form-control" maxlength="11" placeholder="Mobile Number" autocomplete="off" required>
      </div>

      <div class="form-group pr-2" align="right">
        <a class="btn bg-rider btn-login text-white next">NEXT</a>
      </div>
                 
    </fieldset> <!-- personal info -->

    <fieldset id="referral_information">

      <div class="form-group mt-4 pr-2 pl-2">
        <label> Referral ID: </label>
        <input type="text" name="referral_code" id="referral_code" class="form-control" placeholder="Referral [ Optional ]" value="<?php echo $referralid ?>" autocomplete="off"   >
      </div>

      <div class="form-group pr-2" align="right">
        <a class="btn btn-secondary btn-login" id="previous" style="color: #ffffff;">PREVIOUS</a>
        <a class="btn bg-rider btn-login text-white next" id="next">NEXT</a> 
      </div>
    </fieldset>

    <fieldset id="account_information">
  	  <div class="form-group mt-4 pr-2 pl-2">
      <label> Create Username: </label>
  	    <input type="text" name="username" id="username" class="form-control" placeholder="Create a username" autocomplete="off" required>
  	    <small id="availability"></small>
  	  </div>

  	  <div class="form-group mb-0 pr-2 pl-2">
      <label> Create Password: </label>
  	    <input type="password" name="password" id="viewpassword" class="form-control" placeholder="Create a Password"  autocomplete="off"   required>
  	  </div>

  	  <div class="icheck-dark pr-2 pl-2">
         <input type="checkbox" onclick="myFunction()" id="remember">
         <label for="remember">
           <small>Show Password</small>
         </label>
      </div>

      <div class="form-group mt-2 mb-0 pr-2 pl-2">
        <small>By clicking Sign Up, you agree to our <a href="">Terms</a> and <a href="">Data Policy</a>.</small>
      </div>

      <div class="form-group mt-0 pr-2" align="right">
        <a class="btn btn-secondary btn-login" id="previous_account" style="color: #ffffff;">PREVIOUS</a>
        <input type="hidden" name="register" value="register">
        <button type="button" id="action" class="btn btn-success btn-login">
          <span id="button-text">Submit</span>
        </button>
      </div>
    </fieldset>

	  <div class="text-center">
	    <p class="text-link">Already registered? <a href="index.php" class="link click-link">Sign in</a></p>
	  </div>
	</form>

</div>
<script src="dashboard/plugins/jquery/jquery.min.js"></script>
<script src="dashboard/plugins/jquery-validation/jquery.validate.min.js"></script>
<script src="dashboard/plugins/jquery-validation/additional-methods.min.js"></script>
<script src="dashboard/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="dashboard/plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
<script type="text/javascript">
function initialize() {
      var input = document.getElementById('autocomplete');
      var autocomplete = new google.maps.places.Autocomplete(input);
      autocomplete.addListener('place_changed', function () {
      var place = autocomplete.getPlace();
      var addressParts = getAddressParts(place);
      var addressPartsShortName = getAddressPartsShortName(place);
      var city_longname = addressParts.locality;
      var city_shortname = addressPartsShortName.locality;
      $('#city').val(city_longname);
      $('#city_shortname').val(city_shortname);
      $('#lat').val(place.geometry['location'].lat());
      $('#long').val(place.geometry['location'].lng());
    });
}
$(document).ready(function(){ 
  $(".next").click(function(){
    var form = $("#quickForm");
    form.validate({
      errorElement: 'span',
      errorPlacement: function (error, element) {
        error.addClass('invalid-feedback');
        element.closest('.form-group').append(error);
      },
      highlight: function (element, errorClass, validClass) {
        $(element).addClass('is-invalid');
      },
      unhighlight: function (element, errorClass, validClass) {
        $(element).removeClass('is-invalid');
      },
      rules: {
        username: {
          required: true,
        },
        vehicle: {
          required: true,
        }
      }
    });

    if (form.valid() === true){
      if ($('#personal_information').is(":visible")){
        current_fs = $('#personal_information');
        next_fs = $('#referral_information');
      }else if($('#referral_information').is(":visible")){
        current_fs = $('#referral_information');
        next_fs = $('#account_information');
      }else if($('#account_information').is(":visible")) {
        current_fs = $('#account_information');
        next_fs = $('#referral_information');
      }
      next_fs.show();
      current_fs.hide();
    }
});

$('#previous').click(function(){
  if ($('#referral_information').is(":visible")){
    current_fs = $('#referral_information');
    next_fs = $('#personal_information');
  }else if ($('#personal_information').is(":visible")) {
    current_fs = $('#personal_information');
    next_fs = $('#referral_information');
  }

  next_fs.show();
  current_fs.hide();
});

$('#previous_account').click(function(){
  if($('#account_information').is(":visible")){
    current_fs = $('#account_information');
    next_fs = $('#referral_information');
  }else if ($('#referral_information').is(":visible")){
    current_fs = $('#referral_information');
    next_fs = $('#account_information');
  }
  next_fs.show();
  current_fs.hide();
});

$('#action').click(function() {
  if ($('#quickForm').valid() === true){
    $('#button-text').html('Please wait...');
    $('#action').attr('disabled', true);
    $('#quickForm').submit();
  }
});

  $('#username').keyup(function() {
   var username = $(this).val();
   var btn_action = 'checkUsername';
   $.ajax({
     method:"POST",
     data:{user_name:username, btn_action:btn_action},
     url:"fetch.php",
     success:function(data){
       if(data == '0') {
          $('#availability').html('');
          $('#action').attr("disabled", false);
       }else{
          $('#availability').html('<span class="text-danger">Username already taken.</span>');
          $('#action').attr("disabled", true);
       }	
     }
   });
  });

  $('.click-link').click(function() {
    $('.loading').show();
  });

});

function myFunction() {
  var x = document.getElementById("viewpassword");
  if (x.type === "password") {
    x.type = "text";
  } else {
    x.type = "password";
  }
}
if ( window.history.replaceState ) {
  window.history.replaceState( null, null, window.location.href );
}
</script>
</body>
</html>