<?php

if ($_SERVER["REQUEST_METHOD"] === "POST") {
  // 驗證 CSRF token
  if (!isset($_POST['csrfToken']) || $_POST['csrfToken'] !== $_SESSION['csrfToken']) {
    header("Location: login.php");
    exit();
  }
}

$member_id = isset($_POST['member_id']) && $_POST['member_id'] !== '' ? htmlspecialchars(trim($_POST['member_id']), ENT_QUOTES, 'UTF-8') : null;
$username = isset($_POST['username']) && $_POST['username'] !== '' ? htmlspecialchars(trim($_POST['username']), ENT_QUOTES, 'UTF-8') : null;
$startTime = isset($_POST['startTime']) && $_POST['startTime'] !== '' ? htmlspecialchars(trim($_POST['startTime']), ENT_QUOTES, 'UTF-8') : null;
$endTime = isset($_POST['endTime']) && $_POST['endTime'] !== '' ? htmlspecialchars(trim($_POST['endTime']), ENT_QUOTES, 'UTF-8') : null;

// $member_id = checkData($member_id, "number");
// $username = checkData($username, "string");
// $startTime = checkData($startTime, "date");
// $endTime = checkData($endTime, "date");

$fieldarray = array(
  'member_id' => $member_id,
  'username' => $username,
  'startTime' => $startTime,
  'endTime' => $endTime
);

$orderarray = array(
  'registration_date' => 'registration_date',
  'member_id' => 'member_id',
  'username' => 'username',
);
$result = [];
// $result = select_data("members", $fieldarray, $orderarray, 'registration_date', 'registration_date');

?>
<!DOCTYPE html>

<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">

  <title>會員資料</title>
  <meta content="" name="description">
  <meta content="" name="keywords">

  <!-- Favicons -->
  <link href="assets/img/favicon.png" rel="icon">
  <link href="assets/img/apple-touch-icon.png" rel="apple-touch-icon">

  <!-- Google Fonts -->
  <link href="fonts/fonts.css" rel="stylesheet">
  <!-- Vendor CSS Files -->
  <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
 

  <!-- Template Main CSS File -->
  <link href="assets/css/jquery-ui.css" rel="stylesheet">
  <link href="assets/css/style.css" rel="stylesheet">
  <script src="assets/js/jquery-3.7.1.js"></script>
  <script src="assets/js/jquery-ui.js"></script>

  <script>
    $(document).ready(function() {

      $("#UserAccount-nav").addClass("show");
      $("#userData").addClass("active");
      
    });
  </script>

</head>

<body>

  <!--BEGIN HEADER-->
  <?php include_once "template_header.php";?>
  <!--END HEADER-->

  <!--BEGIN SIDEBAR-->
  <?php include_once "template_sidebar.php";?>
  <!--END SIDEBAR-->

  <main id="main" class="main">

    <div class="pagetitle">
      <h1>會員資料</h1>
      <!--BEGIN BREADCRUMB-->
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="index.php">首頁</a></li>
          <li class="breadcrumb-item active">會員資料</li>
        </ol>
      </nav>
      <!--END BREADCRUMB-->
    </div><!-- End Page Title -->

    <section class="section">
      <!-- BRGIN SEARCHTABLE -->
      <div class="card info-card sales-card">
        <div class="card-body">
          <form method="POST" action="" class="mt-4 mb-4">
            <input type="hidden" name="csrfToken" value="<?php echo htmlspecialchars($_SESSION['csrfToken'], ENT_QUOTES, 'UTF-8'); ?>">
            <div class="row mb-3">
              <div class="col-md-2">會員編號：</div>
              <div class="col-md-2">帳號：</div>
              <div class="col-md-2">開始時間：</div>
              <div class="col-md-2">結束時間：</div>
              <div class="col-md-2"></div>
            </div>
            <div class="row mb-3">
              <div class="form-group col-md-2">
                <input type="text" class="form-control" placeholder="" id="member_id" name="member_id" value="<?php echo isset($member_id) ? htmlspecialchars($member_id, ENT_QUOTES, 'UTF-8') : ''; ?>">
              </div>
              <div class="form-group col-md-2">
                <input type="text" class="form-control" placeholder="" id="username" name="username" value="<?php echo isset($username) ? htmlspecialchars($username, ENT_QUOTES, 'UTF-8') : ''; ?>">
              </div>
              <div class="form-group col-md-2">
                <input type="date" class="form-control" placeholder="" id="startTime" name="startTime" value="<?php echo isset($startTime) ? htmlspecialchars($startTime, ENT_QUOTES, 'UTF-8') : ''; ?>">
              </div>
              <div class="form-group col-md-2">
                <input type="date" class="form-control" placeholder="" id="endTime" name="endTime" value="<?php echo isset($endTime) ? htmlspecialchars($endTime, ENT_QUOTES, 'UTF-8') : ''; ?>">
              </div>
              <div class="form-group col-md-2">
                <button type="submit" class="btn btn-primary">
                  查詢
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- END SEARCHTABLE -->

      <!-- BRGIN DATATABLE -->
      <table class="table table-hover table-responsive table-bordered">
        <thead>
          <tr class="table-primary">
            <th scope="col">
              <button type="button" class="btn btn-primary" onclick="changeToEdit(-1)">
                <i class="bi bi-plus-lg"></i>    
              </button>
            </th>
            <th scope="col" class="align-middle">會員編號</th>
            <th scope="col" class="align-middle">帳號</th>
            <th scope="col" class="align-middle">註冊日期</th>
            <th scope="col" class="align-middle">會員狀態</th>
          </tr>
        </thead>

        <tbody>
          
          <?php

            foreach ($result as $row) {

          ?>
a
          <tr>
            <td class="align-middle">
              <button type="button" class="btn btn-primary" onclick="changeToEdit(<?= $row['rid'] ?>)">
                <i class="bi bi-pencil-square"></i>
              </button>
            </td>
            <td class="align-middle"><?php echo isset($row['member_id']) ? htmlspecialchars($row['member_id'], ENT_QUOTES, 'UTF-8') : ''; ?></td>
            <td class="align-middle"><?php echo isset($row['username']) ? htmlspecialchars($row['username'], ENT_QUOTES, 'UTF-8') : ''; ?></td>
            <td class="align-middle"><?php echo isset($row['registration_date']) ? htmlspecialchars(date('Y/m/d H:i', strtotime($row['registration_date'])), ENT_QUOTES, 'UTF-8') : ''; ?></td>
            <td class="align-middle"><?php echo isset($row['status']) ? htmlspecialchars($row['status'], ENT_QUOTES, 'UTF-8') : ''; ?></td>
          </tr>

          <?php

          }

          ?>

        </tbody>
      </table>
      <!-- END DATATABLE -->
    </section>

  </main><!-- End #main -->

  <!--BEGIN FOOTER-->
  <?php include_once "template_footer.php";?>
  <!--END FOOTER-->

  <a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>

  <!-- Vendor JS Files -->
 
  <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="assets/vendor/tinymce/tinymce.min.js"></script>

  <!-- Template Main JS File -->
  <script src="assets/js/main.js"></script>

  <script>

    function changeToEdit (id) {

      window.location.href = "userDataEdit.php?id=" + id;

    }
    
  </script>

</body>

</html>
