@extends('layout')
@section('content')
<style>
    .input_upload {
        width:100px;
        height:35px;
        position:absolute;
        bottom:-34px;
        right:350px;
    }
    .account-sidebar {
        position: sticky;
        top: 20px;
        height: calc(100vh - 100px);
    }
    .account-sidebar .nav-link {
        border-radius: 0;
        padding: 12px 15px;
        color: #495057;
    }
    .account-sidebar .nav-link.active {
        background-color: #f8f9fa;
        color: #0d6efd;
        border-left: 3px solid #0d6efd;
        font-weight: 500;
    }
    .account-sidebar .nav-link:hover:not(.active) {
        background-color: #f8f9fa;
    }
    .balance-sheet-table th {
        background-color: #f8f9fa;
    }
    .total-row {
        font-weight: bold;
        background-color: #f1f1f1;
    }
    .section-header {
        font-weight: bold;
        background-color: #e9ecef;
    }
    .account-indent {
        padding-left: 30px !important;
    }
</style>

<!-- begin::main content -->
<main class="main-content">
    <div class="container-fluid">
        <div class="row">
            <!-- Main content area -->
            <div class="col-md-12">
                <div class="page-header">
                    <h3>Balance Sheet</h3>
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="{{ asset('dashboard') }}">{{ __('message.dashboard') }}</a></li>
                            <li class="breadcrumb-item active" aria-current="page">Balance Sheet</li>
                        </ol>
                    </nav>
                </div>

                <div class="card">
                    <div class="card-header">
                        <form action="{{ asset('account/balance-sheet') }}" method="get">
                            <div class="row mt-3">
                                <div class="col-md-8">   
                                    <div class="row">
                                        <div class="form-group col-md-4">
                                            <label>As of Date</label>
                                            <input type="date" name="end_date" value="{{request('end_date')}}" class="form-control">
                                        </div>
                                        <div class="input-group col-md-4"> 
                                            <div class="pt-4">                                       
                                                <button class="btn border" type="submit">
                                                    <i class="ti ti-search fa-lg"></i>
                                                </button>
                                                <a class="btn btn-white border" href="{{ asset('account/balance-sheet') }}">
                                                    <i class="ti ti-reload fa-lg"></i>
                                                </a>
                                            </div>
                                        </div> 
                                    </div>                                     
                                </div>
                                <div class="col-md-4">
                                    <div class="bg-light p-2 py-3 text-center">
                                        <h5 class="mb-0">Report Date</h5>
                                        <hr>
                                        <span class="font-weight-bold">
                                            {{ request('end_date') ? date('d M Y', strtotime(request('end_date'))) : 'End of Period' }}
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>

                    <div class="card-body">
                        <div class="table-responsive balance-sheet-box">
                            <div class="bg-light p-2 py-3 text-center balance-sheet-period d-none">
                                <h5 class="mb-0">As of Date</h5>
                                <hr>
                                <span class="font-weight-bold">
                                    {{ request('end_date') ? date('d M Y', strtotime(request('end_date'))) : 'End of Period' }}
                                </span>
                            </div>
                            
                            <div class="row">
                                <!-- Assets Column -->
                                <div class="col-md-6">
                                    <table class="table table-bordered balance-sheet-table">
                                        <thead>
                                            <tr>
                                                <th colspan="2" class="text-center">Assets</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <!-- Current Assets -->
                                            <tr class="section-header">
                                                <td colspan="2">Current Assets</td>
                                            </tr>
                                            <tr>
                                                <td class="account-indent">Cash and Cash Equivalents</td>
                                                <td class="text-right">
                                                    @if($cash["cash_type"] == "dr") {{$cash["cash"]}} @endif
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="account-indent">Bank A/c</td>
                                                <td class="text-right">
                                                    @if($bank["bank_type"] == "dr") {{$bank["bank"]}} @endif

                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="account-indent">Inventory</td>
                                                <td class="text-right">{{ number_format($inventory ?? 0, 2) }}</td>
                                            </tr>
                                            <tr>
                                                <td class="account-indent">Prepaid Expenses</td>
                                                <td class="text-right">{{ number_format($prepaid_expenses ?? 0, 2) }}</td>
                                            </tr>
                                            <tr class="total-row">
                                                <td>Total Current Assets</td>
                                                <td class="text-right">{{ number_format($total_current_assets ?? 0, 2) }}</td>
                                            </tr>
                                            
                                            <!-- Fixed Assets -->
                                            <tr class="section-header">
                                                <td colspan="2">Fixed Assets</td>
                                            </tr>
                                            <tr>
                                                <td class="account-indent">Property, Plant & Equipment</td>
                                                <td class="text-right">{{ number_format($fixed_assets ?? 0, 2) }}</td>
                                            </tr>
                                            <tr>
                                                <td class="account-indent">Less: Accumulated Depreciation</td>
                                                <td class="text-right">({{ number_format($accumulated_depreciation ?? 0, 2) }})</td>
                                            </tr>
                                            <tr class="total-row">
                                                <td>Net Fixed Assets</td>
                                                <td class="text-right">{{ number_format($net_fixed_assets ?? 0, 2) }}</td>
                                            </tr>
                                            
                                            <!-- Total Assets -->
                                            <tr class="total-row" style="background-color: #d4edda;">
                                                <td><strong>TOTAL ASSETS</strong></td>
                                                <td class="text-right"><strong>{{ number_format($total_assets ?? 0, 2) }}</strong></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                                
                                <!-- Liabilities & Equity Column -->
                                <div class="col-md-6">
                                    <table class="table table-bordered balance-sheet-table">
                                        <thead>
                                            <tr>
                                                <th colspan="2" class="text-center">Liabilities & Equity</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <!-- Current Liabilities -->
                                            <tr class="section-header">
                                                <td colspan="2">Current Liabilities</td>
                                            </tr>
                                            <tr>
                                                <td class="account-indent">Accounts Payable</td>
                                                <td class="text-right">{{ number_format($accounts_payable ?? 0, 2) }}</td>
                                            </tr>
                                            <tr>
                                                <td class="account-indent">Short-term Loans</td>
                                                <td class="text-right">{{ number_format($short_term_loans ?? 0, 2) }}</td>
                                            </tr>
                                            <tr>
                                                <td class="account-indent">Accrued Expenses</td>
                                                <td class="text-right">{{ number_format($accrued_expenses ?? 0, 2) }}</td>
                                            </tr>
                                            <tr class="total-row">
                                                <td>Total Current Liabilities</td>
                                                <td class="text-right">{{ number_format($total_current_liabilities ?? 0, 2) }}</td>
                                            </tr>
                                            
                                            <!-- Long-term Liabilities -->
                                            <tr class="section-header">
                                                <td colspan="2">Long-term Liabilities</td>
                                            </tr>
                                            <tr>
                                                <td class="account-indent">Long-term Loans</td>
                                                <td class="text-right">{{ number_format($long_term_loans ?? 0, 2) }}</td>
                                            </tr>
                                            <tr class="total-row">
                                                <td>Total Long-term Liabilities</td>
                                                <td class="text-right">{{ number_format($total_long_term_liabilities ?? 0, 2) }}</td>
                                            </tr>
                                            
                                            <!-- Equity -->
                                            <tr class="section-header">
                                                <td colspan="2">Equity</td>
                                            </tr>
                                            <tr>
                                                <td class="account-indent">Owner's Capital</td>
                                                <td class="text-right">{{ number_format($owners_capital ?? 0, 2) }}</td>
                                            </tr>
                                            <tr>
                                                <td class="account-indent">Retained Earnings</td>
                                                <td class="text-right">{{ number_format($retained_earnings ?? 0, 2) }}</td>
                                            </tr>
                                            <tr>
                                                <td class="account-indent">Current Year Profit</td>
                                                <td class="text-right">{{ number_format($current_year_profit ?? 0, 2) }}</td>
                                            </tr>
                                            <tr class="total-row">
                                                <td>Total Equity</td>
                                                <td class="text-right">{{ number_format($total_equity ?? 0, 2) }}</td>
                                            </tr>
                                            
                                            <!-- Total Liabilities & Equity -->
                                            <tr class="total-row" style="background-color: #d4edda;">
                                                <td><strong>TOTAL LIABILITIES & EQUITY</strong></td>
                                                <td class="text-right"><strong>{{ number_format($total_liabilities_equity ?? 0, 2) }}</strong></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            
                            <!-- Balance Check -->
                            @if(($total_assets ?? 0) != ($total_liabilities_equity ?? 0))
                                <div class="alert alert-danger text-center mt-3">
                                    <strong>Warning:</strong> Balance Sheet does not balance! (Assets: {{ number_format($total_assets ?? 0, 2) }} ≠ Liabilities & Equity: {{ number_format($total_liabilities_equity ?? 0, 2) }})
                                </div>
                            @endif
                        </div>
                        
                        <!-- Export buttons -->
                        <div class="mt-4 text-right">
                            <button class="btn btn-outline-primary print-balance-sheet">
                                <i class="ti ti-download"></i> Export as PDF
                            </button>
                            <a class="btn btn-outline-success ml-2" href="{{asset('account/export-balance-sheet')}}">
                                <i class="ti ti-file-excel"></i> Export as Excel
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>
<!-- end::main content -->
@endsection

@push('footer-script')
<!-- begin::dataTable -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.9.3/html2pdf.bundle.min.js"></script>

<!-- end::dataTable -->
<script>
    $(document).ready(function() {
        // Print functionality
        $(".print-balance-sheet").click(function(){
            $(".balance-sheet-period").removeClass("d-none");
            var element = document.querySelector(".balance-sheet-box"); 
            var opt = {
                margin: 0.3,
                filename: 'balance_sheet.pdf',
                image: { type: 'jpeg', quality: 1 },
                html2canvas: { scale: 2 },
                jsPDF: { unit: 'in', format: 'a4', orientation: 'landscape' }
            };

            html2pdf().set(opt).from(element).save();
            setTimeout(()=>{
                $(".balance-sheet-period").addClass("d-none");
            },4000);
        });
    });
</script>
@endpush