<?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');
$mtables = TableRegistry::get('Mtables');
$fds = TableRegistry::get('Fds');
?>
    <STYLE type="text/css">
        .tableTd {
            border-width: 0.5pt;
            border: 0.5px solid;
        }
        .tableTdContent{
            border-width: 0.5pt;
            border: 0.5px solid;
        }
        #titles,
		table th{
            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;
			border-width: 0.5pt;
            border: 0.5px solid;
        }
		 .tbl-wrapper .strong td{
        border-top: 1px solid rgba(128, 55, 55, 0.22);
        border-bottom: 2px solid rgba(128, 55, 55, 0.22);
        font-weight: bold;
    }

    </STYLE>
<?php if(!empty($result)): ?>
        <table class="tbl-wrapper" cellpadding="0" cellspacing="0">
            <thead>
            <tr>
                <th scope="col" colspan="2"><b><div style="clear: both;"><center><h3 class="heading">ABN INVESTMENT</h3></center></div></th>
            </tr>
            <tr>
                <th scope="col" style="width:10%;">CLIENT NAME </th><th ><span style="font-weight: normal;">
								<?php 
									if($customer_name != '' && $customer_name != 'all'){
										echo $customer_name; 
									}elseif($minor_name != '' && $minor_name != 'all'){
										echo !empty($result['fd'][0]['fapplicantname']) ? $minor_name.' U/G '.$result['fd'][0]['fapplicantname'] : $minor_name; 
									}else {
										echo '';
									} ?>
								</span></th>
            </tr>
            <tr>
                <th scope="col" style="width:10%;">PAN NO</th>
                <th scope="col" style="font-weight: normal;">
                    <?php if($applicant_pan != '' && $applicant_pan != 'all'){
                        echo $applicant_pan; }
                    else {
                        echo '';} ?>
                </th>
            </tr>
            <tr>
                <th scope="col" style="width:10%;">ADDRESS</th>
                <th scope="col" style="font-weight: normal;">
                    <?php if($applicant_pan != '' && $applicant_pan != 'all'){
                        $applicant_pan_no = $mtables->find()->where(['pan_no' => $applicant_pan]); 
                        $applicantbr = $applicant_pan_no->toArray(); //pr($applicantbr);
                        $address_combined = !empty($applicantbr[0]->address) ? $applicantbr[0]->address.', ': '';
                        if(!empty($applicantbr[0]->add1)){
                            $address_combined .= ' '. $applicantbr[0]->add1;
                        }
                        if(!empty($applicantbr[0]->add2)){
                            $address_combined .= ', '. $applicantbr[0]->add2;
                        }
                        if(!empty($applicantbr[0]->add3)){
                            $address_combined .= ', '. $applicantbr[0]->add3;
                        }

                        if(!empty($applicantbr[0]->city)){
                            $address_combined .= ', '. $applicantbr[0]->city;
                        }

                        if(!empty($applicantbr[0]->region)){
                            $address_combined .= ', '. $applicantbr[0]->region;
                        }

                        if(!empty($applicantbr[0]->pin)){
                            $address_combined .= ', '. $applicantbr[0]->pin;
                        }
                        echo  $address_combined; }else {
                        echo '';} ?>
                </th>
            </tr>
            <tr>
                <th scope="col" style="width:10%;">MOBILE NO</th>
                <th scope="col" style="font-weight: normal;">
                    <?php
                    echo $applicantbr[0]->mobileno;?>
                </th>
            </tr>
            </thead>
        </table>

        <div class="row">

            <div class="col-md-12">
                <h7><?= __('PORTFOLIO AS ON ').date('d/m/Y'); ?></h7>
            </div>
        </div><br>

        <table class="tbl-wrapper" cellpadding="0" cellspacing="0">
            <thead>
            <tr>
                <th scope="col" colspan="8"><b><?php echo 'FD';?></b></th>
            </tr>
            <tr>
                <th scope="col" style="width:3%;"><?= ('Sr No') ?></th>
                <th scope="col"><?= ('Institution Name') ?></th>
                <th scope="col" style="width:10%;"><?= ('Instrument Type') ?></th>
                <th scope="col"><?= ('Investment Date') ?></th>
                <th scope="col"><?= ('Investment Amount') ?></th>
                <th scope="col" style="width:4%"><?= ('Tenure') ?></th>
                <th scope="col"><?= ('Maturity Date') ?></th>
                <th scope="col" style="width:20%"><?= ('Investor Remark') ?></th>
            </tr>
            </thead>
            <tbody>
            <?php $j=1; $total=0; $tc=0; $Totalamt_cust = 0; $Totalamt_m =0; $Totalamt_md = 0;
			$minorData =array();
            foreach($result['fd'] as $key=>$res): ?>
			
				<?php
					//Get Minor Data
					$minor = false;
					if(empty($minor_name) && $res['fdtable_fdid'] > 0){
						$fddata = $fds->find()->where(['id' => $res['fdtable_fdid'], 'minor' => 1]);
						$data = $fddata->toArray();
						if($data){
							$res['minorname'] = $data[0]->minorname;
							$minorData[$j] = $res;
							$minor = true;
						}
					}
				?>
				<?php  if(empty($minor_name) && !$minor){?>
                <tr>
                    <td><?=$j++ ?></td>
                    <td>
                        <?php echo $res['institution_name'];?>
                    </td>
                    <td style="width:10%;"><?php echo $res['instrument_type']; ?></td>
                    <td><?php echo $res['application_date']; ?></td>
                    <td><?php if(isset($res['investment_amount']))
                                {echo $res['investment_amount'];
                                     $total = str_replace(",","",$res['investment_amount']);
                                $Totalamt_cust += $total;}else{echo 0;} ?>
                    </td>
                    <td><?php echo $res['period'];?></td>
                    <td><?php echo $res['maturity_date'];?></td>
                    <td><?php echo $res['remark'];?></td>
                </tr>
				<?php }elseif(!empty($minor_name)){ ?>
					<?php $mtabledata = $mtables->findByFdtableFdid($res['id'])->first();?>
					<tr>
						<td><?=$j++ ?></td>
						<td>
							<?php echo $mtabledata['institution_name'];?>
						</td>
						<td style="width:10%;"><?php echo $mtabledata['instrument_type']; ?></td>
						<td><?php echo $mtabledata['application_date']; ?></td>
						<td>
                            <?php if(isset($mtabledata['investment_amount']))
                                    {echo $mtabledata['investment_amount'];
                                     $total = str_replace(",","",$mtabledata['investment_amount']);
                                    $Totalamt_m += $total;}else{echo 0;} ?>
                        </td>
						<td><?php echo $mtabledata['period'];?></td>
						<td><?php echo $mtabledata['maturity_date'];?></td>
						<td><?php echo $mtabledata['remark'];?></td>
					</tr>
				<?php }?>
                <?php endforeach;?>
                <?php  if(empty($minor_name) && !$minor){ ?>
                <tr class="strong">
                    <td></td>
                    <td colspan="3" align="left"><?php echo __('Invested Amount Total') ?></td>
                    <td align="left"><?php echo $Totalamt_cust; ?></td>
                    <td></td><td></td><td></td>
                </tr>
				<?php }else if(!empty($minor_name) && !$Totalamt_m == 0){ ?>
                    <tr class="strong">
                    <td></td>
                    <td colspan="3" align="left"><?php echo __('Invested Amount Total') ?></td>
                    <td align="left"><?php echo $Totalamt_m; ?></td>
                    <td></td><td></td><td></td>
                </tr>
				<?php }?>
            </tbody>

        </table>
		
		<?php if(!empty($minorData)){?>
        <h3> Minor Applicant </h3>
		
		<table class="tbl-wrapper" cellpadding="0" cellspacing="0">
            <thead>
            <tr>
                <th scope="col" colspan="9"><b><?php echo 'FD';?></b></th>
            </tr>
            <tr>
                <th scope="col" style="width:3%;"><?= ('Sr No') ?></th>
				<th scope="col"><?= ('Minor Name') ?></th>
                <th scope="col"><?= ('Institution Name') ?></th>
                <th scope="col" style="width:10%;"><?= ('Instrument Type') ?></th>
                <th scope="col"><?= ('Investment Date') ?></th>
                <th scope="col"><?= ('Investment Amount') ?></th>
                <th scope="col" style="width:4%"><?= ('Tenure') ?></th>
                <th scope="col"><?= ('Maturity Date') ?></th>
                <th scope="col" style="width:20%"><?= ('Investor Remark') ?></th>
            </tr>
            </thead>
            <tbody>
            <?php $j=1; $total=0; $tc=0;
            //foreach($minorData as $mdata):
			foreach($result['fd'] as $key=>$res):	
                $minorname = "";
                if($res['fdtable_fdid'] > 0){
                    $fddata = $fds->find()->where(['id' => $res['fdtable_fdid']]);
                    $data = $fddata->toArray();
                    if($data){
                        $minorname = $data[0]->minorname;
                        }
				}?>
				<?php if ($minorname !='' && $res['fdtable_fdid'] > 0): ?>

                <tr>
                    <td><?=$j++ ?></td>
					<!--<td><?php echo $mdata['minorname'].' U/G '.$mdata['customer_name'];?></td>-->
					<td><?php echo $minorname.' U/G '.$customer_name;?></td>
                    <td><?php echo $res['institution_name'];?>
                    </td>
                    <td style="width:10%;"><?php echo $res['instrument_type']; ?></td>
                    <td><?php echo $res['application_date']; ?></td>
                    <td><?php if(isset($res['investment_amount'])){
                                echo $res['investment_amount'];
                                $total = str_replace(",","",$res['investment_amount']);
                                $Totalamt_md += $total;}else{echo 0;} ?>
                    </td>
                    <td><?php echo $res['period'];?></td>
                    <td><?php echo $res['maturity_date'];?></td>
                    <td><?php echo $res['remark'];?></td>
                </tr>
			
                <?php endif; endforeach;?>
                <?php  if($minorname !='' && $res['fdtable_fdid'] > 0){ ?>
                <tr class="strong">
                    <td></td>
                    <td colspan="4" align="left"><?php echo __('Invested Amount Total') ?></td>
                    <td align="left"><?php echo $Totalamt_md; ?></td>
                    <td></td><td></td><td></td>
                </tr>
				<?php }?>
			</tbody>

        </table>
		<?php } ?>

    <?php endif; ?>