<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

use yii\bootstrap\Modal;
use yii\helpers\Url;

$this->title = 'Sign In';

$theAppId = Yii::$app->params['theAppId'];
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://cloud.interactive.co.id/mybilling/api/appLogin",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => "theAppId=$theAppId"
));
$response = json_decode(curl_exec($curl));
curl_close($curl);

if ($errorCode == 1045) {
?>
    <div class="login-box">
        <div class="login-logo">
            <?= Html::img('@web/images/logo-my-connection.png', ['class' => 'logo-img', 'alt' => 'InterActive MyConnection']) ?>
        </div>
        <div class="login-box-body">
            <div class="text-center">Please check your database connection.</div>
        </div>
    </div>
<?php
} elseif (empty($response->orderstatus)) {
?>
    <div class="login-box">
        <div class="login-logo">
            <?= Html::img('@web/images/logo-my-connection.png', ['class' => 'logo-img', 'alt' => 'InterActive MyConnection']) ?>
        </div>
        <div class="login-box-body">
            <div class="text-center">Please activate your account at InterActive MyBilling.</div>
        </div>
    </div>
<?php
} elseif (!empty($response->orderstatus)) {
    $session = Yii::$app->session;
    $session['activeUntil'] = $response->nextduedate;
    
    $fieldOptions1 = [
        'options' => ['class' => 'form-group has-feedback'],
        'inputTemplate' => "{input}<span class='glyphicon glyphicon-envelope form-control-feedback'></span>"
    ];

    $fieldOptions2 = [
        'options' => ['class' => 'form-group has-feedback'],
        'inputTemplate' => "{input}<span class='glyphicon glyphicon-lock form-control-feedback'></span>"
    ];
    ?>

    <div class="login-box">
        <div class="login-logo">
            <?= Html::img('@web/images/logo-my-connection.png', ['class' => 'logo-img', 'alt' => 'InterActive MyConnection']) ?>
        </div>
        <div class="login-box-body">
            <p class="login-box-msg">Sign in to start your session</p>

            <?php $form = ActiveForm::begin(['id' => 'login-form', 'enableClientValidation' => false]); ?>

            <?= $form
                ->field($model, 'username', $fieldOptions1)
                ->label(false)
                ->textInput(['placeholder' => $model->getAttributeLabel('username')]) ?>

            <?= $form
                ->field($model, 'password', $fieldOptions2)
                ->label(false)
                ->passwordInput(['placeholder' => $model->getAttributeLabel('password')]) ?>

            <div class="row">
                <div class="col-xs-8">
                    <?= $form->field($model, 'rememberMe')->checkbox() ?>
                </div>
                <div class="col-xs-4">
                    <?= Html::submitButton('Sign in', ['class' => 'btn btn-primary btn-block btn-flat', 'name' => 'login-button']) ?>
                </div>
            </div>

            <?php ActiveForm::end(); ?>

        </div>
    </div>

    <?php
    Modal::begin([
        'header' => '<h2>DB Connection</h2>',
        'footer' => '<button class="btn btn-danger btn-close btn-cancel">Cancel</button> <button class="btn btn-primary btn-close btn-ok">OK</button>',
        'toggleButton' => [
            'label' => 'Show',
            'class' => 'btn btn-primary',
            'style' => 'opacity:0',
        ],
    ]);
    ?>
        <div class="form-group">
            <label for="dbhost">Host</label>
            <input class="form-control" type="text" name="dbhost" id="dbhost" placeholder="Host">
        </div>
        <div class="form-group">
            <label for="dbname">DB Name</label>
            <input class="form-control" type="text" name="dbname" id="dbname" placeholder="DB Name">
        </div>
        <div class="form-group">
            <label for="dbusername">Username</label>
            <input class="form-control" type="text" name="dbusername" id="dbusername" placeholder="Username">
        </div>
        <div class="form-group">
            <label for="dbpassword">Password</label>
            <input class="form-control" type="password" name="dbpassword" id="dbpassword" placeholder="Password">
        </div>
    <?php
    Modal::end();
    ?>

    <script type="text/javascript">
        $('.btn-close').click(function() {
            $('.modal').modal('hide');
        });

        $('.btn-ok').click(function() {
            var dbhost = $('input#dbhost').val();
            var dbname = $('input#dbname').val();
            var dbusername = $('input#dbusername').val();
            var dbpassword = $('input#dbpassword').val();
            $.ajax({
                url: '<?= Url::to(['site/set-db']); ?>',
                method: "POST",
                data: {dbhost: dbhost, dbname: dbname, dbusername: dbusername, dbpassword: dbpassword},
                success: function(result) {
                    location.reload();
                }
            });
        });
    </script>

<?php
} else {
?>
    <div class="login-box">
        <div class="login-logo">
            <?= Html::img('@web/images/logo-my-connection.png', ['class' => 'logo-img', 'alt' => 'InterActive MyConnection']) ?>
        </div>
        <div class="login-box-body">
            <div class="text-center">Please activate your account at InterActive MyBilling.</div>
        </div>
    </div>
<?php
}

?>