<?php  use Cake\ORM\TableRegistry;
$user1 = $this->request->session()->read('Auth.User');
$user  = TableRegistry::get('Users');
$filename  = TableRegistry::get('Filenames');
$banks = TableRegistry::get('Banks');
$companies = TableRegistry::get('Companies');
$branches = TableRegistry::get('Branches');
$scheme = TableRegistry::get('Mffunds');
?>
    <STYLE type="text/css">
        .tableTd {
            border-width: 0.5pt;
            border: 0.5px solid;
        }
        .tableTdContent{
            border-width: 0.5pt;
            border: 0.5px solid;
        }
        #titles{
            font-weight: bolder;
            background-color:#ccc;
        }

        table {
            border-spacing:0;
            border-collapse: collapse;
        }

        .spy{
            margin-top:10px;
            margin-bottom:10px;
        }

        table .cltr{
            background-color:#DFDFDF;
        }

        table tr th,table tr td{
            padding: 4px 5px;
        }




    </STYLE>
    <h3><?= __('Consolidated Report') ?></h3>
<?php if(!empty($result)): ?>
    <table class="tbl-wrapper" cellpadding="0" cellspacing="0">
        <thead>
        <tr>
            <th class="tableTd" scope="col" colspan="2"><b>
                    <?php
                    if($company != '' && $company != 'all'){
                        $filenameid = $filename->find()->where(['id' => $company]);
                        $filenamebr = $filenameid->toArray();
                        echo 'Company Name :  '. $filenamebr[0]->fname;
                    }else{
                        echo 'Company Name : All';
                    }
                    ?>
                    <!--<div style="clear: both;">Product: ALL </div>-->
            </th>
            <th class="tableTd" scope="col" colspan="1"><b>



                    <?php

                    if(!empty($datedform) && !empty($datedto) && $datedform != '' && $datedto != '' ){

                        echo 'Date :  '. $dated2;
                        echo ' - '.$dated1;
                    }
                    else{
                        echo 'Date : all' ;
                    }
                    ?></b></th>
            <th class="tableTd" scope="col" colspan="2"><b>



                    <?php
                    if($user1['group_id'] == '3')
                    {
                        $branchesid1 = $branches->find()->where(['id' => $user1['branch_id']]);
                        $databr = $branchesid1->toArray();
                        echo 'Branch Name :'. $databr[0]->branchname;;
                    }
                    else{
                        if($branch != '' && $branch != 'all'){
                            $branchesid = $branches->find()->where(['id' => $branch]);
                            $databr = $branchesid->toArray();
                            echo 'Branch Name :  '. $databr[0]->branchname;
                        }else{
                            echo 'Branch Name : All';
                        }
                    }
                    ?></b></th>

        </tr>
        <tr>
            <th class="tableTd" scope="col" style="width:40px;"><?= __('Sr No') ?></th>
            <th class="tableTd" scope="col"><?= __('Company Name') ?></th>
            <th class="tableTd" scope="col"><?= __('Institution Name') ?></th>

            <th class="tableTd" scope="col"><?= __('Investment Amount') ?></th>

            <th class="tableTd" scope="col"><?= __('No of clients') ?></th>

        </tr>
        </thead>
        <tbody>
        <?php $j=1; $total=0; $tc=0;
        foreach($result as $key=>$res): ?>
            <?php //if($key == 'mf'){?>
            <?php  foreach($res as $index =>$fd_res): ?>
                <tr>
                    <td class="tableTdContent"><?=$j ?></td>
                    <td class="tableTdContent"><?php echo !empty($filenames[$fd_res['csvid']]) ? $filenames[$fd_res['csvid']]:'';?></td>
                    <td class="tableTdContent"><?php echo !empty($this->request->query['institution_name']) && $this->request->query['institution_name'] !='all'  ? $this->request->query['institution_name'] : 'All'?></td>
                    <td class="tableTdContent" align="right"><?php echo $fd_res['tamount'];?></td>
                    <td class="tableTdContent">
                        <?php
                        if($fd_res['cnumber'] > 0){ ?>
                            <?php echo $fd_res['cnumber'];?>
                        <?php }else{
                            echo $fd_res['cnumber'];
                        }?>

                    </td>
                </tr>
                <?php //}?>

                <?php
                $j++; $total += $fd_res['tamount']; $tc +=$fd_res['cnumber'];
            endforeach;
        endforeach;?>
        <tr class="total">
            <td class="tableTdContent" colspan="3" style="text-align:right"><b>Grand Total</b></td>
            <td class="tableTdContent" align="right"><b><?= number_format($total,2) ?></b></td>

            <td class="tableTdContent"><b><?= $tc ?><b></td>
        </tr>
        </tbody>
    </table>

<?php endif; ?>