<?php $this->load->view("common/header"); ?>
<!-- end: CSS REQUIRED FOR THIS PAGE ONLY -->
<!-- end: HEAD -->
<!-- start: BODY -->
<!-- Bootstrap 3 required CSS & JS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<body>
    <!-- start: HEADER -->
    <div class="navbar navbar-inverse navbar-fixed-top">
        <!-- start: TOP NAVIGATION CONTAINER -->
        <div class="container">
            <div class="navbar-header">
                <!-- start: RESPONSIVE MENU TOGGLER -->
                <button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button"> <span class="clip-list-2"></span> </button>
                <!-- end: RESPONSIVE MENU TOGGLER -->
                <!-- start: LOGO -->
                <?php $this->load->view("common/logo"); ?>
                <!-- end: LOGO -->
            </div>
            <div class="navbar-tools">
                <!-- start: TOP NAVIGATION MENU -->
                <?php $this->load->view("common/notifications"); ?>
                <!-- end: TOP NAVIGATION MENU -->
            </div>
        </div>
        <!-- end: TOP NAVIGATION CONTAINER -->
    </div>
    <!-- end: HEADER -->
    <!-- start: MAIN CONTAINER -->
    <div class="main-container">
        <div class="navbar-content">
            <!-- start: SIDEBAR -->
            <?php $this->load->view("common/navigation"); ?>
            <!-- end: SIDEBAR -->
        </div>
        <!-- start: PAGE -->
        <div class="main-content">
            <!-- end: SPANEL CONFIGURATION MODAL FORM -->
            <div class="container">
                <!-- start: PAGE HEADER -->
                <div class="row">
                    <div class="col-sm-12">
                        <!-- start: PAGE TITLE & BREADCRUMB -->
                        <ol class="breadcrumb">
                            <li> <a href="<?php echo base_url('dashboard'); ?>"> Dashboard </a> </li>
                            <li> <a href="#"> People </a> </li>
                            <li class="active"> Customer </li>
                            <li class="search-box">
                                <form class="sidebar-search">
                                    <div class="form-group">
                                        <input type="text" placeholder="Start Searching...">
                                        <button class="submit"> <i class="clip-search-3"></i> </button>
                                    </div>
                                </form>
                            </li>
                        </ol>
                        <div class="page-header">
                            <h1>Customers</h1>
                        </div>
                        <p>Please use the table below to navigate or filter the results. </p>
                    </div>
                </div>
                <!-- end: PAGE HEADER -->
                <div class="row">
                    <div class="col-md-12">


                        <!-- Assumes Bootstrap 3 + jQuery are already loaded in your layout -->

                        <div class="container">
                            <h2>Customer Detail</h2>

                            <!-- Nav tabs (BS3) -->
                            <ul class="nav nav-tabs">
                                <li class="active"><a href="#personal" data-toggle="tab">Personal Information</a></li>
                                <li><a href="#business" data-toggle="tab">Business Information</a></li>
                                <li><a href="#documents" data-toggle="tab">Documents</a></li>
                            </ul>

                            <div class="tab-content" style="margin-top:15px;">
                                <!-- PERSONAL -->
                                <div class="tab-pane fade in active" id="personal">
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><strong>Personal Information</strong></div>
                                        <div class="panel-body">
                                            <table class="table table-bordered table-striped table-condensed">
                                                <tr>
                                                    <th style="width:30%">Customer Code</th>
                                                    <td><?= $customer['cus_code'] ?? '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Title</th>
                                                    <td><?= $customer['title'] ?? '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Full Name</th>
                                                    <td><?= $customer['full_name'] ?? ($customer['cus_name'] ?? '-') ?></td>
                                                </tr>
                                                <?php if (!empty($customer['cus_first_name']) || !empty($customer['cus_last_name'])): ?>
                                                    <tr>
                                                        <th>First Name</th>
                                                        <td><?= $customer['cus_first_name'] ?? '-' ?></td>
                                                    </tr>
                                                    <tr>
                                                        <th>Last Name</th>
                                                        <td><?= $customer['cus_last_name'] ?? '-' ?></td>
                                                    </tr>
                                                <?php endif; ?>
                                                <tr>
                                                    <th>Date of Birth</th>
                                                    <td><?= $customer['date_of_birth'] ?? '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>NIC</th>
                                                    <td><?= $customer['cus_nic'] ?? '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>NIC Issue Date</th>
                                                    <td><?= $customer['nic_issue_date'] ?? '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Passport Number</th>
                                                    <td><?= $customer['passport_number'] ?? '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Mobile 01</th>
                                                    <td><?= $customer['cus_mobile'] ?? '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Mobile 02</th>
                                                    <td><?= $customer['cus_mobile_2'] ?? '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Email</th>
                                                    <td><?= $customer['cus_email'] ?? '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Home Phone</th>
                                                    <td><?= $customer['cus_home_phone'] ?? '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Permanent Address</th>
                                                    <td><?= $customer['cus_address'] ?? '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Correspondent Address</th>
                                                    <td><?= $customer['correspondent_address'] ?? '-' ?></td>
                                                </tr>

                                                <tr>
                                                    <th>Customer Type</th>
                                                    <td><?= $customer['cus_type'] ?? '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Status</th>
                                                    <td><?= (isset($customer['cus_status']) && (int)$customer['cus_status'] === 1) ? 'Active' : 'Inactive' ?></td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>

                                    <!-- Banking Relationship -->
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><strong>Banking Relationship with Banks</strong></div>
                                        <div class="panel-body">
                                            <?php if (!empty($bank_accounts)): ?>
                                                <table class="table table-bordered table-striped table-condensed">
                                                    <thead>
                                                        <tr>
                                                            <th>Bank Name</th>
                                                            <th>Branch</th>
                                                            <th>Account Number</th>
                                                            <th>Account Type</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php foreach ($bank_accounts as $bank): ?>
                                                            <tr>
                                                                <td><?= $bank['bank_name'] ?? '-' ?></td>
                                                                <td><?= $bank['branch'] ?? '-' ?></td>
                                                                <td><?= $bank['account_number'] ?? '-' ?></td>
                                                                <td><?= $bank['account_type'] ?? '-' ?></td>
                                                            </tr>
                                                        <?php endforeach; ?>
                                                    </tbody>
                                                </table>
                                            <?php else: ?>
                                                <p>No banking relationships recorded.</p>
                                            <?php endif; ?>
                                        </div>
                                    </div>

                                    <!-- Loans Obtained -->
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><strong>Loans Obtained from Banks / Financial Institutes</strong></div>
                                        <div class="panel-body">
                                            <?php if (!empty($loans)): ?>
                                                <table class="table table-bordered table-striped table-condensed">
                                                    <thead>
                                                        <tr>
                                                            <th>Bank Name</th>
                                                            <th>Loan Type</th>
                                                            <th>Loan Amount</th>
                                                            <th>Outstanding Balance</th>
                                                            <th>Monthly Installment</th>
                                                            <th>Start Date</th>
                                                            <th>End Date</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php foreach ($loans as $loan): ?>
                                                            <tr>
                                                                <td><?= $loan['bank_name'] ?? '-' ?></td>
                                                                <td><?= $loan['loan_type'] ?? '-' ?></td>
                                                                <td><?= !empty($loan['loan_amount']) ? 'LKR ' . number_format($loan['loan_amount'], 2) : '-' ?></td>
                                                                <td><?= !empty($loan['outstanding_balance']) ? 'LKR ' . number_format($loan['outstanding_balance'], 2) : '-' ?></td>
                                                                <td><?= !empty($loan['monthly_installment']) ? 'LKR ' . number_format($loan['monthly_installment'], 2) : '-' ?></td>
                                                                <td><?= $loan['loan_start_date'] ?? '-' ?></td>
                                                                <td><?= $loan['loan_end_date'] ?? '-' ?></td>
                                                            </tr>
                                                        <?php endforeach; ?>
                                                    </tbody>
                                                </table>
                                            <?php else: ?>
                                                <p>No loans recorded.</p>
                                            <?php endif; ?>
                                        </div>
                                    </div>

                                    <!-- Spouse Details -->
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><strong>Spouse Details</strong></div>
                                        <div class="panel-body">
                                            <?php if (!empty($spouse_details)): ?>
                                                <table class="table table-bordered table-striped table-condensed">
                                                    <thead>
                                                        <tr>
                                                            <th>Name</th>
                                                            <th>NIC</th>
                                                            <th>Contact Number</th>
                                                            <th>Occupation</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>

                                                        <tr>
                                                            <td><?= $spouse_details['spouse_name'] ?? '-' ?></td>
                                                            <td><?= $spouse_details['spouse_nic'] ?? '-' ?></td>
                                                            <td><?= $spouse_details['spouse_contact'] ?? '-' ?></td>
                                                            <td><?= $spouse_details['spouse_occupation'] ?? '-' ?></td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            <?php else: ?>
                                                <p>No spouse details recorded.</p>
                                            <?php endif; ?>
                                        </div>
                                    </div>

                                    <!-- Guarantor Details -->
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><strong>Guarantor Details</strong></div>
                                        <div class="panel-body">
                                            <?php if (!empty($guarantor_details)): ?>
                                                <table class="table table-bordered table-striped table-condensed">
                                                    <thead>
                                                        <tr>
                                                            <th>Name</th>
                                                            <th>NIC</th>
                                                            <th>Contact Number</th>
                                                            <th>Address</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <tr>
                                                            <td><?= $guarantor_details['guarantor_name'] ?? '-' ?></td>
                                                            <td><?= $guarantor_details['guarantor_nic'] ?? '-' ?></td>
                                                            <td><?= $guarantor_details['guarantor_contact'] ?? '-' ?></td>
                                                            <td><?= $guarantor_details['guarantor_address'] ?? '-' ?></td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            <?php else: ?>
                                                <p>No guarantor details recorded.</p>
                                            <?php endif; ?>
                                        </div>
                                    </div>

                                    <!-- Assets & Income -->
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><strong>Applications: Ownership of Wealth and Estimated Value</strong></div>
                                        <div class="panel-body">
                                            <table class="table table-bordered table-striped table-condensed">
                                                <tr>
                                                    <th style="width:30%">Residential Property</th>
                                                    <td><?= !empty($customer['residential_property_value']) ? 'LKR ' . number_format($customer['residential_property_value'], 2) : '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Motor Vehicles</th>
                                                    <td><?= !empty($customer['motor_vehicles_value']) ? 'LKR ' . number_format($customer['motor_vehicles_value'], 2) : '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Investments</th>
                                                    <td><?= !empty($customer['investments']) ? 'LKR ' . number_format($customer['investments'], 2) : '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Other Assets / Value</th>
                                                    <td><?= !empty($customer['aoowaev_other']) ? 'LKR ' . number_format($customer['aoowaev_other'], 2) : '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Other Connected Business / Professional Activities</th>
                                                    <td><?= !empty($customer['other_connected_activities']) ? htmlspecialchars($customer['other_connected_activities']) : '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Number of Dependents</th>
                                                    <td><?= isset($customer['dependents']) ? (int)$customer['dependents'] : '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Monthly Income</th>
                                                    <td><?= !empty($customer['income']) ? 'LKR ' . number_format($customer['income'], 2) : '-' ?></td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>



                                    <div class="panel panel-default">
                                        <div class="panel-heading"><strong>Employment / Business Basics (from Personal form)</strong></div>
                                        <div class="panel-body">
                                            <table class="table table-bordered table-striped table-condensed">
                                                <tr>
                                                    <th style="width:30%">Position Held</th>
                                                    <td><?= $customer['position_held'] ?? '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Status Of Residential Address</th>
                                                    <td><?= $customer['status_residential_address'] ?? '-' ?></td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>

                                </div>

                                <!-- BUSINESS -->
                                <div class="tab-pane fade" id="business">
                                    <!-- Core Business Info -->
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><strong>Business Information (Section B)</strong></div>
                                        <div class="panel-body">
                                            <?php if (!empty($business_info)): ?>
                                                <table class="table table-bordered table-striped table-condensed">
                                                    <tr>
                                                        <th style="width:30%">Name of the Business</th>
                                                        <td><?= $business_info->business_name ?? ($business_info->cus_company ?? '-') ?></td>
                                                    </tr>
                                                    <tr>
                                                        <th>Registered Address</th>
                                                        <td><?= $business_info->registered_address ?? '-' ?></td>
                                                    </tr>
                                                    <tr>
                                                        <th>Correspondent Address</th>
                                                        <td><?= $business_info->correspondent_address ?? '-' ?></td>
                                                    </tr>
                                                    <tr>
                                                        <th>Registration Number</th>
                                                        <td><?= $business_info->registration_number ?? '-' ?></td>
                                                    </tr>
                                                    <tr>
                                                        <th>Date of Incorporation</th>
                                                        <td><?= $business_info->date_of_incorporation ?? '-' ?></td>
                                                    </tr>
                                                    <tr>
                                                        <th>Nature of the Business</th>
                                                        <td><?= $business_info->nature_of_business ?? '-' ?></td>
                                                    </tr>
                                                    <tr>
                                                        <th>Contact Number</th>
                                                        <td><?= $business_info->contact_number ?? '-' ?></td>
                                                    </tr>
                                                    <tr>
                                                        <th>Email Address</th>
                                                        <td><?= $business_info->email_address ?? '-' ?></td>
                                                    </tr>
                                                    <!-- Echo the same “type/status” from customer table for context -->
                                                    <tr>
                                                        <th>Customer Type</th>
                                                        <td><?= $customer['cus_type'] ?? '-' ?></td>
                                                    </tr>
                                                    <tr>
                                                        <th>Status</th>
                                                        <td><?= (isset($customer['cus_status']) && (int)$customer['cus_status'] === 1) ? 'Active' : 'Inactive' ?></td>
                                                    </tr>
                                                </table>
                                            <?php else: ?>
                                                <p>No business information available.</p>
                                            <?php endif; ?>
                                        </div>
                                    </div>

                                    <!-- Assets -->
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><strong>Assets owned by the Entity and Estimated Value</strong></div>
                                        <div class="panel-body">
                                            <table class="table table-bordered table-striped table-condensed">
                                                <tr>
                                                    <th style="width:30%">Property</th>
                                                    <td><?= !empty($business_info->property_value) ? 'LKR ' . $business_info->property_value : '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Motor Vehicles</th>
                                                    <td><?= !empty($business_info->vehicles_value) ? 'LKR ' . $business_info->vehicles_value : '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Investments</th>
                                                    <td><?= !empty($business_info->investments_value) ? 'LKR ' . $business_info->investments_value : '-' ?></td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>

                                    <!-- Financial Information -->
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><strong>Financial Information</strong></div>
                                        <div class="panel-body">
                                            <table class="table table-bordered table-striped table-condensed">
                                                <tr>
                                                    <th style="width:30%">Per Day Sales Turnover</th>
                                                    <td><?= !empty($business_info->daily_turnover) ? 'LKR ' . $business_info->daily_turnover : '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Per Day Net Profit/Loss</th>
                                                    <td><?= !empty($business_info->daily_profit) ? 'LKR ' . $business_info->daily_profit : '-' ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Total Liabilities</th>
                                                    <td><?= !empty($business_info->total_liabilities) ? 'LKR ' . $business_info->total_liabilities : '-' ?></td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>

                                    <!-- Banking Relationship -->
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><strong>Banking Relationship with Banks</strong></div>
                                        <div class="panel-body">
                                            <?php
                                            // Expect either $bank_accounts OR $business_info->bank_accounts (controller-dependent)
                                            $banks = [];
                                            if (isset($bank_accounts) && is_array($bank_accounts)) {
                                                $banks = $bank_accounts;
                                            } elseif (isset($business_info->bank_accounts) && is_array($business_info->bank_accounts)) {
                                                $banks = $business_info->bank_accounts;
                                            }
                                            ?>
                                            <?php if (!empty($banks)): ?>
                                                <table class="table table-bordered table-striped table-condensed">
                                                    <thead>
                                                        <tr>
                                                            <th>Bank Name</th>
                                                            <th>Branch</th>
                                                            <th>Account Number</th>
                                                            <th>Account Type</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php foreach ($banks as $bank): ?>
                                                            <tr>
                                                                <td><?= !empty($bank->bank_name) ? $bank->bank_name : (!empty($bank['bank_name']) ? $bank['bank_name'] : '-') ?></td>
                                                                <td><?= !empty($bank->branch) ? $bank->branch : (!empty($bank['branch']) ? $bank['branch'] : '-') ?></td>
                                                                <td><?= !empty($bank->account_number) ? $bank->account_number : (!empty($bank['account_number']) ? $bank['account_number'] : '-') ?></td>
                                                                <td><?= !empty($bank->account_type) ? $bank->account_type : (!empty($bank['account_type']) ? $bank['account_type'] : '-') ?></td>
                                                            </tr>
                                                        <?php endforeach; ?>
                                                    </tbody>
                                                </table>
                                            <?php else: ?>
                                                <p>No banking relationships recorded.</p>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>

                                <!-- DOCUMENTS -->
                                <div class="tab-pane fade" id="documents">
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><strong>Documents</strong></div>
                                        <div class="panel-body">

                                            <?php if (!empty($documents)): ?>
                                                <div class="row">
                                                    <?php foreach ($documents as $doc): ?>
                                                        <?php
                                                        $filePath = base_url($doc->file_path . $doc->file_name);
                                                        //$filePath = $doc->file_path . $doc->file_name;
                                                        $ext = strtolower(pathinfo($doc->file_name, PATHINFO_EXTENSION));
                                                        $isImage = in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']);
                                                        ?>
                                                        <div class="col-sm-4" style="margin-bottom:15px;">
                                                            <div class="panel panel-info">
                                                                <div class="panel-heading">
                                                                    <?= htmlspecialchars($doc->doc_type) ?>
                                                                </div>
                                                                <div class="panel-body" style="text-align:center;">
                                                                    <?php if ($isImage): ?>
                                                                        <a href="<?= $filePath ?>" target="_blank">
                                                                            <img src="<?= $filePath ?>" alt="<?= htmlspecialchars($doc->doc_type) ?>"
                                                                                class="img-responsive img-thumbnail" style="max-height:200px; margin:auto;">
                                                                        </a>
                                                                    <?php else: ?>
                                                                        <p><i class="glyphicon glyphicon-file" style="font-size:40px;"></i></p>
                                                                        <a href="<?= $filePath ?>" target="_blank"><?= htmlspecialchars($doc->file_name) ?></a>
                                                                    <?php endif; ?>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    <?php endforeach; ?>
                                                </div>
                                            <?php else: ?>
                                                <p>No documents uploaded.</p>
                                            <?php endif; ?>
                                        </div>



                                        <!-- Optional checklist rendering (for clarity of 9 expected docs) -->
                                        <hr>
                                        <p><strong>Expected uploads:</strong></p>
                                        <div class="row">
                                            <div class="col-sm-6">
                                                <ul>
                                                    <li>NIC Front Side</li>
                                                    <li>NIC Back Side</li>
                                                    <li>Passport Copy (if available)</li>
                                                    <li>Utility Bill</li>
                                                    <li>Deed or Lease Agreement</li>
                                                </ul>
                                            </div>
                                            <div class="col-sm-6">
                                                <ul>
                                                    <li>Business Registration Certificate</li>
                                                    <li>Business License</li>
                                                    <li>Tax Registration Certificate</li>
                                                    <li>Bank Statements (Last 6 months)</li>
                                                    <li>Financial Statements (if available)</li>
                                                </ul>
                                            </div>
                                        </div>

                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>





                </div>
                <!-- end: DYNAMIC TABLE PANEL -->
            </div>
        </div>

        <!-- end grid -->

    </div>
    <!-- end: PAGE -->
    </div>
    <!-- end: MAIN CONTAINER -->
    <!-- start: FOOTER -->
    <div class="footer clearfix">
        <div class="footer-inner"> <?php echo date("Y"); ?> &copy; A.S.S.K Loan Management System </div>
        <div class="footer-items"> <span class="go-top"><i class="clip-chevron-up"></i></span> </div>
    </div>





</body>
<!-- end: BODY -->

</html>