<?php

use common\helpers\MeanPayment;
use common\helpers\Price;
use common\logic\Document\Document\Module\DocumentModule;
use  common\logic\Document\Invoice\Module\InvoiceModule;
use common\logic\Payment\Module\PaymentModule;
use yii\helpers\Html;
use yii\widgets\ActiveForm;

$documentModule = DocumentModule::getInstance();
$paymentManager = PaymentModule::getInstance();
 $invoiceModule = InvoiceModule::getInstance();

if($documentModule->isStatusValid($model)): ?>
    <div>
        <div class="panel panel-default">
            <div class="panel-heading">
                Règlement
            </div>
            <div class="panel-body">
                <?php if($model->payments && count($model->payments) > 0): ?>
                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th>Moyen de paiement</th>
                            <th>Date transaction</th>
                            <th>Montant</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach($model->payments as $payment): ?>
                            <tr>
                                <td><?= $paymentManager->getStrMeanPayment($payment); ?></td>
                                <td><?= $payment->date_transaction ? date('d/m/Y', strtotime($payment->date_transaction)) : '' ?></td>
                                <td><?= Price::format($payment->amount); ?></td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php endif; ?>

                <?php if( $invoiceModule->isDocumentInvoice($model) && ! $invoiceModule->isInvoicePaid($model)): ?>
                    <div class="row">
                        <?php $form = ActiveForm::begin(); ?>
                        <div class="col-md-3">
                            <?= $form->field($payment, 'mean_payment')->dropDownList(MeanPayment::getAll()); ?>
                        </div>
                        <div class="col-md-3">
                            <?= $form->field($payment, 'date_transaction')->textInput([
                                'class' => 'datepicker form-control'
                            ]); ?>
                        </div>
                        <div class="col-md-3">
                            <?= $form->field($payment, 'amount', [
                                'template' => '{label}<div class="input-group">{input}<span class="input-group-addon"><span class="glyphicon glyphicon-euro"></span></span></div>{hint}',
                            ])->textInput(); ?>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <br>
                                <?= Html::submitButton('Ajouter', ['class' => 'btn btn-primary']) ?>
                            </div>
                        </div>
                        <?php ActiveForm::end(); ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
<?php endif; ?>