<?php  use Cake\ORM\TableRegistry;
	$companies = TableRegistry::get('Companies');
	$branches = TableRegistry::get('Branches');
	$brokers = TableRegistry::get('Brokers');
	$users = TableRegistry::get('Users');
?>
<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-3">
    <h3><?= __('Fixed Deposit Entry Done') ?></h3></div>
    
     <div>
        <?php
        $export = array_merge($this->request->query,array('export' => 'xls'));
        $exportpdf = array_merge($this->request->query,array('export' => 'pdf'));
        ?>
        <div class="btn-link">
            <?= $this->Html->link(__('Excel Export'),['controller' => 'Fds', 'action' => 'fdexcelentry','?'=>$export],['class'=>'green-btn']); ?>
            <?= $this->Html->link(__('Pdf Export'),['controller' => 'Fds', 'action' => 'index','?'=>$exportpdf],['class'=>'green-btn']); ?>
        </div> </div>

    
	<div class="search-wrapper">
        <div class="col-md-12">
            <div class="search-filter">
                <div class=""><?= $this->Search->filterForm($searchFilters) ?></div>
            </div>
            <div class="btn-link margintop-fifteen"><?= $this->Html->link(__('Add Fixed Deposit Entry'), ['controller' => 'Fds', 'action' => 'add'],['class'=>'green-btn', 'title' => 'Add New Group', 'type' => 'button']) ?></div>
        </div>
    </div>
    <table class="tbl-wrapper" cellpadding="0" cellspacing="0">
        <thead>
            <tr>
               <th style="width:40px;" scope="col"><?= $this->Paginator->sort('Sr No') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Applicant Name') ?></th>
				<th scope="col"><?= $this->Paginator->sort('Pan No') ?></th>
                <!--<th scope="col"><?= $this->Paginator->sort('product') ?></th>-->
                
                <th scope="col"><?= $this->Paginator->sort('Referred By') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Email') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Deposit type') ?></th>
                <!--<th scope="col"><?= $this->Paginator->sort('MainBroker') ?></th>-->
                <th scope="col"><?= $this->Paginator->sort('Cheque No / FDR no') ?></th>
                <th scope="col"><?= $this->Paginator->sort('amount') ?></th>
                <th scope="col"><?= $this->Paginator->sort('period (months)') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Branch') ?></th>
                <th scope="col"><?= $this->Paginator->sort('company') ?></th>
                <th scope="col"><?= $this->Paginator->sort('created') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Created By(User Name)') ?></th>
               <th scope="col" class="actions"><?= __('Actions') ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
             $srlno = ($this->request->params['paging']['Fds']['page']-1) * $this->request->params['paging']['Fds']['perPage'];
             $i=1;$total='';
            foreach ($fds as $fd): $srlno++; ?>
            <tr>
                <td><?= $srlno ?></td>
                <td><?php if($fd->minor == 1) {
                             echo  $fd->minorname.'&nbsp;&nbsp;U/G&nbsp;&nbsp;'.$fd->fapplicantname;
                          }else{
                              echo  $fd->fapplicantname;
                          } ?></td>
                <!--<td><?= h($fd->fapplicantname) ?></td>-->
               <td><?= h($fd->customer->panno) ?></td>
              <!-- <td>
		<?php
		//pr($fd->productcompanyid);//die();
		if($fd->productcompanyid > 0){
		$companiesdata = $companies->find()->where(['id' => $fd->productcompanyid]);
		$data2 = $companiesdata->toArray();
		if($data2){
		echo $companiesname = $data2[0]->company;
		}
		} 
		?>
		</td>-->
                
                
			    <td><?php if($fd->subbroker_id > 0){
        				$subbroker = $brokers->find()->where(['id' => $fd->subbroker_id]);
        				$sbdata = $subbroker->toArray();
        				if($sbdata){
        					echo $subbroker = $sbdata[0]->name;
        				 }
    			     } ?>
                 </td>
                <td><?= h($fd->email) ?></td>
                <td><?php 
				if($fd->typeofdeposit == 'onlinerenewal'){
					echo 'online renewal';
				}else if($fd->typeofdeposit == 'onlinefresh'){
					echo 'online fresh';
				}else{
					echo $fd->typeofdeposit ;
				} ?></td>
                <!--<td><?= h($fd->typeofdeposit) ?></td>
                <td><?= $fd->has('broker') ? $this->Html->link($fd->broker->name, ['controller' => 'Brokers', 'action' => 'view', $fd->broker->broker_id]) : '' ?></td>-->

                <td><?php 
                if($fd->typeofdeposit == 'fresh' || $fd->typeofdeposit == 'onlinefresh' ){
                    echo ($fd->chequeno);
                }elseif($fd->typeofdeposit == 'renewal' || $fd->typeofdeposit == 'onlinerenewal'){
                    echo ($fd->fdrno);
                }else{
                    echo ($fd->chequeno).' / '. ($fd->fdrno);
                } ?></td>

                <!--<td><?= h($fd->amount) ?></td>-->
                <td><?php 
                $amt=h($fd->amount);
                $fdramt=h($fd->fdramount);
                $totalamt=$amt+$fdramt;
                echo $totalamt; ?>
                </td>
                <td><?php if($fd->tenure == 0){echo '';} else{echo $fd->tenure;} ?></td>
                <td><?php if($fd->branch_id > 0){
        				$branchdata = $branches->find()->where(['id' => $fd->branch_id]);
        				$data = $branchdata->toArray();
        				if($data){
        					echo $branchdata = $data[0]->branchname;
        				 }
    			     } ?>
                 </td>
                <!--<td><?= $fd->has('company') ? $this->Html->link($fd->company->company, ['controller' => 'Companies', 'action' => 'view', $fd->productcompanyid]) : '' ?></td>-->
                 <td>
                <?php if($fd->productcompanyid > 0) {
			$productcompanyid = $companies->find()->where(['id' => $fd->productcompanyid]);
			$data1 = $productcompanyid->toArray();
			$productcompanyName = $data1[0]->company;
			}
			else{
			$productcompanyName = '';
			}?>
            <?=$productcompanyName?>
            </td>
            <td><?php $created = explode(" ", $fd->created);
                    echo $created[0];
                    ?>
            </td>
            <td><?php if($fd->postedby > 0){
        				$posteddata = $users->find()->where(['id' => $fd->postedby]);
        				$data = $posteddata->toArray();
        				if($data){
        					echo $data[0]->username;
        				 }
    			     } ?>
			  </td>
                <td class="actions">
                    <?= $this->Html->link(__('View'), ['action' => 'view', $fd->id],['class' => 'view','title'=>__('View')]) ?>
                    <?php  if($this->request->session()->read('Auth.User.group_id')=='2'){ ?>  
                    <?= $this->Html->link(__('Edit'), ['action' => 'edit', $fd->id],['class' => 'edit','title'=>__('Edit')]) ?>
                    <!--<?= $this->Html->link(__('Sync'), ['action' => 'addfdmtable', $fd->id],['class' => '','title'=>__('Sync to Master FD')]) ?>-->
                   <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $fd->id], ['class' => 'delete','title'=>__('Delete'),'confirm' => __('Are you sure you want to delete # {0}?', $fd->id)]) ?>
        
                    <?php if($fd->emailSendBy == ""){  ?>
					<?= $this->Html->link(__('Email'), ['action' => 'viewsendemail', $fd->id],['class' => 'eupdate','title'=>__('Send Fixed Deposit Acknowledgement')]) ?>
					<?php }else{?>
						<?= $this->Html->link(__('Email'), ['action' => 'viewsendemail', $fd->id],['class' => 'eupdate eupdatesent','title'=>__('Re-send Fixed Deposit Acknowledgement')]) ?>
					<?php } ?>
        
                    <?php } ?>
<?php  if($this->request->session()->read('Auth.User.group_id')=='3'){ ?>  
                    <?= $this->Html->link(__('Edit'), ['action' => 'edit', $fd->id],['class' => 'edit','title'=>__('Edit')]) ?>
                    <!--<?= $this->Html->link(__('Sync'), ['action' => 'addfdmtable', $fd->id],['class' => '','title'=>__('Sync to Master FD')]) ?>-->
                   <!-- <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $fd->id], ['class' => 'delete','title'=>__('Delete'),'confirm' => __('Are you sure you want to delete # {0}?', $fd->id)]) ?>-->
<?php } ?>
                    <?php  if($this->request->session()->read('Auth.User.group_id')=='4'){ ?>
                        <!--<?= $this->Html->link(__('Edit'), ['action' => 'edit', $fd->id],['class' => 'edit','title'=>__('Edit')]) ?>-->
                    <?php } ?>
                </td>
            </tr>
            <?php //$total += $fd->amount;  
                    //$total += $fd->amount + (isset($fd->fdramount) ? $fd->fdramount : 0);
                    $total = 0;
                    $total += (float)$fd->amount + (float)$fd->fdramount;

                $i++; endforeach; ?>
			<tr class="total">
                <td colspan="7" style="text-align:right"><b>Sub Total</b></td>
                <td><b><?= $total ?></b></td> 
                <td colspan="6"></td> 
            </tr> 
			<?php  if($this->request->params['paging']['Fds']['pageCount'] == $this->request->params['paging']['Fds']['page']){?>
			<tr class="total">
                <td colspan="7" style="text-align:right"><b>Grand Total</b></td>
                <td><b><?= $total_amount ?></b></td> 
                <td colspan="6"></td> 
            </tr> 
            <?php } ?>
        </tbody>
    </table>
<div class="btn-link"><input type="button" value="Back" onclick="window.history.back()" class="green-btn" /> </div>
    <div class="paginator">
        <ul class="pagination">
            <?= $this->Paginator->prev('< ' . __('previous')) ?>
            <?= $this->Paginator->numbers() ?>
            <?= $this->Paginator->next(__('next') . ' >') ?>
        </ul>
        <p><?= $this->Paginator->counter() ?></p>
    </div>
</div>
<script>
$('#minorname').select2();
$(document).ready(function() { 
var availableTags = <?=json_encode($custname)?>;
console.log(availableTags);
    $( "#autocomplete1" ).autocomplete({
      //source: availableTags,
      source: function(request, response) {
        var results = $.ui.autocomplete.filter(availableTags, request.term);

        response(results.slice(0, 10));
    }
    });
	
});
</script>