<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $refiere app\models\Refieren */
/* @var $form yii\widgets\ActiveForm */
?>

<script type="text/javascript">
    function registraBono() {
        if ($('#refieren-bono_id option:selected').val() == 0) {
<?php if (\Yii::$app->user->can('isAdmin')) { ?>
                $('#Bono').css('display', 'block');
<?php } ?>
        } else {
            $('#refieren-bono_otro').val($('#refieren-bono_id option:selected').val());
            $('#Bono').css('display', 'none');
        }
    }
</script>
<?php
$form = ActiveForm::begin();
?>
<div class="refieren-form">
    <div class="box box-solid">
        <div class="box-header with-border">
            <h3 class="box-title">Datos de quien Refiere</h3>
        </div>
        <div class="box-body">
            <div class="row margin">
                <div class="col-md-3">
                    <?php
                    echo $form->field($refiere, 'nombre')->widget(\yii\jui\AutoComplete::classname(), [
                        'clientOptions' => [
                            'source' => app\models\Clientes::find()->select(['CONCAT(nombre," ",apellidop," ",apellidom) as value', 'CONCAT(nombre," ",apellidop," ",apellidom, " - ", telefono) as label', 'id', 'telefono', 'dnref', 'email'])->asArray()->all(),
                            'minLength' => '2',
                            'autoFill' => true,
                            'select' => new \yii\web\JsExpression("function( event, ui ) {
                        $('#refieren-cliente_id').val(ui.item.id);
                        $('#refieren-telefono').val(ui.item.telefono);
                        $('#refieren-correo').val(ui.item.email);
                        }")
                        ],
                        'options' => ['style' => 'text-transform: uppercase', 'class' => 'form-control']
                    ]);
                    echo $form->field($refiere, 'cliente_id')->hiddenInput()->label(false);
                    ?>
                </div>
                <div class="col-md-3">
                    <?php
                    $bonoLista = \yii\helpers\ArrayHelper::map(app\models\Bono::find()->all(), 'id', 'bono');
                    if (\Yii::$app->user->can('isAdmin')) {
                        $bonoList = array_merge(['0' => 'Otro'], $bonoLista);
                    } else {
                        $bonoList = $bonoLista;
                    }
                    echo $form->field($refiere, 'bono_id')->dropDownList(
                            $bonoList, ['prompt' => '-Selecciona Bono-', 'onchange' => 'registraBono();']
                    );
                        ?>
                        <div id="Bono" style="display: none">
                            <?= $form->field($refiere, 'bono_otro')->textInput(['style' => 'text-transform: uppercase', 'maxlength' => false]) ?>
                            <?= $form->field($refiere, 'bono_otro_monto')->textInput(['style' => 'text-transform: uppercase', 'maxlength' => false]) ?>
                        </div>
                </div>
                <div class="col-md-3">
                    <?=
                    $form->field($refiere, 'telefono')->textInput(['maxlength' => true])->widget(\yii\widgets\MaskedInput::className(), [
                        'mask' => '999-999-9999',
                    ])
                    ?>
                </div>
                <div class="col-md-3">
                    <?= $form->field($refiere, 'correo')->textInput(['maxlength' => true]) ?>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="referidos-form">
    <div class="box box-success">
        <div class="box-header with-border">
            <h3 class="box-title">Datos de Referido</h3>
        </div>
        <div class="box-body">
            <div class="row margin">
                <div class="col-md-4">
                    <?= $form->field($referido, 'nombre')->textInput(['style' => 'text-transform: uppercase', 'maxlength' => true]) ?>
                </div>
                <div class="col-md-4">
                    <?=
                    $form->field($referido, 'telefono')->textInput(['maxlength' => true])->widget(\yii\widgets\MaskedInput::className(), [
                        'mask' => '999-999-9999',
                    ])
                    ?>
                </div>
                <div class="col-md-4">
                    <?= $form->field($referido, 'correo')->textInput(['maxlength' => true]) ?>
                </div>
            </div>
            <div class="row margin">
                <?= $form->field($refiere, 'observaciones')->textarea(['style' => 'text-transform: uppercase', 'maxlength' => true]) ?>
            </div>
        </div>
    </div>
</div>
<div class="form-group">
    <?= Html::submitButton($refiere->isNewRecord ? 'Registrar' : 'Actualizar', ['class' => $refiere->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>