<?php

use yii\helpers\Html;
use yii\grid\GridView;
use mdm\admin\components\Helper;
use yii\helpers\Url;
use yii\bootstrap\Modal;

/* @var $this yii\web\View */
/* @var $searchModel mdm\admin\models\searchs\User */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('rbac-admin', 'User');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="content-page manuser">
    <div class="content-page-header updacc-header">
        <span><?= Html::encode($this->title) ?></span>
        <a href="<?= Url::to(['/admin/role']) ?>">Role</a>
        <button class="btn btn-primary pull-right manuser-header-create create-user-modal">Create User</button>
    </div>
    <div class="content-page-content updacc-content">
        <?=
        GridView::widget([
            'dataProvider' => $dataProvider,
            // 'filterModel' => $searchModel,
            'columns' => [
                'username',
                'email:email',
                [
                    'label' => 'Role',
                    'value' => 'userRole',
                ],
                [
                    'attribute' => 'status',
                    'value' => function($model) {
                        return $model->status == 0 ? 'Inactive' : 'Active';
                    },
                    'filter' => [
                        0 => 'Inactive',
                        10 => 'Active'
                    ]
                ],
                [
                    'class' => 'yii\grid\ActionColumn',
                    'template' => '<div class="manuser-more">
                            <i class="manuser-more-button material-icons">more_vert</i>
                            <div class="manuser-popup">
                                '.Helper::filterActionColumn(['edit', 'activate', 'deactivate', 'delete']).'
                            </div>
                        </div>',
                    'buttons' => [
                        'edit' => function($url, $model) {
                            $options = [
                                'title' => Yii::t('rbac-admin', 'Edit'),
                                'aria-label' => Yii::t('rbac-admin', 'Edit'),
                                'class' => 'manuser-popup-item edit-user-modal',
                                'data-id' => $model->id,
                            ];
                            return Html::a('Edit', 'javascript:;', $options);
                        },
                        'activate' => function($url, $model) {
                            if ($model->status == 10) {
                                return '';
                            }
                            $options = [
                                'title' => Yii::t('rbac-admin', 'Activate'),
                                'aria-label' => Yii::t('rbac-admin', 'Activate'),
                                'class' => 'manuser-popup-item',
                                'data-method' => 'post',
                                'data-pjax' => '0',
                            ];
                            return Html::a('Mark as active', $url, $options);
                        },
                        'deactivate' => function($url, $model) {
                            if ($model->status == 0) {
                                return '';
                            }
                            $options = [
                                'title' => Yii::t('rbac-admin', 'Deactivate'),
                                'aria-label' => Yii::t('rbac-admin', 'Deactivate'),
                                'class' => 'manuser-popup-item',
                                'data-method' => 'post',
                                'data-pjax' => '0',
                            ];
                            return Html::a('Mark as inactive', $url, $options);
                        },
                        'delete' => function($url, $model) {
                            $options = [
                                'title' => Yii::t('rbac-admin', 'Delete'),
                                'aria-label' => Yii::t('rbac-admin', 'Delete'),
                                'class' => 'manuser-popup-item',
                                'data-confirm' => Yii::t('rbac-admin', 'Are you sure you want to delete this user?'),
                                'data-method' => 'post',
                                'data-pjax' => '0',
                            ];
                            return Html::a('Delete', $url, $options);
                        }
                    ]
                ],
            ],
            'rowOptions' => function ($model) {
                $class = ($model->id == 1) ? 'hide' : '';
                return ['class' => $class];
            },
            'summary' => '',
            'tableOptions' => ['class' => 'table'],
        ]);
        ?>
    </div>
</div>

<?php
$roles = \Yii::$app->authManager->getRoles();

Modal::begin([
    'header' => '<div>Create User</div>',
    'footer' => '<button class="btn btn-close-modal">CANCEL</button> <button class="btn btn-primary btn-submit-modal">SUBMIT</button>',
    'id' => 'modal-create-user'
]);
?>
<form id="create-user-form">
    <div class="form-group">
        <label class="control-label" for="username">Username</label>
        <input type="text" class="form-control" name="username" placeholder="Enter your username" required>
    </div>
    <div class="form-group">
        <label class="control-label" for="email">Email</label>
        <input type="email" class="form-control" name="email" placeholder="Enter your email" required>
    </div>
    <div class="form-group">
        <label class="control-label" for="password">Password</label>
        <input type="password" class="form-control" name="password" placeholder="Enter your password" required>
    </div>
    <div class="form-group">
        <label class="control-label" for="role">Role</label>
        <select class="form-control modal-create-user-role" name="role" required>
            <?php
            $first = true;
            foreach ($roles as $key => $value) {
                if ($first) {
                    $firstDesc = $value->description;
                    $first = false;
                }
                ?>
                <option value="<?= $key ?>" data-desc="<?= $value->description ?>"><?= $value->name ?></option>
                <?php
            }
            ?>
        </select>
        <div class="modal-create-user-desc"><?= $firstDesc ?></div>
    </div>
</form>
<?php
Modal::end();

Modal::begin([
    'header' => '<div>Edit User</div>',
    'footer' => '<button class="btn btn-close-modal">CANCEL</button> <button class="btn btn-primary btn-submit-modal">SUBMIT</button>',
    'id' => 'modal-edit-user'
]);
?>
<form id="edit-user-form">
    <input type="hidden" name="id" value="">
    <div class="form-group">
        <label class="control-label" for="username">Username</label>
        <input type="text" class="form-control" name="username" placeholder="Enter your username" required>
    </div>
    <div class="form-group">
        <label class="control-label" for="email">Email</label>
        <input type="email" class="form-control" name="email" placeholder="Enter your email" required>
    </div>
    <div class="form-group">
        <label class="control-label" for="password">Password</label>
        <input type="password" class="form-control" name="password" placeholder="Leave it blank if you don't intend to change the password">
    </div>
    <div class="form-group">
        <label class="control-label" for="role">Role</label>
        <select class="form-control modal-create-user-role" name="role" required>
            <?php
            $first = true;
            foreach ($roles as $key => $value) {
                if ($first) {
                    $firstDesc = $value->description;
                    $first = false;
                }
                ?>
                <option value="<?= $key ?>" data-desc="<?= $value->description ?>"><?= $value->name ?></option>
                <?php
            }
            ?>
        </select>
        <div class="modal-create-user-desc"><?= $firstDesc ?></div>
    </div>
</form>
<?php
Modal::end();
?>

<script type="text/javascript">
    $('#modal-create-user .btn-submit-modal').click(function() {
        if ($('#create-user-form')[0].checkValidity()) {
            console.log('valid');
            var username = $('#create-user-form input[name=username]').val();
            var email = $('#create-user-form input[name=email]').val();
            var password = $('#create-user-form input[name=password]').val();
            var role = $('#create-user-form select[name=role]').val();
            $.ajax({
                url: '<?= Url::to(['user/create']); ?>',
                data: {
                    username: username,
                    email: email,
                    password: password,
                    role: role,
                },
                method: 'POST',
                success: function(result) {
                    console.log(result);
                }
            });
        } else
            console.log('invalid');
    });

    $('#modal-edit-user .btn-submit-modal').click(function() {
        if ($('#edit-user-form')[0].checkValidity()) {
            console.log('valid');
            var id = $('#edit-user-form input[name=id]').val();
            var username = $('#edit-user-form input[name=username]').val();
            var email = $('#edit-user-form input[name=email]').val();
            var password = $('#edit-user-form input[name=password]').val();
            var role = $('#edit-user-form select[name=role]').val();
            $.ajax({
                url: '<?= Url::to(['user/edit']); ?>',
                data: {
                    id: id,
                    username: username,
                    email: email,
                    password: password,
                    role: role,
                },
                method: 'POST',
                success: function(result) {
                    console.log(result);
                }
            });
        } else
            console.log('invalid');
    });

    $('.edit-user-modal').click(function() {
        var id = $(this).attr('data-id');
        $.ajax({
            url: '<?= Url::to(['user/get-data']); ?>',
            data: {
                id: id,
            },
            method: 'POST',
            success: function(result) {
                result = JSON.parse(result);
                $('#edit-user-form input[name=id]').val(id);
                $('#edit-user-form input[name=username]').val(result.username);
                $('#edit-user-form input[name=email]').val(result.email);
                $('#edit-user-form input[name=password]').val(result.password);
                $('#edit-user-form select[name=role]').val(result.role);
                $('#modal-edit-user').modal('show');
            }
        });
    });

    $('.manuser-more-button').click(function() {
        var hide = true;
        if ($(this).siblings('.manuser-popup').hasClass('show'))
            hide = true;
        else
            hide = false;
        $('.manuser-popup').removeClass('show');
        if (hide)
            $(this).siblings('.manuser-popup').removeClass('show');
        else
            $(this).siblings('.manuser-popup').addClass('show');
    });
</script>