<?php
session_start();
include '../../function/connect.php';

if (!isset($_SESSION['extplayer'])) {
  header("Location: ../index.php");
  exit;
}

if (isset($_POST['submit'])) {

  $id_user        = $_SESSION['extplayer'];
  $nama_bank      = mysqli_real_escape_string($koneksi, $_POST['nama_bank']);
  $nama_pemilik   = mysqli_real_escape_string($koneksi, $_POST['nama_pemilik']);
  $nomor_rekening = mysqli_real_escape_string($koneksi, $_POST['nomor_rekening']);

  // =========================
  // CEK APAKAH BANK SUDAH ADA
  // =========================
  $cek = mysqli_query($koneksi, "
    SELECT id FROM tb_bank WHERE id_user = '$id_user' LIMIT 1
  ");

  if (mysqli_num_rows($cek) > 0) {

    // UPDATE BANK
    mysqli_query($koneksi, "
      UPDATE tb_bank SET
        nama_bank = '$nama_bank',
        nomor_rekening = '$nomor_rekening',
        nama_pemilik = '$nama_pemilik'
      WHERE id_user = '$id_user'
    ");

  } else {

    // INSERT BANK (kalau memang belum ada)
    mysqli_query($koneksi, "
INSERT INTO tb_bank
(icon, nama_bank, nomor_rekening, nama_pemilik, id_user, level)
VALUES
('', '$nama_bank', '$nomor_rekening', '$nama_pemilik', '$id_user', '0')
    ");

  }

  // =========================
  // UPDATE NAMA LENGKAP USER
  // =========================
  mysqli_query($koneksi, "
    UPDATE tb_user SET
      nama_lengkap = '$nama_pemilik'
    WHERE extplayer = '$id_user'
  ");

  header("Location: ../index.php?page=profile&pesan=32");
  exit;
}