<?php

use yii\grid\GridView;
use kartik\export\ExportMenu;
use kartik\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $searchModel app\models\ClientesSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Registros';
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="clientes-index">   
    <div class="box box-default collapsed-box">
        <div class="box-header with-border">
            <h3 class="box-title">Filtrar</h3>
            <div class="box-tools pull-right">
                <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-plus"></i></button>
            </div><!-- /.box-tools -->
        </div><!-- /.box-header -->
        <div class="box-body" style="display: none;">
            <?php
            $form = ActiveForm::begin([
                        'action' => ['index'],
                        'method' => 'GET',
            ]);
            echo $this->render('_searchValidar', ['model' => $searchModel, 'form' => $form]);
            echo '<div class="box-footer">
                    <button type="submit" class="btn btn-primary">Submit</button>
                  </div>';
            ActiveForm::end();
            ?>
        </div><!-- /.box-body -->
    </div>
</div>
<div class="box box-primary">
    <div class="box-header with-border">
        <h3 class="box-title">Lista</h3>
    </div>
    <div class="box-body">
        <?php
        $role = Yii::$app->authManager->getRole('Agentes');
        $rolesById = Yii::$app->authManager->getRolesByUser(Yii::$app->user->identity->id);
        $gridColumns = [
            'id',
            'numero',
            [
                'label' => 'Nombre',
                'attribute' => 'nombre',
                'value' => function ($model) {
                    return $model->nombre . ' ' . $model->apellidop . ' ' . $model->apellidom;
                },
            ],
            'genero',
            'nip',
            [
                'attribute' => 'fechanac',
                'value' => function($model) {
                    return date_create($model->fechanac)->format('d-m-Y');
                }
            ],
            // 'lugarnac',
            'curp',
            'dnref',
            // 'correo',
            // 'entrega',
            // 'modelo',
//            [
//                'label' => 'Sim',
//                'attribute' => 'sim_id',
//                'value' => 'sim.tipo',
//            ],
//            [
//                'label' => 'Telefono',
//                'attribute' => 'telefono_id',
//                'value' => 'telefono.liberado',
//            ],
            [
                'attribute' => 'created',
                'value' => function($model) {
                    return date_create($model->created)->format('d-m-Y H:i:s');
                }
            ],
            [
                'label' => 'Agente',
                'attribute' => 'created_by',
                'value' => function ($model) {
                    $agente = \common\models\User::findIdentity($model->created_by);
                    if(!$agente)
                        return "Freedom";
                    return ($agente->agente);
                }
            ],
            [
                'label' => 'Status',
                'attribute' => 'status_id',
                'value' => function ($model) {
                    $agente = \app\models\Statusr::findOne($model->status_id);
                    return ($agente->status);
                }
            ],
            ['class' =>
                'yii\grid\ActionColumn',
                'template' => '{update}{editar}  ',
                'header' => 'Acciones',
                'buttons' => [
                    'editar' => function ($url, $model) {
                        return yii\bootstrap\Html::a(''
                                        . '<span class="glyphicon glyphicon-random"></span>', $url, [
                                    'title' => Yii::t('yii', 'Editar'),
                        ]);
                    }
                        ]
                    ],
                ];
                $gridColumnsExport = [
                    'id',
                    [
                        'attribute' => 'numero',
                        'value' => function($model) {
                            return str_replace('-', '', $model->numero);
                        }
                    ],
                    'nombre',
                    'apellidop',
                    'apellidom',
                    'genero',
                    'nip',
                    [
                        'attribute' => 'fechanac',
                        'value' => function($model) {
                            return date_create($model->fechanac)->format('d-m-Y');
                        }
                    ],
                    [
                        'label' => 'Lugar Nac',
                        'attribute' => 'created_by',
                        'value' => function ($model) {
                            $estado = \app\models\Estados::find()->where(['clave' => $model->lugarnac])->one();
                            return ($estado['estado']);
                        }
                            ],
                            //'lugarnac',
                            'curp',
                            'dnref',
                            'correo',
                            'entrega',
                            'modelo',
                            [
                                'label' => 'Sim',
                                'attribute' => 'sim_id',
                                'value' => 'sim.tipo',
                            ],
                            [
                                'label' => 'Telefono',
                                'attribute' => 'telefono_id',
                                'value' => 'telefono.liberado',
                            ],
                            [
                                'attribute' => 'created',
                                'value' => function($model) {
                                    return date_create($model->created)->format('d-m-Y H:i:s');
                                }
                            ],
                            [
                                'label' => 'Agente',
                                'attribute' => 'created_by',
                                'value' => function ($model) {
                                    $agente = \common\models\User::findIdentity($model->created_by);
                                    return ($agente->agente);
                                }
                            ],
                            [
                                'label' => 'Status',
                                'attribute' => 'status_id',
                                'value' => function ($model) {
                                    $agente = \app\models\Statusr::findOne($model->status_id);
                                    return ($agente->status);
                                }
                            ],];

                        // Renders a export dropdown menu
                        echo ExportMenu::widget([
                            'dataProvider' => $dataProvider,
                            'columns' => $gridColumnsExport,
                            'target' => '_self',
                            'filename' => 'Exportado_' . date('d-m-Y His'),
                            'exportConfig' => [
                                'HTML' => false,
                                'CSV' => false,
                                'TXT' => false,
                                'PDF' => false,
                                'Excel5' => false,
                            ],
                            'dropdownOptions' => [
                                'label' => 'Exportar',
                                'class' => 'btn btn-default',
                            ],
                            'columnSelectorOptions' => [
                                'label' => 'Columnas',
                                'class' => 'btn btn-default',
                            ],
                                //'asDropdown'=> false
                        ]);
                        ?>
                        <div class="box-body table-responsive no-padding">
                            <?php
                            echo GridView::widget([
                                'dataProvider' => $dataProvider,
                                'columns' => $gridColumns,
                            ]);
                            ?>
        </div>
    </div>
</div>
