@extends('backend.layouts.master')
@section('title')
    Sale - {{ $title }}
@endsection

@section('styles')
    <style>
        .invoice-container {
            position: relative;
            margin: 0 auto;
            background: #fff;
            padding: 20px;
            border: 1px solid #ddd;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            position: relative;
        }

        /* Watermark */
        .watermark {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%) rotate(-45deg);
            font-size: 100px;
            font-weight: bold;
            color: rgba(0, 0, 0, 0.05);
            z-index: -1;
            pointer-events: none;
        }

        /* Header Styling */
        .header {
            display: flex;
            align-items: center;
            background-color: #ffffff;
            /* White background */
            border-bottom: 2px solid #000000;
            /* Black underline */
            padding: 10px;
        }

        .header-left {
            width: 100%;
            /* Left part takes 20% of the header width */
        }

        .header-left .logo {
            max-width: 100%;
            /* Ensure the image fits within the left part */
            height: auto;
        }

        .header-right {
            width: 50%;
            /* Right part takes 80% of the header width */
            text-align: left;
        }

        .header-right h1 {
            font-size: 32px;
            font-weight: 600;
            color: #000000;
            text-align: right;
            margin: 0;
        }

        .header-right p {
            font-size: 16px;
            font-weight: 600;
            text-align: right;
            /* color: #420297; */
            margin: 5px 0 0;
        }

        /* Invoice Details Section */
        .invoice-details {
            margin-bottom: 20px;
        }

        .invoice-info {
            text-align: left;
            /* Align text to the left */
        }

        /* Button Styling */
        .invoice-button {
            display: block;
            /* Make the button a block element */
            width: 100%;
            /* Full width of the container */
            padding: 5px;
            /* Add padding for better appearance */
            background-color: #007BFF;
            /* Blue background color */
            color: #ffffff;
            /* White text color */
            font-size: 16px;
            /* Font size */
            font-weight: bold;
            /* Bold text */
            text-align: center;
            /* Center the text inside the button */
            border: none;
            /* Remove default border */
            border-radius: 5px;

            /* Pointer cursor on hover */
            margin-bottom: 15px;
            /* Add space below the button */
        }


        .invoice-details,
        .supplier-customer,
        .product-table,
        .totals,
        .attachments,
        .footer {
            margin-bottom: 20px;
        }

        .supplier-customer {
            display: flex;
            justify-content: space-between;
        }

        .supplier,
        .customer {
            width: 48%;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }

        table th,
        table td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }

        table th {
            background-color: #f4f4f4;
        }

        .totals p,
        .attachments ul {
            margin-bottom: 10px;
        }

        .attachments ul {
            list-style-type: disc;
            padding-left: 20px;
        }

        .footer {
            display: flex;
            justify-content: space-between;
        }



        .contact-info a {
            color: #007BFF;
            text-decoration: none;
        }

        .contact-info a:hover {
            text-decoration: underline;
        }

        .watermark {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            opacity: 0.1;
            z-index: 1;
            width: 729px;
            height: auto;
            user-select: none;
            pointer-events: none;
        }

        .watermark img {
            width: 100%;
            height: auto;
        }

        .custom-footer {
            background: #f2f2f2;
            padding: 20px 0;
            text-align: center;
            border-top: 3px solid #007bff;
            /* Blue border for a professional look */
        }

        .footer-container {
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
        }

        .footer-section {
            flex: 1;
            min-width: 250px;
            padding: 10px;
            text-align: left;
        }

        .footer-section h5 {
            color: #333;
            font-size: 16px;
            margin-bottom: 10px;
            font-weight: bold;
        }

        .footer-section p {
            font-size: 14px;
            color: #0c0c0c;
            margin: 5px 0;
        }

        .footer-section a {
            color: #007bff;
            text-decoration: none;
            font-weight: bold;
        }

        .footer-section a:hover {
            text-decoration: underline;
        }

        .prepared-received-section {
            display: flex;
            justify-content: space-between;
            padding: 20px;
            border-top: 2px solid #ddd;
            margin-top: 20px;
        }

        .prepared-by,
        .received-by {
            flex: 1;
            padding: 10px;
        }

        .prepared-by h3,
        .received-by h3 {
            font-size: 18px;
            color: #333;
            margin-bottom: 10px;
            text-decoration: underline;
        }

        .custom-table {
            width: 100%;
            border-collapse: collapse;
        }

        .custom-table td {
            padding: 5px;
            font-size: 14px;
            color: #080808;
        }

        .custom-table td:first-child {
            font-weight: bold;
            width: 100px;
        }

        .signature-line {
            margin-top: 40px;
            font-size: 18px;
            font-weight: bold;
            text-align: left;
        }

        body {
            font-size: 20px;
            /* Adjust as needed */
        }

        h1,
        h2,
        h3,
        h4,
        h5,
        h6 {
            font-size: 26px;
            /* Larger headers */
        }

        table {
            font-size: 18px;
            border-collapse: collapse;
            border: 2px solid black;
            /* Outer border */
        }

        p,
        td,
        th {
            font-size: 18px;

        }

        .btn {
            font-size: 20px;
        }


        .invoice-container {
            font-size: 23px;
            /* Increase font size for better readability */
        }

        h1,
        h2,
        h3 {
            font-size: 30px;
            /* Adjust header sizes */
        }

        table td,
        table th {
            border: 1px solid black;
            /* Inner cell borders */
            font-size: 18px;
            /* Increase table text size */
        }

        p {
            font-size: 20px !important;
        }
    </style>
@endsection

@section('navbar-content')
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0">
                        Sale </h1>
                </div><!-- /.col -->
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="{{ route('home') }}">Dashboard</a></li>
                        @if (helper::roleAccess('sale.sale.index'))
                            <li class="breadcrumb-item"><a href="{{ route('sale.sale.index') }}">Sale</a></li>
                        @endif
                        <li class="breadcrumb-item active"><span>Sale List</span></li>
                    </ol>
                </div><!-- /.col -->
            </div><!-- /.row -->
        </div><!-- /.container-fluid -->
    </div>
@endsection

@section('admin-content')
    <div class="row">
        <div class="col-md-12">

            <div class="card card-default">
                <div class="card-header">
                    <h3 class="card-title">Sales Invoice</h3>

                </div>
                <div class="card-body">
                    <div class="row no-print">
                        <div class="col-12">
                            <a onclick="printInvoice()" class="btn btn-default float-right my-2">
                                <i class="fas fa-print"></i> Print
                            </a>

                        </div>
                    </div>
                </div>
            </div>
            <div class="invoice-container">
                <!-- Watermark -->

                <header class="header">
                    <div class="header-left text-center">
                        <!-- Replace 'logo.png' with the actual path to your logo image -->

                        @if (isset($companyInfo->invoice_logo))
                            <a href="{{ route('home') }}">
                                <img width="300px" class="text-center"
                                    src="{{ asset('/backend/invoicelogo/' . $companyInfo->invoice_logo) }}" style=""
                                    alt="">
                            </a>
                        @endif
                        <p><b style="font-size: 16px !important;">
                                <i class="fas fa-map-marker-alt"></i> {{ $companyInfo->address }}
                            </b></p>
                        <p style="margin-top: -15px">
                            <b style="font-size: 16px !important;">
                                <i class="fas fa-phone-volume"></i> {{ $companyInfo->phone }}
                            </b> ||
                            <b style="font-size: 16px !important;">
                                <i class="fas fa-envelope"></i> {{ $companyInfo->email }}
                            </b>
                        </p>



                    </div>
                    {{-- <div class="header-right">
                        <span> <b> {{ $companyInfo->address }} </b></span>
                        <p>Value Adding is Our Business</p>
                    </div> --}}
                </header>
                <div class="watermark">
                    @if (isset($companyInfo->invoice_logo))
                        <a href="{{ route('home') }}">
                            <img src="{{ asset('/backend/invoicelogo/' . $companyInfo->invoice_logo) }}" style=""
                                alt="">
                        </a>
                    @endif
                </div>
                <section class="invoice-details px-3">
                    <div class="invoice-info">
                        <!-- Add the button here -->
                        <div class="pt-2" style="    padding: 0 433px;">
                            <div class="invoice-button">
                                <span>Bill</span>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6 col-sm-6 text-left">
                                <span style="text-decoration: underline ;">Chalan No:
                                    {{ str_replace('SV', 'CN', $invoice->invoice_no ?? 'N/A') }}</span> <br>
                                <span style="text-decoration: underline ;">Buyer Name:
                                    {{ optional(optional($invoice->customer)->accountable)->name ?? '' }}
                                </span> <br>
                                <span style="text-decoration: underline ;">Address:
                                    {{ optional(optional($invoice->customer)->accountable)->address ?? '' }}
                                </span> <br>
                            </div>
                            <div class="col-md-6 col-sm-6 text-right">
                                <span style="text-decoration: underline ;"> Date :
                                    {{ $invoice->date }}</span> <br>
                                <span style="text-decoration: underline ;">Mobile No:
                                    {{ optional(optional($invoice->customer)->accountable)->phone ?? '' }}
                                </span> <br>
                            </div>
                        </div>

                    </div>
                </section>

                {{-- <section class="supplier-customer px-3">
                    <div class="supplier" style="    border-right: 1px solid;">
                        <h5>Supplied By:</h5>
                        <p><strong>{{ $companyInfo->company_name }}</strong></p>
                        <p><strong>Office Address:</strong> {{ $companyInfo->address }}
                        </p>
                        <p><strong>Factory & Warehouse:</strong> {{ $invoice->branch->address ?? '' }}</p>
                    </div>

                    <div class="customer">
                        <h5>Supplied For:</h5>
                        <p><strong>Customer:</strong> {{ optional(optional($invoice->customer)->accountable)->name ?? '' }}
                        </p>
                        <p><strong>Address:</strong>{{ optional(optional($invoice->customer)->accountable)->address ?? '' }}
                        </p>
                    </div>
                </section> --}}

                <section class="product-table px-3">
                    <table>
                        <thead>
                            <tr>
                                <th>SN</th>
                                <th>Product Name </th>
                                <th>Quantity </th>
                                {{-- <th>Fabrics </th> --}}
                                <th>Unit Price (Tk)</th>
                                <th>Total Price (Tk)</th>
                            </tr>
                        </thead>
                        <tbody>
                            @php
                                $groupedDetails = $invoice->details->groupBy('team');
                                $sl = 0;
                                $gtotalQty = $invoice->details->sum('qty');
                                $gtotalUp = $invoice->details->sum('rate');
                                $gtotalPrice = $invoice->details->sum('price');
                            @endphp

                            @foreach ($groupedDetails as $team => $groupedDetail)
                                @php
                                    $totalQty = 0;
                                    $totalUp = 0;
                                    $totalPrice = 0;
                                    $gsubtotal = 0;
                                @endphp
                                <tr>
                                    <td colspan="5"> <b> ({{ $team }}) </b></td>
                                </tr>
                                @foreach ($groupedDetail as $detail)
                                    @php
                                        $totalQty += $detail->qty;
                                        $totalUp += $detail->rate;
                                        $totalPrice += $detail->price;
                                        $subtotal = number_format($totalPrice, 2);
                                        $sl += 1;

                                    @endphp
                                    <tr>
                                        <td>{{ $sl }}</td>
                                        <td> {{ optional($detail->product)->getRawOriginal('name') ?? 'N/A' }}
                                          
                                        </td>
                                        <td class="text-center">{{ $detail->qty ?? 'N/A' }}
                                            {{ optional(optional(optional($detail)->product)->unit)->name ?? 'N/A' }}</td>
                                        {{-- <td class="text-center">{{ $detail->fabricDetails->name ?? '0' }}</td> --}}
                                        <td class="text-right">{{ number_format($detail->rate, 2) ?? '0' }}
                                        </td>
                                        <td class="text-right">
                                            {{ number_format($detail->price, 2) ?? 'N/A' }}
                                        </td>
                                    </tr>
                                @endforeach

                                <tr>
                                    <th colspan="1" style="text-align: center">SUBTOTAl</th>
                                    <th class="text-right"></th>
                                    <th class="text-right">{{ number_format($totalQty, 2) }}</th>
                                    {{-- <th class="text-right"></th> --}}
                                    <th class="text-right">{{ number_format($totalUp, 2) }}</th>
                                    <th class="text-right">{{ $subtotal }}</th>
                                </tr>
                            @endforeach
                        </tbody>
                        <tfoot>
                            <tr>
                                <th colspan="1" style="text-align: center">TOTAl</th>
                                <th class="text-right"></th>
                                <th class="text-right">{{ number_format($gtotalQty, 2) }}</th>
                                {{-- <th class="text-right"></th> --}}
                                <th class="text-right">{{ number_format($gtotalUp, 2) }}</th>
                                <th class="text-right">{{ number_format($gtotalPrice, 2) }}</th>
                            </tr>
                            @if ($invoice->discount)
                                <tr>
                                    <td colspan="2"></td>
                                    <th class="text-right"></th>
                                    {{-- <th class="text-right"></th> --}}
                                    <th style="text-align:right;">Discount ( - ):</th>
                                    <td style="text-align:right">
                                        @php
                                            $discount = $invoice->discount;
                                        @endphp
                                        <b>
                                            {{ number_format($discount, 2) ?? 0 }}
                                        </b>
                                    </td>
                                </tr>
                            @endif
                            @if ($invoice->carrying_cost)
                                <tr>
                                    <td colspan="2"></td>
                                    <th class="text-right"></th>
                                    {{-- <th class="text-right"></th> --}}
                                    <th style="text-align:right;">Carrying Cost ( + ):</th>
                                    <td style="text-align:right">
                                        @php
                                            $carrying_cost = $invoice->carrying_cost;

                                        @endphp
                                        <b>
                                            {{ number_format($carrying_cost, 2) ?? 0 }}
                                        </b>
                                    </td>
                                </tr>
                            @endif

                            @if ($invoice->labor_bill)
                                <tr>
                                    <td colspan="2"></td>
                                    <th class="text-right"></th>
                                    {{-- <th class="text-right"></th> --}}
                                    <th class="text-right"></th>
                                    <th style="text-align:right;">Labor bill ( + ):</th>
                                    <td style="text-align:right">
                                        @php
                                            $labor_bill = $invoice->labor_bill;
                                        @endphp
                                        <b>
                                            {{ number_format($labor_bill, 2) ?? 0 }}
                                        </b>
                                    </td>
                                </tr>
                            @endif

                            @if ($invoice->previouse_due_amount)
                                <tr>
                                    <td colspan="3"></td>
                                    <th style="text-align:right;">Previous Due:</th>
                                    <td style="text-align:right">
                                        <b>
                                            {{ number_format($invoice->previouse_due_amount, 2) ?? 0 }}
                                        </b>
                                    </td>
                                </tr>
                            @endif

                            {{-- @if ($invoice->advance)
                                <tr>
                                    <td colspan="3"></td>
                                    <th style="text-align:right;">Advance
                                        ({{ $invoice->advance_account->account_name ?? '' }}):</th>
                                    <td style="text-align:right">
                                        <b>
                                            {{ number_format($invoice->advance, 2) ?? 0 }}

                                        </b>
                                    </td>
                                </tr>
                            @endif --}}
                            @php
    $advances = is_string($invoice->advance) ? json_decode($invoice->advance, true) : ($invoice->advance ?? []);
    $advanceLedgers = is_string($invoice->advance_ledger_id) ? json_decode($invoice->advance_ledger_id, true) : ($invoice->advance_ledger_id ?? []);
    $advanceAccounts = \App\Models\ChartOfAccount::whereIn('id', $advanceLedgers)->get()->keyBy('id');
@endphp

@if (!empty($advances))
    @foreach ($advances as $index => $amount)
        @php
            $ledgerId = $advanceLedgers[$index] ?? null;
            $account = $advanceAccounts[$ledgerId] ?? null;
        @endphp

        @if ($account && $amount)
            <tr>
                <th style="text-align:right;" colspan="4">
                    Advance ({{ $account->account_name }}):
                </th>
                <td style="text-align:right">
                    <b>{{ number_format($amount, 2) }}</b>
                </td>
            </tr>
        @endif
    @endforeach

    <tr>
        <th style="text-align:right;" colspan="4">Total Advance:</th>
        <td style="text-align:right">
            <b>{{ number_format(array_sum($advances), 2) }}</b>
        </td>
    </tr>
@endif
                            <tr>
                                <td colspan="3"></td>
                                <th style="text-align:right;">Total Payable Amount({{ isset($invoice->account) ? $invoice->account->account_name : '' }}):</th>
                                <td style="text-align:right">
                                    <b>
                                        {{ number_format($invoice->net_total - $invoice->totalAdvance + $invoice->previouse_due_amount, 2) ?? 0 }}
                                    </b>
                                </td>
                            </tr>

                            {{-- <tr>
                                <td colspan="2"></td>
                                <th class="text-right"></th>
                                <th class="text-right"></th>
                                <th class="text-right"></th>
                                <th style="text-align:right;">Paid(-):</th>
                                <td style="text-align:right">
                                    <b>
                                        {{ number_format($invoice->partialPayment, 2) }}
                                    </b>
                                </td>
                            </tr> --}}
                            {{-- <tr>
                                <td colspan="2"></td>
                                <th class="text-right"></th>
                                <th class="text-right"></th>
                                <th class="text-right"></th>
                                <th style="text-align:right;">Due:</th>
                                <td style="text-align:right">
                                    @php

                                        $due = $invoice->net_total - $invoice->partialPayment;

                                    @endphp
                                    <b>
                                        {{ number_format($due, 2) }}
                                    </b>
                                </td>
                            </tr>
                            <tr>
                                <th colspan="6">In words : {{ ucfirst(Terbilang::make($due)) }}</th>
                            </tr> --}}
                            <tr>
                                <td colspan="6"><b>In Words :</b> {{ ucfirst(Terbilang::make($invoice->net_total)) }}
                                </td>
                            </tr>
                            <tr>
                                <td colspan="6"><b>Note :</b> {{ $invoice->narration ?? 'N/A' }}</td>
                            </tr>
                        </tfoot>
                    </table>
                </section>

                {{-- <section class="attachments px-3">
                    <h3>Attachments:</h3>
                    <ul>
                        <li>Purchase Order</li>
                        <li>Received Challan</li>
                        <li>Beneficiary,s Bank Information as Following: {{ $companyInfo->company_name }}</li>
                    </ul>
                </section> --}}

                <div class="row table-borderless prepared-received-section">
                    <div class="col-md-4 prepared-by text-left">
                        <h3>Prepared By:</h3>
                        <table class="custom-table" style="width: 100%; border-collapse: collapse; border: none;">
                            <tr>
                                <td><strong>Name</strong></td>
                                <td>:</td>
                                <td>{{ $invoice->user->name ?? 'N/A' }}</td>
                            </tr>
                            {{-- <tr>
                                <td><strong>Mobile</strong></td>
                                <td>:</td>
                                <td>{{ $invoice->user->phone ?? 'N/A' }}</td>
                            </tr>
                            <tr>
                                <td><strong>Mail</strong></td>
                                <td>:</td>
                                <td>{{ $invoice->user->email ?? 'N/A' }}</td>
                            </tr> --}}
                        </table>
                    </div>

                    <div class="col-md-4 received-by text-center">
                        <h3>Authorized Signature:</h3>
                        <p class="signature-line"></p>
                    </div>
                    <div class="col-md-4 received-by text-right">
                        <h3>Received By:</h3>
                        <p class="signature-line"></p>
                    </div>
                </div>
                {{--
                <footer class="custom-footer">
                    <div class="footer-container">
                        <div class="footer-section">
                            <h5>Operational Headquarter:</h5>
                            <p>{{ $companyInfo->address }}</p>
                            <p>Cell: {{ $companyInfo->phone }}</p>
                        </div>

                        <div class="footer-section">
                            <h5>Factory Address:</h5>
                            <p>{{ $companyInfo->address ?? '' }}</p>
                            <h5>Warehouse Address:</h5>
                            <p>{{ $invoice->branch->address ?? '' }}</p>
                        </div>

                        <div class="footer-section">
                            <p>Website: <br>
                                <a href="{{ $companyInfo->website }}">{{ $companyInfo->website }}</a>
                            </p>
                            <p>Email: {{ $companyInfo->email }}</p>
                        </div>
                    </div>
                </footer> --}}

            </div>
            <!-- /.col-->
        </div>
    </div>
@endsection
@section('scripts')
    <script>
        function printInvoice() {
            var printContents = document.querySelector('.invoice-container').innerHTML;
            var originalContents = document.body.innerHTML;

            document.body.innerHTML = printContents;
            window.print();
            document.body.innerHTML = originalContents;

            location.reload(); // Reload to restore the original page
        }
    </script>
@endsection
