<?php 

include_once "../libraries/vendor/autoload.php";
include_once("html_iframe_header.php");
include_once("tron_utils.php");

define("TRX_TO_SUN",'1000000');
define("SUN_TO_TRX", '0.000001');

//include all php files that generated by protoc
$dir   = new RecursiveDirectoryIterator('protobuf/core/');
$iter  = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($iter, '/^.+\.php$/', RecursiveRegexIterator::GET_MATCH); // an Iterator, not an array

foreach ( $files as $file ) {
	
	if (is_array($file)) {
		foreach($file as $filename) {
			include $filename;
		}
	} else {
		include $file;
	}
}

$accType = ["0"=>"Normal","1"=>"Asset Issue", "2"=>"Contract"];

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
		$ownerAddressHex = base58check2HexString($_POST['from']);
		$ownerAddressBin = hex2str($ownerAddressHex);
		
		$acctAddressHex = base58check2HexString($_POST['addr']);
		$acctAddressBin = hex2str($acctAddressHex);
		
		$contract = new \Protocol\Transaction\Contract();
		$accountCreateContract = new \Protocol\AccountCreateContract();
		$accountCreateContract->setType($_POST['type']);
		$accountCreateContract->setOwnerAddress($ownerAddressBin);
		$accountCreateContract->setAccountAddress($acctAddressBin);
		
		$any = new \Google\Protobuf\Any();
		$any->pack($accountCreateContract);
		
		$contract->setParameter( $any );
		$contract->setType( \Protocol\Transaction\Contract\ContractType::AccountCreateContract );
		
    ?>
	<div class="alert alert-success">
		<h6 class="mt-3">Contract Serialized Hex</h6>
		<textarea class="form-control" rows="5" id="comment" readonly><?php echo str2hex($contract->serializeToString());?></textarea>
		
		<h6 class="mt-3">Contract Serialized Json</h6>
		<textarea class="form-control" rows="5" id="comment" readonly><?php echo $contract->serializeToJsonString()?></textarea>
	</div>
<?php 
    } catch (Exception $e) {
        $errmsg .= "Problem found. " . $e->getMessage();

    }
} 

if ($errmsg) {
?>
    <div class="alert alert-danger">
        <strong>Error!</strong> <?php echo $errmsg?>
    </div>
<?php
}


$fullNode = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io');
$solidityNode = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io');
$eventServer = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io');

$tron = new \IEXBase\TronAPI\Tron($fullNode, $solidityNode, $eventServer);

$chainParams = $tron->getManager()->request("wallet/getchainparameters", [], "get");
$value = getChainParamValue($chainParams['chainParameter'], $key = "getCreateAccountFee");

$createAccountFee = "N/A";
if ($value !== false) {
	$createAccountFee = bcmul($value, SUN_TO_TRX, 6);
}
?>
<div class="alert alert-info">
  <strong>Info!</strong> Create Account Fee: <?Php echo $createAccountFee?> TRX
</div>
<form action='' method='post'>
    <div class="form-group">
        <label for="from">Owner Address:</label>
        <input class="form-control" type='text' name='from' id='from' value='<?php echo $_POST['from']?>'>
    </div>
	
	<div class="form-group">
        <label for="addr">To Be Activated Address:</label>
        <input class="form-control" type='text' name='addr' id='addr' value='<?php echo $_POST['addr']?>'>
    </div>
	
	<div class="form-group">
		<label for="type">Type:</label>
		<select id="type" name="type" class="form-control" >
			<?php
			foreach($accType as $k=>$v) {
				echo "<option value='{$k}'".($k == $_POST['type'] ? " selected": "").">{$v}</option>";
			}
			?>
		</select>
	</div>
	
    <input type='submit' class="btn btn-success btn-block"/>
</form>
<?php
include_once("html_iframe_footer.php");