<?php 
	ini_set('display_errors', '1');
	//error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE & ~E_WARNING);
	error_reporting(E_ALL);
	require (CONFIG.'dbconnect.php');
	require (PAGE_CONTENT.$PageContent);

	if(isset($_COOKIE["WizSellKrugerrand"])){
		$data = json_decode($_COOKIE['WizSellKrugerrand']);
		$data = json_decode(json_encode($data), True);
	}
	else
		$data = null;
	

	$sell_krugerrand_first_name=(isset($data["sell_krugerrand_first_name"]))?$data["sell_krugerrand_first_name"] :"";
	$sell_krugerrand_last_name =(isset($data["sell_krugerrand_last_name"])) ? $data["sell_krugerrand_last_name"] :"";
	$sell_krugerrand_email = (isset($data["sell_krugerrand_email"])) ? $data["sell_krugerrand_email"] : "" ;
	$sell_krugerrand_id_number =(isset($data["sell_krugerrand_id_number"])) ? $data["sell_krugerrand_id_number"] : "";
	$sell_krugerrand_passport_number =(isset($data["sell_krugerrand_passport_number"])) ? $data["sell_krugerrand_passport_number"] : "";
	
	$sell_krugerrand_telephone =(isset($data["sell_krugerrand_telephone"])) ? $data["sell_krugerrand_telephone"] : "";
	$sell_krugerrand_mobile = (isset($data["sell_krugerrand_mobile"])) ? $data["sell_krugerrand_mobile"] : "";
	$sell_krugerrand_notes = (isset($data["sell_krugerrand_notes"])) ? $data["sell_krugerrand_notes"] : "";
	$BVDlocation = (isset($data["location"]) && $data["location"] == "BRINKS Bedfordview" ) ? "checked" : "" ;
	$CPTlocation = (isset($data["location"]) && $data["location"] == "BRINKS Cape Town" ) ? "checked" : "" ;
	$sell_krugerrand_address_line_one = (isset($data["sell_krugerrand_address_line_one"])) ? $data["sell_krugerrand_address_line_one"] : "" ;
	$sell_krugerrand_address_line_two = (isset($data["sell_krugerrand_address_line_two"])) ? $data["sell_krugerrand_address_line_two"] : "" ;
	$sell_krugerrand_city = (isset($data["sell_krugerrand_city"])) ? $data["sell_krugerrand_city"] : "";
	$sell_krugerrand_postal_code = (isset($data["sell_krugerrand_postal_code"])) ? $data["sell_krugerrand_postal_code"] : "";
	$dblTotalCoinWeight = (isset($data["dblTotalCoinWeight"])) ? $data["dblTotalCoinWeight"] : 0;
	//$dblCurrentSellPrice = (isset($data["dblCurrentSellPrice"])) ? $data["dblCurrentSellPrice"] : 0;
	$dblTotalSellPrice = (isset($data["dblTotalSellPrice"])) ? $data["dblTotalSellPrice"] : 0;
	$sell_krugerrand_quantity = (isset($data["sell_krugerrand_quantity"])) ? $data["sell_krugerrand_quantity"] : 0;
	$sell_krugerrand_quantity_2=(isset($data["sell_krugerrand_quantity_2"])) ? $data["sell_krugerrand_quantity_2"] :0;
	$sell_krugerrand_quantity_3 =(isset($data["sell_krugerrand_quantity_3"]))?$data["sell_krugerrand_quantity_3"] :0;
	$sell_krugerrand_quantity_4 =(isset($data["sell_krugerrand_quantity_4"]))?$data["sell_krugerrand_quantity_4"] :0;

	//20190617 - Change Sell KR and Buy KR pages to use new table web_retail_pricing. -Gael
	//$pdo = $SA->query("SELECT GoldPriceZARPerOz FROM web_gold_pricing ORDER BY ID DESC LIMIT 1");
	$pdo = $SA->query("SELECT GoldPriceZARPerOz, SellToUsSpread FROM web_retail_pricing ORDER BY ID DESC LIMIT 1");
	$pdo->execute();
	$results = $pdo->fetch();
	$KrugerRandTotalPriceZAR = number_format((float)$results['GoldPriceZARPerOz'] + ((float)$results['GoldPriceZARPerOz'] * ((float)$results['SellToUsSpread']/100)) , 2);
	//var_dump($KrugerRandTotalPriceZAR);
	$sellPrice = str_replace(",", "", $KrugerRandTotalPriceZAR);

	$oneOunce = number_format($sellPrice * (float)$data["sell_krugerrand_quantity"], 2, ".", ",");
	$oneHalfOunce = number_format(($sellPrice/2) * (float)$data["sell_krugerrand_quantity_2"], 2, ".", ",");
	$oneQuarterOunce = number_format(($sellPrice/4) * (float)$data["sell_krugerrand_quantity_3"], 2, ".", ",");
	$oneTenthOunce = number_format(($sellPrice/10) * (float)$data["sell_krugerrand_quantity_4"], 2, ".", ",");

	$oneOunceWeight = number_format(1 * (float)$data["sell_krugerrand_quantity"], 2, ".", ",");
	$oneHalfOunceWeight = number_format((1/2) * (float)$data["sell_krugerrand_quantity_2"], 2, ".", ",");
	$oneQuarterOunceWeight = number_format((1/4) * (float)$data["sell_krugerrand_quantity_3"], 2, ".", ",");
	$oneTenthOunceWeight = number_format((1/10) * (float)$data["sell_krugerrand_quantity_4"], 2, ".", ",");


	//print_rr($_SESSION);

	function print_rr($v){
	  echo "<PRE>";
	  print_r($v);
	  echo "</PRE>";
	}

	$data["totalNumberCoins"] = $data['sell_krugerrand_quantity'] + $data['sell_krugerrand_quantity_2'] + $data['sell_krugerrand_quantity_3'] + $data['sell_krugerrand_quantity_4'];

	$data["dblTotalCoinWeight"] = number_format($data["dblTotalCoinWeight"], 2, ".", ",");

	$data["dblDBCoinWeight"] = str_replace(",", "", $data["dblTotalCoinWeight"]);
	$data["dblDBCurrentSellPrice"] = str_replace(",", "", $sellPrice);
	$data["dblDBTotalSellPrice"] = str_replace(",", "", $data["dblTotalSellPrice"]);
?>

<style type="text/css">
	.SellPrice p {
      padding:0; 
      margin:0;
      font-size: 16px;
      color:#B09068;
  	}
  	.tabs .tab a {
  		color: grey;
  	}
  	.tabs .tab a:hover {
  		color: #3b1c5f;
  	}

  	.tabs .tab a {
  		color: #3b1c5f;
  	}

  	@media screen and (min-width: 601px) {
	  	#currentSellPrice {
	    	font-size: 14px;
	  	}
	}

	/* If the screen size is 600px wide or less, set the font-size of <div> to 30px */
	@media screen and (max-width: 600px) {
	  	#spanTotalSellPrice {
	    font-size: 14px;
	  }
	}
</style>
<div id="<?php echo $pageID;?>" class="page-wrapper row">
	<div class="investment-comparison col s12" style="background-color: #f8f7fb;">
		<div class="content-wrap">
			<form id="frmSeller" class="col s12" method="post" action="<?php echo $SiteUrl ;?>/krugerrand/sell/index.php?seller-details">
				<div class="row">
			    	<div class="col s12">
				      	<div class='mt-element-step'>
		                  	<div class='row step-line'>
			                    <div class='col m6 s6 mt-step-col'>
			                      <div class='mt-step-title' style="padding: 0;">
			                        <a id='aStep_' style='text-decoration: none; color: #3b1c5f;' onclick='' href=''>Sell To Us</a>
			                      </div>
			                    </div>
			                    <div class='col m6 s6 mt-step-col done'>
			                      <div class='mt-step-title' style="padding: 0;">
			                        <a id='bStep_' style='text-decoration: none; color: #3b1c5f;' onclick='' href=''>Seller Details</a>
			                      </div>
			                    </div>
		                  	</div>
		              	</div>
			    	</div>
			  	</div>
			  	<div class="container">
				  	<div class="bullion-investment-calc col s12" style="border: 0; padding: 0 !important;">
						<div class="">
							<p>Thank for you choosing to sell your Krugerrands to us.  To proceed kindly;
					      	<ol>
					      		<li>Complete the section below with your personal information and select your drop off location.</li>
					      		<li>Review the transaction summary below and press submit.</li>
					      	</ol>
				      	</p>
							<!-- <h5 class="center-align alrightsansmedium"><?php //echo $bao_headInvestCalc; ?></h5> -->
							<div class="ba-investment-calc col s12">
								<div id="investment-calculator" class="row">

			                  <div class='input-field col s12'>
			                     <input id='sell_krugerrand_first_name' name="sell_krugerrand_first_name" type='text'
			                     value="<?php echo $sell_krugerrand_first_name; ?>">
			                     <label for='sell_krugerrand_first_name'>First Name
			                        <span class='required' aria-required='true' style='color:red'>*</span>
			                     </label>
			                  </div>

			                  <div class='input-field col s12'>
			                     <input id='sell_krugerrand_last_name' name="sell_krugerrand_last_name" type='text'
			                     value="<?php echo $sell_krugerrand_last_name; ?>">
			                     <label for='sell_krugerrand_last_name'>Surname
			                        <span class='required' aria-required='true' style='color:red'>*</span>
			                     </label>
			                  </div>

			                  <div class='input-field col s12'>
			                     <input id='sell_krugerrand_email' name="sell_krugerrand_email" type='email' value="<?php echo $sell_krugerrand_email; ?>" >
			                     <label for='sell_krugerrand_email'>Email Address
			                        <span class='required' aria-required='true' style='color:red'>*</span>
			                     </label>
			                  </div>

			                  <div class='row col s12' style="margin-bottom: 0">
			                  	<br>
			                    	<label for='sa' >
	                              <input id='sa' name='citizen' type='radio' value='sa' onchange="chooseCitizenship();" 
	                              style='position:relative; left:0; visibility:visible;' checked="checked" />
	                              <span style='color:#3b1c5f;'>South African Citizen</span>
	                           </label>
	                           <label for='non-sa'  style='left:10px;'>
	                              <input id='non-sa' name='citizen' type='radio' value='non-sa' onchange="chooseCitizenship();" 
	                              style='position:relative; left:0; visibility:visible;' />
	                              <span style='color:#3b1c5f;'>Non South African Citizen</span>
	                           </label>
			                  </div>

			                  <div class='input-field col s12' id="divIdNumber">
			                     <input class='sell_krugerrand_id_number' name="sell_krugerrand_id_number" id='sell_krugerrand_id_number' type='text' value="<?php echo $sell_krugerrand_id_number; ?>" maxlength="13">
			                     <label for='sell_krugerrand_id_number'>Identification Number
			                        <span class='required' aria-required='true' style='color:red'>*</span>
			                     </label>
			                  </div>

			                  <div class='input-field col s12' style="display: none;" id="divPassport">
			                     <input class='sell_krugerrand_id_number' name="sell_krugerrand_passport_number" id='sell_krugerrand_passport_number' type='text' value="<?php echo $sell_krugerrand_passport_number; ?>" maxlength="20">
			                     <label for='sell_krugerrand_passport_number'>Passport 
			                        <span class='required' aria-required='true' style='color:red'>*</span>
			                     </label>
			                  </div>

			                  <!-- <div class='input-field col s12'>
			                     <input class='sell_krugerrand_telephone' name="sell_krugerrand_telephone" id='sell_krugerrand_telephone' type='text' value="<?php //echo $sell_krugerrand_telephone; ?>">
			                     <label for='sell_krugerrand_telephone'>Telephone number is
			                        <span class='required' aria-required='true' style='color:red'>*</span>
			                     </label>
			                  </div> -->

			                  <div class='input-field col s12'>
			                     <input class='sell_krugerrand_mobile' name="sell_krugerrand_mobile" id='sell_krugerrand_mobile' type='text' value="<?php echo $sell_krugerrand_mobile; ?>">
			                     <label for='sell_krugerrand_mobile'>Mobile number <span class='required' aria-required='true' style='color:red'>*</span></label>
			                  </div>

			                  <div class='alrightsansmedium' id='collectLocation'>
			                     <div class='col s12 m12'>
			                        <label for='location' style='color:#3b1c5f;'>Drop Off Location</label>
			                     </div>
			                     <br>
			                     <div class='col s12 m6'>
			                        <p style='color:#3b1c5f;' style='padding-bottom: 15px;'>
			                           <label for='BBed'>
			                              <input id='BBed' class='with-gap' name='location' type='radio' value='BRINKS Bedfordview'  style='position:relative; left:0; visibility:visible;' <?php echo $BVDlocation; ?> />
			                              <span style='color:#3b1c5f;'>BRINKS Bedfordview</span>
			                           </label>
			                        </p>
			                     </div>
			                     <div class='col s12 m6' style='padding-bottom: 15px;'>
			                        <p style='color:#3b1c5f;'>
			                           <label for='BCap'>
			                              <input id='BCap' class='with-gap' name='location' type='radio' value='BRINKS Cape Town' 
			                              style='position:relative; left:0; visibility:visible;' <?php echo $CPTlocation; ?>/>
			                              <span style='color:#3b1c5f;'>BRINKS Cape Town</span>
			                           </label>
			                        </p>
			                     </div>
			                  </div>

		                  	<div class='input-field col s12'>
		                     	<input class='sell_krugerrand_notes' name='sell_krugerrand_notes' id='sell_krugerrand_notes' type='text' value="<?php echo $sell_krugerrand_notes; ?>">
		                     	<label for='sell_krugerrand_notes'>Additional notes</label>
		                  	</div>

			                  	<br><br>
								  		<input type="hidden" id="strOneOunce" value="<?php echo $oneOunce; ?>">
								  		<input type="hidden" id="strOneHalfOunce" value="<?php echo $oneHalfOunce;?>">
								  		<input type="hidden" id="strOneQuarterOunce" value="<?php echo $oneQuarterOunce;?>">
								  		<input type="hidden" id="strOneTenthOunce" value="<?php echo $oneTenthOunce;?>">
										<input type="hidden" id="dblTotalCoinWeight" value="<?php echo $data['dblDBCoinWeight'] ;?>">
										<input type="hidden" id="dblCurrentSellPrice" value="<?php echo $data['dblDBCurrentSellPrice'] ;?>">
										<input type="hidden" id="dblTotalSellPrice" value="<?php echo $data['dblDBTotalSellPrice'] ;?>">
										<input type="hidden" id="sell_krugerrand_quantity" value="<?php echo $data['sell_krugerrand_quantity'] ;?>">
										<input type="hidden" id="sell_krugerrand_quantity_2" value="<?php echo $data['sell_krugerrand_quantity_2'];?>">
										<input type="hidden" id="sell_krugerrand_quantity_3" value="<?php echo $data['sell_krugerrand_quantity_3'];?>">
										<input type="hidden" id="sell_krugerrand_quantity_4" value="<?php echo $data['sell_krugerrand_quantity_4'];?>">
										<input type="hidden" id="sell_krugerrand_notes" value="<?php echo $data['sell_krugerrand_notes'];?>">

					

								  	<div id="krugerrand-continer-all" class="investment-continer-all  col s12" style="background-color: #f8f7fb;">
									  	<div class="col s12 investment-calculator-container">
										   <h5 class="center-align"><?php echo $resultsHeading; ?></h5>
										   <!-- <p class="center-align  intro-txt-type"><?php //echo $resultsSubHeading; ?></p> -->
			                        <table class='table-no-padding bordered ' border='1'>
			                           <thead>
			                              <tr>
			                              	<th width="">KR Size</th>
			                                 <th width=''></th>
			                                 <th style='text-align:right'>No. Coins</th>
			                                 <th style='text-align:right'>Coin Weight</th>
			                                 <th style='text-align:right'>Total</th>
			                              </tr>
			                           </thead>
			                           <tbody>
			                           <tr>
			                           	<td style='text-align:left'>1 oz Krugerrand</td>
			                           	<td style='text-align:center;'>x</td>
			                              <td style='text-align:right;' id='intOneOunce'><?php echo $data['sell_krugerrand_quantity'];?></td>
			                              <td style='text-align:right'><?php echo $oneOunceWeight;?></td>
			                              <td style='text-align:right' id='dblOneOunce'>R <?php echo $oneOunce; ?></td>
			                           </tr>
			                           <tr>
			                           	<td style='text-align:left'>½ oz Krugerrand</td>
			                           	<td style='text-align:center;'>x</td>
			                              <td style='text-align:right;' id='intHalfOunce'><?php echo $data['sell_krugerrand_quantity_2'];?></td>
			                              <td style='text-align:right'><?php echo $oneHalfOunceWeight;?></td>
			                              <td style='text-align:right' id='dblHalfOunce'>R <?php echo $oneHalfOunce;?></td>
			                           </tr>
			                           <tr>
			                           	<td style='text-align:left'>¼ oz Krugerrand</td>
			                           	<td style='text-align:center;'>x</td>
			                              <td style='text-align:right;' id='intQuarterOunce' ><?php echo $data['sell_krugerrand_quantity_3'];?></td>
			                              <td style='text-align:right'><?php echo $oneQuarterOunceWeight;?></td>
			                              <td style='text-align:right' id='dblQuarterOunce'>R <?php echo $oneQuarterOunce;?></td>
			                           </tr>
			                           <tr>
			                           	<td style='text-align:left'>⅒ oz Krugerrand</td>
			                           	<td style='text-align:center;'>x</td>
			                              <td style='text-align:right;' id='intTenthOunce'><?php echo $data['sell_krugerrand_quantity_4'];?></td>
			                              <td style='text-align:right'><?php echo $oneTenthOunceWeight;?></td>
			                              <td style='text-align:right' id='dblTenthOunce'>R <?php echo $oneTenthOunce;?></td>
			                           </tr>
			                           </tbody>
			                           <tfoot>
			                              <tr>
			                              	<th style='font-size:12px; padding:5px; text-align:left;'></th>
			                                 <th style='font-size:12px; padding:5px; text-align:left; width:10%'></th>
			                                 <th style='font-size:12px; padding:5px; text-align:right'>Total Coins</th>
			                                 
			                                 <th style='font-size:12px; padding:5px; text-align:right;'>Total Coin Weight</th>
			                                 <th style='font-size:12px; padding:5px; text-align:right;'>Total Sell Price</th>
			                              </tr>
			                              <tr>
			                              	<td id='dblCurrentSellPriceReview'></td>
			                              	<td style='text-align:center;'></td>
			                                 <td id='intCoinTotalReview' style='text-align:right; font-weight:bold;'><?php echo $data["totalNumberCoins"];?></td>
			                                 <td style='text-align:right; font-size: 16px; font-weight:bold;' id='dblTotalReview'>
			                                 	<?php echo $data["dblTotalCoinWeight"];?>
			                                 </td>
			                                 <td style='text-align:right; font-size: 16px; font-weight:bold;' id='dblTotalReview'>R <?php echo $data["dblTotalSellPrice"];?></td>
			                              </tr>
			                           </tfoot>
			                        </table>
									  	</div>
									</div>
								</div>
							</div>
						</div>
						<span class="krugerrand-price-info" style="padding: 0 20px 5px 106px;clear: both;float:right;">
							<a href="<?php echo $SiteUrl ;?>/krugerrand/sell/index.php?sell-to-us" class="btn btn-default">Back</a>
							<button id="krugerrandSellCalcOver" class="btn btn-success" style="background-color: #3b1c5f;">Submit</button>
						</span>
					</form>
				</div>
		</div><!-- content wrap -->
	</div><!-- investment-comparison -->

</div><!--  page-wrapper  -->
<script>
	var SiteUrl = '<?php echo $SiteUrl ;?>';
   $(document).ready(function(){
      calculateCoinWeight();
      $("#sell_krugerrand_mobile").inputmask("mask", {
         mask: "(999) 999-9999"
      });
      chooseCitizenship();
   });

   function chooseCitizenship()
   {
   	var radioInput = $('input[name=citizen]:checked').val();
      if(radioInput == "sa"){
      	$('#divPassport').hide('fast');
      	$('#divIdNumber').show('fast');
      }else{
      	$('#divPassport').show('fast');
      	$('#divIdNumber').hide('fast');
      }
   }


   function calculateCoinWeight()
   {
      var quantityOne = parseInt(document.getElementById('sell_krugerrand_quantity').value);
      var quantityTwo = parseInt(document.getElementById('sell_krugerrand_quantity_2').value);
      var quantityThree = parseInt(document.getElementById('sell_krugerrand_quantity_3').value);
      var quantityFour = parseInt(document.getElementById('sell_krugerrand_quantity_4').value);
      

      quantityOne = isNaN(quantityOne) ? 0 : quantityOne;
      quantityTwo = isNaN(quantityTwo) ? 0 : quantityTwo;
      quantityThree = isNaN(quantityThree) ? 0 : quantityThree;
      quantityFour = isNaN(quantityFour) ? 0 : quantityFour;

      var dblTotalCoinWeight = quantityOne + (quantityTwo/2) + (quantityThree/4) + (quantityFour/10);

      // var span = document.getElementById('spanTotalCoinWeight');
      // span.textContent = dblTotalCoinWeight+' Oz';


      $.get(SiteUrl+'/dashboard/api/calc/index.php?krugerrandAmount='+dblTotalCoinWeight+'&krugerrandCalc=true'+'&sellKrugerrand=true')
         .done(function (data) {
          var Myresponse = JSON.parse(data);
         	//alert(Myresponse.KrugerRandPriceZAR);
         if (Myresponse.success == true) 
 			{
            var strCurrentSellPrice = 'R ' + Myresponse.KrugerRandPriceZAR;
            var dblCurrentSellPrice = Myresponse.KrugerRandPriceZAR;
            var dblTotalSellPrice = Myresponse.KrugerRandTotalPriceZAR;

            //alert(dblTotalSellPrice);

            var sellPrice = Myresponse.KrugerRandPriceZAR.replace(',', '');

            var oneOunce = (sellPrice * quantityOne).toFixed(2);
            var oneHalfOunce   = ( (sellPrice) * (quantityTwo/2)   ).toFixed(2);
            var oneQuaterOunce = ( (sellPrice) * (quantityThree/4) ).toFixed(2);
            var oneTenthOunce  = ( (sellPrice) * (quantityFour/10) ).toFixed(2);

            var strOneOunce = 'R ' + addCommas(oneOunce);
            var strOneHalfOunce = 'R ' + addCommas(oneHalfOunce);
            var strOneQuaterOunce = 'R ' + addCommas(oneQuaterOunce);
            var strOneTenthOunce = 'R ' + addCommas(oneTenthOunce);

            $('#lblOneOnce').html(strOneOunce);
            $('#lblOneHalfOnce').html(strOneHalfOunce);
            $('#lblOneQuaterOnce').html(strOneQuaterOunce);
            $('#lblOneTenthOnce').html(strOneTenthOunce);

            //ADD REVIEW Denominations
            // $('#intOneOunce').html(quantityOne);
            // $('#intHalfOunce').html(quantityTwo);
            // $('#intQuarterOunce').html(quantityThree);
            // $('#intTenthOunce').html(quantityFour);
            // $('#intCoinTotalReview').html(dblTotalCoinWeight);

            // $('#dblOneOunce').html(strOneOunce);
            // $('#dblHalfOunce').html(strOneHalfOunce);
            // $('#dblQuarterOunce').html(strOneQuaterOunce);
            // $('#dblTenthOunce').html(strOneTenthOunce);
            // $('#dblTotalReview').html('R '+dblTotalSellPrice);
            //  $('#dblCurrentSellPriceReview').html('R '+dblCurrentSellPrice);
            // --- END --- ADD REVIEW Denominations


            $('#currentSellPrice').html(strCurrentSellPrice);
            $('#spanTotalSellPrice').html('R '+dblTotalSellPrice);
            $('#pTotalSellPrice').html('R '+dblTotalSellPrice);

            //dblCurrentSellPrice = parseFloat(dblCurrentSellPrice.replace(',',''));
            //dblTotalSellPrice = parseFloat(dblTotalSellPrice.replace(',',''));

            $('#dblTotalCoinWeight').val(dblTotalCoinWeight);
            $('#dblCurrentSellPrice').val(dblCurrentSellPrice);
            $('#dblTotalSellPrice').val(dblTotalSellPrice);
         }
      });
   }

   function addCommas(nStr) {
      nStr += '';
      var x = nStr.split('.');
      var x1 = x[0];
      var x2 = x.length > 1 ? '.' + x[1] : '';
      var rgx = /(\d+)(\d{3})/;
      while (rgx.test(x1)) {
         x1 = x1.replace(rgx, '$1' + ',' + '$2');
      }
      return x1 + x2;
   }
</script>
