<?php
include 'db.php';
session_start();

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

/* ============================================================
   HELPER
============================================================ */
function e($value)
{
    return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
}

/* ============================================================
   SAVE BILL
============================================================ */
if (isset($_POST['save_bill'])) {

    $invoice_no   = trim($_POST['invoice_no'] ?? '');
    $invoice_date = $_POST['invoice_date'] ?? date('Y-m-d');

    $client_name  = trim($_POST['client_name'] ?? '');
    $company_name = trim($_POST['company_name'] ?? '');
    $address      = trim($_POST['address'] ?? '');
    $gst_number   = trim($_POST['gst_number'] ?? '');
    $pan_number   = trim($_POST['pan_number'] ?? '');
    $email        = trim($_POST['email'] ?? '');
    $mobile       = trim($_POST['mobile'] ?? '');
    $domain_name  = trim($_POST['domain_name'] ?? '');

    $domain_amount      = (float)($_POST['domain_amount'] ?? 0);
    $email_amount       = (float)($_POST['email_amount'] ?? 0);
    $social_amount      = (float)($_POST['social_amount'] ?? 0);
    $maintenance_amount = (float)($_POST['maintenance_amount'] ?? 0);

    /* CALCULATION */

    $taxable_amount =
        $domain_amount +
        $email_amount +
        $social_amount +
        $maintenance_amount;

    $cgst = ($taxable_amount * 9) / 100;
    $sgst = ($taxable_amount * 9) / 100;

    $grand_total =
        $taxable_amount +
        $cgst +
        $sgst;

    /* PREPARED INSERT */

    $stmt = mysqli_prepare(
        $conn,
        "INSERT INTO invoices
        (
            invoice_no,
            invoice_date,
            client_name,
            company_name,
            address,
            gst_number,
            pan_number,
            email,
            mobile,
            domain_name,
            domain_amount,
            email_amount,
            social_amount,
            maintenance_amount,
            subtotal,
            cgst,
            sgst,
            grand_total
        )
        VALUES
        (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
    );

    if ($stmt) {

        mysqli_stmt_bind_param(
            $stmt,
            "ssssssssssdddddddd",
            $invoice_no,
            $invoice_date,
            $client_name,
            $company_name,
            $address,
            $gst_number,
            $pan_number,
            $email,
            $mobile,
            $domain_name,
            $domain_amount,
            $email_amount,
            $social_amount,
            $maintenance_amount,
            $taxable_amount,
            $cgst,
            $sgst,
            $grand_total
        );

        mysqli_stmt_execute($stmt);
        mysqli_stmt_close($stmt);
    }

    header("Location: bill.php?saved=1");
    exit;
}

/* ============================================================
   LAST INVOICE
============================================================ */
$bill = mysqli_fetch_assoc(
    mysqli_query(
        $conn,
        "SELECT * FROM invoices ORDER BY id DESC LIMIT 1"
    )
);

/* ============================================================
   AMOUNTS
============================================================ */

$domain_amount =
    (float)($bill['domain_amount'] ?? 0);

$email_amount =
    (float)($bill['email_amount'] ?? 0);

$social_amount =
    (float)($bill['social_amount'] ?? 0);

$maintenance_amount =
    (float)($bill['maintenance_amount'] ?? 0);

$taxable_amount =
    (float)($bill['subtotal'] ?? 0);

$cgst =
    (float)($bill['cgst'] ?? 0);

$sgst =
    (float)($bill['sgst'] ?? 0);

$total_gst =
    $cgst + $sgst;

$grand_total =
    (float)($bill['grand_total'] ?? 0);
?>

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport"
      content="width=device-width, initial-scale=1.0">

<title>Generate Invoice | CAPAPP</title>

<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet">

<link
href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css"
rel="stylesheet">

<style>

/* ============================================================
   GLOBAL
============================================================ */

:root{
    --primary:#2563eb;
    --primary-dark:#1d4ed8;
    --navy:#0f172a;
    --navy2:#1e293b;
    --text:#172033;
    --muted:#64748b;
    --bg:#f1f5f9;
    --white:#fff;
    --border:#e2e8f0;
    --green:#16a34a;
}

*{
    box-sizing:border-box;
}

html{
    scroll-behavior:smooth;
}

body{
    margin:0;

    background:
        radial-gradient(
            circle at top right,
            rgba(37,99,235,.08),
            transparent 30%
        ),
        #f1f5f9;

    font-family:
        "Segoe UI",
        Arial,
        Helvetica,
        sans-serif;

    color:var(--text);
}

/* ============================================================
   SIDEBAR
============================================================ */

.sidebar{
    position:fixed;

    left:0;
    top:0;

    width:260px;
    height:100vh;

    padding:20px 15px;

    background:
        linear-gradient(
            180deg,
            #0f172a,
            #172554
        );

    z-index:2000;

    overflow-y:auto;

    box-shadow:
        10px 0 35px rgba(15,23,42,.15);

    transition:.3s;
}

/* LOGO */

.logo-box{
    text-align:center;

    padding:8px 10px 25px;
}

.logo-icon{
    width:58px;
    height:58px;

    margin:0 auto 12px;

    border-radius:17px;

    display:flex;
    align-items:center;
    justify-content:center;

    background:
        linear-gradient(
            135deg,
            #2563eb,
            #7c3aed
        );

    color:#fff;

    font-size:27px;

    box-shadow:
        0 10px 25px rgba(37,99,235,.35);
}

.logo-box h2{
    color:#fff;

    font-size:25px;

    font-weight:800;

    margin:0;
}

.logo-box p{
    color:#94a3b8;

    font-size:12px;

    margin:5px 0 0;
}

/* MENU */

.menu-title{
    color:#64748b;

    font-size:10px;

    text-transform:uppercase;

    letter-spacing:1.5px;

    padding:8px 14px;

    margin-top:5px;
}

.menu a{
    display:flex;

    align-items:center;

    gap:13px;

    padding:13px 14px;

    margin-bottom:5px;

    border-radius:12px;

    color:#cbd5e1;

    text-decoration:none;

    font-size:14px;

    font-weight:600;

    transition:.25s;
}

.menu a i{
    width:23px;

    text-align:center;

    font-size:18px;
}

.menu a:hover{
    color:#fff;

    background:
        rgba(37,99,235,.25);

    transform:translateX(3px);
}

.menu a.active{
    color:#fff;

    background:
        linear-gradient(
            135deg,
            #2563eb,
            #1d4ed8
        );

    box-shadow:
        0 8px 20px rgba(37,99,235,.25);
}

/* ============================================================
   MAIN
============================================================ */

.main{
    margin-left:260px;

    min-height:100vh;

    padding:25px;
}

/* ============================================================
   TOPBAR
============================================================ */

.topbar{
    display:flex;

    align-items:center;

    justify-content:space-between;

    padding:16px 20px;

    margin-bottom:22px;

    border-radius:18px;

    background:rgba(255,255,255,.94);

    border:1px solid var(--border);

    box-shadow:
        0 8px 30px rgba(15,23,42,.06);
}

.top-left{
    display:flex;

    align-items:center;

    gap:13px;
}

.page-icon{
    width:45px;
    height:45px;

    border-radius:13px;

    display:flex;
    align-items:center;
    justify-content:center;

    background:#eff6ff;

    color:var(--primary);

    font-size:21px;
}

.topbar h4{
    margin:0;

    font-size:20px;

    font-weight:800;
}

.topbar small{
    color:var(--muted);

    font-size:12px;
}

.admin-box{
    display:flex;

    align-items:center;

    gap:9px;

    padding:5px 12px 5px 6px;

    border:1px solid var(--border);

    border-radius:50px;

    background:#fff;
}

.admin-icon{
    width:37px;
    height:37px;

    border-radius:50%;

    display:flex;
    align-items:center;
    justify-content:center;

    background:
        linear-gradient(
            135deg,
            #2563eb,
            #7c3aed
        );

    color:#fff;
}

.mobile-toggle{
    display:none;

    border:0;

    width:42px;
    height:42px;

    border-radius:12px;

    background:#eff6ff;

    color:var(--primary);

    font-size:22px;
}

/* ============================================================
   WELCOME / INFO
============================================================ */

.hero-card{
    position:relative;

    overflow:hidden;

    padding:24px;

    border-radius:20px;

    color:#fff;

    margin-bottom:22px;

    background:
        linear-gradient(
            135deg,
            #1d4ed8,
            #2563eb 60%,
            #4f46e5
        );

    box-shadow:
        0 14px 35px rgba(37,99,235,.18);
}

.hero-card::after{
    content:"";

    position:absolute;

    width:180px;
    height:180px;

    right:-55px;
    top:-70px;

    border-radius:50%;

    background:rgba(255,255,255,.10);
}

.hero-card h3{
    position:relative;

    z-index:2;

    margin:0 0 6px;

    font-weight:800;

    font-size:23px;
}

.hero-card p{
    position:relative;

    z-index:2;

    margin:0;

    opacity:.88;

    font-size:13px;
}

/* ============================================================
   FORM CARD
============================================================ */

.card-box{
    background:#fff;

    border:1px solid var(--border);

    border-radius:20px;

    padding:22px;

    margin-bottom:22px;

    box-shadow:
        0 8px 30px rgba(15,23,42,.05);
}

.card-header-custom{
    display:flex;

    align-items:center;

    justify-content:space-between;

    margin-bottom:20px;

    gap:15px;
}

.card-title{
    display:flex;

    align-items:center;

    gap:11px;
}

.card-title-icon{
    width:42px;
    height:42px;

    border-radius:13px;

    display:flex;
    align-items:center;
    justify-content:center;

    background:#eff6ff;

    color:var(--primary);

    font-size:20px;
}

.card-title h5{
    margin:0;

    font-size:17px;

    font-weight:800;
}

.card-title p{
    margin:2px 0 0;

    color:var(--muted);

    font-size:12px;
}

/* FORM */

.form-label{
    color:#475569;

    font-size:12px;

    font-weight:700;

    margin-bottom:7px;
}

.form-control{
    min-height:46px;

    border-radius:11px;

    border:1px solid #dbe3ef;

    font-size:14px;

    box-shadow:none !important;

    transition:.2s;
}

.form-control:focus{
    border-color:#60a5fa;

    box-shadow:
        0 0 0 4px rgba(37,99,235,.08) !important;
}

.amount-input{
    position:relative;
}

.amount-input .currency{
    position:absolute;

    left:13px;
    top:50%;

    transform:translateY(-50%);

    font-weight:700;

    color:#64748b;

    z-index:5;
}

.amount-input input{
    padding-left:32px;
}

.btn-generate{
    min-height:48px;

    border:0;

    border-radius:12px;

    padding:0 25px;

    background:
        linear-gradient(
            135deg,
            #2563eb,
            #1d4ed8
        );

    color:#fff;

    font-weight:700;

    box-shadow:
        0 9px 22px rgba(37,99,235,.20);

    transition:.25s;
}

.btn-generate:hover{
    color:#fff;

    transform:translateY(-2px);

    box-shadow:
        0 13px 28px rgba(37,99,235,.30);
}

/* ============================================================
   SAVED ALERT
============================================================ */

.success-alert{
    border:0;

    border-radius:14px;

    background:#ecfdf5;

    color:#166534;

    padding:13px 16px;

    margin-bottom:20px;

    box-shadow:
        0 6px 20px rgba(22,163,74,.08);
}

/* ============================================================
   INVOICE PREVIEW WRAPPER
============================================================ */

.invoice-section{
    margin-top:10px;
}

.invoice-section-head{
    display:flex;

    align-items:center;

    justify-content:space-between;

    margin-bottom:15px;
}

.invoice-section-head h5{
    margin:0;

    font-size:18px;

    font-weight:800;
}

.invoice-section-head p{
    margin:3px 0 0;

    color:var(--muted);

    font-size:12px;
}

.preview-badge{
    background:#ecfdf5;

    color:#15803d;

    border-radius:50px;

    padding:7px 12px;

    font-size:11px;

    font-weight:700;
}

/* ============================================================
   A4 INVOICE
============================================================ */

.invoice-box{
    width:210mm;

    min-height:297mm;

    margin:0 auto;

    background:#fff;

    border:1px solid #cbd5e1;

    box-shadow:
        0 15px 50px rgba(15,23,42,.12);

    overflow:hidden;

    color:#111827;
}

/* TOP STRIP */

.top-strip{
    display:flex;

    align-items:center;

    justify-content:space-between;

    padding:8px 15px;

    background:#f8fafc;

    border-bottom:1px solid #cbd5e1;

    font-size:11px;

    color:#475569;
}

.top-strip strong{
    color:#1d4ed8;

    letter-spacing:.6px;
}

/* COMPANY */

.company-area{
    display:flex;

    align-items:flex-start;

    gap:15px;

    padding:17px;

    border-bottom:1px solid #cbd5e1;

    background:
        linear-gradient(
            180deg,
            #fff,
            #f8fbff
        );
}

.logo{
    width:85px;
    height:85px;

    object-fit:contain;

    flex-shrink:0;

    border-radius:8px;
}

.company-details{
    font-size:11px;

    line-height:19px;

    color:#334155;
}

.company-details h2{
    margin:0 0 3px;

    color:#0f172a;

    font-size:27px;

    font-weight:900;

    letter-spacing:.4px;
}

.company-details .company-name{
    font-weight:800;

    color:#1e3a5f;

    font-size:12px;
}

/* BILL TO */

.bill-section{
    display:flex;

    border-bottom:1px solid #cbd5e1;
}

.left-box{
    width:60%;

    border-right:1px solid #cbd5e1;
}

.right-box{
    width:40%;
}

.box-title{
    padding:8px 11px;

    background:
        linear-gradient(
            135deg,
            #dbeafe,
            #eff6ff
        );

    border-bottom:1px solid #cbd5e1;

    color:#1e3a5f;

    font-size:10px;

    font-weight:800;

    text-transform:uppercase;

    letter-spacing:.7px;
}

.box-content{
    min-height:150px;

    padding:11px;

    font-size:10.5px;

    line-height:20px;

    color:#334155;
}

.box-content b{
    color:#0f172a;
}

/* TABLE */

.invoice-table{
    width:100%;

    border-collapse:collapse;

    table-layout:fixed;
}

.invoice-table th{
    padding:8px 5px;

    background:
        linear-gradient(
            135deg,
            #1d4ed8,
            #2563eb
        );

    color:#fff;

    border:1px solid #93a4ba;

    font-size:10px;

    font-weight:800;

    text-align:center;

    vertical-align:middle;
}

.invoice-table td{
    padding:8px 5px;

    border:1px solid #cbd5e1;

    font-size:10px;

    vertical-align:top;

    background:#fff;
}

.invoice-table tbody tr:nth-child(even) td{
    background:#f8fbff;
}

.invoice-table .subtotal-row td{
    background:#eff6ff !important;

    font-weight:800;
}

.text-right{
    text-align:right;
}

/* SUMMARY */

.summary-area{
    display:flex;

    border-left:1px solid #cbd5e1;

    border-right:1px solid #cbd5e1;

    border-bottom:1px solid #cbd5e1;
}

.bank-box{
    width:58%;

    padding:12px;

    border-right:1px solid #cbd5e1;

    background:#f8fbff;

    font-size:10px;

    line-height:20px;
}

.bank-title{
    font-size:11px;

    font-weight:800;

    color:#1e3a5f;

    margin-bottom:4px;
}

.total-box{
    width:42%;

    background:#fff;
}

.total-table{
    width:100%;

    border-collapse:collapse;
}

.total-table td{
    padding:7px 9px;

    border-bottom:1px solid #dbe3ef;

    font-size:10px;
}

.total-table tr:nth-child(odd) td{
    background:#f8fbff;
}

.total-table tr.total-gst td{
    background:#eff6ff;

    font-weight:800;
}

.total-table tr.grand-total td{
    background:#dbeafe;

    color:#0f172a;

    font-size:11px;

    font-weight:900;

    border-bottom:0;
}

/* FOOTER */

.footer-sign{
    display:flex;

    justify-content:space-between;

    gap:20px;

    padding:14px;

    min-height:115px;

    align-items:flex-end;

    border-left:1px solid #cbd5e1;

    border-right:1px solid #cbd5e1;

    border-bottom:1px solid #cbd5e1;

    background:#fff;

    font-size:10px;

    line-height:18px;
}

.declaration-title{
    font-weight:800;

    color:#1e3a5f;
}

.signature{
    text-align:center;

    min-width:220px;
}

.signature-line{
    border-top:1px solid #334155;

    padding-top:5px;

    margin-top:30px;

    font-weight:800;
}

.footer-text{
    text-align:center;

    padding:9px;

    border-left:1px solid #cbd5e1;

    border-right:1px solid #cbd5e1;

    border-bottom:1px solid #cbd5e1;

    background:
        linear-gradient(
            135deg,
            #eff6ff,
            #f8fafc
        );

    color:#1e3a5f;

    font-size:11px;

    font-weight:800;

    letter-spacing:.2px;
}

/* ============================================================
   PRINT BUTTON
============================================================ */

.print-area{
    display:flex;

    justify-content:center;

    gap:10px;

    margin:20px 0 30px;
}

.print-btn{
    border:0;

    border-radius:11px;

    padding:11px 25px;

    background:
        linear-gradient(
            135deg,
            #2563eb,
            #1d4ed8
        );

    color:#fff;

    font-weight:700;

    box-shadow:
        0 8px 20px rgba(37,99,235,.20);
}

.print-btn:hover{
    color:#fff;

    transform:translateY(-2px);
}

.top-btn{
    border:1px solid #dbe3ef;

    border-radius:11px;

    padding:11px 18px;

    background:#fff;

    color:#475569;

    font-weight:700;

    text-decoration:none;
}

.top-btn:hover{
    color:#2563eb;

    border-color:#93c5fd;
}

/* ============================================================
   SIDEBAR OVERLAY
============================================================ */

.sidebar-overlay{
    display:none;

    position:fixed;

    inset:0;

    background:rgba(15,23,42,.45);

    z-index:1900;
}

/* ============================================================
   RESPONSIVE
============================================================ */

@media(max-width:1100px){

    .sidebar{
        width:240px;
    }

    .main{
        margin-left:240px;
    }

    .invoice-box{
        transform-origin:top center;
    }

}

@media(max-width:991px){

    .sidebar{
        left:-270px;

        width:260px;
    }

    .sidebar.active{
        left:0;
    }

    .sidebar.active + .sidebar-overlay{
        display:block;
    }

    .main{
        margin-left:0;

        padding:15px;
    }

    .mobile-toggle{
        display:flex;

        align-items:center;
        justify-content:center;
    }

    .admin-box span{
        display:none;
    }

    .bill-section,
    .summary-area,
    .footer-sign{
        flex-direction:column;
    }

    .left-box,
    .right-box,
    .bank-box,
    .total-box{
        width:100%;
    }

    .left-box,
    .bank-box{
        border-right:0;

        border-bottom:1px solid #cbd5e1;
    }

    .invoice-box{
        width:100%;

        min-height:auto;

        overflow-x:auto;
    }

    .invoice-table{
        min-width:900px;
    }

}

@media(max-width:575px){

    .topbar{
        padding:13px 15px;
    }

    .topbar h4{
        font-size:16px;
    }

    .topbar small{
        display:none;
    }

    .page-icon{
        width:38px;
        height:38px;
    }

    .card-box{
        padding:16px;
    }

    .hero-card{
        padding:19px;
    }

    .hero-card h3{
        font-size:20px;
    }

    .invoice-section-head{
        align-items:flex-start;
    }

    .invoice-section-head h5{
        font-size:16px;
    }

    .company-area{
        padding:12px;
    }

    .company-details h2{
        font-size:23px;
    }

    .print-area{
        flex-direction:column;
    }

    .print-btn,
    .top-btn{
        width:100%;

        text-align:center;
    }

}

/* ============================================================
   PRINT
============================================================ */

@media print{

    @page{
        size:A4 portrait;

        margin:0;
    }

    html,
    body{
        width:210mm;

        height:297mm;

        margin:0 !important;

        padding:0 !important;

        background:#fff !important;

        -webkit-print-color-adjust:exact !important;

        print-color-adjust:exact !important;
    }

    .sidebar,
    .sidebar-overlay,
    .topbar,
    .hero-card,
    .card-box,
    .invoice-section-head,
    .print-area{
        display:none !important;
    }

    .main{
        margin:0 !important;

        padding:0 !important;

        width:210mm !important;
    }

    .invoice-box{
        display:block !important;

        width:210mm !important;

        min-height:297mm !important;

        margin:0 !important;

        border:1px solid #cbd5e1 !important;

        box-shadow:none !important;

        overflow:hidden !important;
    }

    .invoice-table th,
    .invoice-table td,
    .box-title,
    .top-strip,
    .footer-text,
    .total-table td{
        -webkit-print-color-adjust:exact !important;

        print-color-adjust:exact !important;
    }

}

</style>

</head>

<body>

<!-- ============================================================
     SIDEBAR
============================================================ -->

<aside class="sidebar" id="sidebar">

    <div class="logo-box">

        <div class="logo-icon">
            <i class="bi bi-globe2"></i>
        </div>

        <h2>CAPAPP</h2>

        <p>Domain Management Panel</p>

    </div>

    <div class="menu">

        <div class="menu-title">
            Main Menu
        </div>

        <a href="dashboard.php">
            <i class="bi bi-speedometer2"></i>
            <span>Dashboard</span>
        </a>

        <a href="index.php">
            <i class="bi bi-globe2"></i>
            <span>All Domains</span>
        </a>

        <a href="add-domain.php">
            <i class="bi bi-plus-circle"></i>
            <span>Add Domain</span>
        </a>

        <div class="menu-title">
            Finance
        </div>

        <a href="domain-charges.php">
            <i class="bi bi-cash-stack"></i>
            <span>Domain Charges</span>
        </a>

        <a href="add-payment.php">
            <i class="bi bi-wallet2"></i>
            <span>Receive Payment</span>
        </a>

        <a href="domain-ledger.php">
            <i class="bi bi-journal-text"></i>
            <span>Balance Sheet</span>
        </a>

        <div class="menu-title">
            Documents
        </div>

        <a href="bill.php" class="active">
            <i class="bi bi-receipt"></i>
            <span>Generate Invoice</span>
        </a>

        <a href="expired-domains.php">
            <i class="bi bi-exclamation-circle"></i>
            <span>Expired Domains</span>
        </a>

        <div class="menu-title">
            Account
        </div>

        <a href="logout.php">
            <i class="bi bi-box-arrow-right"></i>
            <span>Logout</span>
        </a>

    </div>

</aside>

<div
    class="sidebar-overlay"
    id="sidebarOverlay"
    onclick="toggleMenu()">
</div>


<!-- ============================================================
     MAIN
============================================================ -->

<main class="main">

    <!-- TOPBAR -->

    <div class="topbar">

        <div class="top-left">

            <button
                type="button"
                class="mobile-toggle"
                onclick="toggleMenu()">

                <i class="bi bi-list"></i>

            </button>

            <div class="page-icon">

                <i class="bi bi-receipt-cutoff"></i>

            </div>

            <div>

                <h4>Generate Invoice</h4>

                <small>
                    Create professional CAPAPP tax invoices
                </small>

            </div>

        </div>

        <div class="admin-box">

            <div class="admin-icon">
                <i class="bi bi-person"></i>
            </div>

            <span class="fw-semibold">
                Admin
            </span>

        </div>

    </div>


    <!-- SUCCESS -->

    <?php if(isset($_GET['saved'])): ?>

        <div class="success-alert">

            <i class="bi bi-check-circle-fill me-2"></i>

            Invoice successfully generated and saved.

        </div>

    <?php endif; ?>


    <!-- HERO -->

    <div class="hero-card">

        <h3>
            <i class="bi bi-file-earmark-text me-2"></i>
            CAPAPP Invoice Generator
        </h3>

        <p>
            Enter customer and service details to generate
            a professional GST invoice.
        </p>

    </div>


    <!-- ========================================================
         FORM
    ========================================================= -->

    <div class="card-box">

        <div class="card-header-custom">

            <div class="card-title">

                <div class="card-title-icon">
                    <i class="bi bi-pencil-square"></i>
                </div>

                <div>

                    <h5>Invoice Details</h5>

                    <p>
                        Enter billing and service information
                    </p>

                </div>

            </div>

        </div>


        <form method="POST">

            <div class="row g-3">

                <!-- INVOICE NO -->

                <div class="col-12 col-md-4">

                    <label class="form-label">
                        Invoice Number
                    </label>

                    <input
                        type="text"
                        name="invoice_no"
                        class="form-control"
                        placeholder="e.g. CAP/2026/001"
                        required>

                </div>


                <!-- DATE -->

                <div class="col-12 col-md-4">

                    <label class="form-label">
                        Invoice Date
                    </label>

                    <input
                        type="date"
                        name="invoice_date"
                        class="form-control"
                        value="<?= date('Y-m-d') ?>"
                        required>

                </div>


                <!-- CLIENT -->

                <div class="col-12 col-md-4">

                    <label class="form-label">
                        Client Name
                    </label>

                    <input
                        type="text"
                        name="client_name"
                        class="form-control"
                        placeholder="Client name">

                </div>


                <!-- COMPANY -->

                <div class="col-12 col-md-6">

                    <label class="form-label">
                        Company Name
                    </label>

                    <input
                        type="text"
                        name="company_name"
                        class="form-control"
                        placeholder="Company name">

                </div>


                <!-- ADDRESS -->

                <div class="col-12 col-md-6">

                    <label class="form-label">
                        Billing Address
                    </label>

                    <input
                        type="text"
                        name="address"
                        class="form-control"
                        placeholder="Complete address">

                </div>


                <!-- GST -->

                <div class="col-12 col-md-6">

                    <label class="form-label">
                        GST Number
                    </label>

                    <input
                        type="text"
                        name="gst_number"
                        class="form-control"
                        placeholder="GSTIN">

                </div>


                <!-- PAN -->

                <div class="col-12 col-md-6">

                    <label class="form-label">
                        PAN Number
                    </label>

                    <input
                        type="text"
                        name="pan_number"
                        class="form-control"
                        placeholder="PAN">

                </div>


                <!-- EMAIL -->

                <div class="col-12 col-md-6">

                    <label class="form-label">
                        Email Address
                    </label>

                    <input
                        type="email"
                        name="email"
                        class="form-control"
                        placeholder="client@example.com">

                </div>


                <!-- MOBILE -->

                <div class="col-12 col-md-6">

                    <label class="form-label">
                        Mobile Number
                    </label>

                    <input
                        type="text"
                        name="mobile"
                        class="form-control"
                        placeholder="Mobile number">

                </div>


                <!-- DOMAIN -->

                <div class="col-12">

                    <label class="form-label">
                        Domain Name
                    </label>

                    <input
                        type="text"
                        name="domain_name"
                        class="form-control"
                        placeholder="example.com">

                </div>


                <!-- SERVICES -->

                <div class="col-12">

                    <div class="mt-2 mb-1">

                        <strong style="font-size:14px;">
                            <i class="bi bi-cart-check me-1 text-primary"></i>
                            Service Charges
                        </strong>

                    </div>

                </div>


                <!-- DOMAIN AMOUNT -->

                <div class="col-12 col-sm-6 col-xl-3">

                    <label class="form-label">
                        Domain & Hosting
                    </label>

                    <div class="amount-input">

                        <span class="currency">₹</span>

                        <input
                            type="number"
                            step="0.01"
                            min="0"
                            name="domain_amount"
                            class="form-control"
                            placeholder="0.00"
                            value="0">

                    </div>

                </div>


                <!-- EMAIL -->

                <div class="col-12 col-sm-6 col-xl-3">

                    <label class="form-label">
                        Email Renewal
                    </label>

                    <div class="amount-input">

                        <span class="currency">₹</span>

                        <input
                            type="number"
                            step="0.01"
                            min="0"
                            name="email_amount"
                            class="form-control"
                            placeholder="0.00"
                            value="0">

                    </div>

                </div>


                <!-- SOCIAL -->

                <div class="col-12 col-sm-6 col-xl-3">

                    <label class="form-label">
                        Social Promotion
                    </label>

                    <div class="amount-input">

                        <span class="currency">₹</span>

                        <input
                            type="number"
                            step="0.01"
                            min="0"
                            name="social_amount"
                            class="form-control"
                            placeholder="0.00"
                            value="0">

                    </div>

                </div>


                <!-- MAINTENANCE -->

                <div class="col-12 col-sm-6 col-xl-3">

                    <label class="form-label">
                        Maintenance
                    </label>

                    <div class="amount-input">

                        <span class="currency">₹</span>

                        <input
                            type="number"
                            step="0.01"
                            min="0"
                            name="maintenance_amount"
                            class="form-control"
                            placeholder="0.00"
                            value="0">

                    </div>

                </div>


                <!-- BUTTON -->

                <div class="col-12 mt-2">

                    <button
                        type="submit"
                        name="save_bill"
                        class="btn-generate">

                        <i class="bi bi-file-earmark-check me-2"></i>

                        Generate Invoice

                    </button>

                </div>

            </div>

        </form>

    </div>


    <!-- ========================================================
         INVOICE PREVIEW
    ========================================================= -->

    <?php if($bill): ?>

    <div class="invoice-section">

        <div class="invoice-section-head">

            <div>

                <h5>
                    Invoice Preview
                </h5>

                <p>
                    Latest generated invoice
                </p>

            </div>

            <span class="preview-badge">

                <i class="bi bi-check-circle me-1"></i>

                Ready to Print

            </span>

        </div>


        <!-- ====================================================
             A4 INVOICE
        ===================================================== -->

        <div
            class="invoice-box"
            id="invoiceArea">


            <!-- TOP -->

            <div class="top-strip">

                <div>

                    <strong>
                        TAX INVOICE
                    </strong>

                </div>

                <div>
                    Original / Duplicate Bill
                </div>

            </div>


            <!-- COMPANY -->

            <div class="company-area">

                <img
                    src="logo.jpg"
                    class="logo"
                    alt="CAPAPP Logo">

                <div class="company-details">

                    <h2>CAPAPP</h2>

                    <div class="company-name">
                        CAPAPP IT SOLUTION PRIVATE LIMITED
                    </div>

                    Address :
                    KH NO-464 PROP.No -59,
                    VIJAY BLOCK,
                    LAXMI NAGAR,
                    EAST DELHI 110092
                    <br>

                    PAN No :
                    AAICC7009E

                    <br>

                    GSTIN :
                    07AAICC7009E1ZV

                    <br>

                    Mobile :
                    7428465992

                    <br>

                    Email :
                    hr@capapp.in

                    <br>

                    Website :
                    www.capapp.in

                </div>

            </div>


            <!-- BILL TO -->

            <div class="bill-section">

                <div class="left-box">

                    <div class="box-title">
                        Bill To
                    </div>

                    <div class="box-content">

                        <b>Name :</b>
                        <?= e($bill['client_name']) ?>

                        <br>

                        <b>Company :</b>
                        <?= e($bill['company_name']) ?>

                        <br>

                        <b>Address :</b>
                        <?= e($bill['address']) ?>

                        <br>

                        <b>GSTIN :</b>
                        <?= e($bill['gst_number']) ?>

                        <br>

                        <b>PAN :</b>
                        <?= e($bill['pan_number']) ?>

                        <br>

                        <b>Mobile :</b>
                        <?= e($bill['mobile']) ?>

                        <br>

                        <b>Email :</b>
                        <?= e($bill['email']) ?>

                    </div>

                </div>


                <div class="right-box">

                    <div class="box-title">
                        Invoice Details
                    </div>

                    <div class="box-content">

                        <b>Invoice No :</b>
                        <?= e($bill['invoice_no']) ?>

                        <br>

                        <b>Invoice Date :</b>

                        <?= date(
                            "d-m-Y",
                            strtotime($bill['invoice_date'])
                        ) ?>

                        <br>

                        <b>Domain :</b>
                        <?= e($bill['domain_name']) ?>

                        <br>

                        <b>Payment Mode :</b>
                        ONLINE

                        <br>

                        <b>Reverse Charge :</b>
                        NO

                    </div>

                </div>

            </div>


            <!-- =================================================
                 ITEMS
            ================================================== -->

            <table class="invoice-table">

                <thead>

                    <tr>

                        <th style="width:5%;">
                            Sr
                        </th>

                        <th style="width:28%;">
                            Description
                        </th>

                        <th style="width:10%;">
                            HSN
                        </th>

                        <th style="width:10%;">
                            Qty
                        </th>

                        <th style="width:12%;">
                            Rate
                        </th>

                        <th style="width:12%;">
                            Taxable
                        </th>

                        <th style="width:6%;">
                            GST %
                        </th>

                        <th style="width:8%;">
                            GST Amt.
                        </th>

                        <th style="width:9%;">
                            Total
                        </th>

                    </tr>

                </thead>


                <tbody>

                <?php

                $sr = 1;

                $gst_items_total = 0;

                ?>


                <!-- DOMAIN -->

                <?php if($domain_amount > 0):

                    $gst =
                        ($domain_amount * 18) / 100;

                    $total =
                        $domain_amount + $gst;

                    $gst_items_total += $gst;

                ?>

                <tr>

                    <td class="text-center">
                        <?= $sr++ ?>
                    </td>

                    <td>
                        Domain &amp; Hosting Renewal
                    </td>

                    <td class="text-center">
                        998314
                    </td>

                    <td class="text-center">
                        1 Nos
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($domain_amount,2) ?>
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($domain_amount,2) ?>
                    </td>

                    <td class="text-center">
                        18%
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($gst,2) ?>
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($total,2) ?>
                    </td>

                </tr>

                <?php endif; ?>


                <!-- EMAIL -->

                <?php if($email_amount > 0):

                    $gst =
                        ($email_amount * 18) / 100;

                    $total =
                        $email_amount + $gst;

                    $gst_items_total += $gst;

                ?>

                <tr>

                    <td class="text-center">
                        <?= $sr++ ?>
                    </td>

                    <td>
                        Email Renewal Service
                    </td>

                    <td class="text-center">
                        998314
                    </td>

                    <td class="text-center">
                        1 Nos
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($email_amount,2) ?>
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($email_amount,2) ?>
                    </td>

                    <td class="text-center">
                        18%
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($gst,2) ?>
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($total,2) ?>
                    </td>

                </tr>

                <?php endif; ?>


                <!-- SOCIAL -->

                <?php if($social_amount > 0):

                    $gst =
                        ($social_amount * 18) / 100;

                    $total =
                        $social_amount + $gst;

                    $gst_items_total += $gst;

                ?>

                <tr>

                    <td class="text-center">
                        <?= $sr++ ?>
                    </td>

                    <td>
                        Social Media Promotion
                    </td>

                    <td class="text-center">
                        998314
                    </td>

                    <td class="text-center">
                        1 Nos
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($social_amount,2) ?>
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($social_amount,2) ?>
                    </td>

                    <td class="text-center">
                        18%
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($gst,2) ?>
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($total,2) ?>
                    </td>

                </tr>

                <?php endif; ?>


                <!-- MAINTENANCE -->

                <?php if($maintenance_amount > 0):

                    $gst =
                        ($maintenance_amount * 18) / 100;

                    $total =
                        $maintenance_amount + $gst;

                    $gst_items_total += $gst;

                ?>

                <tr>

                    <td class="text-center">
                        <?= $sr++ ?>
                    </td>

                    <td>
                        Website Maintenance
                    </td>

                    <td class="text-center">
                        998314
                    </td>

                    <td class="text-center">
                        1 Nos
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($maintenance_amount,2) ?>
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($maintenance_amount,2) ?>
                    </td>

                    <td class="text-center">
                        18%
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($gst,2) ?>
                    </td>

                    <td class="text-right">
                        ₹ <?= number_format($total,2) ?>
                    </td>

                </tr>

                <?php endif; ?>


                <!-- SUBTOTAL -->

                <tr class="subtotal-row">

                    <td colspan="5"
                        class="text-right">

                        Sub-Total

                    </td>

                    <td class="text-right">

                        ₹ <?= number_format(
                            $taxable_amount,
                            2
                        ) ?>

                    </td>

                    <td class="text-center">
                        18%
                    </td>

                    <td class="text-right">

                        ₹ <?= number_format(
                            $gst_items_total,
                            2
                        ) ?>

                    </td>

                    <td class="text-right">

                        ₹ <?= number_format(
                            $grand_total,
                            2
                        ) ?>

                    </td>

                </tr>

                </tbody>

            </table>


            <!-- =================================================
                 SUMMARY
            ================================================== -->

            <div class="summary-area">

                <div class="bank-box">

                    <div class="bank-title">
                        <i class="bi bi-bank me-1"></i>
                        Our Bank Details
                    </div>

                    Bank Name :
                    ICICI BANK

                    <br>

                    Account Name :
                    CAPAPP IT SOLUTION PRIVATE LIMITED

                    <br>

                    Account Number :
                    107005012337

                    <br>

                    IFSC Code :
                    ICIC0001070

                    <br>

                    Address :
                    B-11 &amp; 12,
                    SECTOR - NOIDA 201301

                </div>


                <div class="total-box">

                    <table class="total-table">

                        <tr>

                            <td width="60%">
                                Taxable Amount
                            </td>

                            <td class="text-right">

                                ₹ <?= number_format(
                                    $taxable_amount,
                                    2
                                ) ?>

                            </td>

                        </tr>


                        <tr>

                            <td>
                                CGST 9%
                            </td>

                            <td class="text-right">

                                ₹ <?= number_format(
                                    $cgst,
                                    2
                                ) ?>

                            </td>

                        </tr>


                        <tr>

                            <td>
                                SGST 9%
                            </td>

                            <td class="text-right">

                                ₹ <?= number_format(
                                    $sgst,
                                    2
                                ) ?>

                            </td>

                        </tr>


                        <tr class="total-gst">

                            <td>
                                Total GST 18%
                            </td>

                            <td class="text-right">

                                ₹ <?= number_format(
                                    $total_gst,
                                    2
                                ) ?>

                            </td>

                        </tr>


                        <tr class="grand-total">

                            <td>
                                Total Amount
                            </td>

                            <td class="text-right">

                                ₹ <?= number_format(
                                    $grand_total,
                                    2
                                ) ?>

                            </td>

                        </tr>

                    </table>

                </div>

            </div>


            <!-- =================================================
                 FOOTER
            ================================================== -->

            <div class="footer-sign">

                <div>

                    <div class="declaration-title">
                        Declaration
                    </div>

                    <br>

                    1. Subject to Delhi jurisdiction
                    <br>

                    2. Goods once sold not returnable
                    <br>

                    3. Computer generated invoice
                    <br><br>

                    E. &amp; O.E.

                </div>


                <div class="signature">

                    For,

                    <b>
                        CAPAPP IT SOLUTION PRIVATE LIMITED
                    </b>

                    <div class="signature-line">

                        Authorised Signatory

                    </div>

                </div>

            </div>


            <!-- FOOTER -->

            <div class="footer-text">

                Thank You For Business With Us!

            </div>

        </div>


        <!-- ====================================================
             ACTION BUTTONS
        ===================================================== -->

        <div class="print-area">

            <button
                type="button"
                onclick="window.print()"
                class="print-btn">

                <i class="bi bi-printer me-2"></i>

                Print Invoice

            </button>

            <a
                href="#invoiceArea"
                class="top-btn">

                <i class="bi bi-arrow-up me-1"></i>

                Invoice Preview

            </a>

        </div>

    </div>

    <?php endif; ?>

</main>


<script>

/* ============================================================
   MOBILE MENU
============================================================ */

function toggleMenu(){

    const sidebar =
        document.getElementById("sidebar");

    sidebar.classList.toggle("active");

}


/* CLOSE MOBILE MENU AFTER LINK CLICK */

document
    .querySelectorAll(".sidebar a")
    .forEach(function(link){

        link.addEventListener(
            "click",
            function(){

                if(window.innerWidth <= 991){

                    document
                        .getElementById("sidebar")
                        .classList
                        .remove("active");

                }

            }
        );

    });


/* AUTO CLOSE ON DESKTOP */

window.addEventListener(
    "resize",
    function(){

        if(window.innerWidth > 991){

            document
                .getElementById("sidebar")
                .classList
                .remove("active");

        }

    }
);

</script>

</body>

</html>