@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;
    }
    .trial-balance-table th {
        background-color: #f8f9fa;
    }
    .dr-amount {
        color: #dc3545;
    }
    .cr-amount {
        color: #28a745;
    }
    .total-row {
        font-weight: bold;
        background-color: #f1f1f1;
    }
</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>Profit And Loss</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">Profit & Loss</li>
                        </ol>
                    </nav>
                </div>

                <div class="card">
                    <div class="card-header">
                        <form action="{{ asset('account/profit-and-loss') }}" method="get">
                            <div class="row mt-3">
                                <div class="col-md-8">   
                                    <div class="row">
                                        <div class="form-group col-md-4">
                                            <label>Start Date</label>
                                            <input type="date" name="start_date" value="{{request('start_date')}}" class="form-control">
                                        </div>
                                        <div class="form-group col-md-4">
                                            <label>End 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/profit-and-loss') }}">
                                                    <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 Period </h5>
                                            <hr>
                                            <span class="font-weight-bold">
                                                {{ request('start_date') ? date('d M Y', strtotime(request('start_date'))) : '1st' }} 
                                                to 
                                                {{ request('end_date') ? date('d M Y', strtotime(request('end_date'))) : 'End of Month' }}
                                            </span>
                                        
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>

                    <div class="card-body">
                        <div class="table-responsive trial-box">
                            <div class=" bg-light p-2 py-3 text-center trial_period d-none">
                                <h5 class="mb-0">Report Period </h5>
                                    <hr>
                                    <span class="font-weight-bold">
                                        {{ request('start_date') ? date('d M Y', strtotime(request('start_date'))) : '1st' }} 
                                        to 
                                        {{ request('end_date') ? date('d M Y', strtotime(request('end_date'))) : 'End of Month' }}
                                    </span>
                                
                            </div>
                            <table class="table table-bordered trial-balance-table">
                                <thead>
                                    <tr>
                                        <th>Account Name</th>
                                        <th class="text-right">Debit (Dr)</th>
                                        <th class="text-right">Credit (Cr)</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @php
                                        $total_debit = 0;
                                        $total_credit = 0;
                                    @endphp
                                    
                                    
                                    <tr>
                                        <td>Purchase</td>
                                        <td class="text-right dr-amount">
                                           @php $total_debit += $purchase;  @endphp
                                            {{$purchase? $purchase : 0}}
                                        </td>
                                        <td class="text-right cr-amount">
                                            _____
                                        </td>
                                        
                                    </tr>
                                    <tr>
                                        <td>Debit Note</td>
                                        <td class="text-right dr-amount">
                                            _____
                                        </td>
                                        <td class="text-right cr-amount">
                                           @php $total_credit += $debit_note;  @endphp
                                            {{$debit_note? $debit_note : 0}}
                                        </td>
                                        
                                    </tr>
                                    <tr>
                                        <td>Sales</td>
                                        <td class="text-right dr-amount">
                                            _____
                                        </td>
                                        <td class="text-right cr-amount">
                                           @php $total_credit += $sales;  @endphp
                                            {{$sales? $sales : 0}}
                                        </td>
                                        
                                    </tr>
                                    <tr>
                                        <td>Credit Note</td>
                                        <td class="text-right dr-amount">
                                           @php $total_debit += $credit_note;  @endphp
                                            {{$credit_note? $credit_note : 0}}
                                        </td>
                                        <td class="text-right cr-amount">
                                            _____
                                        </td>
                                    
                                    </tr>
                                    <tr>
                                        <td>Expenses</td>
                                        <td class="text-right dr-amount">
                                           @php $total_debit += $expenses;  @endphp
                                            {{$expenses? $expenses : 0}}
                                        </td>
                                        <td class="text-right cr-amount">
                                            _____
                                        </td>
                                    
                                    </tr>
                                    
                                    <!-- Total Row -->
                                    <tr class="total-row">
                                        <td><strong>Total</strong></td>
                                        <td class="text-right dr-amount"><strong>₹ {{ number_format($total_debit, 2) }}</strong></td>
                                        <td class="text-right cr-amount"><strong>₹ {{ number_format($total_credit, 2) }}</strong></td>
                                    </tr>
                                    
                                    <!-- Difference Row (should be zero if balanced) -->
                                    <tr>
                                        <td><strong>Difference</strong></td>
                                        <td colspan="2" class="text-center {{ $total_debit == $total_credit ? 'text-success' : 'text-danger' }}">
                                            <strong>
                                                @if($total_debit > $total_credit)
                                                    Net Loss (₹ 0.00)
                                                @else
                                                    Net Profit (₹ {{ number_format(abs($total_debit - $total_credit), 2) }})
                                                @endif
                                            </strong>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                        
                        <!-- Export buttons -->
                        <div class="mt-4 text-right">
                            <button class="btn btn-outline-primary print-trial">
                                <i class="ti ti-download"></i> Export as PDF
                            </button>
                            <a class="btn btn-outline-success ml-2" href="{{asset('account/export-profit-and-loss')}}">
                                <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-trial").click(function(){
            $(".trial_period").removeClass("d-none");
            var element = document.querySelector(".trial-box"); 
            var opt = {
                margin: 0.3,
                filename: 'profit_and_loss.pdf',
                image: { type: 'jpeg', quality: 1 },
                html2canvas: { scale: 2 },
                jsPDF: { unit: 'in', format: 'a4', orientation: 'landscape' }
            };

           
            html2pdf().set(opt).from(element).save();
            setTimeout(()=>{
                $(".trial_period").addClass("d-none");
            },4000);
            
       });
        
        // You can add export functionality here
    });
</script>
@endpush