<?php
/**
 * UPN Delivery Module – Create Consignment View
 */

use yii\helpers\Html;
use yii\bootstrap4\ActiveForm;
use PortalPeople\upn\models\ConsignmentForm;

/**
 * @var yii\web\View $this
 * @var PortalPeople\upn\models\ConsignmentForm $model
 */

$this->title = 'Create UPN Pallet Consignment';
$this->params['breadcrumbs'][] = ['label' => 'UPN Delivery', 'url' => ['/upn/config']];
$this->params['breadcrumbs'][] = $this->title;

// Show/hide delivery datetime when Timed or Booked service is selected
$this->registerJs(<<<JS
    function toggleDeliveryDateTime() {
        var svc   = parseInt($('#consignmentform-additionalsvc').val());
        var extra = parseInt($('#consignmentform-extrasvc').val());
        var show  = (svc === 7 || svc === 10 || extra === 12);
        $('#delivery-datetime-row').toggle(show);
    }

    $('#consignmentform-additionalsvc, #consignmentform-extrasvc').on('change', toggleDeliveryDateTime);
    toggleDeliveryDateTime();
JS
);
?>

<div class="upn-consignment-create">

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

    <?php if (Yii::$app->session->hasFlash('success')): ?>
        <div class="alert alert-success"><?= Html::encode(Yii::$app->session->getFlash('success')) ?></div>
    <?php endif; ?>
    <?php if (Yii::$app->session->hasFlash('error')): ?>
        <div class="alert alert-danger"><?= Html::encode(Yii::$app->session->getFlash('error')) ?></div>
    <?php endif; ?>

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

    <!-- References & Despatch Date -->
    <div class="card mb-4">
        <div class="card-header">Consignment Reference</div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-4">
                    <?= $form->field($model, 'custRef')->textInput(['maxlength' => 15])
                        ->hint('Unique reference, max 15 characters.') ?>
                </div>
                <div class="col-md-4">
                    <?= $form->field($model, 'additionalRef')->textInput(['maxlength' => 30])
                        ->hint('Secondary reference (optional).') ?>
                </div>
                <div class="col-md-4">
                    <?= $form->field($model, 'despatchDate')->input('date')
                        ->hint('Leave blank to default to today.') ?>
                </div>
            </div>
        </div>
    </div>

    <!-- Delivery Address -->
    <div class="card mb-4">
        <div class="card-header">Delivery Address</div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <?= $form->field($model, 'deliveryCoName')->textInput(['maxlength' => 100]) ?>
                </div>
                <div class="col-md-6">
                    <?= $form->field($model, 'contactName')->textInput(['maxlength' => 100]) ?>
                </div>
            </div>
            <?= $form->field($model, 'deliveryAdd1')->textInput(['maxlength' => 100]) ?>
            <?= $form->field($model, 'deliveryAdd2')->textInput(['maxlength' => 100]) ?>
            <div class="row">
                <div class="col-md-4">
                    <?= $form->field($model, 'deliveryTown')->textInput(['maxlength' => 100]) ?>
                </div>
                <div class="col-md-4">
                    <?= $form->field($model, 'deliveryCounty')->textInput(['maxlength' => 100]) ?>
                </div>
                <div class="col-md-4">
                    <?= $form->field($model, 'deliveryPostcode')->textInput(['maxlength' => 8]) ?>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <?= $form->field($model, 'contactPhone')->textInput(['maxlength' => 15]) ?>
                </div>
                <div class="col-md-6">
                    <?= $form->field($model, 'contactEmail')->input('email', ['maxlength' => 100]) ?>
                </div>
            </div>
        </div>
    </div>

    <!-- Service Selection -->
    <div class="card mb-4">
        <div class="card-header">Service</div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-3">
                    <?= $form->field($model, 'mainSvc')->dropDownList(ConsignmentForm::mainSvcOptions()) ?>
                </div>
                <div class="col-md-3">
                    <?= $form->field($model, 'additionalSvc')->dropDownList(ConsignmentForm::additionalSvcOptions())
                        ->hint('Premium service add-on (optional).') ?>
                </div>
                <div class="col-md-3">
                    <?= $form->field($model, 'extraSvc')->dropDownList(ConsignmentForm::extraSvcOptions())
                        ->hint('Book In or Booked service (optional).') ?>
                </div>
                <div class="col-md-3">
                    <?= $form->field($model, 'tailLift')->checkbox()->hint('Check if a tail lift is required at the delivery point.') ?>
                </div>
            </div>
            <div class="row" id="delivery-datetime-row" style="display:none;">
                <div class="col-md-6">
                    <?= $form->field($model, 'deliveryDateTime')->input('datetime-local')
                        ->hint('Required for Timed or Booked service.') ?>
                </div>
            </div>
        </div>
    </div>

    <!-- Pallet Details -->
    <div class="card mb-4">
        <div class="card-header">Pallet Details</div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-3">
                    <?= $form->field($model, 'palletType')->dropDownList(ConsignmentForm::palletTypeOptions()) ?>
                </div>
                <div class="col-md-3">
                    <?= $form->field($model, 'palletSize')->dropDownList(ConsignmentForm::palletSizeOptions()) ?>
                </div>
                <div class="col-md-2">
                    <?= $form->field($model, 'totalPallets')->textInput(['type' => 'number', 'min' => 1])
                        ->hint('All pallets must be the same type and size.') ?>
                </div>
                <div class="col-md-2">
                    <?= $form->field($model, 'totalWeight')->textInput(['type' => 'number', 'min' => 1])
                        ->hint('Total weight in kg.') ?>
                </div>
            </div>
            <?= $form->field($model, 'palletDescription')->textInput(['maxlength' => 255])
                ->hint('Free text description of pallet contents (optional).') ?>
            <?= $form->field($model, 'specialInstructions')->textInput(['maxlength' => 255]) ?>
        </div>
    </div>

    <!-- Additional Options -->
    <div class="card mb-4">
        <div class="card-header">Additional Options</div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-4">
                    <?= $form->field($model, 'paperworkType')->dropDownList(ConsignmentForm::paperworkTypeOptions()) ?>
                </div>
                <div class="col-md-2">
                    <?= $form->field($model, 'custPaperwork')->textInput(['type' => 'number', 'min' => 0])
                        ->hint('Number of paperwork pages.') ?>
                </div>
                <div class="col-md-3">
                    <?= $form->field($model, 'localArea')->textInput(['type' => 'number', 'min' => 0])
                        ->hint('0 = network routing; 1 = local only; or enter a depot number.') ?>
                </div>
            </div>
        </div>
    </div>

    <div class="form-group">
        <?= Html::submitButton('Create Consignment', ['class' => 'btn btn-success']) ?>
        <?= Html::a('Cancel', ['/upn/config'], ['class' => 'btn btn-secondary']) ?>
    </div>

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

</div>
