<?php

use yii\helpers\Html;
use yii\grid\GridView;
use backend\models\Category;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\CategorySearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

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

<div class="category-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('Create Category', ['create'], ['class' => 'btn btn-success']) ?>
        <!--<a onclick="js:history.go(-1);returnFalse;" class="btn btn-danger">Back</a>-->
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            //'id',
            /*[
                'attribute' => 'id',
                'format' => 'text',
                'value' => function($data) {
                        return $data->id;
                    },
                'contentOptions'=>['style'=>'text-align: center;']
            ],*/
           // 'title',
		   [
                'attribute'=>'image',
                'value'=>function($data)
                {
                    if(strlen($data->image) > 0){
                        return Yii::$app->urlManager->baseUrl . '/images/category/'.$data->image;
                    }
                    else
                    {
                        return Yii::$app->urlManager->baseUrl . '/images/www/no_image.jpg';
                    }

                },
                'format' => ['image',['width'=>'100px']],
                'contentOptions'=>['style'=>'width: 50px;']
            ],
            [
                'attribute' => 'title',
                'format' => 'text',
                'value' => function($data) {
                            return $data->title;
                    },
                'contentOptions'=>['style'=>'text-align: center;']
            ],
            //'order',
            //'status',
            [
                'attribute' => 'status',
                'format' => 'text',
                'value' => function($data) {
                        if($data->status == 1)
                            return 'Active';
                        else
                            return 'Inactive';
                    },
                'contentOptions'=>['style'=>'text-align: center;'],
                'filter'=>\backend\models\Category::itemAlias("status"),
            ],
            [
                //'attribute' => 'status',
                'header' => 'Relate',
                'format' => 'html',
                'value' => function($data) {
                        $c = Category::countBook($data->id);
                        //if($c){
                            return '<span class="badge badge-large badge-class-very_good">'.$c.'</span>';
                            //return '<label class="btn btn-success">'.$c.'</label>';
                        //}
                        //return '<label class="label label-default">'.$c.'</label>';
                    },
                'contentOptions'=>['style'=>'text-align: center;'],
            ],
            ['class' => 'yii\grid\ActionColumn',
                'template' => '{view} {update} {delete}',
                'buttons' => [
                    'delete' => function ($url,$model) {
                            //var_dump($model);exit;
                            if((Category::countBook($model->id)) == 0)
                                return Html::a(
                                    '<span class="glyphicon glyphicon-trash"</span>',
                                    $url,
                                    [
                                        'title' => 'Delete',
                                        'data-confirm' => 'Do you want to continue?',
                                        'data-method'=>'post',
                                        'data-pjax' => '0',
                                    ]
                                );
                            else
                                return '<span class="glyphicon glyphicon-exclamation-sign tooltips" style="cursor:pointer;" data-placement="top" data-toggle="tooltip" data-original-title="Can not delete this category !"></span>';
                        },
                ],
            ],
        ],
    ]); ?>

</div>

