<?= $this->extend(config('Auth')->views['layout']) ?>
<?= $this->section('title') ?><?= lang('Auth.login') ?> <?= $this->endSection() ?>
<?= $this->section('main') ?>

<div class="login-box">
    <div class="login-logo">
        <img src="<?= $logo ?>" alt="Satria" class="img img-round" width="250"><br>
        <a href="#"><b>G-Round</b> | Pegadaian</a>
    </div>
    <!-- /.login-logo -->
    <div class="login-box-body">
        <p class="login-box-msg"><?= lang('Auth.login') ?></p>

        <?php if (session('error') !== null) : ?>
        <div class="alert alert-danger" role="alert"><?= session('error') ?></div>
        <?php elseif (session('errors') !== null) : ?>
        <div class="alert alert-danger" role="alert">
            <?php if (is_array(session('errors'))) : ?>
                <?php foreach (session('errors') as $error) : ?>
                    <?= $error ?>
                    <br>
                <?php endforeach ?>
            <?php else : ?>
                <?= session('errors') ?>
            <?php endif ?>
        </div>
        <?php endif ?>

        <?php if (session('message') !== null) : ?>
        <div class="alert alert-success" role="alert"><?= session('message') ?></div>
        <?php endif ?>

        <div id="fg_error">
            <strong class="control-label" for="error"><p class="error-message" id="em_error" style="display: none; color: #dd4b39;"></p></strong>
        </div>

        <?= form_open('', 'id="users_form"') ?>
            <?= form_input('email', $email) ?>
            <?= form_password('password', $password) ?>

            <div class="form-group has-feedback">
                <?php if (setting('Auth.sessionConfig')['allowRemembering']): ?>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="remember" <?php if (old('remember')): ?> checked<?php endif ?>> <?= lang('Auth.rememberMe') ?>
                    </label>
                </div>
                <?php endif; ?>
            </div>
            <div class="form-group has-feedback">
                <button type="submit" class="btn btn-primary btn-block btn-flat" id="btn_users"><?= lang('Auth.login') ?></button>
            </div>
        </form>

        <div class="social-auth-links text-center">
            <p>- OR -</p>
            <a href="#" class="btn btn-block btn-success btn-flat">Sign in using SSO Pegadaian</a>
        </div>
    </div>
    <!-- /.login-box-body -->
    <div class="row">
        <div class="foot text-center">
            Copyright &copy; <?= date('Y') ?> Pegadaian<br>
        </div>
    </div>
</div>
<!-- /.login-box -->

<?= $this->endSection() ?>

<?= $this->section('pageScripts') ?>
<script type="text/javascript">
$("#users_form").submit(function(e){
    e.preventDefault();
    var url = '<?= $action ?>'
    var formData = $(this).serialize();
    $.ajax({
        url: url,
        type: "post",
        dataType: 'json',
        data: formData,
        beforeSend  : function() {
            $(".form-group").removeClass("has-error");
            $(".error-message").html('').hide();
            $("#btn_users").button('loading');
        },
        error: function (xhr, status, message) {
            if (xhr.status == 400) {
                var res = JSON.parse(xhr.responseText);
                $(res.messages).each(function(i,val){
                    var inputname = Object.keys(val)[0];
                    $.each(val,function(k,v){
                        $("#"+inputname).focus();
                        $("#fg_"+k).addClass("has-error");
                        $("#em_"+k).html(v).fadeIn();
                    });
                });
            } else {
                alert(xhr.statusText );
            }
        },
        success: function(result) {
            window.location.href = result.message;
        },
        complete: function() {
            $("#btn_users").button('reset');
        }
    });
});
</script>
<?= $this->endSection() ?>