@extends('layouts.dash')
@section('title','Grand Livre')
@section('title_card',"")
@section('subtitle_card',"")
@section('content')
    <div class="card">
        @if (Session::has('message'))
            <div class="alert alert-warning">{!! Session::get('message') !!}</div>
        @endif

        @if ($errors)
            @foreach ($errors->all() as $error)
                <div class='bg-danger alert'>{!! $error !!}</div>
            @endforeach
        @endif

        @if (Session::has('success'))
            <div class="alert alert-success">{!! \Session::get('success') !!}</div>
        @endif  
        
        <div class="card-header bg-warning">
            <h3 class="card-title">Compte de résultat <small>{!! $exercice !!}</small></h3>
        </div>

        <div class="card-body">
            <div class="col-xs-4" style="padding-left:0px;padding-top:10px"> 
                {!! html()->select('categ')
                    ->options(["R"=>"CLR","F"=>"CCPF","A"=>"AS/AV"],$categ)
                    ->attributes(['id' => 'categ2','class' => 'form-control selectpicker','data-width' => '100%','style' => 'height:50px;font-size:24px;font-weight:bold;background-color: rgba(255,255,255,.5);']) 
                !!}
            </div>

            <div class="table-responsive" style="margin-top:0px">
                <table id="" style="background-color:white"  class="table table-condensed table-striped table-bordered">
                    <thead style="background-color:lightgrey;font-weight:bold">
                        <tr class="bg-success">
                            <th width='1%' colspan="6" class="text-center">PRODUITS AU 31/12</th>
                        </tr>
                        <tr>
                            <th rowspan="2" width='1%'>Compte</th>
                            <th rowspan="2">Nom</th>
                            <th rowspan="2" width='1%'>(N-1)</th>
                            <th colspan="3" width='1%'>N</th>
                        </tr>
                        <tr>
                            <th width='1%'>BRUT</th>
                            <th width='1%'>AMORT</th>
                            <th width='1%'>NET</th>
                        </tr>
                    </thead>

                    <tbody>
                        <?php $categ_old="";$categ=""; 
                            $total_netprec=0;$total_brut=0;$total_amort=0;
                            $totalgene_netprec=0;$totalgene_brut=0;$totalgene_amort=0;
                        ?>
                        @foreach($t_CR_produits as $value)
                            <?php
                                $t_value=explode(";",$value);
                                $categ=substr($t_value[0],0,1);
                                $total_netprec+=$t_value[2];
                                $total_brut+=$t_value[3];
                                $total_amort+=$t_value[4];
                                $totalgene_netprec+=$t_value[2];
                                $totalgene_brut+=$t_value[3];
                                $totalgene_amort+=$t_value[4];
                            ?>
                            @if(($categ!=$categ_old)&&($categ_old!=""))
                            <tr style="background-color:lightgrey;font-weight:bold" class="text-center">
                                <td nowrap colspan='2'>TOTAL</td>
                                <td nowrap align='right'>{!! number_format($total_netprec,0,","," ") !!}</td>
                                <td nowrap align='right'>{!! number_format($total_brut,0,","," ") !!}</td>
                                <td nowrap align='right'>{!! number_format($total_amort,0,","," ") !!}</td>
                                <td nowrap align='right'>{!! number_format($total_brut-$total_amort,0,","," ") !!}</td>
                            </tr>
                            @endif
                            <tr>
                                <td nowrap>
                                    <a href="{!! url('compta_old/journal_ltd')."/".$exercice."/".$t_value[0] !!}" 
                                        class="jnx_detail fancybox.iframe" 
                                        style="margin-right: 3px;">
                                        {!! $t_value[0]   !!}
                                    </a>
                                </td>
                                <td nowrap>{!! $t_value[1] !!}</td>
                                <td nowrap align='right'>{!! number_format($t_value[2],0,","," ") !!}</td>
                                <td nowrap align='right'>{!! number_format($t_value[3],0,","," ") !!}</td>
                                <td nowrap align='right'>{!! number_format($t_value[4],0,","," ") !!}</td>
                                <td nowrap align='right'>{!! number_format($t_value[5],0,","," ") !!}</td>
                            </tr>
                            <?php $categ_old=$categ; ?>
                        @endforeach
                    </tbody>
                    <tfooter>
                        <tr style="background-color:lightgrey;font-weight:bold" class="text-center">
                            <td nowrap colspan='2'>TOTAL ACTIF</td>
                            <td nowrap align='right'>{!! number_format($totalgene_netprec,0,","," ") !!}</td>
                            <td nowrap align='right'>{!! number_format($totalgene_brut,0,","," ") !!}</td>
                            <td nowrap align='right'>{!! number_format($totalgene_amort,0,","," ") !!}</td>
                            <td nowrap align='right'>{!! number_format($totalgene_brut-$totalgene_amort,0,","," ") !!}</td>
                        </tr>
                    </tfooter>
                </table>
            </div>

            <div class="table-responsive" style="margin-top:50px">
                <table id="" style="background-color:white"  class="table table-condensed table-striped table-bordered">
                    <thead style="background-color:lightgrey;font-weight:bold">
                        <tr class="bg-danger">
                            <th width='1%' colspan="6" class="text-center">CHARGES AU 31/12</th>
                        </tr>
                        <tr>
                            <th width='1%'>Compte</th>
                            <th >Nom</th>
                            <th width='1%'>(N-1)</th>
                            <th width='1%'>N</th>
                        </tr>
                    </thead>

                    <tbody>
                        <?php $categ_old="";$categ=""; 
                            $totalgene_netprec=0;$totalgene_net=0;
                        ?>
                        @foreach($t_CR_charges as $value)
                        <?php
                            $t_value=explode(";",$value);
                            $categ=substr($t_value[0],0,3);
                            $totalgene_netprec+=$t_value[2];
                            $totalgene_net+=$t_value[3];

                        ?>

                        <tr>
                            <td nowrap>
                                <a href="{!! url('compta_old/journal_ltd')."/".$exercice."/".$t_value[0] !!}" 
                                    class="jnx_detail fancybox.iframe" 
                                    style="margin-right: 3px;">
                                    {!! $t_value[0]   !!}
                                </a>

                            </td>
                            <td nowrap>{!! $t_value[1] !!}</td>
                            <td nowrap align='right'>{!! number_format(abs($t_value[2]),0,",","") !!}</td>
                            <td nowrap align='right'>{!! number_format(abs($t_value[3]),0,",","") !!}</td>                </tr>
                        <?php $categ_old=$categ; ?>
                        @endforeach
                    </tbody>

                    <tfooter>
                        <tr style="background-color:lightgrey;font-weight:bold" class="text-center">
                            <td nowrap colspan='2'>TOTAL PASSIF</td>
                            <td nowrap align='right'>{!! number_format(abs($totalgene_netprec),0,",","") !!}</td>
                            <td nowrap align='right'>{!! number_format(abs($totalgene_net),0,",","") !!}</td>
                        </tr>
                    </tfooter>
                </table>
            </div>

        </div>
    </div>
@endsection


@section('styles')
    <!-- FancyBox -->
    <link media="all" type="text/css" rel="stylesheet" href="{{ url('css/jquery.fancybox.css') }}">
    <link media="all" type="text/css" rel="stylesheet" href="{{ url('css/jquery.fancybox-buttons.css') }}">
@stop

@section('scripts')
    <!-- FancyBox -->
    <script src="{{ url('js/jquery.fancybox.js') }}"></script>

    <script>
        $(document).ready(function() {
            $('#categ2').on('change', function() {
                location.href="{!! url('compta_old/compte_resultat') !!}/"+$('#categ2').val();
            });
        });
    </script>

    <script>
        $(document).ready(function() {
            $(".jnx_detail").fancybox({
                //maxWidth    : 800,
                //maxHeight   : 600,
                fitToView   : false,
                width       : '80%',
                height      : '40%',
                autoSize    : true,
                autoScale   : true,
                closeClick  : false,
                openEffect  : 'none',
                closeEffect : 'none',
                afterClose  : function() { 
                    window.location.reload();
                }
            });
        });
    </script>
@endsection




