<?php

use kartik\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([
                        'method' => 'GET',
            ]);
            echo $this->render('_search', ['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>

<?php
if ($reingresosCount) {
    ?>
    <div class="alert alert-warning">
        <h4><i class="icon fa fa-warning"></i> ¡¡Atenci&oacute;n!!</h4>
        Tienes <?= $reingresosCount ?> registros en reingreso, por favor revisalos <?= yii\helpers\Html::a('Aqui', ['/registros/reingresados']) ?>
        <?  ?>
    </div>

    <?php
}
?>
<div class="box box-primary">
    <div class="box-header with-border">
        <h3 class="box-title">Lista</h3>
    </div>
    <div class="box-body">
        <?php
        $gridColumns = [
            'id',
            [
                'class' => 'kartik\grid\ExpandRowColumn',
                'value' => function($model, $key, $index, $column) {
                    return GridView::ROW_COLLAPSED;
                },
//                'visible' => Yii::$app->user->can('isAdmin'),
                'detail' => function ($model, $key, $index, $column) {
                    $searchModel = \app\models\Registros::find()->where(['id' => $model->id])->one();
                    return Yii::$app->controller->renderPartial('ver', [
                                'model' => $searchModel,
                    ]);
                }
                    ],
                    [
                        'attribute' => 'numero',
                        'value' => function($model) {
                            return str_replace('-', '', $model->numero);
                        }
                    ],
                    [
                        'label' => 'Nombre',
                        'attribute' => 'nombre',
                        'value' => function ($model) {
                            return $model->nombre . ' ' . $model->apellidop . ' ' . $model->apellidom;
                        },
                    ],
                    'nip',
                    // 'lugarnac',
                    'curp',
                    [
                        'class' => 'kartik\grid\EditableColumn',
                        'attribute' => 'status_id',
                        'value' => function ($model) {
                            $agente = \app\models\Statusr::findOne($model->status_id);
                            return ($agente->status);
                        },
                        'visible' => Yii::$app->user->can('isAdmin'),
                        'editableOptions' => function ($model, $key, $index) {
                            return [
                                'header' => 'Status',
                                'inputType' => \kartik\editable\Editable::INPUT_DROPDOWN_LIST,
                                'data' => \yii\helpers\ArrayHelper::map(app\models\Statusr::find()->all(), 'id', 'status'),
                                'afterInput' => function ($form, $widget) use ($model, $index) {
                                    return $form->field($model, "aceptado");
                                }
                            ];
                        },
                            ],
                            [
                                'label' => 'Status',
                                'attribute' => 'status_id',
                                'visible' => !Yii::$app->user->can('isAdmin'),
                                'value' => function ($model) {
                                    $agente = \app\models\Statusr::findOne($model->status_id);
                                    return ($agente->status);
                                }
                            ],
                            'aceptado',
                            // '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);
                                }
                            ],
                            ['class' =>
                                'yii\grid\ActionColumn',
                                'visible' => Yii::$app->user->can('isAdmin'),
                                '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);
                                                }
                                            ],];
                                        ?>
                                        <div class="box-body table-responsive no-padding">
                                            <?php
                                            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
                                            ]);
                                            echo GridView::widget([
                                                'dataProvider' => $dataProvider,
                                                'columns' => $gridColumns,
                                                'responsive' => true,
                                                'hover' => false,
                                                'pjax' => true, // pjax is set to always true for this demo
                                                'bordered' => false,
                                                'striped' => true,
                                                'condensed' => true,
                                                'panel' => [
                                                    'type' => GridView::TYPE_PRIMARY,
                                                ],
                                                'toolbar' => [
                                                    '{export}',
                                                ],
                                                // set export properties
                                                'export' => [
                                                    'fontAwesome' => true
                                                ],
                                                'exportConfig' => [
                                                ]
                                            ]);
                                            ?>
        </div>
    </div>
</div>
