{include="header"}

{if condition="$notice != ''"}
    <div id="notice"><p>{$notice}</p></div>
{/if}

<div id="quick_summary">
    <h2>Bilan</h2>
    {if condition="$current_user->getAdmin()"}
        <p class="center">Lire <em>ligne</em> doit <em>case</em> {$currency} à <em>colonne</em>. Vous pouvez cliquer sur les liens pour confirmer les remboursements.
        <table id="balance_table"> 
            <tr>
                <th>Doit\À</th>
                {loop="users"}
                <th>{$value->getDisplayName()}</th>
                {/loop}
            </tr>
            {loop="users"}
                <tr>
                    <th>{$value->getDisplayName()}</th>
                    {loop="users"}
                        {if condition="$balances[$value1->getId()][$value2->getId()] === 'X'"}
                        <td class="cell-disabled"></td>
                        {elseif condition="$balances[$value1->getId()][$value2->getId()] === '-' || ($value2->getId() != $current_user->getId() && !$current_user->getAdmin())"}
                            <td>{$balances[$value1->getId()][$value2->getId()]}</td>
                        {else}
                            <td><a href="?do=payall&amp;from={$value1->getId()}&amp;to={$value2->getId()}&amp;token={$token}">{$balances[$value1->getId()][$value2->getId()]}</a></td>
                        {/if}
                    {/loop}
                </tr>
            {/loop}
        </table>
    {else}
        <table>
            <tr>
                <th>Utilisateurs</th>
                <th>Vous devez</th>
                <th>Il vous doit</th>
            </tr>
            {loop="$users"}
                {if condition="$balances[$value->getId()][$current_user->getId()] !== 'X' && $balances[$value->getId()][$current_user->getId()] !== '-'"}
                    </tr>
                        <td>{$value->getDisplayName()}</td>
                        <td>-</td>
                        <td>{$balances[$value->getId()][$current_user->getId()]}{$currency}</td>
                    </tr>
                {elseif condition="$balances[$current_user->getId()][$value->getId()] !== 'X' && $balances[$current_user->getId()][$value->getId()] !== '-'"}
                    </tr>
                        <td>{$value->getDisplayName()}</td>
                        <td>{$balances[$current_user->getId()][$value->getId()]}{$currency}</td>
                        <td>-</td>
                    </tr>
                {/if}
            {/loop}
        </table>
        <p class="center">Solde : {if condition="$user_balance > 0"}+{/if}{$user_balance}{$currency}</p>
    {/if}
</div>
<div id="detailed_summary">
    <h2>Liste détaillée des dépenses{if condition="$all == 0"} du dernier mois{/if}</h2>

    {if condition="$all == 0"}
        <p class="center"><a href="?all=1">Afficher toutes les dépenses</a></p>
    {else}
        <p class="center"><a href="?all=0">Afficher uniquement les dépenses du dernier mois</a></p>
    {/if}

    {if condition="$invoices !== false && count($invoices)>=1"}
        <table id="list_expenses">
            <tr>
                <th>Date</th>
                <th>Payé par</th>
                <th>Participants</th>
                <th>Montant</th>
                <th>Quoi ?</th>
                <th>Modifier</th>
                <th>Supprimer</th>
            </tr>
            {loop="invoices"}
                <tr>
                    <td>{$value->getDate('d/m/Y A')}</td>
                    <td>{$users[$value->getBuyer()]->getDisplayName()}</td>
                    <td>
                        {loop="$value->getUsersIn()->get()"}
                            {$users[$key2]->getDisplayName()}
                            {if condition="$value2 > 1"}
                                ({$value2} invités)
                            {elseif condition="$value2 == 1"}
                                ({$value2} invité)
                            {/if}
                            {if condition="$value1->getBuyer() != $key2"}
                                - 
                                {if condition="$paybacks[$value1->getId()] === false || !in_array($key2, array_keys($paybacks[$value1->getId()]))"}
                                    {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
                                        <a href="?do=confirm_payback&amp;from={$key2}&amp;to={$value1->getBuyer()}&amp;invoice_id={$value1->getId()}&amp;token={$token}" title="Confirmer le remboursement">
                                    {/if}
                                        Reste {$value1->getAmountPerPerson($key2)} {$currency}
                                    {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
                                        </a>
                                    {/if}
                                {else}
                                    {if condition="$paybacks[$value1->getId()][$key2]->getAmount() == $value1->getAmountPerPerson($key2)"}
                                        {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
                                            <a href="?do=delete_payback&amp;from={$key2}&amp;to={$value1->getBuyer()}&amp;invoice_id={$value1->getId()}&amp;token={$token}" title="Supprimer le remboursement">
                                        {/if}
                                            Payé
                                        {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
                                            </a>
                                        {/if}   
                                    {else}
                                        {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
                                            <a href="?do=confirm_payback&amp;from={$key2}&amp;to={$value1->getBuyer()}&amp;invoice_id={$value1->getId()}&amp;payback_id={$paybacks[$value1->getId()][$key2]->getId()}&amp;token={$token}" title="Confirmer le remboursement">
                                        {/if}
                                            Reste {$value1->getAmountPerPerson($key2) - $paybacks[$value1->getId()][$key2]->getAmount()}{$currency}
                                        {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
                                            </a>
                                        {/if}
                                    {/if}
                                {/if}
                            {/if}
                            <br/>
                        {/loop}
                    </td>
                    <td>{$value->getAmount()}{$currency}</td>
                    <td>{$value->getWhat()}</td>
                    <td>
                        {if condition="$value->getBuyer() == $current_user->getId() || $current_user->getAdmin()"}
                            <a href="index.php?do=edit_invoice&id={$value->getId()}">Modifier</a>
                        {else}
                        -
                        {/if}
                    </td>
                    <td>
                        {if condition="$value->getBuyer() == $current_user->getId() || $current_user->getAdmin()"}
                            <a href="index.php?do=delete_invoice&id={$value->getId()}&amp;token={$token}">Supprimer</a>
                        {else}
                            -
                        {/if}
                    </td>
                </tr>
            {/loop}
        </table>
    {else}
        <p class="center">Aucune dépense à afficher.</p>
    {/if}
</div>
{include="footer"}