<?php
session_start();

// Sertakan file koneksi
include 'koneksi.php';
if (session_status() === PHP_SESSION_NONE) {
  session_start();
}


if (!isset($_SESSION['admin_id'])) {
  header("Location: index");
  exit();
}
// Ambil nama tabel dari parameter
if (!isset($_GET['table'])) {
  die("Tabel tidak ditentukan.");
}
$table = $_GET['table'];

// Ambil daftar kolom tabel
$query = $pdo->prepare("SHOW COLUMNS FROM `$table`");
$query->execute();
$columns = $query->fetchAll(PDO::FETCH_ASSOC);

// Daftar kolom sensitif yang perlu disembunyikan
define('HIDDEN_COLUMNS', ['password', 'private_key', 'wallet_address', 'token', 'api_key', 'secret', 'hash']);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $data = [];

  // Mengambil nilai data dari form, kecuali kolom tersembunyi
  foreach ($columns as $column) {
    $colName = $column['Field'];
    if (in_array(strtolower($colName), HIDDEN_COLUMNS)) {
      continue;
    }
    $data[$colName] = $_POST[$colName] ?? null;
  }

  // Jika tabel adalah 'jenis_iuran', lakukan proses khusus dengan periode
  if ($table === 'jenis_iuran') {
    $periode = isset($_POST['periode']) ? (int)$_POST['periode'] : 0; // Default 0 jika tidak diisi
    $startPeriode = isset($_POST['start_periode']) ? $_POST['start_periode'] : null; // Ambil start_periode jika ada
    $createdAt = (new DateTime())->format('Y-m-d H:i:s'); // created_at tetap pertama kali input

    if ($periode > 0 && $startPeriode) {
      $periodeDate = new DateTime($startPeriode);  // Gunakan start_periode sebagai dasar

      for ($i = 0; $i < $periode; $i++) {
        $data['created_at'] = $createdAt; // Tetap gunakan waktu pertama kali input
        $data['periode'] = $periodeDate->format('Y-m-d'); // Periode berubah tiap bulan

        $columnsNames = implode(", ", array_keys($data));
        $columnsValues = implode(", ", array_fill(0, count($data), "?"));

        $stmt = $pdo->prepare("INSERT INTO `$table` ($columnsNames) VALUES ($columnsValues)");
        $stmt->execute(array_values($data));

        // Tambah 1 bulan untuk periode
        $periodeDate->modify('+1 month');
      }
    }
  } else {
    // Untuk tabel selain 'jenis_iuran', langsung insert satu data
    $columnsNames = implode(", ", array_keys($data));
    $columnsValues = implode(", ", array_fill(0, count($data), "?"));

    $stmt = $pdo->prepare("INSERT INTO `$table` ($columnsNames) VALUES ($columnsValues)");
    $stmt->execute(array_values($data));
  }

  $table = $_GET['table']; // Ambil tabel dari parameter
  header("Location: home?tab=$table");
  exit();
}


?>

<!DOCTYPE html>
<html lang="en" dir="ltr" data-bs-theme="light" data-color-theme="Blue_Theme" data-layout="vertical">

<head>
  <!-- Required meta tags -->
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />

  <!-- Favicon icon-->
  <link rel="shortcut icon" type="image/png" href="assets/images/logos/logo.png" />

  <!-- Core Css -->
  <link rel="stylesheet" href="assets/css/styles.css" />

  <title>Tambah Data - Master Admin</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <!-- DataTables CSS -->
  <link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.5.0/css/responsive.bootstrap5.min.css">

  <!-- DataTables JS -->
  <script src="https://cdn.datatables.net/responsive/2.5.0/js/dataTables.responsive.min.js"></script>
  <script src="https://cdn.datatables.net/responsive/2.5.0/js/responsive.bootstrap5.min.js"></script>
  <style>
    table.dataTable {
      width: 100% !important;
      table-layout: auto !important;
    }

    /* Memastikan tabel bisa di-scroll secara horizontal di layar lebar */
    @media (min-width: 992px) {
      .table-responsive {
        overflow-x: auto !important;
        white-space: nowrap !important;
      }
    }
  </style>

</head>

<body>
  <!-- <div class="preloader">
    <img src="assets/images/logos/logo.png" alt="loader" class="lds-ripple img-fluid" style="width: 10vw; max-width: 1000px; min-width: 100px;" />
  </div> -->
  <div id="main-wrapper">
    <!-- Sidebar Start -->
    <aside class="left-sidebar with-vertical">
      <div><!-- ---------------------------------- -->
        <!-- Start Vertical Layout Sidebar -->
        <!-- ---------------------------------- -->
        <!-- Sidebar scroll-->
        <nav class="sidebar-nav scroll-sidebar" data-simplebar>
          <!-----------Profile------------------>
          <div class="user-profile position-relative" style="background: url(assets/images/backgrounds/user-info.jpg) no-repeat;">
            <!-- User profile image -->
            <div class="profile-img">
              <img src="assets/images/profile/user-1.jpg" alt="user" class="w-100 rounded-circle overflow-hidden" />
            </div>
            <!-- User profile text-->
            <div class="profile-text hide-menu pt-1 dropdown">
              <a href="javascript:void(0)" class="dropdown-toggle u-dropdown w-100 text-white
                  d-block
                  position-relative
                " id="dropdownMenuLink" data-bs-toggle="dropdown" aria-expanded="false"><?= $_SESSION['admin_username'] ?></a>
              <div class="dropdown-menu animated flipInY" aria-labelledby="dropdownMenuLink">

                <a class="dropdown-item d-flex gap-2" href="logout">
                  <i data-feather="log-out" class="feather-sm text-danger "></i>
                  Logout
                </a>

              </div>
            </div>
          </div>
          <!-----------Profile End------------------>


          <ul id="sidebarnav">
            <li class="nav-small-cap">
              <iconify-icon icon="solar:menu-dots-bold" class="nav-small-cap-icon fs-4"></iconify-icon>
              <span class="hide-menu">Dashboard</span>
            </li>
            <li class="sidebar-item">
              <a class="sidebar-link" href="dashboard">
                <iconify-icon icon="bi-house" class="aside-icon"></iconify-icon>
                <span class="hide-menu">Dashboard</span>
              </a>
            </li>
            <?php
            $icons = [
              'chats' => 'bi-chat',
              'chat_messages' => 'bi-envelope',
              'chat_participants' => 'bi-person',
              'jenis_iuran' => 'bi-cash-coin',
              'pembayaran_iuran' => 'bi-credit-card',
              'ppob' => 'bi-coin',
              'transactions' => 'bi-wallet',
              'menu_items' => 'bi-list',
              'setting' => 'bi-sliders',
              'users' => 'bi-person-fill',

            ];

            $menuSections = [
              'Message' => ['chats', 'chat_messages', 'chat_participants'],
              'Koperasi' => ['jenis_iuran', 'pembayaran_iuran', 'ppob', 'transactions'],
              'Admin' => ['menu_items', 'setting', 'users']
            ];
            $firstTable = true; // Menandai menu pertama agar aktif saat pertama kali dibuka

            foreach ($menuSections as $sectionName => $tables): ?>
              <li class="nav-small-cap">
                <iconify-icon icon="solar:menu-dots-bold" class="nav-small-cap-icon fs-4"></iconify-icon>
                <span class="hide-menu"><?php echo $sectionName; ?></span>
              </li>

              <?php foreach ($tables as $table):
                $tableLabel = ucwords(str_replace('_', ' ', $table));
                $icon = $icons[$table] ?? 'bi-question-circle';
              ?>
                <li class="sidebar-item">
                  <a class="sidebar-link tab-link <?php echo $firstTable ? 'active' : ''; ?>" data-bs-toggle="tab" href="#<?php echo $table; ?>">
                    <iconify-icon icon="<?php echo $icon; ?>" class="aside-icon"></iconify-icon>
                    <span class="hide-menu"><?php echo $tableLabel; ?></span>
                  </a>
                </li>

                <?php $firstTable = false; ?>
              <?php endforeach; ?>
              <?php
              if ($sectionName == "Koperasi"): ?>
                <li class="sidebar-item">
                  <a class="sidebar-link" href="deposit">
                    <iconify-icon icon="bi-piggy-bank" class="aside-icon"></iconify-icon>
                    <span class="hide-menu">Deposit</span>
                  </a>
                </li>
                <li class="sidebar-item">
                  <a class="sidebar-link" href="withdraw">
                    <iconify-icon icon="mdi:wallet-outline" class="aside-icon"></iconify-icon>
                    <span class="hide-menu">Withdraw</span>
                  </a>
                </li>

              <?php endif; ?>
            <?php endforeach; ?>

            <li class="nav-small-cap">
              <iconify-icon icon="solar:menu-dots-bold" class="nav-small-cap-icon fs-4"></iconify-icon>
              <span class="hide-menu">Laporan</span>
            </li>
            <li class="sidebar-item">
              <a class="sidebar-link" href="unpaid_users">
                <iconify-icon icon="bi-exclamation-circle" class="aside-icon"></iconify-icon>
                <span class="hide-menu">Anggota belum bayar iuran</span>
              </a>
            </li>
            
            <!-- Market Place -->
                        <li class="nav-small-cap">
                            <iconify-icon icon="solar:menu-dots-bold" class="nav-small-cap-icon fs-4"></iconify-icon>
                            <span class="hide-menu">MarketPlace</span>
                        </li>

                        <li class="sidebar-item">
                            <a class="sidebar-link" href="kategori_barang">
                                <iconify-icon icon="ph:tag-duotone" class="aside-icon"></iconify-icon>
                                <span class="hide-menu">Kategori Barang</span>
                            </a>
                        </li>

                        <li class="sidebar-item">
                            <a class="sidebar-link" href="produk">
                                <iconify-icon icon="ph:package-duotone" class="aside-icon"></iconify-icon>
                                <span class="hide-menu">Produk</span>
                            </a>
                        </li>

                        <li class="sidebar-item">
                            <a class="sidebar-link" href="history_belanja">
                                <iconify-icon icon="ph:shopping-cart-duotone" class="aside-icon"></iconify-icon>
                                <span class="hide-menu">Histori Belanja</span>
                            </a>
                        </li>

            <li class="nav-small-cap">
              <iconify-icon icon="solar:menu-dots-bold" class="nav-small-cap-icon fs-4"></iconify-icon>
              <span class="hide-menu">Blogs</span>
            </li>
            <li class="sidebar-item">
              <a class="sidebar-link" href="kategori">
                <iconify-icon icon="bi-bookmark" class="aside-icon"></iconify-icon>
                <span class="hide-menu">Kategori</span>
              </a>
            </li>
            <li class="sidebar-item">
              <a class="sidebar-link" href="post">
                <iconify-icon icon="bi-file-earmark-text" class="aside-icon"></iconify-icon>
                <span class="hide-menu">Post</span>
              </a>
            </li>
            <li class="sidebar-item">
              <a class="sidebar-link" href="comment">
                <iconify-icon icon="bi-chat-text" class="aside-icon"></iconify-icon>
                <span class="hide-menu">Comment</span>
              </a>
            </li>
            <li class="nav-small-cap">
              <iconify-icon icon="solar:menu-dots-bold" class="nav-small-cap-icon fs-4"></iconify-icon>
              <span class="hide-menu">Website Profile</span>
            </li>

            <li class="sidebar-item">
              <a class="sidebar-link" href="tim">
                <iconify-icon icon="mdi:account-group" class="aside-icon"></iconify-icon>
                <span class="hide-menu">Tim</span>
              </a>
            </li>

            <li class="sidebar-item">
              <a class="sidebar-link" href="galeri">
                <iconify-icon icon="mdi:image-multiple" class="aside-icon"></iconify-icon>
                <span class="hide-menu">Galeri</span>
              </a>
            </li>


          </ul>



        </nav>

        <!-- End Sidebar scroll-->
      </div>
    </aside>
    <!--  Sidebar End -->
    <div class="page-wrapper">
      <!--  Header Start -->
      <header class="topbar rounded-0 border-0 bg-primary">
        <div class="with-vertical"><!-- ---------------------------------- -->
          <!-- Start Vertical Layout Header -->
          <!-- ---------------------------------- -->
          <nav class="navbar navbar-expand-lg px-lg-0 px-3 py-0">
            <div class="d-none d-lg-block">
              <div class="brand-logo d-flex align-items-center justify-content-between">
                <a href="home" class="text-nowrap logo-img d-flex align-items-center gap-2">
                  <b class="logo-icon">
                    <!--You can put here icon as well // <i class="wi wi-sunset"></i> //-->
                    <!-- Dark Logo icon -->
                    <img src="assets/images/logos/logo.png" alt="homepage" class="dark-logo" style="width: 5vw; max-width: 50px; min-width: 50px;" />
                    <!-- Light Logo icon -->
                    <img src="assets/images/logos/logo.png" alt="homepage" class="light-logo" style="width: 5vw; max-width: 50px; min-width: 50px;" />
                  </b>
                  <!--End Logo icon -->
                  <!-- Logo text -->

                </a>
              </div>
            </div>

            <ul class="navbar-nav gap-2">

              <li class="nav-item nav-icon-hover-bg rounded-circle">
                <a class="nav-link nav-icon-hover sidebartoggler" id="headerCollapse" href="javascript:void(0)">
                  <iconify-icon icon="solar:list-bold"></iconify-icon>
                </a>
              </li>
            </ul>

            <div class="d-block d-lg-none">
              <div class="brand-logo d-flex align-items-center justify-content-between">
                <a href="../main/index.html" class="text-nowrap logo-img d-flex align-items-center gap-2">
                  <b class="logo-icon">
                    <!--You can put here icon as well // <i class="wi wi-sunset"></i> //-->
                    <!-- Dark Logo icon -->
                    <img src="assets/images/logos/logo.png" alt="homepage" class="dark-logo" style="width: 5vw; max-width: 50px; min-width: 50px;" />
                    <!-- Light Logo icon -->
                    <img src="assets/images/logos/logo.png" alt="homepage" class="light-logo" style="width: 5vw; max-width: 50px; min-width: 50px;" />
                  </b>
                  <!--End Logo icon -->
                  <!-- Logo text -->

                </a>
              </div>
            </div>
            <ul class="navbar-nav flex-row  gap-2 align-items-center justify-content-center d-flex d-lg-none">
              <li class="nav-item dropdown nav-icon-hover-bg rounded-circle">
                <a class="navbar-toggler nav-link text-white nav-icon-hover border-0" href="javascript:void(0)" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                  <span class="">
                    <i class="ti ti-dots fs-7"></i>
                  </span>
                </a>
              </li>
            </ul>
            <div class="collapse navbar-collapse justify-content-end" id="navbarNav">
              <div class="d-flex align-items-center justify-content-between py-2 py-lg-0">

                <ul class="navbar-nav gap-2 flex-row ms-auto align-items-center justify-content-center">

                  <li class="nav-item nav-icon-hover-bg rounded-circle">
                    <a class="nav-link nav-icon-hover moon dark-layout" href="javascript:void(0)">
                      <iconify-icon icon="solar:moon-line-duotone" class="moon"></iconify-icon>
                    </a>
                    <a class="nav-link nav-icon-hover sun light-layout" href="javascript:void(0)">
                      <iconify-icon icon="solar:sun-2-line-duotone" class="sun"></iconify-icon>
                    </a>
                  </li>

                  <!-- ------------------------------- -->
                  <!-- start profile Dropdown -->
                  <!-- ------------------------------- -->
                  <li class="nav-item hover-dd dropdown">
                    <a class="nav-link nav-icon-hover" href="javascript:void(0)" id="drop2" aria-expanded="false">
                      <img src="assets/images/profile/user-1.jpg" alt="user" class="profile-pic rounded-circle round-30" />
                    </a>
                    <div class="dropdown-menu pt-0 content-dd overflow-hidden pt-0 dropdown-menu-end user-dd" aria-labelledby="drop2">
                      <div class="profile-dropdown position-relative" data-simplebar>
                        <div class=" py-3 border-bottom">
                          <div class="d-flex align-items-center px-3">
                            <img src="assets/images/profile/user-1.jpg" class="rounded-circle round-50" alt="" />
                            <div class="ms-3">
                              <h5 class="mb-1 fs-4"><?= $_SESSION['admin_username'] ?></h5>
                              <p class="mb-0 fs-2 d-flex align-items-center text-muted">
                                <?= $_SESSION['admin_username'] ?>
                              </p>
                            </div>
                          </div>
                        </div>
                        <div class="message-body pb-3">


                          <div class="px-3">
                            <div class="py-8 px-3 d-flex justify-content-between dropdown-item align-items-center h6 mb-0  rounded-2 link">
                              <a href="javascript:void(0)" class="">
                                Mode
                              </a>
                              <div>
                                <a class="moon dark-layout" href="javascript:void(0)">
                                  <iconify-icon icon="solar:moon-line-duotone" class="moon"></iconify-icon>
                                </a>
                                <a class="sun light-layout" href="javascript:void(0)">
                                  <iconify-icon icon="solar:sun-2-line-duotone" class="sun"></iconify-icon>
                                </a>
                              </div>
                            </div>

                            <div class="h6 mb-0 dropdown-item py-8 px-3 rounded-2 link">
                              <a href="logout" class=" d-flex  align-items-center ">
                                Sign Out
                              </a>
                            </div>
                          </div>
                        </div>

                      </div>
                    </div>
                  </li>


                  <!-- ------------------------------- -->
                  <!-- end profile Dropdown -->
                  <!-- ------------------------------- -->
                </ul>
              </div>
            </div>
          </nav>
          <!-- ---------------------------------- -->
          <!-- End Vertical Layout Header -->
          <!-- ---------------------------------- -->

          <!-- ------------------------------- -->
          <!-- apps Dropdown in Small screen -->
          <!-- ------------------------------- -->
          <!--  Mobilenavbar -->
          <div class="offcanvas offcanvas-start" data-bs-scroll="true" tabindex="-1" id="mobilenavbar" aria-labelledby="offcanvasWithBothOptionsLabel">
            <nav class="sidebar-nav scroll-sidebar">
              <div class="offcanvas-header justify-content-between">
                <a href="../main/index.html" class="text-nowrap logo-img d-block">
                  <b class="logo-icon">
                    <!--You can put here icon as well // <i class="wi wi-sunset"></i> //-->
                    <!-- Dark Logo icon -->
                    <img src="assets/images/logos/logo.png" alt="homepage">
                  </b>
                  <!--End Logo icon -->
                  <!-- Logo text -->

                </a>
                <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
              </div>

            </nav>
          </div>
        </div>
        <div class="app-header with-horizontal">
          <nav class="navbar navbar-expand-xl container-fluid">
            <ul class="navbar-nav gap-2 align-items-center">
              <li class="nav-item d-block d-xl-none">
                <a class="nav-link sidebartoggler ms-n3" id="sidebarCollapse" href="javascript:void(0)">
                  <iconify-icon icon="solar:hamburger-menu-line-duotone"></iconify-icon>
                </a>
              </li>
              <li class="nav-item d-none d-xl-block">
                <div class="brand-logo d-flex align-items-center justify-content-between">
                  <a href="../main/index.html" class="text-nowrap logo-img d-flex align-items-center gap-2">
                    <b class="logo-icon">
                      <!--You can put here icon as well // <i class="wi wi-sunset"></i> //-->
                      <!-- Dark Logo icon -->
                      <img src="assets/images/logos/logo.png" alt="homepage" class="dark-logo" />
                      <!-- Light Logo icon -->
                      <img src="assets/images/logos/logo.png" alt="homepage" class="light-logo" />
                    </b>
                    <!--End Logo icon -->

                  </a>
                </div>
              </li>

              <li class="nav-item d-none d-lg-block search-box">
                <a class="nav-link nav-icon-hover d-none d-md-flex waves-effect waves-dark" href="javascript:void(0)" data-bs-toggle="modal" data-bs-target="#exampleModal">
                  <iconify-icon icon="solar:magnifer-linear"></iconify-icon>
                </a>
              </li>


            </ul>


            <a class="navbar-toggler nav-icon-hover p-0 border-0 text-white" href="javascript:void(0)" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
              <span class="p-2">
                <i class="ti ti-dots fs-7"></i>
              </span>
            </a>
            <div class="collapse navbar-collapse justify-content-end" id="navbarNav">
              <div class="d-flex align-items-center justify-content-between">
                <ul class="navbar-nav gap-2 flex-row ms-auto align-items-center justify-content-center">





                  <!-- ------------------------------- -->
                  <!-- start profile Dropdown -->
                  <!-- ------------------------------- -->
                  <li class="nav-item hover-dd dropdown">
                    <a class="nav-link nav-icon-hover" href="javascript:void(0)" id="drop2" aria-expanded="false">
                      <img src="assets/images/profile/user-1.jpg" alt="user" width="30" class="profile-pic rounded-circle" />
                    </a>
                    <div class="dropdown-menu pt-0 content-dd overflow-hidden pt-0 dropdown-menu-end user-dd" aria-labelledby="drop2">
                      <div class="profile-dropdown position-relative" data-simplebar>
                        <div class=" py-3 border-bottom">
                          <div class="d-flex align-items-center px-3">
                            <img src="assets/images/profile/user-1.jpg" class="rounded-circle round-50" alt="" />
                            <div class="ms-3">
                              <h5 class="mb-1 fs-4"><?= $_SESSION['admin_username'] ?></h5>
                              <p class="mb-0 fs-2 d-flex align-items-center text-muted">
                                <?= $_SESSION['admin_username'] ?>
                              </p>
                            </div>
                          </div>
                        </div>
                        <div class="message-body pb-3">

                          <div class="px-3">
                            <div class="py-8 px-3 d-flex justify-content-between dropdown-item align-items-center h6 mb-0  rounded-2 link">
                              <a href="javascript:void(0)" class="">
                                Mode
                              </a>
                              <div>
                                <a class="moon dark-layout" href="javascript:void(0)">
                                  <iconify-icon icon="solar:moon-line-duotone" class="moon"></iconify-icon>
                                </a>
                                <a class="sun light-layout" href="javascript:void(0)">
                                  <iconify-icon icon="solar:sun-2-line-duotone" class="sun"></iconify-icon>
                                </a>
                              </div>
                            </div>

                            <div class="h6 mb-0 dropdown-item py-8 px-3 rounded-2 link">
                              <a href="../main/authentication-login.html" class=" d-flex  align-items-center ">
                                Sign Out
                              </a>
                            </div>
                          </div>
                        </div>

                      </div>
                    </div>
                  </li>

                  <!-- ------------------------------- -->
                  <!-- end profile Dropdown -->
                  <!-- ------------------------------- -->
                </ul>
              </div>
            </div>
          </nav>
        </div>
      </header>
      <!--  Header End -->



      <div class="body-wrapper">
        <div class="container-fluid">
          <div class="font-weight-medium shadow-none position-relative overflow-hidden mb-7">
            <div class="card-body px-0">
              <div class="d-flex justify-content-between align-items-center">
                <div>
                  <h4 class="font-weight-medium  mb-0">FormData</h4>
                  <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                      <li class="breadcrumb-item">
                        <a class="text-muted text-decoration-none" href="">Home
                        </a>
                      </li>
                      <li class="breadcrumb-item text-muted" aria-current="page">Form Data</li>
                    </ol>
                  </nav>
                </div>

              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-12">
              <div class="card">
                <div class="card-body">
                  <h4 class="card-title mb-3">Tambah Data - <?php echo ucwords(str_replace('_', ' ', $_GET["table"])); ?></h4>


                  <form action="" method="post">
                    <input type="hidden" name="table" value="<?php echo $table; ?>">
                    <input type="hidden" name="created_at" value="<?php echo date('Y-m-d H:i:s'); ?>">

                    <div class="row">
                      <?php foreach ($columns as $column):
                        $fieldName = $column['Field'];
                        $fieldType = $column['Type'];
                        $isAutoIncrement = strpos($column['Extra'], 'auto_increment') !== false;

                        // Sembunyikan field auto_increment dan created_at
                        if ($isAutoIncrement || strtolower($fieldName) === 'created_at' || in_array(strtolower($fieldName), HIDDEN_COLUMNS)) {
                          continue;
                        }
                      ?>

                        <div class="col-md-6">
                          <div class="form-floating mb-3">
                            <?php if ($_GET["table"] === 'jenis_iuran' && $fieldName === 'periode'): ?>
                              <!-- Periode hanya untuk jenis_iuran -->
                              <select name="periode" id="periode" class="form-control">
                                <option value="">Pilih Periode</option>
                                <?php for ($i = 1; $i <= 12; $i++): ?>
                                  <option value="<?php echo $i; ?>"><?php echo $i . " Bulan"; ?></option>
                                <?php endfor; ?>
                              </select>
                              <label>Periode</label>

                            <?php elseif ($_GET["table"] === 'pages' && $fieldName === 'content'): ?>
                              <!-- CKEditor untuk field content -->
                              <textarea name="content" id="ckeditor" class="form-control"><?php echo isset($_POST['content']) ? $_POST['content'] : ''; ?></textarea>
                              <label>Content</label>

                            <?php elseif (strpos($fieldType, 'enum') !== false): ?>
                              <!-- Enum Field -->
                              <?php
                              preg_match_all("/'(.*?)'/", $fieldType, $enumValues);
                              ?>
                              <select name="<?php echo $fieldName; ?>" class="form-control">
                                <?php foreach ($enumValues[1] as $value): ?>
                                  <option value="<?php echo $value; ?>"><?php echo $value; ?></option>
                                <?php endforeach; ?>
                              </select>
                              <label><?php echo ucwords(str_replace('_', ' ', $fieldName)); ?></label>

                            <?php else: ?>
                              <!-- Default Input -->
                              <input type="text" name="<?php echo $fieldName; ?>" class="form-control" placeholder="<?php echo ucwords(str_replace('_', ' ', $fieldName)); ?>">
                              <label><?php echo ucwords(str_replace('_', ' ', $fieldName)); ?></label>
                            <?php endif; ?>
                          </div>
                        </div>
                      <?php endforeach; ?>

                      <!-- Tambahkan Start Periode jika tabel adalah 'jenis_iuran' -->
                      <?php if ($_GET["table"] === 'jenis_iuran'): ?>
                        <div class="col-md-6">
                          <div class="form-floating mb-3">
                            <input type="date" name="start_periode" id="start_periode" class="form-control">
                            <label>Start Periode</label>
                          </div>
                        </div>
                      <?php endif; ?>
                    </div>

                    <div class="d-flex align-items-center">
                      <button type="submit" class="btn btn-success me-2">
                        <i class="ti ti-send fs-4"></i>
                        Simpan
                      </button>
                      <a href="home" class="btn btn-danger">Batal</a>
                    </div>
                  </form>

                  <!-- Tambahkan CKEditor hanya jika tabelnya "pages" -->
                  <?php if ($_GET["table"] === 'pages'): ?>
                    <script src="https://cdn.ckeditor.com/4.18.0/standard/ckeditor.js"></script>
                    <script>
                      CKEDITOR.replace('ckeditor', {
                        height: 300
                      });
                    </script>
                  <?php endif; ?>


                </div>
              </div>

            </div>
          </div>

        </div>
      </div>
    </div>


  </div>

  <script src="assets/js/vendor.min.js"></script>
  <!-- Import Js Files -->
  <script src="assets/js/breadcrumb/breadcrumbChart.js"></script>
  <script src="assets/libs/apexcharts/dist/apexcharts.min.js"></script>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="assets/libs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
  <script src="assets/libs/simplebar/dist/simplebar.min.js"></script>
  <script src="assets/js/theme/app.init.js"></script>
  <script src="assets/js/theme/theme.js"></script>
  <script src="assets/js/theme/app.min.js"></script>
  <script src="assets/js/theme/sidebarmenu.js"></script>
  <script src="assets/js/theme/feather.min.js"></script>

  <!-- solar icons -->
  <script src="https://cdn.jsdelivr.net/npm/iconify-icon@1.0.8/dist/iconify-icon.min.js"></script>
  <script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
  <script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
  <script>
    document.addEventListener("DOMContentLoaded", function() {
      document.querySelector("form").addEventListener("submit", function(e) {
        let periode = document.getElementById("periode")?.value;
        let startPeriode = document.getElementById("start_periode")?.value;

        if (periode && startPeriode) {
          let dates = [];
          let startDate = new Date(startPeriode);

          for (let i = 0; i < periode; i++) {
            let newDate = new Date(startDate);
            newDate.setMonth(newDate.getMonth() + i);
            dates.push(newDate.toISOString().split("T")[0]);
          }

          console.log("Generated Periode Dates:", dates);
          // Jika perlu, bisa dikirim ke backend dengan input hidden
        }
      });
    });
  </script>

</body>

</html>