<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use  \app\models\base\Story;
use  \app\models\base\NewsTopic;
use  \app\models\base\Tags;

$this->title = 'Добавить новый блок';
$this->params['breadcrumbs'][] = ['label' => 'Блоки', 'url' => ['blocks']];
$this->params['breadcrumbs'][] = $this->title;

$form = ActiveForm::begin([
    'id' => 'design-form',
    'options' => ['class' => 'form-horizontal', 'enctype' => 'multipart/form-data']]);

?>
<input type="hidden" name="Design[id]" value="<?=$model->id?>">

<div class="row">
	<div class="col-sm-7">
		<div class="input-group">
			<label class="control-label col-form-label mr-3" >Источник</label>
			<select class="custom-select custom-select-sm" name="Design[type]" id="design-type">
				<option value="0" disabled selected hidden>Источник модуля</option>
				<option value="story" <?=($model->type == 'story')?'selected':''?> data-toggle="collapse" data-target="#collapseStory" aria-expanded="true" aria-controls="collapseStory">Сюжет</option>
				<option value="topic" <?=($model->type == 'topic')?'selected':''?> data-toggle="collapse" data-target="#collapseTopic" aria-expanded="false" aria-controls="collapseTopic">Рубрика</option>
				<option value="tag" <?=($model->type == 'tag')?'selected':''?> data-toggle="collapse" data-target="#collapseTag" aria-expanded="false" aria-controls="collapseTag">Тэг</option>
			</select>
		</div>
	</div>
</div>
<!-- Тэги --->
<div class="card-body col-sm-7">
	<div class="collapse <?=($model->type == 'tag')?'show':''?>" id="collapseTag">
		<div class="input-group">
			<label class="control-label col-form-label mr-3" for="tag">Тэг</label>
			<div class="input-group-prepend">
				<span class="btn btn-primary" id="validatedInputGroupPrepend"><i class="fas fa-search"></i></span>
			</div>
			<input type="text" class="form-control" aria-describedby="validatedInputGroupPrepend" id="tag">
		</div>
<?
	$smodel = new Tags();
	if( $model->type == 'tag' && $model->parent_id > 0 ){
		$s = $smodel->findOne(['id' => $model->parent_id]);
	}
?>
		<div class="custom-control custom-checkbox">
			<div id="tag_items">
<?
	if( isset($s) && $s )
	{
		echo '<div class="custom-control custom-checkbox"><input class="custom-control-input" type="checkbox" name="Design[Tag]" value="'.$s->id.'" checked id="tagi'.$s->id.'"><label class="custom-control-label" for="tagi'.$s->id.'">'.$s->title."</label></div>";
	}
?>
			</div>
		</div>
	</div>
<!-- #################### --->

<!-- сюжеты --->
<div class="card-body col-sm-7">
	<div class="collapse <?=($model->type == 'story')?'show':''?>" id="collapseStory">
		<div class="input-group">
			<label class="control-label col-form-label mr-3" for="story">Сюжет</label>
			<div class="input-group-prepend">
				<span class="btn btn-primary" id="validatedInputGroupPrepend"><i class="fas fa-search"></i></span>
			</div>
			<input type="text" class="form-control" aria-describedby="validatedInputGroupPrepend" id="story">
		</div>
<?
	$smodel = new Story();
	if( $model->type == 'story' && $model->parent_id > 0 ){
		$s = $smodel->findOne(['id' => $model->parent_id]);
	}
?>
		<div class="custom-control custom-checkbox">
			<div id="story_items">
<?
	if( isset($s) && $s )
	{
		echo '<div class="custom-control custom-checkbox"><input class="custom-control-input" type="checkbox" name="Design[Story]" value="'.$s->id.'" checked id="storyi'.$s->id.'"><label class="custom-control-label" for="storyi'.$s->id.'">'.$s->title."</label></div>";
	}
?>
			</div>
		</div>
	</div>
<!-- #################### --->

<!-- Рубрики --->
	<div class="collapse <?=($model->type == 'topic')?'show':''?>" id="collapseTopic">
		<div class="input-group">
		<label class="control-label col-form-label mr-3" for="design-topic">Рубрика</label>
		<select class="custom-select custom-select-sm" name="Design[Topic]" id="design-topic">
<?
		$topics = new \yii\data\ActiveDataProvider(
			[
				"query"=>\app\models\base\NewsTopic::find()->where(['active'=>1])->orderBy(["topic_id"=>SORT_ASC,"order"=>SORT_ASC]),
				"pagination" => false
			]
		);

foreach( $topics->getModels() as $item ){
?>
			<option value="<?=$item->id?>" <?=($model->parent_id == $item->id)?'selected':''?>><?=$item->title?></option>
<?
}
?>
		</select>
		</div>
	</div>

</div>


<div class="row">
	<div class="col-sm-7">
		<div class="input-group">
			<label class="control-label col-form-label mr-3" >Дизайн блока</label>
			<select class="custom-select custom-select-sm" name="Design[design]" id="design-type">
				<option value="0" disabled selected hidden>Вариант вывода</option>
				<option value="1" <?=($model->design == 1)?'selected':''?>>1 большой+реклама</option>
				<option value="2" <?=($model->design == 2)?'selected':''?>>только текст</option>
				<option value="3" <?=($model->design == 3)?'selected':''?>>Стандартный сюжет</option>
				<option value="4" <?=($model->design == 4)?'selected':''?>>3 с фото</option>
				<option value="5" <?=($model->design == 5)?'selected':''?>>Радужная</option>
				<option value="6" <?=($model->design == 6)?'selected':''?>>9may</option>
			</select>
		</div>
	</div>
</div>


<div class="row">
	<div class="col-sm-7">
		<div class="position-relative p-3 my-4 bg-gray ">
			<div class="ribbon-wrapper">
				<div class="ribbon bg-primary">Подмена</div>
			</div>
			<div class="form-group field-news-title required">
				<label class="control-label col-form-label" for="news-title">Заголовок</label>
				<input type="text" id="news-title" class="form-control col-sm-9" name="Design[title]" placeholder="Заголовок блока" aria-required="true" value='<?=str_replace("'", '&apos;', $model->title)?>'>
			</div>
			<div class="form-group">
				<label class="control-label" for="news-link">Прямой переход на URL</label>
				<input type="text" class="form-control col-sm-9" name="Design[url]" id="news-link" value="<?=$model->url?>">
			</div>
			<small class="form-text my-0">Если нужны индивидуальные настройки, если не заполнить берётся автоматом из сюжета или другого источника</small>
		</div>
	</div>
</div>

<div class="form-group form-check">
	<div class="btn-group">
		<label class="btn btn-primary active">
			<input type="checkbox" name="Design[active]" id="active" style="width:3vw;height:3vh;top: 3px;position: relative;" class="active"<?=($model->active == 'Y')?' checked':''?> value="Y"><i class="far fa-play-circle"></i> Показывать</label>
		</label>
	</div>
</div>
<div class="form-group"><?
echo Html::submitButton('Send', ['class' => 'btn btn-success']);
?>
</div>

<script>
$(function() {
    var cache = {};
    var cache2 = {};
    $( "#story" ).autocomplete({
        source: function( request, response ) {
            var term = request.term;
            if ( term in cache ) {
                response( cache[ term ] );
                return;
            }

            $.getJSON( "/manager/news/ajaxstory", {'q':request.term}, function( data, status, xhr ) {
				if( data.status == 'ok' ){
					cache[ term ] = data.item;
					response( data.item );
				}
            });
        },
        minLength: 3,
        select: function( event, ui ) {
            if( $( "#story_items  :input[value='"+ui.item.id+"']" ).val() != ui.item.id ){
                $( "#story_items" ).html('<div class="custom-control custom-checkbox"><input class="custom-control-input" type="checkbox" name="Design[Story]" value="'+ui.item.id+'" checked id="storyi'+ui.item.id+'"><label class="custom-control-label" for="storyi'+ui.item.id+'">'+ ui.item.label +"</label></div>");
            }
        },
    });

    $( "#tag" ).autocomplete({
        source: function( request, response ) {
            var term = request.term;
            if ( term in cache2 ) {
                response( cache2[ term ] );
                return;
            }

            $.getJSON( "/manager/tags/ajaxtag", {'q':request.term}, function( data, status, xhr ) {
				if( data.status == 'ok' ){
					cache2[ term ] = data.item;
					response( data.item );
				}
            });
        },
        minLength: 3,
        select: function( event, ui ) {
            if( $( "#tag_items  :input[value='"+ui.item.id+"']" ).val() != ui.item.id ){
                $( "#tag_items" ).html('<div class="custom-control custom-checkbox"><input class="custom-control-input" type="checkbox" name="Design[Tag]" value="'+ui.item.id+'" checked id="tagi'+ui.item.id+'"><label class="custom-control-label" for="tagi'+ui.item.id+'">'+ ui.item.label +"</label></div>");
            }
        },
    });



	$('.collapse').on('show.bs.collapse', function () {
		$('.collapse').collapse('hide');
	});

});
</script>
<?
ActiveForm::end();

