<?php

/** @var yii\web\View $this */
/** @var common\forms\CustomerForm $form */
/** @var common\models\Customer[] $businesses */

use common\models\Customer;
use yii\bootstrap5\ActiveForm;
use yii\bootstrap5\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
?>

<?php $f = ActiveForm::begin(['id' => 'customer-form']) ?>

<div class="row g-3">
    <div class="col-md-6">
        <?= $f->field($form, 'name')->textInput(['placeholder' => 'Full name']) ?>
    </div>
    <div class="col-md-3">
        <?= $f->field($form, 'phone')->textInput(['placeholder' => '+91…']) ?>
    </div>
    <div class="col-md-3">
        <?= $f->field($form, 'email')->input('email') ?>
    </div>
    <div class="col-md-3">
        <?= $f->field($form, 'customer_type')->dropDownList(Customer::typeLabels(), [
            'id'      => 'customer-type',
            'onchange'=> 'toggleBusinessFields(this.value)',
        ]) ?>
    </div>
    <div class="col-md-9" id="business-fields" style="display:<?= $form->customer_type === Customer::TYPE_BUSINESS ? 'flex' : 'none' ?>; gap:1rem; flex-wrap:wrap;">
        <div class="flex-fill" style="min-width:200px">
            <?= $f->field($form, 'company_name')->textInput(['placeholder' => 'Company / Gym name']) ?>
        </div>
        <div style="min-width:160px">
            <?= $f->field($form, 'gst_number')->textInput(['placeholder' => 'GST Number']) ?>
        </div>
    </div>
    <div class="col-md-4">
        <?= $f->field($form, 'parent_customer_id')->dropDownList(
            array_merge(['' => '— No Parent —'], ArrayHelper::map($businesses, 'id', 'name'))
        ) ?>
    </div>
    <div class="col-md-8">
        <?= $f->field($form, 'address')->textarea(['rows' => 2, 'placeholder' => 'Full address']) ?>
    </div>
    <div class="col-md-8">
        <?= $f->field($form, 'notes')->textarea(['rows' => 2, 'placeholder' => 'Internal notes']) ?>
    </div>
    <div class="col-md-4 d-flex align-items-end pb-3">
        <?= $f->field($form, 'is_active')->checkbox(['label' => 'Active Customer']) ?>
    </div>
</div>

<div class="d-flex gap-2 mt-2">
    <?= Html::submitButton('<i class="bi bi-check-lg"></i> Save Customer', ['class' => 'btn btn-primary']) ?>
    <?= Html::a('Cancel', Url::to(['/customer/index']), ['class' => 'btn btn-outline-secondary']) ?>
</div>

<?php ActiveForm::end() ?>

<script>
function toggleBusinessFields(val) {
    document.getElementById('business-fields').style.display = val === 'business' ? 'flex' : 'none';
}
</script>
