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

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

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

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

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

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

        <?= $form->field($article, 'article_pid')
                    ->dropDownList(Article::findArticleDropdown($article->article_id), ['prompt' => Yii::t('app', 'Select parent')]) ?>

        <?= $form->field($article, 'articleCategoriesArray')
                    ->dropDownList(ArticleCategory::findArticleCategoryDropdown(NULL), ['multiple' => 'multiple'])->label(\Yii::t('app', 'Article Categories Array')); ?>

        <?= $form->field($article, '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-media-form?type=full']),
                    'form' => $form,
                    'data_langs' => $article->getIsNewRecord()?$images:ArticleMedia::find()->where(['article_id' => $article->article_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_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-media-form?type=preview']),
                    'form' => $form,
                    'data_langs' => $article->getIsNewRecord()?$images:ArticleMedia::find()->where(['article_id' => $article->article_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_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_media_id' => $oneimage->article_media_id, 'remove_media' => true]) ?>',
                                ></a>
                            </div>
                            <?php
                        }
                    }
                }
                ?>
            </div>
        </div>

        <hr>

        <?php
            echo Multilanguage::widget([
                'data' => $article_langs,
                'form' => $form,
                'ajaxView' => '@common/modules/blog/views/ajax/_article_form',
            ]);
            /*
        $multilang = Multilang::begin(['ajaxpath' => Url::to(['/blog/ajax/article-form']), 'form' => $form, 'data_langs' => $article_langs]);
        ?>
            <?php
            $first = 1;
            foreach($article_langs as $index => $article_lang) {
                ?>
                <div role="" class="tab-pane <?php if($first) { echo 'active main-tab'; } ?>" id="<?=$multilang->id?>-<?=$index?>">

                    <?= $form->field($article_langs[$index], "[$index]language_id")->label(false)->hiddenInput(['value' => $index]) ?>

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

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

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

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

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

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

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

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

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

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

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

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

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