<?php
use common\modules\blog\models\ArticleCategoryMedia;
use common\widgets\Multilang;
use yii\bootstrap\ActiveForm;
use common\modules\blog\models\ArticleCategory;
use yii\bootstrap\Html;
use mihaildev\ckeditor\CKEditor;
use yii\helpers\Json;
use yii\helpers\Url;

$def_lang = array_keys($langs)[0];
$uploaddir = \Yii::getAlias('@saveImageDir');
?>
<div class="category-form">

    <?php $form = \yii\bootstrap\ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

        <?= $form->field($category, 'code')->hint(Yii::t('app', 'Insensitive latin non-space'))->textInput() ?>

        <?= $form->field($category, 'tag')->hint(Yii::t('app', 'Comma-separated'))->textInput() ?>

        <?= $form->field($category, 'sort')->input('number') ?>

        <?= $form->field($category, 'article_category_pid')
                    ->dropDownList(ArticleCategory::findArticleCategoryDropdown($category->article_category_id), ['prompt' => Yii::t('app', 'Select parent')]) ?>

        <?= $form->field($category, 'status')->checkbox() ?>

        <ul class="nav nav-tabs" id="image-tabs" role="tablist">
            <li role="image_inputs" class="active" data-type="full"><a href="#image-full" aria-controls="image-full" role="tab" data-toggle="tab"><span><?= \Yii::t('app', 'full')?></span></a></li>
            <li role="image_inputs" class="" data-type="preview"><a href="#image-preview" aria-controls="image-preview" role="tab" data-toggle="tab"><span><?= \Yii::t('app', 'preview')?></span></a></li>
            <li role="image_inputs" class="" data-type="additional"><a href="#image-additional" aria-controls="image-additional" role="tab" data-toggle="tab"><span><?= \Yii::t('app', 'additional')?></span></a></li>
        </ul>
        <div class="tab-content image-tab-content">
            <div role="" class="tab-pane active main-tab" id="image-full">
                <?php
                $imagelang = Multilang::begin([
                    'ajaxpath' => Url::to(['/blog/ajax/article-category-media-form?type=full']),
                    'form' => $form,
                    'data_langs' => $category->getIsNewRecord()?$images:ArticleCategoryMedia::find()->where(['article_category_id' => $category->article_category_id, 'type' => 'full'])->indexBy('language_id')->all()
                ]);
                $first = 1;
                foreach($images as $lang => $value) {
                    if(!array_key_exists('full', $value)) continue;
                    ?>
                    <div role="" class="tab-pane <?php if($first) { echo 'active main-tab'; } ?>" id="<?=$imagelang->id?>-<?=$lang?>">
                        <?php
                        echo $form->field($images[$lang]['full'], "[{$lang}][full]language_id")->label(false)->hiddenInput(['value' => $lang]);
                        echo $form->field($images[$lang]['full'], "[{$lang}][full]imageFile")->fileInput(['class' => 'image_inputs_field']);
                        if(!empty($images[$lang]['full']->article_category_media_id)) {
                            echo "<img src='/images/upload/{$images[$lang]['full']->media->hash}/original.{$images[$lang]['full']->media->extension}' width='100' class='image_inputs_prev'>";
                        }
                        ?>
                    </div>
                    <?php
                    $first = 0;
                }
                $imagelang->end();
                ?>
            </div>
            <div role="" class="tab-pane" id="image-preview">
                <?php
                $imagelang = Multilang::begin([
                    'ajaxpath' => Url::to(['/blog/ajax/article-category-media-form?type=preview']),
                    'form' => $form,
                    'data_langs' => $category->getIsNewRecord()?$images:ArticleCategoryMedia::find()->where(['article_category_id' => $category->article_category_id, 'type' => 'preview'])->indexBy('language_id')->all()
                ]);
                $first = 1;
                foreach($images as $lang => $value) {
                    if(!array_key_exists('preview', $value)) continue;
                    ?>
                    <div role="" class="tab-pane <?php if($first) { echo 'active main-tab'; } ?>" id="<?=$imagelang->id?>-<?=$lang?>">
                        <?php
                        echo $form->field($images[$lang]['preview'], "[{$lang}][preview]language_id")->label(false)->hiddenInput(['value' => $lang]);
                        echo $form->field($images[$lang]['preview'], "[{$lang}][preview]imageFile")->fileInput(['class' => 'image_inputs_field']);
                        if(!empty($images[$lang]['preview']->article_category_media_id)) {
                            echo "<img src='/images/upload/{$images[$lang]['preview']->media->hash}/original.{$images[$lang]['preview']->media->extension}' width='100' class='image_inputs_prev'>";
                        }
                        ?>
                    </div>
                    <?php
                    $first = 0;
                }
                $imagelang->end();
                ?>
            </div>
            <div role="" class="tab-pane" id="image-additional">
                <?php
                echo $form->field(is_array($images[0]['additional'])?$images[0]['additional'][0]:$images[0]['additional'], "[0][additional]imageFile[]")->fileInput(['multiple' => 'multiple', 'class' => 'image_inputs_field']);
                if(is_array($images[0]['additional']) && count($images[0]['additional']) > 1) {
                    foreach($images[0]['additional'] as $onefield => $oneimage) {
                        if($onefield) {
                            ?>
                            <div class="additional_image_container">
                                <img src='/images/upload/<?= $oneimage->media->hash ?>/original.<?= $oneimage->media->extension ?>' width='100'>
                                <a
                                    href="<?= Url::to(['ajax/remove-image']) ?>"
                                    class="remove_image glyphicon glyphicon-remove-circle"
                                    data-params='<?= Json::encode(['article_category_media_id' => $oneimage->article_category_media_id, 'remove_media' => true]) ?>',
                                ></a>
                            </div>
                            <?php
                        }
                    }
                }
                ?>
            </div>
        </div>

        <hr>

        <?php
        $multilang = Multilang::begin(['ajaxpath' => Url::to(['/blog/ajax/category-form']), 'form' => $form, 'data_langs' => $category_langs])
        ?>
            <?php
            $first = 1;
            foreach($category_langs as $index => $category_lang) {
                ?>
                <div role="" class="tab-pane <?php if($first) { echo 'active main-tab'; } ?>" id="lang-<?=$index?>">
                    <?= $form->field($category_langs[$index], "[$index]language_id")->label(false)->hiddenInput(['value' => $index]) ?>

                    <?= $form->field($category_langs[$index], "[$index]text")->widget(CKEditor::className(),['editorOptions' => [ 'preset' => 'full', 'inline' => false, ], ]); ?>

                    <?= $form->field($category_langs[$index], "[$index]preview")->widget(CKEditor::className(),['editorOptions' => [ 'preset' => 'full', 'inline' => false, ], ]); ?>

                    <?= $form->field($category_langs[$index], "[$index]seo_url")->textInput() ?>

                    <?= $form->field($category_langs[$index], "[$index]name")->textInput() ?>

                    <?= $form->field($category_langs[$index], "[$index]meta_title")->textInput() ?>

                    <?= $form->field($category_langs[$index], "[$index]meta_descr")->textarea(); ?>

                    <?= $form->field($category_langs[$index], "[$index]meta_keyword")->textInput() ?>

                    <?= $form->field($category_langs[$index], "[$index]h1_tag")->textInput() ?>

                    <?= $form->field($category_langs[$index], "[$index]tag")->textInput() ?>

                </div>
            <?php
                $first = 0;
            }
            ?>
            <?php
            $multilang->end();
            ?>

        <div class="form-group">
            <?= Html::submitButton($category->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $category->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
        </div>

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

</div>
<script>
    var confirm_message = '<?= \Yii::t('app', 'Remove image?') ?>';
</script>