<?php  use Cake\ORM\TableRegistry;
	   $user1 = $this->request->session()->read('Auth.User'); 
	   $user  = TableRegistry::get('Users');
       $banks = TableRegistry::get('Banks');
	   $filename  = TableRegistry::get('Filenames');
       $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(($('#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">
<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' ;
			  } */
			  $filenameid = $filename->find()->where(['id' => $company]);
					$filenamebr = $filenameid->toArray();echo 'Company Name: '. $filenamebr[0]->fname; 
					}else {
					echo 'Company Name: All';}
			  ?></b>
			</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') ?></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>
    <?php //} ?>
 <script>
    function print_function(){
       window.print();
    }
</script>