<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\web\View;
use common\widgets\GridView;
use yii\data\ArrayDataProvider;

/* @var $this View */
/* @var $history array */
/* @var $table string|null */

$this->title = 'Backup History';
$this->params['breadcrumbs'][] = ['label' => 'Database Backup', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

$dataProvider = new ArrayDataProvider([
    'allModels' => $history,
    'pagination' => [
        'pageSize' => 50,
    ],
]);

?>

<div class="backup-history">

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

    <p>
        <a href="<?= Url::to(['index']) ?>" class="btn btn-default">
            <i class="fa fa-arrow-left"></i> Back to Backup Manager
        </a>
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            
            [
                'attribute' => 'table_name',
                'label' => 'Table',
            ],
            
            [
                'attribute' => 'filename',
                'label' => 'Filename',
                'format' => 'raw',
                'value' => function($model) {
                    return Html::tag('code', $model->filename);
                },
            ],
            
            [
                'attribute' => 'file_size',
                'label' => 'Size',
                'value' => function($model) {
                    return $model->fileSizeFormatted;
                },
            ],
            
            [
                'attribute' => 'status',
                'label' => 'Status',
                'format' => 'raw',
                'value' => function($model) {
                    $class = '';
                    switch ($model->status) {
                        case 'success':
                            $class = 'label-success';
                            break;
                        case 'failed':
                        case 'restore_failed':
                            $class = 'label-danger';
                            break;
                        case 'restored':
                            $class = 'label-info';
                            break;
                    }
                    return Html::tag('span', $model->statusLabel, ['class' => "label {$class}"]);
                },
            ],
            
            [
                'attribute' => 'created_at',
                'label' => 'Created',
                'value' => function($model) {
                    return $model->createdAtFormatted;
                },
            ],
            
            [
                'attribute' => 'error_message',
                'label' => 'Error',
                'format' => 'raw',
                'value' => function($model) {
                    if ($model->error_message) {
                        return Html::tag('span', Html::encode($model->error_message), [
                            'class' => 'text-danger',
                            'title' => $model->error_message,
                        ]);
                    }
                    return '';
                },
            ],
        ],
    ]); ?>

</div>
