<?php
session_start();
require_once '../models/User.php';
require_once '../include/DatabaseConn.php';
require_once '../include/Constants.php';
require_once '../models/Admin.php';

$admin = Admin::getCurrentAdmin();

if (is_null($admin)) {
    $url = $_SERVER['REQUEST_URI'];
    header("location: ./login?" . RETURN_URL . "=" . $url);
    die();
}

$db = new DatabaseConn();
$link = $db->connect();

if (!isset($_GET['email'])) {
    header("location: index");
    die();
}
$user = User::getCurrentUser($link, $_GET['email']);
if (is_null($user)) {
    header("location: index");
    die();
}
?>
<!DOCTYPE html>
<html lang="en">

    <head>
        <?php require_once './partials/head.php'; ?>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.1/animate.min.css" />
        <link href="../css/form.css" rel="stylesheet" type="text/css" />
        <link href="../css/snackbar.min.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
        <div id="app">
            <?php require_once './partials/top-header.php'; ?>
            <!--  BEGIN MAIN CONTAINER  -->
            <div class="main-container" id="container">
                <div class="overlay"></div>
                <div class="search-overlay"></div>
                <?php require_once './partials/side-bar.php'; ?>
                <!--  BEGIN CONTENT AREA  -->
                <div id="content" class="main-content">
                    <div class="layout-px-spacing">
                        <div class="row layout-top-spacing layout-spacing">
                            <div class="col-lg-11 offset-lg-1">
                                <div class="statbox widget box box-shadow">
                                    <div class="widget-content widget-content-area">
                                        <form method="post" action="edit-user?email=<?php echo ($_GET['email']); ?>">
                                            <?php if (isset($error)) { ?>
                                                <div class="alert alert-danger mb-4" role="alert">
                                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                                        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
                                                             viewBox="0 0 24 24" fill="none" stroke="currentColor"
                                                             stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
                                                             class="feather feather-x close" data-dismiss="alert">
                                                        <line x1="18" y1="6" x2="6" y2="18"></line>
                                                        <line x1="6" y1="6" x2="18" y2="18"></line>
                                                        </svg>
                                                    </button>
                                                    <strong>Error!</strong> <?php echo "$error"; ?>
                                                </div>
                                            <?php } ?>
                                            <div class="form-group row mb-4">
                                                <label class="col-xl-2 col-sm-3 col-sm-2 col-form-label">Full Name</label>
                                                <div class="col-xl-10 col-lg-9 col-sm-10">
                                                    <update url="v1/change-name" type="text" value="<?php
                                                    if (isset($_POST['name'])) {
                                                        echo ($_POST['name']);
                                                    } else {
                                                        echo ($user->getName());
                                                    }
                                                    ?>" id="<?php echo ($user->getId()); ?>" required='true'></update>
                                                </div>
                                            </div>
                                            <div class="form-group row mb-4">
                                                <label for="hPassword"
                                                       class="col-xl-2 col-sm-3 col-sm-2 col-form-label">Country</label>
                                                <div class="col-xl-10 col-lg-9 col-sm-10">
                                                    <select data-id='<?php echo ($user->getId()); ?>'
                                                            class='placeholder js-states form-control' required id="country"
                                                            name="country">
                                                                <?php include_once '../include/countries.php'; ?>
                                                    </select>

                                                </div>
                                            </div>

                                            <div class="form-group row mb-4">
                                                <label class="col-xl-2 col-sm-3 col-sm-2 col-form-label">Email</label>
                                                <div class="col-xl-10 col-lg-9 col-sm-10">
                                                    <update url="v1/change-email" type='email' value="<?php
                                                    if (isset($_POST['email'])) {
                                                        echo ($_POST['email']);
                                                    } else {
                                                        echo ($user->getEmail());
                                                    }
                                                    ?>" id="<?php echo ($user->getId()); ?>" required='true'></update>
                                                </div>
                                            </div>
                                            <div class="form-group row mb-4">
                                                <label for="hPassword"
                                                       class="col-xl-2 col-sm-3 col-sm-2 col-form-label">Phone</label>
                                                <div class="col-xl-10 col-lg-9 col-sm-10">
                                                    <update url="v1/change-phone" type="text" value="<?php
                                                    if (isset($_POST['phone'])) {
                                                        echo ($_POST['phone']);
                                                    } else {
                                                        echo ($user->getPhone());
                                                    }
                                                    ?>" id="<?php echo ($user->getId()); ?>" required="true"
                                                            placeholder=""> </update>
                                                </div>
                                            </div>
                                            <div class="form-group row mb-4">
                                                <label for="hPassword"
                                                       class="col-xl-2 col-sm-3 col-sm-2 col-form-label">Password</label>
                                                <div class="col-xl-10 col-lg-9 col-sm-10">
                                                    <update type="text" url="v1/change-password" value="<?php
                                                    if (isset($_POST['password'])) {
                                                        echo ($_POST['password']);
                                                    } else {
                                                        echo ($user->getPassword());
                                                    }
                                                    ?>" id="<?php echo ($user->getId()); ?>" required="true"
                                                            placeholder=""> </update>
                                                </div>
                                            </div>

                                            <div class="form-group row">
                                                <div class="col-sm-10">
                                                    <button type="button" onclick="window.location.href = 'index'"
                                                            class="btn cursor btn-outline-primary mt-3">Cancel</button>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php require_once './partials/footer.php'; ?>
                </div>
                <!--  END CONTENT AREA  -->
            </div>
        </div>
        <!-- END MAIN CONTAINER -->
        <?php require_once './partials/scripts.php'; ?>
        <script src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/select2.min.js"></script>
        <script src="../js/jqueryblockui.js" type="text/javascript"></script>
        <script src="../js/customblockui.js" type="text/javascript"></script>
        <script>
<?php if (isset($_POST['country'])) { ?>
                                                            var theValue = '<?php echo($_POST['country']); ?>';
                                                            var query = 'select#country option[value="' + theValue + '"]';
                                                            $(query).attr("selected", true);
<?php } else { ?>
                                                            var theValue = '<?php echo ($user->getCountry()); ?>';
                                                            var query = 'select#country option[value="' + theValue + '"]';
                                                            $(query).attr("selected", true);

<?php } ?>
                                                        $(".placeholder").select2({
                                                            placeholder: "Make a Selection"
                                                        });
        </script>
        <script src="../js/snackbar.min.js" type="text/javascript"></script>

        <script>
                                                        $('select#country').on('select2:select', function (e) {
                                                            // Do something
                                                            var data = e.params.data.text;
                                                            $.blockUI({
                                                                message: "<svg v-show='saving' xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round' class='feather feather-loader spin'><line x1='12' y1='2' x2='12' y2='6'></line><line x1='12' y1='18' x2='12' y2='22'></line><line x1='4.93' y1='4.93' x2='7.76' y2='7.76'></line><line x1='16.24' y1='16.24' x2='19.07' y2='19.07'></line><line x1='2' y1='12' x2='6' y2='12'></line><line x1='18' y1='12' x2='22' y2='12'></line><line x1='4.93' y1='19.07' x2='7.76' y2='16.24'></line><line x1='16.24' y1='7.76' x2='19.07' y2='4.93'></line></svg>",
                                                                fadeIn: 800,
                                                                overlayCSS: {
                                                                    backgroundColor: '#1b2024',
                                                                    opacity: 0.8,
                                                                    zIndex: 1200,
                                                                    cursor: 'wait'
                                                                },
                                                                css: {
                                                                    border: 0,
                                                                    color: '#fff',
                                                                    zIndex: 1201,
                                                                    padding: 0,
                                                                    backgroundColor: 'transparent'
                                                                }
                                                            });
                                                            axios.get('v1/change-country', {
                                                                params: {
                                                                    id: $(this).attr('data-id'),
                                                                    value: data
                                                                }
                                                            }).then(response => {
                                                                var data = response.data
                                                                if (data.error) {
                                                                    Snackbar.show({
                                                                        text: data.error,
                                                                        pos: 'top-right'
                                                                    });
                                                                } else {
                                                                    Snackbar.show({
                                                                        text: data.msg,
                                                                        pos: 'top-right'
                                                                    });
                                                                }
                                                            })
                                                                    .catch(error => {
                                                                        console.log(error);
                                                                    }).finally(() => {
                                                                $(this).attr('disabled', false);
                                                                $.unblockUI();
                                                            });
                                                        });
        </script>
    </body>

</html>