<?php
include_once 'src/inc/db/function.php';
include_once 'createToken.php';
include_once 'sendEmail.php';

$error = '';
if (isset($_REQUEST['action']) && !empty($_REQUEST['action'])) {
  if ($_POST['action'] == 'forget') {

    /*** DATA ***/
    $account = isset($_POST['account']) && $_POST['account'] !== '' ? htmlspecialchars(trim($_POST['account']), ENT_QUOTES, 'UTF-8') : null;
    $email = isset($_POST['email']) && $_POST['email'] !== '' ? htmlspecialchars(trim($_POST['email']), ENT_QUOTES, 'UTF-8') : null;
    $sql = "Select user_id, email FROM users WHERE userNo = ? AND email = ? ";
    $stmt = $conn->prepare($sql);
    $stmt->execute([$account, $email]);
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    if ($result) {
      $Npassword = getToken(8);
      $vol = send_email_regist($C_email = $result['email'], $mgo = $Npassword);
      if ($vol) {
        $mhsah1 = password_hash($Npassword, PASSWORD_DEFAULT);
        $fieldarray = array(
          'userPassword' => $mhsah1,
          'update_date' => $now,
          'updated_by' => 'forget'
        );
        $rid = $result['user_id'];
        $where_claud = "user_id= $rid";
        $n_rid = update_data("users", $fieldarray, $where_claud);
        if ($n_rid !== 0) {
          $error = '登入密碼已寄至您的信箱，請重新登入！';
        } else {
          $error = 'Email發送失敗！';
        }
      }
    } else {
      $error = '帳號或電子信箱輸入錯誤！';
    }

  } else {

    $error = '請重新輸入！';

  }
}

?>
<!DOCTYPE html>
<html lang="zh-TW">

<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/style.css" rel="stylesheet">
    <script src="assets/js/jquery-3.7.1.js"></script>
    <script>
      window.onload = function() {
        // 检查 PHP 变量是否包含错误消息
        var errorMessage = "<?php echo $error; ?>";
        // 如果错误消息不为空，则显示警报消息
        if (errorMessage !== '') {
          if (errorMessage === '登入密碼已寄至您的信箱，請重新登入！') {
            // 按下確定後跳轉到登入頁面
            window.location.href = 'login.php';
          } else {
            alert(errorMessage);
          }
        }
      };
    </script>
    <style>
        body {
            background-color: #f5faed;
        }

        .container {
            display: flex;
            min-height: 100vh;
            flex-direction: column;
            justify-content: space-between;
        }

        .main-content {
            display: flex;
            justify-content: center;
            align-items: center;
            flex-grow: 1;
        }

        .login-form {
            background-color: #f8f9fa;
            padding: 20px;
            border-radius: 10px;
            width: 100%;
            max-width: 400px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>

<body>
    <div class="container">
      <div class="main-content">
        <form method="POST" action="" class="mt-4 mb-4" id="form1" enctype="multipart/form-data">
          <div class="login-form">
            <input type="hidden" name="csrfToken" value="<?php echo htmlspecialchars($_SESSION['csrfToken'], ENT_QUOTES, 'UTF-8'); ?>">
            <h2 class="mb-3 fw-normal text-center" style="color: #323e35;">忘記密碼</h2>
            <div class="row mb-2">
              <label class="form-label">帳號</label>
              <div class="col">
                <input type="text" class="form-control" id="account" name="account" placeholder="帳號" required>
              </div>
            </div>
            <div class="row mb-2">
              <label class="form-label">Email</label>
              <div class="col">
                <input type="email" class="form-control" id="email" name="email" placeholder="Email" required>
              </div>
            </div>
            <br>
            <br>
            <div class="row text-center">
              <div class="col-6">
                <a class="btn btn-secondary w-100" id="cancelButton" href="login.php" role="button">取消</a>
              </div>
              <div class="col-6">
                <button type="submit" id="saveButton" name="action" value="forget" class="btn btn-primary w-100" >送出</button>
              </div>
            </div>
          </div>
        </form>
      </div>
    </div>

    <footer id="footer" style="background-color: #f1f1f1; font-size: 12px; padding: 10px 0;">  
        <div class="row">
            <!-- 左側內容 -->
            <div class="col-12 col-md-4 mb-3 mb-md-0" style="text-align: left;">
                <br>
                <div style="font-weight: bold; margin-bottom: 5px;">
                    海洋能及負碳高速運算資料管理平臺
                </div>
                <div>指導單位：</div>
                <div>執行單位：</div>
            </div>
            <!-- 中間內容 -->
            <div class="col-12 col-md-4 mb-3 mb-md-0">
                <br>
                <br>
                <div style="text-align: left; padding-left: 10px; border-left: 1px dashed #000;">
                    <div>聯絡單位：</div>
                    <div>聯絡信箱：</div>
                    <div>執行電話：</div>
                </div>
            </div>
            <!-- 右側內容 -->
            <div class="col-12 col-md-4">
                <div class="row mb-3">
                    <div class="col-3">
                        <button>平台簡介</button>
                    </div>
                    <div class="col-3">
                        <button>執行團隊</button>
                    </div>
                    <div class="col-5">
                        <a>隱私權保護</a>
                        <a>網站安全政策</a>
                    </div>
                </div>
                <br>
                <br>
                <div>© 2024 財團國家實驗研究院國家高速網路與計算中心</div>
            </div>
        </div>
    </footer>  




</body>

</html>