<?php
    ob_start();
    session_start();
    error_reporting(E_ALL);
    include "config/config.php";
    include "config/session.php";

    if(!isset($session)) {
        header('location: login.php');exit;
    }


    $statement_users = $pdo->prepare("SELECT * FROM  tbl_users WHERE tbl_users.id=?");
    $statement_users->execute(array($session_id));
    $user_datas = $statement_users->fetchAll(PDO::FETCH_ASSOC);
    $session_user_name = $user_datas[0]['name'];
    $current_date_time = date("d/m/Y h:i:s A");

    $ids = $_GET['id'];
    $statement = $pdo->prepare("SELECT cs.*,c.*,s.name AS 'scheme_name'
                                FROM tbl_customer_scheme AS cs 
                                LEFT JOIN tbl_customers AS c ON c.id = cs.customer_id
                                LEFT JOIN tbl_schemes AS s ON s.id = cs.scheme_id 
                                WHERE cs.id =?");
    $statement->execute(array($ids));
    $datas = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    if($totalData>0){
    
        $proof_details = $proof_name = $date_of_birth =  " ";
        
        $scheme_id = $datas[0]['scheme_id'];
        $scheme_no = $datas[0]['scheme_no'];
        $scheme_added_date = date('d/m/Y', strtotime($datas[0]['scheme_added_date']));
        $scheme_completed_date =date('d/m/Y', strtotime('+10 month', strtotime($datas[0]['scheme_added_date'])));
        // $scheme_complete_date = $datas[0]['scheme_completed_date'];
        // if($scheme_complete_date != '0000-00-00'){
        //     $scheme_completed_date = date('d/m/Y', strtotime($scheme_complete_date));
        // }
        $scheme_name = $datas[0]['scheme_name'];
        $scheme_amount = $datas[0]['scheme_amount'];
        $scheme_months = $datas[0]['scheme_months'];
        
        $location_id = $datas[0]['location_id'];

        $customer_id = $datas[0]['customer_id'];
        $customer_name = $datas[0]['customer_name'];
        $customer_address = $datas[0]['address'];
        $mobile_no = $datas[0]['mobile_no'];
        $dob = $datas[0]['dob'];
        if($dob != '0000-00-00'){
            $date_of_birth = date('d/m/Y', strtotime($dob));
        }
        $email = $datas[0]['email'];
        $bank_name = isset($datas[0]['bank_name']) ? $datas[0]['bank_name'] : '';
        $bank_account_holder_name = isset($datas[0]['bank_account_holder_name']) ? $datas[0]['bank_account_holder_name'] : '' ;
        $account_no = isset($datas[0]['account_no']) ? $datas[0]['account_no'] : '';
        $ifsc_code = isset($datas[0]['ifsc_code']) ? $datas[0]['ifsc_code'] : '';
        $branch = isset($datas[0]['branch']) ? $datas[0]['branch'] : '';
        

        $proof = $datas[0]['proof'];
        $pan_no = $datas[0]['pan_no'];
        $aadhaar_no = $datas[0]['aadhaar_no'];
        $gst_no = $datas[0]['gst_no'];
        $driving_no = $datas[0]['driving_no'];
        $voter_no = $datas[0]['voter_no'];
        $others_no = $datas[0]['others_no'];
        
        $nominee_name = $datas[0]['nominee_name'];
        $nominee_mobile_no = $datas[0]['nominee_mobile_no'];
        $nominee_address = $datas[0]['nominee_address'];
        $nominee_type = $datas[0]['nominee_type'];
       
        if($proof == 1){
            $proof_details = $aadhaar_no;
            $proof_name = "Aadhaar Number";
        }elseif ($proof == 2) {
            $proof_details = $pan_no;
            $proof_name = "Pan Number";
        }elseif ($proof ==  3) {
           $proof_details = $driving_no;
           $proof_name = "Driving Licence";
        }elseif ($proof ==  4) {
           $proof_details = $voter_no;
           $proof_name = "Voter Id";
        }elseif ($proof ==  5) {
           $proof_details = $others_no;
           $proof_name = "Other Number";
        }
            
        $statement_locations = $pdo->prepare("SELECT * FROM tbl_business_branch b WHERE b.id=?");
        $statement_locations->execute(array($location_id));
        $business_locations_datas = $statement_locations->fetchAll(PDO::FETCH_ASSOC);
     
        
            $business_name = isset($business_locations_datas[0]['business_name']) ? $business_locations_datas[0]['business_name'] : '';
            $business_mobile_no = isset($business_locations_datas[0]['mobile_no']) ? $business_locations_datas[0]['mobile_no'] : '' ;
            $business_email = isset($business_locations_datas[0]['business_email']) ? $business_locations_datas[0]['business_email'] : '';
            $business_address = isset($business_locations_datas[0]['business_address']) ? $business_locations_datas[0]['business_address'] : '';
            $business_gst = isset($business_locations_datas[0]['business_gst']) ? $business_locations_datas[0]['business_gst'] : '';
            $location_name = isset($business_locations_datas[0]['location_name']) ? $business_locations_datas[0]['location_name'] : '';
        

?>


<style>
    .p10{
        font-size: 10px;
    }
	.titles{
	    font-size: 10px;
	    font-weight: 600;
	    color: black;
	    vertical-align: top;
	}
	.values{
	    font-size: 10px;
	    font-weight: 600;
	    color: black;
	    vertical-align: top;
	}
	.titless{
	    font-size: 10px;
	    font-weight: 600;
	    color: black;
	    vertical-align: top;
	}
	.valuess{
	    font-size: 10px;
	    font-weight: 600;
	    color: black;
	    vertical-align: top;
	}
	.colon{
	    font-size: 10px;
	    font-weight: 600;
	    color: black;
	    vertical-align: top;
	}
    .signature-section {
        display: flex;
        justify-content: space-between;
        padding:25px 10px 5px;
        border-left: 1px solid black;
        border-right: 1px solid black;
        border-bottom: 1px solid black;
        border-collapse: collapse;
        vertical-align: top;
    }
    .signature-line {
        border: 1px solid #dcdcdc;
        width: 250px;
        margin: 0 auto;
        padding:20px
    }
    .signature-section .center {
        text-align: center;
    }
</style>


<p>Duplicate For Recipient</p>
<table style="width: 100%;margin-bottom:10px;">
    <tbody>
        <tr>
            <td style="width: 30%; vertical-align: top;"></td>
            <td style="width: 35%;text-align: center;vertical-align: top;">
                <img src="logo/spm-logos.png" alt="SPM Gold" style="text-align:center; width: 50%;object-fit:contain">
            </td>
            <td style="width: 35%;text-align: right;vertical-align: top;"> </td>

        </tr>
    </tbody>
</table>

<table style="width:100%;border-collapse:collapse;">
     <!-- Branch Details -->
    <tr style="border-top: 1px solid black;border-bottom: 1px solid black;border-collapse: collapse;">
        <td style="width: 60%;border-left: 1px solid black;font-size: 11px;border-collapse: collapse;vertical-align: top;padding:10px;">
			<table style="width: 100%;">
				<tr>
					<td style="width: 35%;" class="titles">Branch Name</td>
					<td style="width: 1%;vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$business_name?></td>
				</tr>
				<tr>
					<td style="" class="titles">Address</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$business_address?></td>
				</tr>
				<tr>
					<td style="" class="titles">Mobile</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$business_mobile_no?></td>
				</tr>
				<tr>
					<td style="" class="titles">State</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values">Tamil Nadu</td>
				</tr>
				<tr>
					<td style="" class="titles">District</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$location_name?></td>
				</tr>
				
			</table>
		</td>

        <td style="width: 50%;border-right: 1px solid black;vertical-align: top;border-collapse: collapse;font-size: 11px;padding:10px;">
			<table style="width:100%;vertical-align: top;">
				<tr>
					<td style="width: 35%;" class="titles">GSTIN</td>
					<td style="width: 1%; vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$business_gst?></td>
				</tr>
				<tr>
					<td style="" class="titles">Email Id</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$business_email?></td>
				</tr>
			</table>
		</td>
        
    </tr>
    <!-- <tr style="width:50%;border-collapse:collapse;text-align:center">
        <td style="border-bottom: 1px solid black;margin:5px">Golden Bloom Application Form</td>
    </tr> -->
    
</table>

<table style="width:100%;border-collapse:collapse;">
     <!-- Customer Details -->
    <tr style="border-collapse: collapse;">
        <!-- Applicant Details -->
        <td style="width: 60%;border-left: 1px solid black;font-size: 11px;border-collapse: collapse;vertical-align: top;padding:10px;">
			<table style="width: 100%;">
				<tr>
					<td style="width: 35%;" class="titles">Application ID</td>
					<td style="width: 1%;vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?= $scheme_no ?></td>
				</tr>
				<tr>
					<td style="" class="titles">Plan</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$scheme_name?></td>
				</tr>
				<tr>
					<td style="" class="titles">Customer</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?php echo ucwords($customer_name); ?></td>
				</tr>
				<tr>
					<td style="" class="titles">Address</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?php echo $customer_address; ?></td>
				</tr>
				<tr>
					<td style="" class="titles">Phone</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$mobile_no?></td>
				</tr>
                <?php if(!empty($alter_mob_no)){ ?>
                <tr>
					<td style="" class="titles">Mobile</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$alter_mob_no?></td>
				</tr>
                <?php } ?>
                <?php if(!empty($email)){ ?>
				<tr>
					<td style="" class="titles">Email</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$email?></td>
				</tr>
                <?php } ?>
                <tr>
					<td style="" class="titles">Date Of Birth</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$date_of_birth?></td>
				</tr>
                <tr>
					<td style="" class="titles">Identity Type</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$proof_name?></td>
				</tr>
                <tr>
					<td style="" class="titles">Identity Details</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$proof_details?></td>
				</tr>
				
			</table>
		</td>
        <!-- Nominee Details -->
        <td style="width: 50%;border-right: 1px solid black;vertical-align: top;border-collapse: collapse;font-size: 11px;padding:10px;">
			<table style="width:100%;vertical-align: top;">
				<tr>
					<td style="width: 35%;" class="titles">Nominee Name</td>
					<td style="width: 1%; vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$nominee_name?></td>
				</tr>
				<tr>
					<td style="" class="titles">Nominee Phone</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$nominee_mobile_no?></td>
				</tr>
                <tr>
					<td class="titless">Nominee Address</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$nominee_address?></td>
				</tr>
				
				<tr>
					<td class="titless">Relation With Nominee</td>
					<td class="colon">:</td>
					<td class="valuess"><?=$nominee_type?></td>
				</tr>
			</table>
		</td>

    </tr>
    <!-- Scheme Details -->
    <tr style="border-collapse: collapse;">

        <td style="width: 60%;border-left: 1px solid black;font-size: 11px;border-collapse: collapse;vertical-align: top;padding:10px;">
			<table style="width: 100%;">
				<tr>
					<td style="width: 35%;" class="titles">Start Date</td>
					<td style="width: 1%;vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$scheme_added_date?></td>
				</tr>
				<tr>
					<td style="" class="titles">Monthly Average</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$scheme_amount?></td>
				</tr>
				<tr>
					<td style="" class="titles">Gold Rate Application</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values">Average Rate</td>
				</tr>
				
				
			</table>
		</td>

        <td style="width: 50%;border-right: 1px solid black;vertical-align: top;border-collapse: collapse;font-size: 11px;padding:10px;">
			<table style="width:100%;vertical-align: top;">
				<tr>
					<td style="width: 35%;" class="titles">End Date</td>
					<td style="width: 1%; vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$scheme_completed_date?></td>
				</tr>
				<tr>
					<td style="" class="titles">Installment Due Date</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values">10th of every month</td>
				</tr>
                <tr>
					<td class="titless">Remainder Mode</td>
					<td class="colon">:</td>
					<td class="valuess">SMS</td>
				</tr>
				
				
			</table>
		</td>

    </tr>
    <!-- Payment Plan Details -->
    <tr style="border-collapse: collapse;">

        <td style="width: 60%;border-left: 1px solid black;font-size: 11px;border-collapse: collapse;vertical-align: top;padding:10px;">
            <table style="width: 100%;">
                <tr>
                    <td style="width: 35%;" class="titles">Bank A/C Holder Name</td>
                    <td style="width: 1%;vertical-align: top;" class="colon">:</td>
                    <td style="" class="values"><?=$bank_account_holder_name?></td>
                </tr>
                <tr>
                    <td style="" class="titles">Account No</td>
                    <td style="vertical-align: top;" class="colon">:</td>
                    <td style="" class="values"><?=$account_no?></td>
                </tr>
                <tr>
                    <td style="" class="titles">Branch</td>
                    <td style="vertical-align: top;" class="colon">:</td>
                    <td style="" class="values"><?=$branch?></td>
                </tr>
                
                
            </table>
        </td>

        <td style="width: 50%;border-right: 1px solid black;vertical-align: top;border-collapse: collapse;font-size: 11px;padding:10px;">
            <table style="width:100%;vertical-align: top;">
                <tr>
                    <td style="width: 35%;" class="titles">Bank Name</td>
                    <td style="width: 1%; vertical-align: top;" class="colon">:</td>
                    <td style="" class="values"><?=$bank_name?></td>
                </tr>
                <tr>
                    <td style="" class="titles">IFSC Code</td>
                    <td style="vertical-align: top;" class="colon">:</td>
                    <td style="" class="values"><?=$ifsc_code?></td>
                </tr>
                <!--<tr>-->
                <!--    <td class="titless">Proof Of Bank A/C</td>-->
                <!--    <td class="colon">:</td>-->
                <!--    <td class="valuess"></td>-->
                <!--</tr>-->
                
                
            </table>
        </td>

    </tr>

</table>

<table style="width:100%;border-collapse:collapse;">

    <!-- Signature Section -->
    <tr style="border-collapse: collapse;">
        <td class="signature-section">
            <div>
                <strong>Name: </strong><?php echo ucwords($customer_name);?><br>
                <strong>Place: </strong>Salem<br>
                <strong>Date: </strong><?=$current_date_time?>
            </div>
            <div>
                <span >Signature :</span>
                <div class="signature-line"></div>
            </div>
            <div class="center">
                <strong>Company Name<br><br><br>
                <strong>Authorised Signatory
            </div>
        </td>
    </tr>

</table>

<?php } else { ?>

<center>No Records Found</center>

<?php } ?>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" ></script>
<script>
    window.print(); 
    setTimeout(function(){window.close();},5000);
</script>
