<?php if(!defined('FastCore')){exit('Opss!');}

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 0);

?>
<h3>Управление платежными системами</h3>
<?php

if(isset($_POST['getcurrency'])){
require_once  "./core/PaykassaCurrency.php";

$pairs = [
	"TRX_TRX",
	"DOGE_TRX",
	"LTC_TRX",
	"USDT_TRX",
	"BTC_TRX",
	"DASH_TRX",
	"BNB_TRX",
	"ETH_TRX",
	"BCH_TRX",
	"XRP_TRX",
	"XLM_TRX",
	];

    $res = PaykassaCurrency::getCurrencyPairs($pairs);

    if ($res["error"]) {
        die($res["message"]);
    } else {
        $map_pairs = [];
        array_map(function ($pairs) use (&$map_pairs) {
            foreach ($pairs as $pair => $value) {
                $map_pairs[$pair] = $value;
            }
        }, $res["data"]);
echo'<div class="card m-2 col-lg-4 text-center"><table class="table table-sm table-bordered mb-0">';
        foreach ($map_pairs as $pair => $rate) {
            echo sprintf("<tr><td>Pairs - %s </td><td> %s</td></tr>", $pair, $rate);

	$cursValut = strstr($pair, '_', true); // This is; // чистим все что до

	$db->query("UPDATE db_paysystem SET pairs = '$rate' WHERE currency = '$cursValut'"); // обновляем курс валют

        }
echo'</table><b>ВАЛЮТА САЙТА TRX</b></div>';

    }
}
?>

<?php 
	if(isset($_POST['update'])){
	$psid = $_POST['psid'];
	$title = htmlspecialchars($_POST['title']);
	$name = htmlspecialchars($_POST['name']);
	$currency = htmlspecialchars($_POST['currency']);
	$pairs = $_POST['pairs'];
	$minDep = $_POST['mindep'];

	$db->Query("UPDATE db_paysystem SET title = '$title', name = '$name', currency = '$currency', pairs = '$pairs', mindep = '$minDep' WHERE id = '$psid'");
	 echo '<div class="alert alert-success">Платежная система обновлена</div>';
	}

	if(isset($_POST['add'])){
	$title = 'VIEW PAYMENT';
	$name = 'NAME PAYMENT';
	$currency = 'TRX';
	$pairs = 1;
	$minDep = 10;

    # Добавить поле
    $db->query('INSERT INTO db_paysystem (title,name,currency,pairs,mindep) VALUES (?,?,?,?,?)', array($title,$name, $currency, $pairs, $minDep)); 
	 echo '<div class="alert alert-success">Платежная система добавлена</div>';
	}
?>

<table class="table table-bordered table-striped text-center bg-white">
<thead>
<tr>
	<th>#</th>
	<th>Вид</th>
	<th>Название</th>
	<th>Валюта</th>
	<th>Курс валют <form action="" method="post" class="m-0 p-0 d-inline">
		<button class="btn btn-outline-warning btn-sm" name="getcurrency" type="submit"><i class="fa fa-exchange"></i></button>
	</form></th>
	<th>Мин. Депозит</th>
	<th colspan="2">Действие</th>
</tr>
</thead>
<tbody>


<?php

$paySystems = $db->query("SELECT * FROM `db_paysystem` ORDER BY `id` DESC LIMIT 50")->fetchAll();
foreach ($paySystems as $ps) {
?>
<tr>
	<td><?=$ps['id']; ?></td>
	<td>
	<form action="" method="post" class="m-0 p-0"><input type="text" name="title" class="form-control form-control-sm m-0" value="<?=$ps['title']; ?>"/></td>
	<td><input type="text" name="name" class="form-control form-control-sm m-0" value="<?=$ps['name']; ?>"/></td>
	<td><input type="text" name="currency" class="form-control form-control-sm m-0" value="<?=$ps['currency']; ?>"/></td>
	<td><input type="text" name="pairs" class="form-control form-control-sm m-0" value="<?=$ps['pairs']; ?>"/></td>
	<td><input type="text" name="mindep" class="form-control form-control-sm m-0" value="<?=$ps['mindep']; ?>"/></td>
	<td><input type="hidden" name="psid" value="<?=$ps['id'];?>" /><button class="btn btn-primary btn-sm" name="update" type="submit">Изменить</button></form></td>
	<td>
	<form action="" method="post" class="m-0 p-0">
		<input type="hidden" name="delete" value="<?=$ps['id'];?>" />
		<button class="btn btn-danger btn-sm" type="submit">X</button>
	</form>
	</td>
</tr>
<?php
	}
?> 
  </tbody>
</table>
<div class="p-2 mb-3">
	<form action="" method="post" class="m-0 p-0">
		<button class="btn btn-success" name="add" type="submit">Добавить новое поле</button>
	</form>
</div>
