<META NAME="robots" CONTENT="noindex,nofollow">


<?php
// DataGetter/MarketStatus.php
header('Access-Control-Allow-Origin: https://sharemarketipo.in');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: Content-Type');

/* -------------------- Robust fetch: real timeouts + one retry -------------------- */
$response = false;
$httpCode = null;

for ($attempt = 1; $attempt <= 2; $attempt++) {
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL            => 'https://nwmw.nuvamawealth.com/api/Market/MarketStatus',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING       => '',
        CURLOPT_MAXREDIRS      => 10,
        CURLOPT_TIMEOUT        => 8,
        CURLOPT_CONNECTTIMEOUT => 5,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST  => 'GET',
    ));
    $response = curl_exec($curl);
    $errno    = curl_errno($curl);
    $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);

    if ($errno === 0 && $httpCode === 200 && !empty($response)) break;
    if ($errno === 0 && $httpCode !== 0 && $httpCode < 500) break;
}

$data = ($httpCode === 200 && !empty($response)) ? json_decode($response, true) : null;

function getLogoPath(string $exc): string {
    $map = [
        'NSE'   => 'images/NSE.png',
        'BSE'   => 'images/BSE.png',
        'CDS'   => 'images/CDS.png',
        'MCX'   => 'images/MCX.png',
        'NCDEX' => 'images/NCDEX.png',
    ];
    foreach ($map as $key => $path) {
        if (stripos($exc, $key) !== false) return $path;
    }
    return '';
}

function getStatusInfo(string $sts): array {
    $lower = strtolower($sts);
    if (str_contains($lower, 'pre') || str_contains($lower, 'pre-open')) {
        return ['preopen', 'Pre-Open'];
    }
    if (str_contains($lower, 'open')) {
        return ['open', 'Open'];
    }
    if (str_contains($lower, 'clos')) {
        return ['closed', 'Closed'];
    }
    return ['closed', htmlspecialchars($sts)];
}

if (is_array($data) && count($data) > 0):
?>
<div class="status-grid">
<?php
        foreach ($data as $i => $list):
            $exc      = htmlspecialchars($list['exc'] ?? '');
            $sts      = $list['sts'] ?? '';
            $logoPath = getLogoPath($exc);
            [$statusVariant, $statusLabel] = getStatusInfo($sts);
            $delay = number_format($i * 0.06, 2);
?>
    <article class="status-card" style="animation-delay:<?php echo $delay; ?>s">

        <?php if ($logoPath): ?>
            <img src="<?php echo $logoPath; ?>"
                 alt="<?php echo $exc; ?>"
                 class="status-card__logo"
                 onerror="this.style.display='none';this.nextElementSibling.style.display='block'">
            <div class="status-card__name" style="display:none"><?php echo $exc; ?></div>
        <?php else: ?>
            <div class="status-card__name"><?php echo $exc; ?></div>
        <?php endif; ?>

        <span class="status-badge status-badge--<?php echo $statusVariant; ?>">
            <span class="status-badge__dot"></span>
            <?php echo $statusLabel; ?>
        </span>

    </article>
<?php
        endforeach;
?>
</div>
<?php
    else:
?>
<div class="empty">
    <div class="empty__icon">i</div>
    <div class="empty__text">Status Unavailable</div>
    <div class="empty__sub">Could not fetch market status