<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\models\CreditHistorique; 
use common\models\Etablissement;

$this->title = 'Créditer <small>'.Html::encode($user->nom.' '.$user->prenom).'</small>';
$this->params['breadcrumbs'][] = ['label' => 'Clients', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => Html::encode($user->nom.' '.$user->prenom)];
$this->params['breadcrumbs'][] = 'Créditer';

?>

<div class="user-credit">
    
    <?php
        $etablissement = Etablissement::find()
                    ->where(['id' => Yii::$app->user->identity->id])
                    ->one() ;
        if(!$etablissement->credit_pain)
        {
            echo '<div class="alert alert-warning">Attention, la fonctionnalité <strong>Crédit Pain</strong> est désactivée dans vos <a href="'.Yii::$app->urlManager->createurl(['etablissement/update']).'">paramètres</a>.'
                    . ' Pensez à l\'activer si vous souhaitez qu\'elle soit visible de vos clients.</div>' ;
        }
    ?>
    
    <div class="col-md-12">
        <h1><?= $this->title ?></h1>
        <?php $form = ActiveForm::begin(); ?>
        <?= $form->field($credit_historique, 'montant')->textInput() ?>
        <?= $form->field($credit_historique, 'moyen_paiement')->dropDownList([
            CreditHistorique::MOYEN_ESPECES => 'Espèces',
            CreditHistorique::MOYEN_CB => 'Carte bancaire',
            CreditHistorique::MOYEN_CHEQUE => 'Chèque',
        ]) ?>
        <div class="form-group">
            <?= Html::submitButton( 'Créditer', ['class' => 'btn btn-primary']) ?>
        </div>
        <?php ActiveForm::end(); ?>
    </div>
    
    <div class="col-md-12">
        <h2>Historique</h2>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Type</th>
                    <th>Montant</th>
                    <th>Paiement</th>
                </tr>
            </thead>
            <tbody>
                <?php if(count($historique)): ?>
                    <?php foreach($historique as $ch): ?>
                        <tr>
                            <td><?= date('d/m/Y à H:i:s',strtotime($ch->date)) ?></td>
                            <td>
                                <?php if($ch->type == CreditHistorique::TYPE_CREDIT_INITIAL): ?>Crédit initial<?php endif; ?>
                                <?php if($ch->type == CreditHistorique::TYPE_CREDIT): ?>Crédit<?php endif; ?>
                                <?php if($ch->type == CreditHistorique::TYPE_PAIEMENT): ?>
                                    Paiement commande 
                                        <?php if(isset($ch->commande)): ?>
                                            du <?= date('d/m/Y',strtotime($ch->commande->date)) ?>
                                        <?php else: ?>
                                            (supprimée)
                                        <?php endif; ?>
                                <?php endif; ?>
                                <?php if($ch->type == CreditHistorique::TYPE_REMBOURSEMENT): ?>Remboursement commande <?php if(isset($ch->commande)): ?>du <?= date('d/m/Y',strtotime($ch->commande->date)) ?><?php else: ?>(supprimée)<?php endif; ?><?php endif; ?>
                            </td>
                            <td>
                                <?= $ch->montant ; ?> €
                            </td>
                            <td>
                                <?php if($ch->moyen_paiement == CreditHistorique::MOYEN_ESPECES): ?>Espèces<?php endif; ?>
                                <?php if($ch->moyen_paiement == CreditHistorique::MOYEN_CB): ?>Carte bancaire<?php endif; ?>
                                <?php if($ch->moyen_paiement == CreditHistorique::MOYEN_CHEQUE): ?>Chèque<?php endif; ?>
                                <?php if(!$ch->moyen_paiement): ?>Crédit pain<?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr><td colspan="4">Aucun résultat</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
</div>