use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\data\ActiveDataProvider;
use yii\grid\GridView;
use yii\widgets\ActiveForm;
use yii\widgets\ListView;
use yii\db\Query;

$search_keyword = '';
if(isset($_GET['keyword']) && !empty($_GET['keyword']))
    $search_keyword = $_GET['keyword'];

echo '<style>
    .search-wrapper{ padding:8em 0; background-image:url(assets/dynaweb/images/bg2.jpg);}
    .search-wrapper h3{color : black;font-weight: 900;}
    .search-wrapper h4{color : black;font-weight: 300;}
    .search-input{box-shadow: 0 0 15px rgb(0 0 0 / 18%);padding:1.5em;width:100%;border:0;border-radius: 6px;}
    .modules-list{box-shadow: 0 0 15px rgb(0 0 0 / 18%);margin-bottom:2em !important;border-radius: 6px;}
    .modules-link{padding:0.2em;text-align:center;border:1px solid #ccc;color:#171717;border-radius: 6px;display:inline-block;width:3em;}
    .modules-list i{color:inherit;}
    .summary{display:none;}
    #w0 .col-sm-4{padding:0 1em;}
    .pagination{display:block;width:100%;overflow:hidden;padding:1em 0;}
</style>
<script>
    function downloadmodule(id,url){
        $.ajax({
            url: "admin/moduleRepository/module-list/adddownload?id="+id,
            method: "GET",
        });
        window.location.href = "admin/"+url;
    }
</script>
<div class="search-wrapper">
    <div class="container">
        <h3>More than 100 plugin to download for FREE!</h3>
        <h4>Advance modular application development framework brought to you by BLUNET.co</h4>
        <form action="index.php" method="get">
            <input type="hidden" name="id" value="3" />
            <input type="hidden" name="page_id" value="12" />
            <input type="text" placeholder="Enter keyword here (e.g : article, gallery)" class="search-input" name="keyword" value="'.$search_keyword.'">
        </form>
    </div>
</div>
<div class="section-empty">
    <div class="container content">';
        
$querymodules = new Query;
$querymodules->select(['module_list.*'])->from('module_list');
if(!empty($search_keyword))
    $querymodules->andWhere(['like','module_name',$search_keyword]);

$querymodules->orderBy(['module_name'=>SORT_ASC]);
$dataProvider = new ActiveDataProvider(['query' => $querymodules,'pagination' => ['pageSize' => 1,],]);
echo ListView::widget([
    'dataProvider' => $dataProvider,
    'itemOptions' => ['class' => 'col-sm-4'],
    'options' => [
        'tag' => 'div',
        'class' => 'row',
    ],
    'itemView' => function ($model, $key, $index, $widget) {
        $img_thumbnail = 'admin/images/noimage.jpg';
        if(!empty($model['module_image']))
            $img_thumbnail = 'admin/'.$model['module_image'];
        
        $querymodules2 = new Query;
        $querymodules2->select(['COUNT(*)'])
            ->from('module_version')
            ->join('inner join','module_download','module_download.version_id=module_version.version_id')
            ->where(['module_version.module_id'=>$model['module_id']]);
        $commandmodules2 = $querymodules2->createCommand();
        $total_download = $commandmodules2->queryScalar();
        
        $querymodules3 = new Query;
        $querymodules3->select(['*'])->from('module_version')->where(['module_id'=>$model['module_id']])->orderBy(['version_id'=>SORT_DESC])->limit(1);
        $commandmodules3 = $querymodules3->createCommand();
        $modelversion = $commandmodules3->queryAll();
        $last_update = '';
        $version_id = '';
        $module_url = '';
        if(!empty($modelversion)){
            foreach($modelversion as $rowversion){
                $last_update = date("d F Y", strtotime($rowversion['created_at']));
                $version_id = $rowversion['version_id'];
                $module_url = $rowversion['module_url'];
            }
        }
            
        return '<div class="modules-list">
                <img src="'.$img_thumbnail.'" style="width:100%;height:250px;border-radius:6px 6px 0 0;">
                <div style="padding:1em;">
                    <small style="color:#ccc;">Last updated on '.$last_update.' by '.$model['module_developer'].'</small>
                    <p style="color:#171717;height:70px;"><b>'.$model['module_name'].'</b></p>
                    <div class="row">
                        <div class="col-sm-6">
                            <div style="color:#171717;">'.$total_download.' downloads</div>
                        </div>
                        <div class="col-sm-6" style="text-align:right;">
                            <a href="index.php?id=3&page_id=15&module_id='.$model['module_id'].'" class="modules-link"><i class="fa fa-info" aria-hidden="true"></i></a>
                            <a href="#" class="modules-link" target="_blank" onclick="downloadmodule('.$version_id.',\''.$module_url.'\')"><i class="fa fa-download" aria-hidden="true"></i></a>
                        </div>
                    </div>
                </div>
            </div>';
    },
]);
    echo '
    </div>
</div>';