_form.php 11.4 KB
<?php
    
    use kartik\select2\Select2;
    use yii\bootstrap\Html;
    use yii\helpers\Url;
    use yii\web\JsExpression;
    use yii\web\View;
    use yii\widgets\ActiveForm;
    
    /**
     * @var \yii\web\View              $this
     * @var \artbox\order\models\Order $model
     * @var array                      $labels
     * @var array                      $deliveries
     * @var array                      $payments
     * @var ActiveForm                 $form
     */
    
    $js = <<< JS
$(document).on('click', '#create-add', function(e) {
  e.preventDefault();
  var variant_id = $('#add-to-order').val();
  var count = $('#count-to-order').val();
  
  $.ajax({
      url: '/admin/order-product/add',
      type: "GET",
      data: {
          variant_id: variant_id,
          count: count
      },
      success: function(data) {
          if (data.id != 0) {
             var row = $('#products #' + data.id);
             if (row.length !== 0) {
                 row.find('.count-input').val(function(i, oldVal) {
                   return parseInt(oldVal, 10) + data.count;
                 });
             } else {
                $('#products').append(data.row);                 
             }
          }
      }
  });
});
$(document).on('click', '.delete-product-row', function(e) {
  e.preventDefault();
  $(this).parent().parent().remove();
})
JS;
    
    $this->registerJs($js, View::POS_READY);

?>

<div class="order-form">
    
    <?php $form = ActiveForm::begin(); ?>
    
    <?php
        if ($model->isNewRecord) {
            echo $form->field($model, 'user_id')
                      ->textInput();
        }
    ?>
    
    <?= $form->field($model, 'name')
             ->textInput([ 'maxlength' => true ]) ?>
    
    <?= $form->field($model, 'phone')
             ->textInput([ 'maxlength' => true ]) ?>
    
    <?= $form->field($model, 'email')
             ->textInput([ 'maxlength' => true ]) ?>
    
    <?= $form->field($model, 'city')
             ->textInput([ 'maxlength' => true ]) ?>
    
    <?= $form->field($model, 'address')
             ->textInput([ 'maxlength' => true ]) ?>
    
    <?= $form->field($model, 'comment')
             ->textInput([ 'maxlength' => true ]) ?>
    
    <?= $form->field($model, 'label_id')
             ->dropDownList($labels) ?>
    
    <?= $form->field($model, 'delivery_id')
             ->dropDownList($deliveries) ?>
    
    <?= $form->field($model, 'payment_id')
             ->dropDownList($payments) ?>
    <?php
        $sum = 0;
        if (!$model->isNewRecord) {
            ?>
          <div class="order-product-container">
            <div id="order-product-pjax" style="position: relative;">
              <div class="row strong">
                <div class="col-md-4">
                    <?php
                        echo Html::tag('strong', \Yii::t('order', 'Product'));
                    ?>
                </div>
                <div class="col-md-4">
                    <?php
                        echo Html::tag('strong', \Yii::t('order', 'Price'));
                    ?>
                </div>
                <div class="col-md-4">
                    <?php
                        echo Html::tag('strong', \Yii::t('order', 'Count'));
                    ?>
                </div>
              </div>
    
                <?php
                 
                    foreach ($model->orderProducts as $index => $orderProduct) {
                      $sum += ($orderProduct->count * $orderProduct->price)
                        ?>
                      <div class="row row-order-product" data-price="<?=$orderProduct->price?>">
                        <div class="col-md-4">
                            <?php
                                echo $form->field($orderProduct, "[$index]variant_id")
                                          ->hiddenInput()
                                          ->label(false);
                                echo $orderProduct->variant->product->lang->title . '(' . $orderProduct->variant->sku . ')';
                            ?>
                        </div>
                        <div class="col-md-4">
                            <?php echo $orderProduct->price; ?>
                        </div>
                        <div class="col-md-3">
                            <?php
                                echo $form->field($orderProduct, "[$index]count")
                                          ->textInput(['class' => 'count'])
                                          ->label(false);
                            ?>
                        </div>
                        <div class="col-md-1">
                            <?php
                                echo Html::a(
                                    Html::icon(
                                        'trash-o',
                                        [
                                            'prefix' => 'fa fa-',
                                        ]
                                    ),
                                    '#',
                                    [
                                        'class' => 'remove-order-product',
                                        'data'  => [
                                            'variant' => $orderProduct->variant->id,
                                            'id'      => $model->id,
                                        ],
                                    ]
                                )
                            ?>
                        </div>
                      </div>
                        <?php
                    }
                ?>
            </div>
            <div class="row">
              <div class="col-md-8">
                  <?php
                      echo Select2::widget(
                          [
                              'name'          => 'add-to-order',
                              'options'       => [
                                  'placeholder' => \Yii::t('order', 'Select product'),
                              ],
                              'pluginOptions' => [
                                  'allowClear'         => true,
                                  'minimumInputLength' => 3,
                                  'language'           => [
                                      'errorLoading' => new JsExpression(
                                          "function() {return '" . \Yii::t('order', 'Waiting for results') . "'; }"
                                      ),
                                  ],
                                  'ajax'               => [
                                      'url'      => Url::to([ 'product-list' ]),
                                      'dataType' => 'json',
                                      'data'     => new JsExpression('function(params) { return {q:params.term}; }'),
                                  ],
                                  'escapeMarkup'       => new JsExpression('function (markup) { return markup; }'),
                                  'templateResult'     => new JsExpression('function(city) { return city.text; }'),
                                  'templateSelection'  => new JsExpression('function (city) { return city.text; }'),
                              ],
                              'id'            => 'add-to-order',
                          ]
                      );
                  ?>
              </div>
              <div class="col-md-3">
                  <?php
                      echo Html::textInput(
                          'count-to-order',
                          null,
                          [
                              'class' => 'form-control',
                              'id'    => 'count-to-order',
                          ]
                      );
                  ?>
              </div>
              <div class="col-md-1">
                  <?php
                      echo Html::a(
                          Html::icon(
                              'plus-circle',
                              [
                                  'prefix' => 'fa fa-',
                              ]
                          ),
                          '#',
                          [
                              'class'   => 'variant-to-order',
                              'data-id' => $model->id,
                          ]
                      );
                  ?>
              </div>
            </div>
          </div>
            <?php
        } else {
            ?>
          <div class="row">
            <div class="col-md-8">
                <?php
                    echo Select2::widget(
                        [
                            'name'          => 'add-to-order',
                            'options'       => [
                                'placeholder' => \Yii::t('order', 'Select product'),
                            ],
                            'pluginOptions' => [
                                'allowClear'         => true,
                                'minimumInputLength' => 3,
                                'language'           => [
                                    'errorLoading' => new JsExpression(
                                        "function() {return '" . \Yii::t('order', 'Waiting for results') . "'; }"
                                    ),
                                ],
                                'ajax'               => [
                                    'url'      => Url::to([ 'product-list' ]),
                                    'dataType' => 'json',
                                    'data'     => new JsExpression('function(params) { return {q:params.term}; }'),
                                ],
                                'escapeMarkup'       => new JsExpression('function (markup) { return markup; }'),
                                'templateResult'     => new JsExpression('function(city) { return city.text; }'),
                                'templateSelection'  => new JsExpression('function (city) { return city.text; }'),
                            ],
                            'id'            => 'add-to-order',
                        ]
                    );
                ?>
            </div>
            <div class="col-md-3">
                <?php
                    echo Html::input(
                        'number',
                        'count-to-order',
                        null,
                        [
                            'class' => 'form-control',
                            'id'    => 'count-to-order',
                        ]
                    );
                ?>
            </div>
            <div class="col-md-1">
                <?php
                    echo Html::a(
                        '<i class="fa fa-plus"></i>',
                        '#',
                        [
                            'class' => 'btn btn-success',
                            'id'    => 'create-add',
                        ]
                    );
                ?>
            </div>
          </div>
  
          <div id="products"></div>
          
            <?php
        }
    ?>
  
  <div class="ln_solid"></div>
  <div class="form-group sum_all">Всего: <p><?=$sum?></p></div>
  <div class="form-group">
      <?= Html::submitButton(
          $model->isNewRecord ? Yii::t('order', 'Create') : Yii::t('order', 'Update'),
          [ 'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary' ]
      ) ?>
  </div>
    
    <?php ActiveForm::end(); ?>

</div>