<?php
    
    use common\models\ProductToProject;
    use common\models\Project;
    use common\modules\product\models\Product;
    use yii\helpers\Html;
    use yii\web\View;
    use yii\widgets\ActiveForm;
    
    /**
     * @var View             $this
     * @var Project          $project
     * @var Product[]        $products
     * @var ProductToProject $model
     * @var array            $variants
     */
    if($model->isNewRecord) {
        $this->title = \Yii::t('app', 'Create product to project');
    } else {
        $this->title = \Yii::t('app', 'Update product to project');
    }
    $this->params[ 'breadcrumbs' ][] = [
        'label' => \Yii::t('app', 'Project'),
        'url'   => [
            'index',
        ],
    ];
    $this->params[ 'breadcrumbs' ][] = [
        'label' => \Yii::t('app', 'Products to project' . ': ' . $project->lang->title),
        'url'   => [
            'product',
            'id' => $project->project_id,
        ],
    ];
    $this->params[ 'breadcrumbs' ][] = $this->title;
?>
    <div class="articles-create">
        <h1><?= Html::encode($this->title) ?></h1>
        <?php
            $form = ActiveForm::begin();
            echo $form->field($model, 'product_id')
                      ->dropDownList($products, [ 'prompt' => \Yii::t('app', 'Выберите коллекцию') ]);
        ?>
        <div class="<?php echo( !empty( $variants ) ? '' : 'hidden' ); ?> product_variant_container">
            <?php
                echo $form->field($model, 'product_variant_id')
                          ->dropDownList($variants, [ 'prompt' => \Yii::t('app', 'Выберите товар') ]);
            ?>
        </div>
        <?php
            echo Html::submitInput(( $model->isNewRecord ? \Yii::t('app', 'Добавить') : \Yii::t('app', 'Обновить') ), [ 'class' => 'btn btn-success' ]);
            $form::end();
        ?>
    </div>
<?php
    $this->registerJs("
          $(document).on(
                'change', '#producttoproject-product_id', function(e)
                {
                    var value = parseInt($(this).val());
                    var dropDown = $('#producttoproject-product_variant_id');
                    var container = $(dropDown).parents('.product_variant_container');
                    $(dropDown).find('option[value!=\"\"]').remove();
                    $(container).addClass('hidden');
                    if(value)
                    {
                        $.get(
                            '/admin/project/get-variants?product_id=' + value, function(data)
                            {
                                if(data.length > 0)
                                {
                                    $.each(
                                        data, function(key, value)
                                        {
                                       
                                            $(dropDown)
                                            .append('<option value=\"' + value.product_variant_id + '\">' + value.lang.name + '</option>');
                                        }
                                    );
                                    $(container).removeClass('hidden');
                                }
                            }
                        );
                    }
                }
            );
");
?>