<?php
    if (is_post()) {
        csrf_check_token();
    	$system = post('sys');
    	$status = get_value("SELECT `status` FROM `payment_system` WHERE `name` = ".escape_db($system));
        $new_status = $status === "on" ? "off" : "on";
        $sql = "UPDATE `payment_system` SET `status` = ".escape_db($new_status)." WHERE `name` = ".escape_db($system);
    	if ($mysqli->query($sql)) {
            $report = message_success(T("Статус платежной системы ").escape($system)." успешно переключен!");
        } else {
            $report = message_error(T("Ошибка смены статуса!"));
        }
    }

	$table = "pay_methods";

	while (is_post("update_fields")) {
		if (isset($_POST['ids'][2]) && (int)$_POST['ids'][2] > 0) {
			$res = adg_create_file_to_payeer($_POST['ids'][2]);
			echo $res['message'];
		}
		foreach (post('ids') as $id => $value) {
			$sql = "UPDATE $table SET ";
			$sql .= " `value` = ".escape_db(trim($value));
			$sql .= " WHERE `id` = ".escape_db($id);
			if (!$mysqli->query($sql)) {
				$row = get_row_by_id($table, $id);
				$message = "Ошибка обновления поля ".$row['name'];
				$report = message_error($message);
				break 2;
			}
		}
		$message = "Поля обновлены";
		$report = message_success($message);
		break;
	}

	$serices = [
		'PayKassa' => [
			'name' => 'PayKassa',
			'system_type' => 'PayKassa',
		],
        'Casepay' => [
            'name' => 'Casepay',
            'system_type' => 'Casepay',
        ],
		'FreeKassa' => [
			'name' => 'FreeKassa',
			'system_type' => 'FreeKassa',
		],
		'Block' => [
			'name' => 'Blockio',
			'system_type' => 'Block.io',
		]
	];

?>

<div id="page-wrapper">
	<div class="container-fluid">
		<!-- Flot Charts -->
		<div class="row">
			<div class="col-lg-12">
				<h2 class="page-header"><?php echo T('Платежные системы'); ?></h2>
			</div>
		</div>
		<!-- /.row -->

		<div class="row">
			<div class="col-lg-12">
				<?php include(ROOT."/adg/pages/show_report.php"); ?>
				<!-- Список платежных систем -->
				<div class="panel panel-primary">
					<div class="panel-heading">
						<h3 class="panel-title"><i class="fa fa-bar-chart-o"></i> <?php echo T('Список доступных платежных систем:'); ?> </h3>
					</div>
					<div class="panel-body">
						<div class="flot-chart">
							<div class="flot-chart-content" id="flot-line-chart">
								<div class="table-responsive">
									<table class="table table-bordered table-hover">
										<thead>
											<tr>
												<th>id</th>
												<th><?php echo T('Название'); ?></th>
												<th><?php echo T('Статус'); ?></th>
												<th><?php echo T('Вкл/Выкл'); ?></th>
												<th><?php echo T('Комментарий'); ?></th>
											</tr>
										</thead>
										<style type="text/css">
											td {
												vertical-align: middle!important;
											}
										</style>
										<tbody>
											<?php
												#Вывод всех планов
												foreach (get_all_payment_system() as $item) {
											?>
                                                <?php if ($item['show_admin']) { ?>
    											<tr>
    												<td>
    													<?php echo (int)$item['id']; ?>
    												</td>
    												<td>
    													<?php echo escape($item['name']); ?>
    												</td>
    												<td>
    													<?php echo $item['status'] === 'on' ? $class_success : $class_error; ?>
    												</td>
    												<td>
    													<form action = "" method = "post">
                                                            <?php echo csrf_install(); ?>
    														<button class = "btn btn-default" name = "<?php echo escape($item['name']); ?>"><?php echo T('Вкл/Выкл'); ?></button>
    														<input name="sys" value="<?php echo escape($item['name']); ?>" hidden />
    													</form>
    												</td>
    												<td>
    													<form action = "/adg.php" method = "get">
    														<button class = "btn btn-default">Настройки</button>
    														<input name="page" value="pay_methods" hidden />
    														<input name="system" value="<?php echo escape($item['name']); ?>" hidden />
    													</form>
    												</td>
    											</tr>
                                                <?php } ?>
											<?php } ?>
										<tbody>
									</table>
								</div>
							</div>
						</div>
					</div>
				</div>
				<!-- /Список платежных систем -->

				<div class="panel-group" id="accordion">
					<?php foreach ($serices as $serice) { ?>
					<div class="panel panel-default">
						<div class="panel-heading"  data-toggle="collapse" data-parent="#accordion" href="#<?php echo escape($serice['name']); ?>">
							<h4 class="panel-title">
								<a>Настройка <?php echo escape($serice['name']); ?></a>
							</h4>
						</div>
						<div id="<?php echo escape($serice['name']); ?>" class="panel-collapse collapse">
							<form action = "" method = "post" class = "settings">
								<input type="hidden" name="update_fields" value="1">
								<!-- Содержимое 1 панели -->
								<div class="panel-body table-responsive">
									<table class="table table-bordered table-hover">
										<tr>
											<td colspan="3" style="text-align: center;"> Прием </td>
										</tr>
									<?php
										$list = get_list("pay_methods", "`system_type` = ".escape_db($serice['system_type']));
										foreach ($list as $item) {
											if ($item['type'] !== 'invest') {
												continue;
											} ?>
											<tr>
												<td><?php echo escape($item['title']);?></td>
												<td><?php echo escape($item['value']);?></td>
												<td>
													<input name = "ids[<?php echo $item['id']; ?>]" class = "form-control" value="<?php echo escape($item['value']);?>">
												</td>
											</tr>
									<?php } ?>
										<tr>
											<td colspan="3" style="text-align: center;"> Выплаты </td>
										</tr>
									<?php
										foreach ($list as $item) {
											if ($item['type'] !== 'payment') {
												continue;
											}
									?>
											<tr>
												<td><?php echo escape($item['title']);?></td>
												<td><?php echo escape($item['value']);?></td>
												<td>
													<input name = "ids[<?php echo $item['id']; ?>]" class = "form-control" value="<?php echo escape($item['value']);?>">
												</td>
											</tr>
									<?php } ?>
										<tr align="right">
											<td colspan="3"><?php button_update(); ?></td>
										</tr>
									</table>
								</div>
							</form>
						</div>
					</div>
					<?php } ?>
				</div>
			</div>
		</div>
	</div>
</div>