<?php

use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use kartik\time\TimePicker;
use app\models\SystemConfigurations;
use kartik\file\FileInput;

$sub_categories_url = json_encode(Yii::$app->urlManager->createUrl('/items/get-sub-categories'));
$vars_limit = SystemConfigurations::find()->where(['config_option' => 'max_var_num'])->one()->value;
$images_limit = SystemConfigurations::find()->where(['config_option' => 'max_item_images'])->one()->value;
$lang = json_encode(Yii::$app->language);

$js=<<< JS

    var i = 2;
    var lang = $lang;

    $("#add-option").click(function(){
        var rowsCount = $('#variations-table').find('tr').length - 1;
        if (rowsCount < $vars_limit){
            $("#variations-table tr:last").after('<tr id="option_">' +
                    '<td> <input required name="Vars[var' + i + '][arabic_description]" class="form-control" type="text"> </td>' +
                    '<td> <input required name="Vars[var' + i + '][english_description]" class="form-control" type="text"> </td>' +
                    '<td> <input required name="Vars[var' + i + '][arabic_details]" class="form-control" type="text"> </td>' +
                    '<td> <input required name="Vars[var' + i + '][english_details]" class="form-control" type="text"> </td>' +
                    '<td> <input required name="Vars[var' + i + '][price]" class="form-control" type="number" min="0"> </td>' +
                    '<td style="text-align:center"> <input type="button" class="btn btn-danger" onClick="deleteRow(this);" value="' + (lang == 'ar' ? 'حذف' : 'Delete')  +'"> </td>' +
                '</tr>'
            );
            i++;
        }
    });

JS;
$this->registerJs($js);

$this->title = Yii::t('app', 'Create Item');
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Items'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="items-form">

    <h1><?= Html::encode($this->title) ?></h1>

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

        <div class="row">
            <div class="col-md-6 ">
                <?= $form->field($model, 'main_category_id')->dropDownList(ArrayHelper::map($main_categories, 'id', Yii::$app->language == 'en' ? 'english_name' : 'arabic_name'),[
                            'prompt' => Yii::t('app', 'Select Main Category'),
                            'onchange' => 'subCategories(this);'
                        ]) 
                ?>
            </div>
            <div class="col-md-6 ">
                <?= $form->field($model, 'sub_category_id')->dropDownList([], [
                        'prompt' => Yii::t('app', 'Select Sub Category'),
                        'class' => 'form-control sub_category',
                    ]) 
                ?>
            </div>
        </div>
        
        <div class="row">
            <div class="col-md-6 ">
                <?= $form->field($model, 'arabic_name')->textInput(['maxlength' => true]) ?>
            </div>
            
            <div class="col-md-6 ">
                <?= $form->field($model, 'english_name')->textInput(['maxlength' => true]) ?>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6 ">
                <?= $form->field($model, 'arabic_description')->textInput(['maxlength' => true]) ?>
            </div>
            <div class="col-md-6 ">
                <?= $form->field($model, 'english_description')->textInput(['maxlength' => true]) ?>
            </div>
             <div class="col-md-6 ">
                <?= $form->field($model, 'preperation_method_ar')->textInput(['maxlength' => true]) ?>
            </div>
            <div class="col-md-6 ">
                <?= $form->field($model, 'preperation_method_en')->textInput(['maxlength' => true]) ?>
            </div>
            <div class="col-md-6">
                <?= $form->field($model, 'arabic_additional_sauces')->textArea(['rows' => 1, 'style' => 'resize:none;'])->label() ?>
            </div>
            <div class="col-md-6">
                <?= $form->field($model, 'english_additional_sauces')->textArea(['rows' => 1, 'style' => 'resize:none;'])->label() ?>
            </div>
        </div>
        
        
        
        <div class="row">
        <div class="col-md-3 ">
                <?= $form->field($model, 'calories')->textInput(['maxlength' => true, 'type' => 'number', 'min' => 1])->label() ?>
            </div>
            <div class="col-md-3 <?= $lang == 'ar' ? 'col-md-push-9' : 'col-md-6' ?>">
                <?= $form->field($model, 'preparation_time')->textInput(['value' => 10,'maxlength' => true, 'type' => 'number']) 
                ?>
            </div>
            <div class="col-md-3 <?= $lang == 'ar' ? 'col-md-push-6' : '' ?>">
                <?= $form->field($model, 'status')->dropDownList(['Active' => Yii::t('app', 'Active'), 'Inactive' => Yii::t('app', 'Inactive')], ['placeholder' => 'Select Status']) ?>
            </div>
            <div class="col-md-3 <?= $lang == 'ar' ? 'col-md-push-6' : '' ?>">
                <?= $form->field($model, 'is_new')->dropDownList([0 => Yii::t('app', 'No'), 1 => Yii::t('app', 'Yes')], ['placeholder' => 'Select Status']) ?>
            </div>
        </div>
    <div class="row">
            <div class="col-md-12 ">
                <?= $form->field($images, 'image_path[]')->widget(FileInput::classname(), [
                    'options' => ['multiple' => true, 'accept' => 'image/*'],
                    'pluginOptions' => ['maxFileCount' => $images_limit,'showUpload' => false,]
                ]) ?>
            </div>
    </div>
    <div class="row">
        
        <div class="col-md-12">
        <h2><?= Yii::t('app', 'Price Variations') ?>:</h2>

        <table id= "variations-table" class='table table-bordered table-striped'>
            <tr>
                <th width='10%' style="text-align:center"><?= Yii::t('app', 'Arabic Description') ?></th>
                <th width='10%' style="text-align:center"><?= Yii::t('app', 'English Description') ?></th>
                <th width='30%' style="text-align:center"><?= Yii::t('app', 'Arabic Details') ?></th>
                <th width='30%' style="text-align:center"><?= Yii::t('app', 'English Details') ?></th>
                <th width='10%' style="text-align:center"><?= Yii::t('app', 'Price') ?></th>
                <th width='15%' style="text-align:center"><?= Html::Button(Yii::t('app', 'Add Variation'), ['class' => 'btn btn-primary', 'id' => 'add-option']) ?></th>
            </tr>
            <tr>
                <td><?= $form->field($item_vars, 'arabic_description')->textInput()->label(false); ?></td>
                <td><?= $form->field($item_vars, 'english_description')->textInput()->label(false); ?></td>
                <td><?= $form->field($item_vars, 'arabic_details')->textInput()->label(false); ?></td>
                <td><?= $form->field($item_vars, 'english_details')->textInput()->label(false); ?></td>
                <td><?= $form->field($item_vars, 'price')->textInput(['type' => 'number', 'min' => 0])->label(false); ?></td>
                <td style="text-align:center">-</td>
            </tr>
        </table>
        </div>
    </div>
    

    <div class="form-group">
        <?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>
    </div>

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

</div>

<script>
    var subCategories = function(this_sub_category){
        var _this = this_sub_category;
        var val = $(_this).val();
        $.ajax({
            method: 'POST',
            url: <?= $sub_categories_url ?>,
            data: {val: val},
            success: function(response){
                var parsed_response = JSON.parse(response);
                $('#items-sub_category_id').html(parsed_response);
            },
            error: function(error){
                
            }
        });
    };

    function deleteRow(e){
        $(e).closest('tr').remove();
    }
</script>