<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\SystemConfigurations;
use kartik\file\FileInput;

$lang = Yii::$app->language;
$vars_limit = SystemConfigurations::find()->where(['config_option' => 'max_var_num'])->one()->value;
$images_limit = SystemConfigurations::find()->where(['config_option' => 'max_item_images'])->one()->value;

$this->title = Yii::t('app', 'Update Images: ');
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Items'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title . ($lang == 'ar' ? $item->arabic_name : $item->english_name);
?>
<?php if (count($modelImages) > 1) : ?>
    <p>
        <?= Html::a(Yii::t('app', 'Images order'), ['images-order', 'id' => $item->id], ['class' => 'btn btn-primary']) ?>
    </p>
<?php endif; ?>

<div class="row" style="text-align:center">
    <?php foreach ($modelImages as $index => $item_image) : ?>
        <div class="col-md-3">
            <div class="div-update-images">
                <img class="img-responsive update-image" src="<?= Yii::$app->request->baseUrl ?>/<?=$item_image->image_path?>" >
            </div>
            <?php if (count($modelImages) > 1) : ?>
            <?= Html::a(Yii::t('app', 'Delete'), ['delete-image', 'id' => $item_image->id], [
                'class' => 'btn btn-danger',
                'data' => [
                    'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'),
                    'method' => 'post',
                ],
            ]) ?>
            <?php endif; ?>
        </div>
    <?php endforeach; ?>
</div>
<br>
<?php if($image_count < $images_limit) : ?>
<div class="update-images-form">

    <?php $form = ActiveForm::begin(); ?>
    <h3><?= Yii::t('app', 'Add Images') ?>:</h3>
    <?= $form->field($images, 'image_path[]')->widget(FileInput::classname(), [
                'options' => ['multiple' => true, 'accept' => 'image/*'],
                'pluginOptions' => ['maxFileCount' => $images_limit - $image_count]
            ]) ?> 

    <div class="form-group">
        <?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
<?php else : ?>
<div class="alert alert-warning" role="alert">
  <?= Yii::t('app', 'You can\'t add more than {vars_limit} images.' , ['vars_limit' => $images_limit])?>
</div>
<?php endif; ?>

<?php

    if(Yii::$app->session->hasFlash('updated')){
        echo "<div class='alert alert-success'>";
        echo Yii::$app->session->getFlash('updated');
        echo "</div>";
    }
    
?>