<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\ArrayHelper;

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

$this->title = 'Books';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="book-index">

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

    <p>
        <?= Html::a('Create Book', ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            //'class' => 'yii\grid\CheckboxColumn',
           // 'id',
            /*[
                'attribute'=>'id',
                'format' => 'html',
                'value' => 'id',
                'contentOptions'=>['style'=>'width: 20px;']
            ],*/
            //'title',
            [
                'attribute'=>'title',
                'format' => 'html',
                'value' => 'title',
                //'contentOptions'=>['style'=>'width: 200px;']
            ],
           /* [
                'attribute'=>'description',
                'format' => 'html',
                'value' => 'description',
                'contentOptions'=>['style'=>'width: 100px;']
            ],*/
           // 'author',
            [
                'attribute'=>'author',
                'format' => 'html',
                'value' => 'author',
                'contentOptions'=>['style'=>'width: 100px;']
            ],

            //'publisher',
            [
                'attribute'=>'publisher',
                'format' => 'html',
                'value' => 'publisher',
                'contentOptions'=>['style'=>'width: 150px;']
            ],
            [
                'attribute'=>'categoryId',
                'value'=>function($data)
                    {
                        $id = $data->categoryId;
                        if(strlen($id)>0)
                        {
                            $cate = \backend\models\Category::find()->where('id ='.$id)->one();
                            if(count($cate)>0)
                                return $cate->title;
                            else
                                return 'Updating';
                        }
                        else
                            return 'Updating';
                    },
                'format' => 'html' ,
                'contentOptions'=>['style'=>'width: 100px;'],
                'filter' => Arrayhelper::map($cate, 'id', 'title'),
            ],
            [
                'attribute'=>'image',
                'value'=>function($data)
                {
                    if(strlen($data->image) > 0){
                        return Yii::$app->urlManager->baseUrl . '/images/book/'.$data->image;
                    }
                    else
                    {
                        return Yii::$app->urlManager->baseUrl . '/images/www/no_image.jpg';
                    }

                },
                'format' => ['image',['width'=>'100px']],
                'contentOptions'=>['style'=>'width: 50px;']
            ],
            [
                'label'=>'Manage Chapters',
                'value'=>function($data)
                    {
                        //if($data->type == 1) // book khong chua file pdf hoac epub
                        return '<a style="text_align:center;" data-toggle="tooltip" data-placement="top" data-original-title="'.'Chapters'.'" href="'. Yii::$app->urlManager->createUrl(array('chapter/index','ChapterSearch[bookId]'=>$data->id,'bookid'=>$data->id)).'" class="glyphicon glyphicon-edit"></a>';
                        /*else
                            return '';*/
                    },
                'format' => 'html',
                'contentOptions'=>['style'=>'width: 50px;']
            ],

            ['class' => 'yii\grid\ActionColumn',
                'template' => ' {update} {delete}',
                'buttons' => [
                    'delete' => function ($url) {
                            return Html::a(
                                '<span class="glyphicon glyphicon-trash"</span>',
                                $url,
                                [
                                    'title' => 'Delete',
                                    'data-confirm' => 'Delete this book will delete its chapters, pages. Do you want to continue?',
                                    'data-method'=>'post',
                                    'data-pjax' => '0',
                                ]
                            );
                        },
                ],
                'contentOptions'=>array('style'=>'width: 80px;')
            ],
        ],
    ]); ?>

</div>
