<?php
session_start();
include("../include/config.php");
include("../include/functions.php"); 
 validate_admin();
 if($_REQUEST['id']!=''){
$sql=$obj->query("select * from $tbl_admin where id=".$_REQUEST['id']);
$result=$obj->fetchNextObject($sql);

if(SITE_URL==MAINSITE_URL){
  include("include/bt-production.php");
  $mastermerchntid="MyValue365Inc_marketplace";
  }else{
  include("include/bt-sandbox.php");
  $mastermerchntid="myvalue365";
  }

  $vaultuserid='QS'.$_REQUEST['id'];
}

  $full_name=mysqli_real_escape_string($GLOBALS['conn'],$_POST['full_name']);
  $username=mysqli_real_escape_string($GLOBALS['conn'],$_POST['username']);
  $password='myvalue@123';
  $email=mysqli_real_escape_string($GLOBALS['conn'],$_POST['email']);
  $mobile=mysqli_real_escape_string($GLOBALS['conn'],$_POST['mobile']);
  $location=$_POST['location'];
  $location=implode(",",$location);
  if($_POST['enable_payment']){
  	$enable_payment=1;
  	$card=mysqli_real_escape_string($GLOBALS['conn'],$_POST['card']);
  	$expiry=mysqli_real_escape_string($GLOBALS['conn'],$_POST['expiry']);
  	$cvv=mysqli_real_escape_string($GLOBALS['conn'],$_POST['cvv']);
  }else{
  	$enable_payment=0;
  }
  
  if($_REQUEST['submitForm']=='yes'){
  if($_REQUEST['id']==''){
	  $userArr=$obj->query("select * from   $tbl_admin where  username='$username'  ");
	  if($obj->numRows($userArr)<1){
		  
	  
	   $obj->query("insert into $tbl_admin set full_name='$full_name',username='$username',password='$password',email='$email',mobile='$mobile',register_date=now(),user_type='emp',location='$location',status=1,shopper_payment='$enable_payment' ");
	   $_SESSION['sess_msg']='Employee added sucessfully'; 
	   header("location:employee-list.php");
      	   exit();
	  }else{
		 $_SESSION['sess_msg']='This Employee already regisred,Plese choose another Login Username';    
	  }
	  
       }else{
        $obj->query("update $tbl_admin set full_name='$full_name',email='$email',mobile='$mobile',user_type='emp',location='$location',status=1,shopper_payment='$enable_payment' where id=".$_REQUEST['id']);
	   $_SESSION['sess_msg']='Employee updated sucessfully';
       //card starts
       	if($enable_payment==1){
//Create vault user in BT 
  try { $customer = Braintree_Customer::find($vaultuserid);} catch (Braintree\Exception\NotFound $e) {  }
  if(isset($customer) && !empty($customer)) {
  $token=$customer->creditCards[0]->token;

  $updateCustomer = Braintree_Customer::update(
    $vaultuserid,
    [
      //'id' => 'QS',
      'firstName' => $result->full_name,
      'lastName' => '',
      'company' => 'Quicklly Inc',
      'email' => 'hello@quicklly.com',
      'phone' => '',
      'fax' => '',
      'website' => ''
    ]
);

  if($token==''){
$result = Braintree_creditCard::create([
    'customerId' => $vaultuserid,
    'number' => $card,
    'expirationDate' => $expiry,
    'cvv' => $cvv,
    'options' => [
        'verifyCard' => false,
        'makeDefault' => true
    ]
]);
  }else{
 
 $updateCard = Braintree_creditCard::update($token, [
    'cardholderName' => $result->full_name,
    'number' => $card,
    'expirationDate' => $expiry,
    'cvv' => $cvv,
    'options' => [
        'verifyCard' => false,
        'makeDefault' => true
    ]
]);
  }
if($updateCard->success || $result->success){ 
 $session_msg="Details updated successfully!";
}else{
  $session_msg="Card failed to update!";
  echo "<pre>";
  print_r($result);
  die;
 }

  }else{
    $result = Braintree_Customer::create([
    'id' => $vaultuserid,
    'firstName' => $result->full_name,
    'lastName' => '',
    'company' => 'Quicklly Inc',
    'email' => 'hello@quicklly.com',
    'phone' => '',
    'fax' => '',
    'website' => ''
]);
    $resultCard = Braintree_creditCard::create([
    'customerId' => $vaultuserid,
    'number' => $card,
    'expirationDate' => $expiry,
    'cvv' => $cvv,
    'options' => [
        'verifyCard' => false,
        'makeDefault' => true
    ]
]);
    if($resultCard->success){
    $session_msg="Customer details added!";
  }else{
    $session_msg="Customer details failed to add!";
  }
  }

  echo $_SESSION['sess_msg']=$session_msg;

/*echo "<pre>";
  print_r($result);
*/
  // end BT Vault user ends
}
      // card ended
	   
	   header("location:employee-list.php");
       exit(); 
        }
  
  }    	   
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo SITE_TITLE; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="css/admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="javascript">
function validate(obj)
{
if(obj.full_name.value==''){
alert("Please enter employee name");
obj.full_name.focus();
return false;
}
if(obj.username.value==''){
alert("Please enter login username");
obj.username.focus();
return false;
}
if(obj.email.value==''){
alert("Please enter email");
obj.email.focus();
return false;
}
if(!obj.email.value.match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/))
	{
	alert("Please enter valid email.");
	obj.email.focus();
	return false;
	}
if(obj.enable_payment.checked){
if(obj.card.value==''){
alert("Please enter correct Card Number");
obj.card.focus();
return false;
}
if(obj.expiry.value==''){
alert("Please enter correct Expiry Date");
obj.expiry.focus();
return false;
}
if(obj.cvv.value==''){
alert("Please enter correct CVV");
obj.cvv.focus();
return false;
}
}
	
}
</script>
</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<?php include("header.php") ?>
<?php if($_SESSION['sess_admin_id']!=1 &&  $myRols!='' && !in_array(25, $myRols)){ 
header("location:welcome-emp.php");
exit();
 } ?>
<tr>
	<td align="right" class="paddRtLt70" valign="top">
		<table width="99%" border="0" cellspacing="0" cellpadding="0">
			<tr>
				
				<td align="right" valign="top">
					<table width="100%" border="0" cellspacing="0" cellpadding="0">
						<tr>
							<td align="left" valign="middle" class="headingbg bodr text14">
					<em><img src="images/arrow2.gif" width="21" height="21" hspace="10" align="absmiddle" /></em>Admin: Add Employee 
					<span  style="float:right; padding-right:10px;">
					<input type="button" name="add" value="View Employee"  class="button" onclick="location.href='employee-list.php'" /></span></td>
						</tr>
						<form name="frm" method="POST" enctype="multipart/form-data" action="" onsubmit="return validate(this)">
						<input type="hidden" name="submitForm" value="yes" />
						<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" />
						<tr>
							<td height="100" align="left" valign="top" bgcolor="#f3f4f6" class="bodr">
							<table width="100%" cellpadding="0" cellspacing="0">
									<tr>
										<td align="center" colspan="2" class="paddRt14 paddBot11">
										<font color="#FF0000"><strong><?php echo $_SESSION['sess_msg']; $_SESSION['sess_msg']='';?></strong></font></td>
									</tr>
									<tr>
										<td width="18%" align="right" class="paddBot11 paddRt14">&nbsp;</td>
										<td width="82%" align="left" class="paddBot11"></td>
									</tr>
									
					       <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Employee Name:</strong></td>
									  <td align="left" class="paddBot11"><input name="full_name" type="text" id="full_name" size="36" value="<?php echo stripslashes($result->full_name);?>" /></td>
							  </tr>
							  <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Employee Location:<br /></strong><br />*You can select multiple locations</td>
									  <td align="left" class="paddBot11"><select name="location[]" multiple="multiple" required style="width:240px" required>

<option value="">Select Location...</option>

<?php $sql=$obj->query("select distinct(state) from tbl_city where status=1 and state!='UPS' order by state");

while($record=$obj->fetchNextObject($sql)){ 
$locationArr=explode(',',$result->location); ?>

<option value="<?php echo strtoupper($record->state); ?>" <?php if(in_array($record->state,$locationArr)){ ?> selected="selected" <?php } ?>><?php echo strtoupper($record->state); ?></option>

<?php } ?>

</select></td>
							  </tr>
									 <tr>
									   <td align="right" class="paddBot11 paddRt14"><strong>Login Username:</strong></td>
									   <td align="left" class="paddBot11"><?php if($_REQUEST['id']){ echo $result->username;}else{?><input name="username" type="text" id="username" size="36" value="<?php echo stripslashes($result->username);?>" /><?php } ?></td>
						      </tr>
									 <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Email:</strong></td>
									  <td align="left" class="paddBot11"><input name="email" type="text" id="email" size="36" value="<?php echo stripslashes($result->email);?>" /></td>
							  </tr>
                               <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Mobile:</strong></td>
									  <td align="left" class="paddBot11"><input name="mobile" type="text" id="mobile" size="36" value="<?php echo stripslashes($result->mobile);?>" /></td>
							  </tr>
<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Enable Payment:</strong></td>
									  <td align="left" class="paddBot11"><input type="checkbox" name="enable_payment" <?php if($result->shopper_payment==1){ ?>checked=""<?php } ?>></td>
							  </tr>
									
									
							  <?php if($_REQUEST['id']!='' && $result->shopper_payment==1){ echo $vaultuserid;
							  try { $customer = Braintree_Customer::find($vaultuserid);} catch (Braintree\Exception\NotFound $e) {  }
  if(isset($customer) && !empty($customer)) {
  $token=$customer->creditCards[0]->token;
  if($token!=''){ ?>
  	<tr>
									  <td align="right"  style="color:red" class="paddRt14 paddBot11"><strong>Note:</strong></td>
									  <td align="left" class="paddBot11"  style="color:red"><strong>Card details exist, you can update entries by entering new details again!</strong></td>
							  </tr>
							<?php }
}
  ?>		
							  	<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Card Number:</strong></td>
						<td align="left" class="paddBot11"><input type="text" name="card" max="9999999999999999" placeholder="4111111111111111"></td>
							  </tr>
							  <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Expiry Date:</strong></td>
						<td align="left" class="paddBot11"><input type="text" name="expiry" placeholder="12/2025"> Eg: mm/yyyy</td>
							  </tr>
							  <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>CVV</strong></td>
						<td align="left" class="paddBot11"><input type="number" max="999" min='99' placeholder="123" name="cvv"></td>
							  </tr>
							<?php } ?>
							<?php if($_REQUEST['id']==''){ ?>
								<tr>
									  <td align="right" class="paddRt14 paddBot11">&nbsp;</td>
									  <td align="left" class="paddBot11"><strong>Note:</strong><br/>
                                      * Default password for employee will be <strong>myvalue@123</strong><br/>
                                      * Employee can change his password after Login.<br/>
                                      * Admin can reset password of any employee if he/she forgot.</td>
							  </tr>
							<?php } ?>
									<tr>
									  <td align="right" class="paddRt14 paddBot11">&nbsp;</td>
									  <td align="left" class="paddBot11">&nbsp;</td>
							  </tr>
									<tr>
										<td width="18%" align="right" class="paddRt14 paddBot11">&nbsp;</td>
										<td width="82%" align="left" class="paddBot11">
											<input type="submit" name="submit" value="Submit"  class="submit" border="0"/> 	                  		 &nbsp;&nbsp;
											<input name="Reset" type="reset" id="Reset" value="Reset" class="submit" border="0" />									  </td>
									</tr>
								</table>
							</td>
						</tr>
						</form>
					</table>
				</td>
			</tr>
		</table>
	</td>
</tr>
<?php include('footer.php'); ?>
</table>
</body>
</html>