<?php
include "../components/functions.php";
include "../components/smsPanel.php";


//todo fix multiple sends

$phone = "";
$error = "";
if (isset($_GET["mobile"]) && isset($_GET["code"])) {
    $phone = $_GET["mobile"];
    if (checkValidationCode($_GET["mobile"], $_GET["code"])) {
        $id = createNewUser($phone);
        echo $id;
        if($id!=0){
            session_start();
            $_SESSION["id"]=$id;
            header("location:info.php");
        }else{
            $error = 'کد نامعتبر است';
        }
    } else {
        $error = 'کد نامعتبر است';
    }
} else if (isset($_GET["mobile"])) {
    $phone = $_GET["mobile"];
    sendCode($phone);
} else {
    header("location:login.php");
}
?>
<!DOCTYPE>
<html>
<head>
    <?php
    echo css_include();
    echo script_include();
    ?>
    <script>
        var counter = 60;
        var interval = setInterval(function () {
            counter--;
            // Display 'counter' wherever you want to display it.
            if (counter < 0) {
                clearInterval(interval);
                document.getElementById("time").outerHTML = "<a href=\"#\" target=\"_self\">کدی برای من ارسال نشده است</a>";
                return;
            } else {
                document.getElementById("time").innerHTML = counter.toString(10);
            }
        }, 1000);


    </script>

</head>
<style>
    #time {
        color: green;
    }

    .center {
        display: block;
        margin-left: auto;
        margin-right: auto;
        width: 50%;
    }
</style>
<body>
<div class="container" id="login_container">
    <div class="flex-container">
        <form class="border" id="form_border">
            <div class="form-group py-4">
                <span class="login_title py-3" style="font-family:'B Titr';font-size: larger">تلفن همراه خود را تایید کنید</span>
            </div>
            <div class="form-group">
                <img src="https://assets.dryicons.com/uploads/icon/svg/6030/05a807e5-191c-4842-84a0-71d2e9442d36.svg"
                     style="max-width: 100px;" class="center">
            </div>
            <div class="form-group">
                <p style="text-align: center"><?php echo fa_number($phone) ?></p>
            </div>
            <div class="form-group" id="codeInput">
                <label for="mobile">کد پنج‌رقمی</label>
                <input type="text" class="form-control rounded-pill" id="mobile" aria-describedby="mobileErr"
                       placeholder="00000" maxlength="5" style="letter-spacing: 20px;text-align: center">
                <small id="mobileErr" style="color: red">
                    <?php
                    echo $error;
                    ?>
                </small>
            </div>
            <div class="form-group">
                <span id="dsc">لطفا کد ۵ رقمی ارسال شده به تلفن همراه خود را در فیلد بالا وارد کنید</span>
            </div>
            <div class="form-group">
                <p style="text-align: center">
                    <label id="time" data-number-target="fa">60</label>
                </p>
            </div>
            <div class="row">
                <button type="button"
                        class="btn btn-primary rounded-pill pb-2 col-5" id="submitButton" onclick="f()">تایید
                </button>
                <div class="col-2"></div>
                <button onclick="location.href='register.php';"
                        type="button" class="btn btn-primary rounded-pill col-5" id="button">تغییر شماره
                </button>
            </div>
        </form>
        <script src="script/ChangeNumbers.min.js"></script>
    </div>
</div>

</body>

<script>
    function f(e) {

        var v = document.getElementById("mobile").value;
        var code = document.getElementById("mobile").value;
        if (code.length === 5) {
            var params = "?mobile=<?php echo $phone?>&code=" + code;
            location.href = "verify.php" + params;
        } else {
            document.getElementById('mobileErr').innerHTML = 'کد نامعتبر است';
            return false;
        }


    }
</script>

</html>