<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/tngweb/header.php');
$fee    = $udata['gst_to_pan_details_fee'];
checkServiceStatus($udata, 'gst_to_pan_details_fee_status');


if ($_POST['inputGST']) {
    $inputGST = $_POST['inputGST'];
    $appliedby = $udata['phone'];
    $debit_fee = $udata['balance'] - $fee;
     if ($udata['balance'] >= $fee) {
    $request = [
        'gstin' => $inputGST,
        ];

        $resdata = gst_to_pan_with_details_verification_api_run($request);

        $status = $resdata['Message'] ?? '';
        $sampleCode = $resdata['sampleCode'] ?? '';
        $gstin = $resdata['gstin'] ?? '';
        $pan = $resdata['pan'] ?? '';
        $fullname = $resdata['fullname'] ?? '';
        $error = $resdata['error'] ?? '';

        if ($sampleCode === "200") {
            $parentPhone = $udata['parent'] ?? null;
        addCommission($tng_conn, $appliedby, $fee, $parentPhone, "GST to PAN With Gst Details Find Verification");
            $debit = mysqli_query($tng_conn, "UPDATE users SET balance=balance-$fee WHERE phone='$appliedby'");
            $updatehistory = mysqli_query($tng_conn, "INSERT INTO wallethistory(userid, amount, balance, purpose, status, type) VALUES ('$appliedby','$fee','$debit_fee','GST to PAN With Gst Details Find','1','Debit')");
        } else {
            echo '
            <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
            <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
            <script>
                $(function(){
                    Swal.fire({
                        icon: "error",
                        title: "Error!",
                        text: "' . ($error ?: 'Invalid GST number or API failed') . '",
                        timer: 5000,
                        timerProgressBar: true
                    });
                    setTimeout(() => { window.location = ""; }, 4000);
                });
            </script>';
        }
    } else {
        echo '
        <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
        <script>
            $(function(){
                Swal.fire({
                    icon: "error",
                    title: "Low Wallet Balance!",
                    text: "Please recharge to continue.",
                    timer: 2000,
                    timerProgressBar: true
                });
                setTimeout(() => { window.location = "wallet.php"; }, 1200);
            });
        </script>';
    }}

?>


<div class="page-wrapper">
    <div class="page-content">
        <div class="mobile-menu-overlay"></div>
        <div class="main-container">
            <div class="col-lg-12">
                <div class="card" style="margin-left: 10px; padding-left: 30px; padding-top: 12px; box-shadow: 1px 5px 5px 5px;">
                    <div class="stat-widget-two">
                        <div class="stat-content">
                            <div class="stat-text">
                                <div class="container-fluid">
                                    <div class="row">
                                        <div class="col-lg-4 col-md-6 col-sm-6">
                                            <div class="card">
                                                <div class="card-body">
                                                        <div class="mb-4">
        <a href="javascript:history.back()" class="back-btn">
            <i class="bi bi-arrow-left-circle"></i> Go Back
        </a>
    </div>
                                                    <div class="alert alert-primary" role="alert">
                                                        Gst to Pan With Details Check
                                                        <a href="" class="alert-link">Verify Pan With Details using Gst</a>
                                                    </div>
                                                    <form action="" method="POST" class="row g-3">
                                                        <div class="card-body">
                                                            <div class="col-md-12">
                                                                <div class="form-group">
                                                                    <label for="inputGST">Enter Gst Number</label>
                                                                    <input name="inputGST" type="text" id="inputGST"  placeholder="Enter Valid Gst Number" class="form-control" required>
                                                                </div>
                                                                <hr>
                                                                <div class="row mt-3">
                                                                    <div class="col-md-6">
                                                                        <input class="form-control" value="Fee ₹ <?php echo $fee; ?>" readonly>
                                                                    </div>
                                                                    <div class="col-md-6 text-end">
                                                                        <button class="btn btn-success" name="submit" id="submit"><i class="fa fa-check-circle"></i> Submit</button>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>

                                        <?php if ($sampleCode === "200") {
    $data = $resdata['data'];
    $pradr = $data['pradr']['addr'] ?? [];
    $pradr_ntr = $data['pradr']['ntr'] ?? '';
?>
<div class="card" style="background-color: #F8F9F9;" id="printSection">
    <div class="card-body">
        <h4 class="mb-3 text-center">GST to PAN Full Details</h4>
        <table class="table table-bordered table-sm">
            <tr><th>GSTIN</th><td><?= htmlspecialchars($data['gstin']) ?></td></tr>
            <tr><th>Trade Name</th><td><?= htmlspecialchars($data['tradeNam']) ?></td></tr>
            <tr><th>Legal Name</th><td><?= htmlspecialchars($data['lgnm']) ?></td></tr>
            <tr><th>PAN</th><td><?= htmlspecialchars($pan) ?></td></tr>
            <tr><th>Business Type</th><td><?= htmlspecialchars($data['ctb']) ?></td></tr>
            <tr><th>Status</th><td><?= htmlspecialchars($data['sts']) ?></td></tr>
            <tr><th>GSTIN Registration Date</th><td><?= htmlspecialchars($data['rgdt']) ?></td></tr>
            <tr><th>Last Updated</th><td><?= htmlspecialchars($data['lstupdt']) ?></td></tr>
            <tr><th>GST Jurisdiction</th><td><?= htmlspecialchars($data['stj']) ?></td></tr>
            <tr><th>Center Jurisdiction</th><td><?= htmlspecialchars($data['ctj']) ?></td></tr>
            <tr><th>E-invoicing Enabled</th><td><?= htmlspecialchars($data['einvoiceStatus']) ?></td></tr>
            <tr><th>Main Nature of Business</th><td><?= implode(', ', $data['nba']) ?></td></tr>
        </table>

        <h5 class="mt-4">Primary Business Address</h5>
        <table class="table table-bordered table-sm">
            <tr><th>Building</th><td><?= htmlspecialchars($pradr['bnm'] ?? '-') ?></td></tr>
            <tr><th>Street</th><td><?= htmlspecialchars($pradr['st'] ?? '-') ?></td></tr>
            <tr><th>Location</th><td><?= htmlspecialchars($pradr['loc'] ?? '-') ?></td></tr>
            <tr><th>District</th><td><?= htmlspecialchars($pradr['dst'] ?? '-') ?></td></tr>
            <tr><th>State</th><td><?= htmlspecialchars($pradr['stcd'] ?? '-') ?></td></tr>
            <tr><th>Pincode</th><td><?= htmlspecialchars($pradr['pncd'] ?? '-') ?></td></tr>
            <tr><th>Floor</th><td><?= htmlspecialchars($pradr['flno'] ?? '-') ?></td></tr>
            <tr><th>Nature of Business</th><td><?= htmlspecialchars($pradr_ntr) ?></td></tr>
        </table>

        <h5 class="mt-4">Additional Business Addresses</h5>
        <?php foreach ($data['adadr'] as $i => $entry): 
            $addr = $entry['addr']; ?>
            <div class="card mb-2">
                <div class="card-body" style="background-color: #FCF3CF;">
                    <h6>Address <?= $i + 1 ?></h6>
                    <ul style="margin-bottom: 0;">
                        <li><strong>Building:</strong> <?= htmlspecialchars($addr['bnm'] ?? '-') ?></li>
                        <li><strong>Floor:</strong> <?= htmlspecialchars($addr['flno'] ?? '-') ?></li>
                        <li><strong>Street:</strong> <?= htmlspecialchars($addr['st'] ?? '-') ?></li>
                        <li><strong>Location:</strong> <?= htmlspecialchars($addr['loc'] ?? '-') ?></li>
                        <li><strong>District:</strong> <?= htmlspecialchars($addr['dst'] ?? '-') ?></li>
                        <li><strong>State:</strong> <?= htmlspecialchars($addr['stcd'] ?? '-') ?></li>
                        <li><strong>Pincode:</strong> <?= htmlspecialchars($addr['pncd'] ?? '-') ?></li>
                        <li><strong>Nature of Business:</strong> <?= htmlspecialchars($entry['ntr']) ?></li>
                    </ul>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>
<?php } ?> </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <br><br><br><br>
            </div>
        </div>
    </div>
</div>

<?php include('../template/tngweb/footer.php'); ?>
<script src="../template/tngweb/assets/js/bootstrap.bundle.min.js"></script>
<script src="../template/tngweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
<script src="../template/tngweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
<script src="../template/tngweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
<script src="../template/tngweb/assets/js/app.js"></script>
</html>