<?php

use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use yii\web\View;

/* @var $this yii\web\View */
/* @var $model backend\models\Chapter */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="chapter-form">

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

    <?=
    $form->field($model, 'bookId')->dropDownList(ArrayHelper::map(\backend\models\Book::find()->all(), 'id', 'title'), array('prompt' => 'Select Book', 'disabled' => 'disabled'))
    ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => 255]) ?>

    <?= $form->field($model, 'chapterIndex')->textInput(['maxlength' => 255]) ?>
    <?=
    $form->field($model, 'type')->dropDownList(['0' => 'PAGES', '1' => 'PDF', '2' => 'EPUB', '3' => 'VIDEO', '4' => 'MP3', '6' => 'Embed',], ['prompt' => 'Select Type'])
    ?>


    <?php
    $styleDisplay = '';
    if ($model->isNewRecord || $model->type != 6){
        $styleDisplay = '';
        echo $form->field($model, 'directURL')->textInput();
    }
    else{
        $styleDisplay = 'display:none';
        echo $form->field($model, 'directURL')->textInput()->label('Embed (embed must start with &lt;iframe... and have no width and height property.)');
    }

    ?>

    <div id="fileInput" style="<?php echo $styleDisplay?>">
        <?= $form->field($model, 'attach')->fileInput(); ?>
        <?php
        if ($model->id):
            ?>
            <div class="form-group">

                <?php echo $model->attachment ?>
            </div>
        <?php endif; ?>
    </div>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
        &nbsp;<a href="javascript:history.go(-1)" class="btn btn-danger">Back</a>
    </div>

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

</div>

<?php
$this->registerJs(' $("#chapter-type").change(function(){
   var type = $("#chapter-type").val();
   var uu = $("#chapter-directurl");
   var strLabel = uu.prev();

   if(type == 6) // type custom html
   {
        $("#directUrl").show();
        $("#fileInput").hide();
        strLabel.html("Embed (embed must start with &lt;iframe... and have no width and height property.)");
   }
   else  // type orther
   {
        $("#directUrl").show();
        $("#fileInput").show();
         strLabel.html("Direct URL (priority)");
   }
   
});', View::POS_READY);
?>
