<?php
header('Content-Type: text/html; charset=utf-8');
$conn = new mysqli("localhost", "crmsahavasco", "sa123AWEsadfa", "crmsahavasco_arevastockexpsheet");

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$conn->set_charset("utf8mb4");
?><?php

$conn->set_charset("utf8mb4");
mysqli_query($conn,"SET NAMES 'utf8mb4'");
mysqli_query($conn,"SET CHARACTER SET utf8mb4");
mysqli_query($conn,"SET SESSION collation_connection = 'utf8mb4_unicode_ci'");

?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vishnu Sahasranamam</title>

<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Devanagari:wght@400;600&family=Playfair+Display:wght@600&display=swap" rel="stylesheet">

<style>
body {
    margin: 0;
    padding: 0;
    background: linear-gradient(to bottom, #fff8e1, #ffe0b2);
    font-family: 'Noto Sans Devanagari', sans-serif;
}

.container {
    width: 100%;
    max-width: 900px;
    margin: 40px auto;
    background: #ffffff;
    padding: 40px;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.1);
}

.title {
    text-align: center;
    font-family: 'Playfair Display', serif;
    font-size: 28px;
    color: #b71c1c;
    margin-bottom: 20px;
}

.name-number {
    text-align:center;
    font-size:18px;
    color:#6d4c41;
    margin-bottom:15px;
}

.section-title {
    margin-top: 25px;
    font-size: 20px;
    color: #bf360c;
    border-bottom: 2px solid #ffcc80;
    padding-bottom: 5px;
}

.content-block {
    margin-top: 12px;
    font-size: 17px;
    line-height: 1.9;
    color: #333;
}

.highlight {
    color: #d84315;
    font-weight: 600;
}

.shloka {
    text-align: center;
    font-size: 22px;
    color: #4e342e;
    margin-bottom: 20px;
}
p { text-align:justify; }
</style>
</head>

<body>
<a href="download.php">
    <button>Download as Word</button>
</a>

<!-- <div class="title" style="padding-top:10px;">&#2405; &#2357;&#2367;&#2359;&#2381;&#2339;&#2369; &#2360;&#2361;&#2360;&#2381;&#2352;&#2344;&#2366;&#2350; &#2405;</div>
-->
<div style="
  max-width: 900px;
  margin: 40px auto;
  padding: 50px 60px;
  background: #fdfcf9;
  font-family: 'Georgia', serif;
  line-height: 1.9;
  color: #2c2c2c;
  box-shadow: 0 0 30px rgba(0,0,0,0.08);
  border-radius: 8px;
">

  <!-- Title -->
<div style="text-align:center; margin-bottom:20px; line-height:1.2;">
  
  <h2 style="color:#c75b12; letter-spacing:1px; margin:0;">
    ॥ श्री विष्णु सहस्रनाम ॥
  </h2>
  
</div></div>
<?php
function cleanSingleName($text) {
    $text = trim($text);

    // remove ॐ
    $text = preg_replace('/^ॐ\s*/u', '', $text);

    // remove नमः, नमः।, नमः।।
    $text = preg_replace('/\s*नमः[।॥]*\s*$/u', '', $text);

    return trim($text);
}
?>
<?php 
$sql = "SELECT DISTINCT shlokas FROM vishnu_sahasranamam ORDER BY id ASC";
$result = $conn->query($sql);

if(!$result){
    die("SQL Error: " . $conn->error);
}

while($row1 = $result->fetch_assoc()) {  
    $shlokas = $row1['shlokas'];
?>

 <div class="shloka">
        <?php echo nl2br($row1['shlokas']); ?>
    </div>
<?php 
$stmt = $conn->prepare("SELECT * FROM vishnu_sahasranamam WHERE shlokas = ? ORDER BY id ASC");
$stmt->bind_param("s", $shlokas);
$stmt->execute();
$result1 = $stmt->get_result();
while($row = $result1->fetch_assoc()) { ?>
<div class="container">


   
    <div class="name-number">
    नाम क्रमांक: <?php echo $row['id']; ?> -
    <strong>
        <?php echo htmlspecialchars(cleanSingleName($row['Sname']), ENT_QUOTES, 'UTF-8'); ?>
    </strong>
</div>

   

    <div class="section-title">&#2350;&#2352;&#2366;&#2336;&#2368; &#2309;&#2352;&#2381;&#2341;</div>

    <div class="content-block">
        <span class="highlight" style="font-size:24px">शब्द:</span><br>
        <?php echo nl2br($row['single_words_marathi']); ?>
</div>

    <div class="content-block"> <span class="highlight"  style="font-size:24px"><strong>अर्थ </strong></span><br>
        <?php echo nl2br($row['simple_meaning']); ?></div>

    <div class="content-block">
        <span class="highlight"  style="font-size:24px"> विवेचन </span><br>
        <?php echo nl2br($row['details_information']); ?>
</div>

  

</div>

<?php } ?><?php } ?>


</body>
</html>

<?php
$conn->close();
?>