<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/ahkweb/header.php');

// fetch fee from database 
$price = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT * FROM pricing WHERE service_name='gst_to_pan_details_fee' "));
$fee = $price['price'];


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

    if ($udata['balance'] >= $fee) {
        
       
        
        $url = "$tng_apiurl/gst_to_pan_with_details_verification?gstin=$inputGST&apikey=$tng_apikey";
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
        ));

        $response = curl_exec($curl);
        curl_close($curl);
        $resdata = json_decode($response, true) ?? [];

        $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($ahk_conn, $appliedby, $fee, $parentPhone, "GST to PAN With Gst Details Find Verification");
            $debit = mysqli_query($ahk_conn, "UPDATE users SET balance=balance-$fee WHERE phone='$appliedby'");
            $updatehistory = mysqli_query($ahk_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="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 $price['price']; ?>" 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/ahkweb/footer.php'); ?>
<script src="../template/ahkweb/assets/js/bootstrap.bundle.min.js"></script>
<script src="../template/ahkweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
<script src="../template/ahkweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
<script src="../template/ahkweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
<script src="../template/ahkweb/assets/js/app.js"></script>
</html>