<?php 
$id = $_SESSION['master'];
$stmt = $pdo->prepare("SELECT payment_gateway,apikey_payment_gateway,url_payment_gateway,status_payment_gateway,tokenkey_payment_gateway FROM data_server WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$data = $stmt->fetchAll();
$payment_gateway = $data[0]['payment_gateway'];
$apikey = $data[0]['apikey_payment_gateway'];
$tokenkey = $data[0]['tokenkey_payment_gateway'];
$enpoint = $data[0]['url_payment_gateway'];
$status = $data[0]['status_payment_gateway'];

?>
<!-- Main content -->
<section class="content">
  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header bg-secondary">
            <div class="row">
              <div class="col-sm-6">
                <h3 class="card-title">Payment Gateway Setting</h3>
              </div>
              <div class="col-sm-6">
              </div>
            </div>
          </div>
          <!-- /.card-header -->
            <div class="card-body">
              <div class="row">
                <div class="col-md-5">
                  <div class="card">
                    <div class="card-body">
                      <center>
                        <h3 class="text-danger">PERHATIAN..!</h3>
                        <p>Untuk mengaktifkan payment gateway anda harus terlebih dahulu mempunyai akun FLIP BUSSINESS</p>
                        <hr>
                        <?php if($status == "ACTIVE"){ ?>
                          <div class="card bg-success">
                            <h5 class="text-light m-1">STATUS : <?php echo $status; ?></h5>
                          </div>
                        <?php } else { ?>
                          <div class="card bg-danger">
                            <h5 class="text-light m-1">STATUS : <?php echo $status; ?></h5>
                          </div>
                        <?php } ?>
                      </center>
                      <form method="post" enctype="multipart/form-data">
                        <div class="form-group">
                          <label>Jenis Payment Gateway</label>
                          <select class="form-control" name="jenis">
                            <option value="">Pilih Salah Satu</option>
                            <option value="flip" <?php if($payment_gateway == 'flip'){ echo "selected"; } ?>>Flip Bussiness</option>
                          </select>
                        </div>
                        <div class="form-group">
                          <label>ENDPOINT URL</label>
                          <input class="form-control" type="text" name="enpoint" value="<?php echo $enpoint; ?>">
                        </div>
                        <div class="form-group">
                          <label>API SECRET KEY</label>
                            <div class="input-group">
                              <input type="password" class="form-control password-input" id="password1" name="apikey" value="<?php echo $apikey; ?>">
                              <div class="input-group-append">
                                <div class="input-group-text password-toggle" data-target="password1">
                                  <span class="fas fa-eye"></span>
                                </div>
                              </div>
                            </div>
                        </div>
                        <div class="form-group">
                          <label>TOKEN VALIDASI</label>
                            <div class="input-group">
                              <input type="password" class="form-control password-input" id="password2" name="tokenkey" value="<?php echo $tokenkey; ?>">
                              <div class="input-group-append">
                                <div class="input-group-text password-toggle" data-target="password2">
                                  <span class="fas fa-eye"></span>
                                </div>
                              </div>
                            </div>
                        </div>
                        <input type="submit" class="btn btn-sm btn-primary m-1" name="simpan" value="Simpan Konfigurasi">
                        <?php if($status == "ACTIVE"){ ?>
                          <input type="submit" class="btn btn-sm btn-danger m-1" name="inactive" value="Non Aktifkan Payment Gateway">
                        <?php } else { ?>
                          <input type="submit" class="btn btn-sm btn-success m-1" name="active" value="Aktifkan Payment Gateway">
                        <?php } ?>
                      </form>
                    </div>
                  </div>
                </div>
                <div class="col-md-7">
                    <?php
                    // echo "<pre>";
                    // print_r($_SERVER);
                    // echo "</pre>";
                    ?>
                </div>
              </div>
            </div>
            <div class="card-footer">
              
            </div>
          </div>
      </div>
    </div>
  </div>
</section>

<?php
//Aktifkan Payment Gateway
  if (isset($_POST['active'])) {
    try {
      if(!empty($apikey) || !empty($tokenkey) || !empty($enpoint) ) {
        $stmt = $pdo->prepare("UPDATE data_server SET status_payment_gateway = 'ACTIVE' WHERE id = :idtik");
        $stmt->bindParam(':idtik', $id, PDO::PARAM_INT);
        $stmt->execute();
        sweetAlert("Payment Gateway Telah Di Aktifkan", "success");
      }
      else {
        sweetAlert("Gagal : Silahkan isi dulu API paramenter", "error");
      }
    } catch (PDOException $e) {
      sweetAlert("Gagal: " . $e->getMessage(), "error");
    }
  }
//Non Aktifkan Payment Gateway
  if (isset($_POST['inactive'])) {
    try {
      $stmt = $pdo->prepare("UPDATE data_server SET status_payment_gateway = 'INACTIVE' WHERE id = :idtik");
      $stmt->bindParam(':idtik', $id, PDO::PARAM_INT);
      $stmt->execute();
      sweetAlert("Payment Gateway Telah Di Non Aktifkan", "success");

    } catch (PDOException $e) {
      sweetAlert("Gagal: " . $e->getMessage(), "error");
    }
  }
//Simpan data Payment gateway
  if (isset($_POST['simpan'])) {
    try {
      $stmt = $pdo->prepare("UPDATE data_server SET payment_gateway = :payment_gateway, apikey_payment_gateway = :apikey_payment_gateway, url_payment_gateway = :url_payment_gateway, tokenkey_payment_gateway = :tokenkey_payment_gateway WHERE id = :idtik");
      $stmt->bindParam(':payment_gateway', $_POST['jenis']);
      $stmt->bindParam(':apikey_payment_gateway', $_POST['apikey']);
      $stmt->bindParam(':url_payment_gateway', $_POST['enpoint']);
      $stmt->bindParam(':tokenkey_payment_gateway', $_POST['tokenkey']);
      $stmt->bindParam(':idtik', $id, PDO::PARAM_INT);
      $stmt->execute();
      sweetAlert("Payment Gateway telah ubah", "success");

    } catch (PDOException $e) {
      sweetAlert("Gagal: " . $e->getMessage(), "error");
    }
  }
?>
<script>
    document.querySelectorAll('.password-toggle').forEach(function(toggle) {
        toggle.addEventListener('click', function() {
            var targetId = this.getAttribute('data-target');
            var passwordInput = document.getElementById(targetId);
            var icon = this.querySelector('span');

            if (passwordInput.type === 'password') {
                passwordInput.type = 'text';
                icon.classList.remove('fa-eye');
                icon.classList.add('fa-eye-slash');
            } else {
                passwordInput.type = 'password';
                icon.classList.remove('fa-eye-slash');
                icon.classList.add('fa-eye');
            }
        });
    });
</script>