<?php
include('top.php');

if (!isset($_GET['bill_id'])) {
    header("Location: index.php");
    exit();
}

$bill_id = mysqli_real_escape_string($con, $_GET['bill_id']);
$lineman_id = getLinemanId();

// Get bill details - modified to match the cbill table structure
$sql = "SELECT b.*,c.id as customerserial, c.name as customer_name, c.phone as customer_mobile, c.address, 
               c.houseno, c.custid as username, c.packname as package_name,c.landmark,
               c.packpr as package_charge, c.packpr_id as package_id,c.vc as customervc,c.stb as customerstb,
               c.pexpiry as expiry_date, c.packpr as package_price
        FROM cbill b
        JOIN adduser c ON b.bl_cid = c.id
        WHERE b.blid = $bill_id";

if ($lineman_id) {
   // $sql .= " AND b.bl_line = $lineman_id";
}

$result = mysqli_query($con, $sql);
$bill = mysqli_fetch_assoc($result);

// if (!$bill) {
//     $_SESSION['error'] = "Bill not found or you don't have permission to view it";
//     header("Location: list_bills.php");
//     exit();
// }

// Calculate GST (assuming 18% total - 9% CGST and 9% SGST)
$gst_rate = 0.09; // 9%

// Extract base price from cbill.bill_amt (which includes 18% GST)
$bill_amt = (float)$bill['bl_amt'];
$package_charge = round($bill_amt / 1.18); // Package charge without GST

$cgst = $package_charge * $gst_rate;
$sgst = $package_charge * $gst_rate;
$total_amount = $package_charge + $cgst + $sgst + (float)$bill['bl_prevdue'];

// Get company details - you'll need to create a company table or adjust this

//$company=f(q("SELECT * FROM tbl_company WHERE c_id='53'"));
$company = $_SESSION['company'];
$showGst = (isset($company['isgst']) && $company['isgst'] == '1');

?>

<!DOCTYPE html>
<html>
<head>
    <title><?php echo $showGst ? 'Tax Invoice' : 'Receipt'; ?> - <?php echo $bill['bl_no']; ?></title>
    <style>
        @page {
            size: A5 portrait;
            margin: 5mm;
        }
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            font-size: 11px;
            background-color: #f8f9fa;
        }
        .container {
            width: 100%;
            max-width: 680px;
            margin: 20px auto;
            padding: 15px;
            border: 1px solid #ccc;
            border-radius: 10px;
            background: #fff;
            box-shadow: 0 4px 12px rgba(0,0,0,0.05);
            box-sizing: border-box;
        }
        .header {
            text-align: center;
            margin-bottom: 5px;
        }
        .header h1 {
            font-size: 16px;
            margin: 2px 0;
            text-transform: uppercase;
        }
        .header p {
            margin: 1px 0;
            font-size: 11px;
        }
        .company-info {
            text-align: left;
            margin-bottom: 2px;
            line-height: 1;
        }
        .customer-info {
            margin-bottom: 2px;
        }
        .customer-info strong {
            display: block;
            font-size: 13px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 7px;
        }
        table th, table td {
            border: 1px solid #000;
            padding: 4px 6px;
            text-align: left;
        }
        table th {
            background-color: #f0f0f0;
        }
        .text-right {
            text-align: right;
        }
        .total-row {
            font-weight: bold;
        }
        .payment-info {
            margin-top: 1px;
        }
        .payment-info table {
            margin-top: 2px;
        }
        .footer {
            margin-top: 15px;
            font-size: 11px;
            text-align: center;
        }
        .signature {
            margin-top: 20px;
            text-align: right;
            padding-right: 50px;
        }
        @media print {
            @page {
                size: A5 portrait;
                margin: 5mm;
            }
            .no-print {
                display: none;
            }
            body {
                padding: 0;
                background-color: #fff;
                font-size: 9px !important;
            }
            .container {
                margin: 0;
                padding: 8px;
                max-width: 100%;
                border: 1px solid #000 !important;
                border-radius: 8px !important;
                box-shadow: none;
            }
            td, th, p, div, span, strong {
                font-size: 9px !important;
            }
            .company-name {
                font-size: 18px !important;
            }
            .counter-slip h2 {
                font-size: 12px !important;
            }
            table th, table td {
                padding: 3px 4px !important;
            }
            .signature {
                margin-top: 15px;
            }
            hr {
                margin: 8px 0 !important;
            }
            .counter-slip {
                margin-top: 8px !important;
                padding: 6px !important;
                border-radius: 6px !important;
            }
        }
       
    </style>
    <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;800&display=swap" rel="stylesheet">
<style>

.company-name {
  font-family: 'Inter', Arial, sans-serif;
  font-weight: 800;
  letter-spacing: -0.5px;
}

.counter-slip {
    margin-top: 20px;
    border: 1px solid #000;
    padding: 10px;
    font-size: 11px;
}
.counter-slip h2 {
    font-size: 14px;
    text-align: center;
    margin-bottom: 10px;
}
.counter-slip table {
    width: 100%;
    border-collapse: collapse;
}
.counter-slip table td {
    padding: 3px;
    border: none;
}

</style>
</head>
<body>
    <div class="container">
        <div class="no-print" style="text-align: right; margin-bottom: 10px;">
            <button onclick="window.print()" style="padding: 5px 10px; cursor: pointer;">Print Bill</button>
        </div>

        <div style="width: 100%; overflow: hidden; border-bottom: 2px solid #000; padding-bottom: 12px; margin-bottom: 15px;">
            <div style="width: 65%; float: left;">
                <h1 class="company-name" style="font-size: 24px; color: #111; margin: 0 0 5px 0; text-transform: uppercase;"><?php echo $company['c_name']; ?></h1>
                <p style="margin: 2px 0; font-size: 11px; color: #555; text-transform: uppercase; letter-spacing: 0.5px;">[ Digital Cable & High Speed Broadband Service Provider ]</p>
                <div style="margin-top: 8px; font-size: 12px; line-height: 1.4; color: #333;">
                    <strong>Address:</strong> <?php echo $company['c_address']; ?><br>
                    <strong>Phone:</strong> <?php echo $company['c_mobile']; ?><br>
                    <?php if (!empty($company['c_emailalt'])): ?>
                        <strong>GSTIN:</strong> <?php echo $company['c_emailalt']; ?><br>
                    <?php endif; ?>
                </div>
            </div>
            <div style="width: 30%; float: right; text-align: right;">
                <div style="display: inline-block; border: 2px solid #000; padding: 6px 15px; font-weight: bold; font-size: 16px; letter-spacing: 1px; color: #000; margin-top: 5px;">
                    <?php echo $showGst ? 'TAX INVOICE' : 'RECEIPT'; ?>
                </div>
            </div>
        </div>
        <table>
            <tr>
                <td>
                    <div class="customer-info">
                        <strong><?php echo $bill['customer_name']; ?></strong>
                        <?php echo $bill['houseno'] . ', ' . $bill['address']; ?><br>
                        Mobile No. <?php echo $bill['customer_mobile']; ?><br>
                        Customer ID: <?php echo $bill['username']; ?><br>
                        <!-- VC No: <?php echo $bill['customervc']; ?><br> -->
                        STB / VC No:<?php echo $bill['customervc']; ?>
                    </div>
                </td>
                <td>
                    <strong>Invoice No : <?php echo $bill['bl_no']; ?></strong><br>
                    
                    <strong>Billing Period:</strong> <?php echo date('d-m-Y', strtotime($bill['bl_stdt'])); ?> to <?php echo date('d-m-Y', strtotime($bill['bl_expiry'])); ?><br>
                    <strong>Bill Date:</strong> <?php echo date('d-m-Y', strtotime($bill['bl_dt'])); ?><br>
                    <strong>Landmark:</strong> <?php echo $bill['landmark']; ?>
                </td>
            </tr>
        </table>
        <table>
            <tr>
                <td style="width:45%;">
                        <div>
                            <strong>Your account Summary</strong>
                            <table>
                                <tr>
                                    <td>Package Charge</td>
                                    <td class="text-right"><?php echo number_format($package_charge, 2); ?></td>
                                </tr>
                                <tr>
                                    <td>REBATE /DISCOUNT</td>
                                    <td class="text-right">0.00</td>
                                </tr>
                                <tr>
                                    <td>CGST @ 9 %</td>
                                    <td class="text-right"><?php echo number_format($cgst, 2); ?></td>
                                </tr>
                                <tr>
                                    <td>SGST @ 9 %</td>
                                    <td class="text-right"><?php echo number_format($sgst, 2); ?></td>
                                </tr>
                                <tr>
                                    <td>PREVIOUS DUES/ ADVANCES</td>
                                    <td class="text-right"><?php echo number_format($bill['bl_prevdue'], 2); ?></td>
                                </tr>
                                <tr class="total-row">
                                    <td><strong>TOTAL CHARGES (Rounded Off)</strong></td>
                                    <td class="text-right"><strong><?php echo number_format(round($total_amount), 2); ?></strong></td>
                                </tr>
                            </table>
                        </div>
                </td>
                <td  style="width:45%;vertical-align: top;">
                    <?php 
                            $fetchpack=mysqli_query($con,"SELECT * FROM tbl_change_package_details WHERE customer_id='$bill[customerserial]'");
                            while($packarr=mysqli_fetch_array($fetchpack)){

                                $getpack=f(q("SELECT * FROM tbl_pack WHERE pid='$packarr[pac_id]'"));
                                echo $getpack['pname'].' / ';

                            }
                    
                    ?>
                    <?php 
                    // Show a note if package price was calculated from bill_amt
                    if (empty($bill['package_price']) || (float)$bill['package_price'] == 0): 
                    ?>
                        <div style="font-size: 10px; margin-top: 10px; color: #666;">
                            * Package charge calculated from bill amount (incl. GST)
                        </div>
                    <?php endif; ?>
                </td>
            </tr>
        </table>
        

        <?php if ($bill['bl_stat'] == 1): ?>
        <div class="payment-info">
            <table>
                <tr>
                    <td>PAY DATE</td>
                    <td><?php echo date('d-M-y', strtotime($bill['bl_paydt'])); ?></td>
                    <td>AMOUNT PAID</td>
                    <td class="text-right"><?php echo number_format($bill['bl_payamt'], 2); ?></td>
                    <td>DISCOUNT</td>
                    <td class="text-right">0</td>
                    <td>DUE</td>
                    <td class="text-right"><?php echo number_format($bill['bl_due'], 2); ?></td>
                </tr>
            </table>
        </div>
        <?php endif; ?>
        <table>
            <tr>
                <td style="text-align:left;">
                    <!-- WORKING HOURS Monday to Sunday 9.30 am to 2pm & 4 pm to<br>
            8.30 pm. -->
                </td>
                <td style="text-align:right;"><br><br><br>
                    Signature _______________________
                </td>
            </tr>
        </table>    
        <div class="footer">
            
        </div>

        <div class="signature" style="text-align:right;">

        </div>

        <hr style="border-top: 1px dotted #000; margin: 20px 0;">

        <div class="counter-slip">
            <h2>Counter Slip</h2>
            <table>
                <tr>
                    <td style="width: 50%; vertical-align: top;">
                        <table>
                            <tr>
                                <td><strong>Customer Name:</strong></td>
                                <td><?php echo $bill['customer_name']; ?></td>
                            </tr>
                            <tr>
                                <td><strong>Address:</strong></td>
                                <td><?php echo $bill['houseno'] . ', ' . $bill['address']; ?></td>
                            </tr>
                            <tr>
                                <td><strong>Mobile:</strong></td>
                                <td><?php echo $bill['customer_mobile']; ?></td>
                            </tr>
                            <tr>
                                <td><strong>Customer ID:</strong></td>
                                <td><?php echo $bill['username']; ?></td>
                            </tr>
                            <!-- <tr>
                                <td><strong>VC No:</strong></td>
                                <td><?php echo $bill['customervc']; ?></td>
                            </tr> -->
                            <tr>
                                <td><strong>STB No:</strong></td>
                                <td><?php echo $bill['customerstb']; ?></td>
                            </tr>
                        </table>
                    </td>
                    <td style="width: 50%; vertical-align: top;">
                        <table>
                            <tr>
                                <td><strong>Invoice No:</strong></td>
                                <td><?php echo $bill['bl_no']; ?></td>
                            </tr>
                            <tr>
                                <td><strong>Bill Date:</strong></td>
                                <td><?php echo date('d-m-Y', strtotime($bill['bl_stdt'])); ?></td>
                            </tr>
                            <tr>
                                <td><strong>Start Date:</strong></td>
                                <td><?php echo date('d-m-Y', strtotime($bill['bl_stdt'])); ?></td>
                            </tr>
                            <tr>
                                <td><strong>End Date:</strong></td>
                                <td><?php echo date('d-m-Y', strtotime($bill['bl_expiry'])); ?></td>
                            </tr>
                            <tr>
                                <td><strong>Total Amount:</strong></td>
                                <td><?php echo number_format(round($total_amount), 2); ?></td>
                            </tr>
                            
                            <?php if ($bill['bl_stat'] == 1): ?>
                            <tr>
                                <td><strong>Payment Date:</strong></td>
                                <td><?php echo date('d-M-y', strtotime($bill['bl_paydt'])); ?></td>
                            </tr>
                            <tr>
                                <td><strong>Amount Paid:</strong></td>
                                <td><?php echo number_format($bill['bl_payamt'], 2); ?></td>
                            </tr>
                            <?php endif; ?>
                        </table>
                    </td>
                </tr>
            </table>
        </div>

    </div>
</body>
</html>