<?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');
?>
<?= $this->Html->script('schemeload.js'); ?>
<script>
   $(document).ready(function() {     
        $('.printButton').on('click', function() {
          var visible =  $('#actions-sidebar,.search-wrapper').toggle().is(":visible");
            if(visible){
               $('.daily').css('width','83%');
            }else{
               $('.daily').css('width','100%');
            }
        }); 
        
      $("form").submit(function(){
           // e.preventDefault();
            if($('#branch').val() ==''){
                alert("Please choose the branch for report");
                return false;
            }
            
            if($('#branch').val() =='all'){
                if(($('#inv-datedform').val() =='' && $('#datedform').val() =='') && ($('#inv-datedto').val() =='' && $('#datedto').val() == '')){
                   alert("Please choose  Start And End Date for report");
                   return false;
              }

              if(($('#inv-datedform').val() =='' && $('#datedform').val() =='') && ($('#inv-datedto').val() !='' || $('#datedto').val() != '')){
                   alert("Please choose  Start And End Date for report");
                   return false;
              }

              if(($('#inv-datedform').val() !='' || $('#datedform').val() !='') && ($('#inv-datedto').val() =='' && $('#datedto').val() == '')){
                   alert("Please choose  Start And End Date for report");
                   return false;
              }
              
              if(($('#inv-datedform').val() !='' || $('#datedform').val() !='') && ($('#inv-datedto').val() =='' && $('#datedto').val() == '')){
                   alert("Please choose  Start And End Date for report");
                   return false;
              }

              if(($('#inv-datedform').val() !='' && $('#datedform').val() =='') && ($('#inv-datedto').val() =='' && $('#datedto').val() != '')){
                   alert("Please choose  Start And End Date for report");
                   return false;
              }

              if(($('#inv-datedform').val() =='' && $('#datedform').val() !='') && ($('#inv-datedto').val() !='' && $('#datedto').val() == '')){
                   alert("Please choose  Start And End Date for report");
                   return false;
              }
			}
      }); 
  });
  </script>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
      <?= $this->element('admin_sidebar') ?>
</nav>
<div class="fds index large-9 medium-8 columns content">
    <h3><?= __('Consolidated Report') ?></h3>
<div class="btn-link">
	<!--<button onclick="print_function()" class="green-btn print-btn">Print</button>-->
	
      </div> 
      <div class="btn-link"><input type="button" value="Back" onclick="window.history.back()" class="green-btn" /> </div>
     <div class="search-wrapper">
        <div class="search-filter" style="width: 88%;"><?= $this->Search->filterForm($searchFilters) ?></div>         
   </div>

     <?php if(!empty($result)): ?>
        
    <!--<div class="search-wrapper">  
      <div class="btn-link">
        <?= $this->Html->link(__('Excel Export'), [
           'controller' => 'Reports', 'action' => 'companywisemfexcelreport',
           '?' =>['company'=> $company,'branch'=> $branch,   
            'datedform'=> $datedform,
            'datedto'=> $datedto]],['class'=>'green-btn']); ?></div> 
      </div>-->  
  
     <table class="tbl-wrapper" cellpadding="0" cellspacing="0">
        <thead>
          <tr>
			<th 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 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 scope="col" colspan="3"><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 scope="col" style="width:40px;"><?= $this->Paginator->sort('Sr No') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Company Name') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Institution Name') ?></th>

                <th scope="col"><?= $this->Paginator->sort('Investment Amount') ?></th>
               
                <th scope="col"><?= $this->Paginator->sort('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><?=$j ?></td>  
					<td><?php echo !empty($filenames[$fd_res['csvid']]) ? $filenames[$fd_res['csvid']]:'';?></td>
					<td><?php echo !empty($this->request->query['institution_name']) && $this->request->query['institution_name'] !='all'  ? $this->request->query['institution_name'] : 'All'?></td>
					<td class="amtrgt"><?php echo $fd_res['tamount'];?></td>
					<td>
                        <?php
                        if($fd_res['cnumber'] > 0){ ?>
                            <?php echo $this->Html->link($fd_res['cnumber'] , ['controller' => 'Fds','action' => 'fdmaturity', '?' =>
                                  [ 'fname'=>$fd_res['csvid'],
                                    'institution_name' => !empty($this->request->query['institution_name']) ? $this->request->query['institution_name'] :'',
                                    'datedform'=>!empty($this->request->query['datedform']) ? $this->request->query['datedform'] :'',
                                    'datedto'=>!empty($this->request->query['datedto']) ? $this->request->query['datedto'] :'',
                                    'inv_datedform'=>!empty($this->request->query['inv_datedform']) ? $this->request->query['inv_datedform'] :'',
                                    'inv_datedto'=>!empty($this->request->query['inv_datedto']) ? $this->request->query['inv_datedto'] :''
                                  ]
                                ], ['class' => '','title'=>__('View Clients')]) ?>
                        <?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 colspan="3" style="text-align:right"><b>Grand Total</b></td>
                <td class="amtrgt"><b><?= number_format($total,2) ?></b></td> 
                
                <td><b><?= $tc ?><b></td> 
            </tr> 
        </tbody>
		 <?php endif; ?>
    </table>
  
     
   <!--  <div class="paginator">
        <ul class="pagination">
            <?= $this->Paginator->first(__('First', true), array('class' => 'disabled')); ?>
            <?= $this->Paginator->prev('< ' . __('previous',true), array('class' => 'disabled')) ?>
            <?= $this->Paginator->numbers() ?>
            <?= $this->Paginator->next(__('next' . ' >',true), array('class' => 'disabled')) ?>
		 <?=  $this->Paginator->last(__('Last', true), array('class' => 'disabled')); ?>
        </ul>
        <p><?= $this->Paginator->counter() ?></p>
    </div> -->
</div>
    <?php //} ?>
 <script>
    function print_function(){
       window.print();
    }
</script>