<?php
/**
 * User Address info
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

 global $fbbttm_helperobj;

 $currentuserid = '';
 $userdata = array();

 $fname=$lname=$email=$country=$state=$fbbstreetaddress1=$fbbstreetaddress2=$fbbttmcity=$fbbttmpincode=$fbbttmphone= $countryname = $statename= $addid =''; 

 $country = $state = 0;

 if ( is_user_logged_in() ) 
 {
	 	$currentuserid = get_current_user_id();
	 	$delivarydata = $fbbttm_helperobj->fbbttm_comman_single_tableinfo(FBBTTM_DELIVERY_ADDRESS,'fbbttm_user_id',$currentuserid);

	 $userdata = $fbbttm_helperobj->fbbttm_get_user_info($currentuserid);
	 $email = $userdata->user_email;

		if(!empty($delivarydata)){
		 	foreach ($delivarydata as $delivaryaddress) {
		 		$addid = $delivaryaddress->fbbttm_delivery_id;   
		 		$fname = $delivaryaddress->fbbttm_first_name;    
		 		$lname = $delivaryaddress->fbbttm_last_name;     
		 		$email = $delivaryaddress->fbbttm_user_email;    
		 		if($delivaryaddress->fbbttm_user_country!=''){
		 			$country = $delivaryaddress->fbbttm_user_country;
	 			}
		 		$state = $delivaryaddress->fbbttm_user_state;
		 		$fbbstreetaddress1 = $delivaryaddress->fbbttm_street_1;
		 		$fbbstreetaddress2 = $delivaryaddress->fbbttm_street_2;
		 		$fbbttmcity = $delivaryaddress->fbbttm_city;
		 		$fbbttmpincode = $delivaryaddress->fbbttm_pincode;
		 		$fbbttmphone = $delivaryaddress->fbbttm_phone;

		 		if($country != '0'){
		 			$countrydata = $fbbttm_helperobj->fbbttm_comman_single_tableinfo(FBBTTM_COUNTRIES,'id',$country);
			 		foreach($countrydata as $cdata){
			 			$countryname = $cdata->name;
			 		}

		 		$statedata = $fbbttm_helperobj->fbbttm_comman_single_tableinfo(FBBTTM_STATES,'id',$state);

		 		foreach($statedata as $sdata){
		 			$statename = $sdata->name;
		 		}

	 		}
	 		

		}
	 }
  }
 

 $addedittext =  (!empty($delivarydata)) ? 'Edit' : 'Add';

 ?> 

 <div class="fbbttm-addressinfo fbbttm-myaccountcontent">
 	<div class="row">

 		<div class="col-md-12">
 		
 		<h3><?php echo esc_html__('Delivery address','foodboxbooker');?></h3>	
 			<span class="fbbttm-delivery" id="fbbttm-delivery"><?php echo esc_html($addedittext);?></span>
 			<div class="fbbttm-billing-addressinfo">
 				<p><?php echo esc_html($fname);?>	<?php echo esc_html($lname);?>	</p>
 				<p><?php echo esc_html($fbbstreetaddress1);?>		</p>
 				<p><?php echo esc_html($fbbttmcity);?></p>
 				<p><?php echo esc_html($statename);?> <?php echo esc_html($countryname);?> <?php echo esc_html($fbbttmpincode);?></p>
 				<p><?php echo esc_html($fbbttmphone);?></p>
	 		</div>
	 		
 		</div>

 	</div>


<div class="fbbttm-deliveryaddress-info fbbttm-myaccountcontent">
 	<h3><?php echo esc_html__('Edit Delivery Address','foodboxbooker');?></h3>
 	<div id="result"></div>
  <form class="fbbttm-deliveryaddress" id="fbbttm-deliveryaddress" method="post">
  	 <?php
    	// Noncename needed to verify where the data originated
    	wp_nonce_field( 'fbbttm_meta_box_nonce', 'fbbttm_meta_box_noncename' );
	  ?>
		<div class="row">
 	    <div class="col-md-6">
			<label><?php echo esc_html__('First Name *','foodboxbooker');?></label>
			<input type="text" name="fbbttm-fname" value="<?php echo  esc_html((!empty($fname)? $fname: ''));?>" class="input-text" required>
			<div class="fbbttm-error fbbttmfname"></div>
		</div>

		<div class="col-md-6">
			<label><?php echo esc_html__('last Name *','foodboxbooker');?></label>
			<input type="text" name="fbbttm-lname" value="<?php echo  esc_html((!empty($lname)? $lname: ''));?>" class="input-text" required>
			<div class="fbbttm-error fbbttmlname"></div>

		</div>
  	 </div>

	  	<div class="row ">
	  		<div class="col-md-6">
		  		<label><?php echo esc_html__('Email Address *','foodboxbooker');?></label>
				 <input type="email" name="fbbttm-email" value="<?php echo  esc_html((!empty($email)? $email: ''));?>" class="input-text" required>
				 <div class="fbbttm-error fbbttmemail"></div>
			 </div>
			 <div class="col-md-6">
		  		<label><?php echo esc_html__('Phone *','foodboxbooker');?></label>
				<input type="text" name="fbbttm-phone" value="<?php echo  esc_html((!empty($fbbttmphone)? $fbbttmphone: ''));?>" class="input-text">
				<div class="fbbttm-error fbbttmphone"></div>
			</div>
	  	</div>	
	  	<div class="row">
	  		<div class="col-md-6">
	  			 <?php 
	  				$counries = $fbbttm_helperobj->fbbttm_comman_single_tableinfo(FBBTTM_COUNTRIES,'id');
	  			 ?>
	  			<label><?php echo esc_html__('Country *','foodboxbooker');?></label>
				<select id="fbbttm-country" name="fbbttm-country">
			 	<option><?php echo esc_html('');?></option>
			 	<?php foreach($counries as $countdata ){
			 		$selectdrop = ($countdata->id == $country) ? 'selected="selected"': '';
			 		?>
			 		<option value="<?php echo esc_html($countdata->id);?>" data-attr="<?php echo esc_html($countdata->shortname);?>" <?php echo esc_attr($selectdrop);?>>
			 			<?php echo esc_html($countdata->name);?></option>
			 		<?php
			 	}
			 ?>
			 </select>
			 <div class="fbbttm-error fbbttmcountry"></div>
			 </div>	
			 <div class="col-md-6">

			 <label><?php echo esc_html__('State *','foodboxbooker');?></label>
			 <select id="fbbttm-state" name="fbbttm-state">
			 	<option><?php echo esc_html__('Select State *','foodboxbooker');?></option>
			 </select>	
			 <div class="fbbttm-error fbbstate"></div>
			 </div>	

	  	</div>	

	  	<div class="row">
	  		<div class="col-md-6">
		  		<label><?php echo esc_html__('Street Address *','foodboxbooker');?></label>
				 <input type="text" name="fbbttm-street-address-1" value="<?php echo  esc_html((!empty($fbbstreetaddress1)? $fbbstreetaddress1: ''));?>" class="input-text">
				 <div class="fbbttm-error fbbttmstreet1"></div>
			 </div>
			 <div class="col-md-6">
		  		<label><?php echo esc_html__('Town / City *','foodboxbooker');?></label>
				 <input type="text" name="fbbttm-city" value="<?php echo  esc_html((!empty($fbbttmcity)? $fbbttmcity: ''));?>" class="input-text">
				 <div class="fbbttm-error fbbttmcity"></div>
			 </div>
	  	</div>	

	  	<div class="row">
	  		<div class="col-md-12">
		  		<label><?php echo esc_html__('Pincode *','foodboxbooker');?></label>
				<input type="text" name="fbbttm-pincode" value="<?php echo  esc_html((!empty($fbbttmpincode)? $fbbttmpincode: ''));?>" class="input-text">
				<div class="fbbttm-error fbbttmpincode"></div>
			</div>
	  	</div>	
	  	
	  	<div class="row">
	  		<div class="col-md-12">
		  		<input type="hidden" name="fbbttm-hiddencid" id="fbbttm-hiddencid" value="<?php echo esc_html($country);?>">
		  		<input type="hidden" name="fbbttm-hiddensid" id="fbbttm-hiddensid" value="<?php echo esc_html($state);?>">
		  		<input type="hidden" name="fbbttm-userid" id="fbbttm-userid" value="<?php echo esc_html($currentuserid);?>">
		  		<input type="hidden" name="fbbttm-useraddressid" id="fbbttm-useraddressid" value="<?php echo esc_html($addid);?>"/>
			 	<input type="submit" name="fbbttm-submit" id="fbbttm-submit" value="<?php echo esc_html__('Submit','foodboxbooker');?>" class="input-submit">
			 	<div class="fbbttm-loader hide"></div> 
			 	<div class="fbbttm-error php-error"></div>
			 	
		 	 </div>
	  	</div>
	  	<input type="hidden" name="hidtablename" id="hidtablename" value="<?php echo esc_html(FBBTTM_DELIVERY_ADDRESS);?>">
	  	<input type="hidden" name="hidcheckoutpage" id="hidcheckoutpage" value="">
  </form>	
 </div>
 </div>