<?php

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

$member_name = isset($_POST['member_name']) && $_POST['member_name'] !== '' ? htmlspecialchars(trim($_POST['member_name']), ENT_QUOTES, 'UTF-8') : null;
$position = isset($_POST['position']) && $_POST['position'] !== '' ? htmlspecialchars(trim($_POST['position']), 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_name = checkData($member_name, "string");
// $position = checkData($position, "string");
// $startTime = checkData($startTime, "date");
// $endTime = checkData($endTime, "date");

$fieldarray = array(
  'member_name' => $member_name,
  'position' => $position,
  'startTime' => $startTime,
  'endTime' => $endTime
);

$orderarray = array(
  'join_date' => 'join_date',
  'member_name' => 'member_name',
  'position' => 'position',
);

// $result = select_data("execution_team", $fieldarray, $orderarray, 'join_date', 'join_date');
$result = []; 
?>
<!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() {

      $("#index-nav").addClass("show");
      $("#execution_team").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_name" name="member_name" value="<?php echo isset($member_name) ? htmlspecialchars($member_name, ENT_QUOTES, 'UTF-8') : ''; ?>">
              </div>
              <div class="form-group col-md-2">
                <input type="text" class="form-control" placeholder="" id="position" name="position" value="<?php echo isset($position) ? htmlspecialchars($position, 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['team_id'] ?>)">
                <i class="bi bi-pencil-square"></i>
              </button>
            </td>
            <td class="align-middle"><?php echo isset($row['member_name']) ? htmlspecialchars($row['member_name'], ENT_QUOTES, 'UTF-8') : ''; ?></td>
            <td class="align-middle"><?php echo isset($row['position']) ? htmlspecialchars($row['position'], ENT_QUOTES, 'UTF-8') : ''; ?></td>
            <td class="align-middle"><?php echo isset($row['email']) ? htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8') : ''; ?></td>
            <td class="align-middle"><?php echo isset($row['join_date']) ? htmlspecialchars(date('Y/m/d H:i', strtotime($row['join_date'])), 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 = "execution_teamEdit.php?id=" + id;

    }
    
  </script>

</body>

</html>
