<?php if (!defined('RESTRICTED')) { include ('./../../templates/error/404.php'); } else { ?>

<!DOCTYPE html>
<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="msapplication-tap-highlight" content="no">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <title>Account Deletion Request</title>
    <!-- Favicon-->
    <link rel="icon" href="assets/images/favicon.png" type="image/x-icon">

    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Roboto:400,700&subset=latin,cyrillic-ext" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" type="text/css">

    <!-- Bootstrap Core Css -->
    <link href="assets/plugins/bootstrap/css/bootstrap.css" rel="stylesheet">
    
</head>

<body class="container">

    <?php 

    include('includes/config.php');
    include('includes/strings.php');
    include('assets/css.min.php');
    include('functions.php');

    $sql = "SELECT account_deletion_message FROM tbl_settings ORDER BY id DESC LIMIT 1";
    $result = $connect->query($sql);
    $data = $result->fetch_assoc();

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

        $email = clean($_POST['email']);
        $password = clean($_POST['password']);
        $reason = clean($_POST['reason']);
        $date = clean($_POST['date']);

        $sql = "SELECT * FROM tbl_users WHERE email = '$email' AND password = '$password'";
        $result = mysqli_query($connect, $sql);
        if (mysqli_num_rows($result) > 0) {
            $data = array(
                'email'  => $email,
                'account_deletion_request' => '1',
                'account_deletion_reason' => $reason,
                'account_deletion_date' => $date
            );  

            $update = update('tbl_users', $data, "WHERE email = '$email'");

            if ($update > 0) {
                echo "<script>
                alert('Thank you for completing the form');
                window.location.href='page.php?account-deletion-request';
                </script>";
            }
        } else {
            echo "<script>
            alert('Whoops! Invalid email or password!');
            window.location.href='page.php?account-deletion-request';
            </script>";
        }

    }

    $date_time = date_create('now')->format('Y-m-d H:i:s');

    ?>

    <div class="container-fluid" style="margin-top: 20px;">

        <div class="row clearfix">
            <div class="">

                <form id="form_validation" method="post">
                    <div class="card corner-radius">
                        <div class="body">

                            <center>
                                <h2><?php echo $app_name; ?></h2>
                                <h1>Account Deletion Request</h1>
                            </center>
                            <br>
                            <p>
                                <?php echo $data['account_deletion_message']; ?>
                            </p>         

                            <div class="row clearfix">

                                <div class="form-group form-float col-sm-12">
                                    <div class="form-line">
                                        <div class="font-12">Email *</div>
                                        <input type="text" class="form-control" name="email" placeholder="Email" required>
                                    </div>
                                </div>

                                <div class="form-group form-float col-sm-12">
                                    <div class="form-line">
                                        <div class="font-12">Password *</div>
                                        <input type="password" class="form-control" name="password" placeholder="password" required>
                                    </div>
                                </div>

                                <div class="form-group form-float col-sm-12">
                                    <div class="form-line">
                                        <div class="font-12">Reason *</div>
                                        <textarea class="form-control" name="reason" class="form-control" placeholder="Describe your reason" required></textarea>
                                    </div>
                                </div>

                                <input type="hidden" class="form-control" name="date" value="<?php echo $date_time; ?>" required>

                                <div class="col-sm-12">
                                    <input type="checkbox" name="confirm" id="confirm" class="chkbox filled-in chk-col-blue" required />
                                    <label for="confirm"><?php echo $account_deletion_message; ?></label>
                                </div>

                                <div class="col-sm-12">
                                    <button class="button button-rounded waves-effect waves-float pull-right" type="submit" name="submit" id="submit">Request My Account Deletion</button>
                                </div>

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

            </div>
        </div>

    </div>

    <?php include_once ('assets/js.min.php'); ?>

    <script type="text/javascript">
        $('#submit').click(function () {
            if (!$('#confirm').is(':checked')) {
                alert('The acknowledgement box is not marked');
                return false;
            }
        });
    </script>

</body>

</html>

<?php } ?>