<?php  use Cake\ORM\TableRegistry;
	   $user1 = $this->request->session()->read('Auth.User'); 
	   $user  = TableRegistry::get('Users');
       $banks = TableRegistry::get('Banks');
       $companies = TableRegistry::get('Companies');  
	   $branches = TableRegistry::get('Branches');
	   	$scheme = TableRegistry::get('Mffunds');
?>

<script>
   $(document).ready(function() {     
       
      $("form").submit(function(){
           // e.preventDefault();
            if($('#branch').val() ==''){
                alert("Please choose the branch for report");
                return false;
            }
			if($('#company').val() ==''){
                alert("Please choose the company for report");
                return false;
            }
            if($('#branch').val() =='all'){
                if($('#datedform').val() ==''){
                 alert("Please choose Start date for report");
                 return false;
                }
                if($('#datedto').val() == ''){
                 alert("Please choose 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">
<div class="col-md-6">
    <h3><?= __('Company Wise Report') ?></h3> </div>
	<div class="col-md-6">
<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>
     <div class="search-wrapper">
        <div class="search-filter" style="width: 100%;"><?= $this->Search->filterForm($searchFilters) ?></div>         
     </div>

     <?php if(!empty($result)): ?> 
     <table class="tbl-wrapper" cellpadding="0" cellspacing="0">
        <thead>
          <tr>
			<th scope="col" colspan="2"><b>
			  <?php 
			  if($company != '' &&  $company != 'all')
			  {
				$companyid= $companies->find()->where(['id' => $company]);
				 $databr = $companyid->toArray();
				echo 'Company :'. $databr[0]->company;
			  }
			  else{
			  echo 'Company : all' ;
			  }
			  ?></b>
			</th>			  
              <th scope="col" colspan="1"><b>
			  <?php 
			 
                 if($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') ?></th>
			<th scope="col"><?= $this->Paginator->sort('Company') ?></th>     
			<th scope="col"><?= $this->Paginator->sort('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'){?>
				<tr>
					<td><?=$j ?></td>  
					<td><?= $key;?></td>	
					<td class="amtrgt"><?= isset($res['0']['tamount'])?$res['0']['tamount']:0;?></td>
					<td><?= isset($res['0']['cnumber'])?$res['0']['cnumber']:0 ?></td>
				</tr> 
				<?php //}?>
			   
	   <?php $j++;
	    $total += isset($res['0']['tamount'])?$res['0']['tamount']:0; $tc +=isset($res['0']['cnumber'])?$res['0']['cnumber']:0;
	   endforeach;?>
           <tr class="total">
                <td colspan="2" style="text-align:right"><b>Grand Total</b></td>
                <td><b><?= number_format($total,2) ?></b></td> 
                
                <td><b><?= $tc ?><b></td> 
            </tr> 
        </tbody>
		 <?php endif; ?>
    </table>
</div>
    <?php //} ?>
 <script>
    function print_function(){
       window.print();
    }
</script>