<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\DetailView;
use kartik\editable\Editable;

$this->title = $model->username;
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Users'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="users-view">

    <h1><?= Html::encode($this->title) ?></h1>

    <?php if(Yii::$app->session->hasFlash('change-password')): ?>
        <div class="alert alert-success">
            <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
            <?= Yii::$app->session->getFlash('change-password') ?>
        </div>
    <?php endif; ?>

    <?php if($model->role->item_name != 'User'): ?> 
    <p>
        <?= Html::a(Yii::t('app', 'Change Password'), 
            ['change-user-password', 'id' => $model->id], 
            ['class' => 'btn btn-primary', 'id' => 'change-password-btn']) ?>
    </p>
    <?php endif; ?>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            [
                'attribute' => 'username',
                'format' => 'raw',
                'value' => function($data){
                    return Editable::widget([
                        'name' => 'username', 
                        'model' => $data,
                        'attribute' => 'username',
                        'asPopover' => true, 
                        'closeOnBlur' => true,
                        'displayValue' => Yii::t('app', $data->username),
                        'format' => Editable::FORMAT_LINK,
                        'formOptions' => ['action' => Url::base(true) . '/users/change-username?id='. $data->id, 'method' => 'get'],
                        'preHeader' => Yii::t('app', 'Edit') . ' ',
                        'header' => Yii::t('app', 'Username'),
                        'footer' => '{buttons}' . Html::a('<i class="glyphicon glyphicon-ban-circle"></i>', [''], ['class' => 'btn btn-sm btn-danger close-editable']),
                        'buttonsTemplate' => '{submit}' . (Yii::$app->language == 'ar' ? ' ' : ''),
                        'size'=> 'md',
                        'inputType' => Editable::INPUT_TEXT,
                        'editableValueOptions' => ['class' => ''],
                        'options' => ['id' => 'username_' . $data->id],
                    ]);
                },
            ],
            [
                'attribute' => 'status',
                'format' => 'raw',
                'filter' => ['Active' => Yii::t('app', 'Active'), 'Inactive' => Yii::t('app', 'Inactive')],
                'value' => function($data){
                    return Editable::widget([
                        'name' => 'status', 
                        'model' => $data,
                        'attribute' => 'status',
                        'asPopover' => true, 
                        'closeOnBlur' => true,
                        'displayValue' => Yii::t('app', $data->status) ,
                        'displayValueConfig' => [
                            'Active' => '<span class="green">' . Yii::t('app', 'Active') . ' </span>',
                            'Inactive' => '<span class="red">' . Yii::t('app', 'Inactive') . ' </span>',
                        ],
                        'format' => Editable::FORMAT_LINK,
                        'formOptions' => ['action' => Url::base(true) . '/users/change-status?id='. $data->id, 'method' => 'get'],
                        'preHeader' => Yii::t('app', 'Edit') . ' ',
                        'header' => Yii::t('app', 'Status'),
                        'footer' => '{buttons}' . Html::a('<i class="glyphicon glyphicon-ban-circle"></i>', [''], ['class' => 'btn btn-sm btn-danger close-editable']),
                        'buttonsTemplate' => '{submit}' . (Yii::$app->language == 'ar' ? ' ' : ''),
                        'data' => ['Active' => Yii::t('app', 'Active'), 'Inactive' => Yii::t('app', 'Inactive')],
                        'size'=> 'md',
                        'inputType' => Editable::INPUT_DROPDOWN_LIST,
                        'editableValueOptions' => ['class' => ''],
                        'options' => ['id' => 'status_' . $data->id],
                        'pluginEvents' => [
                            "editableSuccess" => "function(event, val, form, data) {
                                if(val == 'Active'){
                                    $(this).removeClass('red');
                                    $(this).addClass('green');
                                }
                                else if(val == 'Inactive'){
                                    $(this).removeClass('green');
                                    $(this).addClass('red');
                                }
                            }",
                        ], 
                    ]);
                },
            ],
            [
                'attribute' => 'role',
                'label' => Yii::t('app', 'Role'),
                'format' => 'raw',
                'value' => function($data){
                    $data->_role = $data->role->item_name;
                    return Editable::widget([
                        'name' => 'role', 
                        'model' => $data,
                        'attribute' => '_role',
                        'asPopover' => true, 
                        'closeOnBlur' => true,
                        'displayValue' => Yii::t('app', $data->role->item_name),
                        'displayValueConfig' => [
                            'Admin' => Yii::t('app', 'Admin'),
                            'Branch Admin' => Yii::t('app', 'Branch Admin'),
                            'User' => Yii::t('app', 'User'),
                        ],
                        'format' => Editable::FORMAT_LINK,
                        'formOptions' => ['action' => Url::base(true) . '/users/change-role?id='. $data->id, 'method' => 'get'],
                        'preHeader' => Yii::t('app', 'Edit') . ' ',
                        'header' => Yii::t('app', 'Role'),
                        'footer' => '{buttons}' . Html::a('<i class="glyphicon glyphicon-ban-circle"></i>', [''], ['class' => 'btn btn-sm btn-danger close-editable']),
                        'buttonsTemplate' => '{submit}' . (Yii::$app->language == 'ar' ? ' ' : ''),
                        'data' => ['Admin' => Yii::t('app', 'Admin'), 'Branch Admin' => Yii::t('app', 'Branch Admin'), 'User' => Yii::t('app', 'User')],
                        'size'=> 'md',
                        'inputType' => Editable::INPUT_DROPDOWN_LIST,
                        'editableValueOptions' => ['class' => ''],
                        'options' => ['id' => 'role_' . $data->id],
                        'pluginEvents' => [
                            "editableSuccess" => "function(event, val, form, data) {
                                console.log(val);
                                if(val == 'Admin' || val == 'Branch Admin'){
                                    $('#change-password-btn').show();
                                }
                                else{
                                    $('#change-password-btn').hide();
                                }
                            }",
                        ],
                    ]);
                },
            ]
        ],
    ]) ?>

</div>
