<?php
  // Avater Change Request
  if ($section_1 == 'avater') {

    //  Only Login Request Can handle
    if ($writer_login == true) {
      // Check if the file is being uploaded
      if ($_SERVER['REQUEST_METHOD'] === 'POST') {

          if (isset($_FILES['avatar']) && $_FILES['avatar']['error'] === UPLOAD_ERR_OK) {
              $fileTmpPath = $_FILES['avatar']['tmp_name'];
              $fileName = $_FILES['avatar']['name'];
              $fileSize = $_FILES['avatar']['size'];
              $fileType = $_FILES['avatar']['type'];

              // Destination directory
              $uploadFileDir = 'uploads/user/';
              $customFileName = 'MahamudFutureIT.com_'.$the_writer_id.'_'.time().'.jpg';
              $destPath = $uploadFileDir . $customFileName;

              // Create the directory if it doesn't exist
              if (!is_dir($uploadFileDir)) {
                  mkdir($uploadFileDir, 0755, true);
              }

              // Move the uploaded file
              if (move_uploaded_file($fileTmpPath, $destPath)) {

                // Delete  Old Photo
                $writer_old_photo = $the_writer['UserAvater'];
                if ($writer_old_photo != '') {
                  $filePath = 'uploads/user/'.$writer_old_photo;
                  if (file_exists($filePath)) {
                    // Attempt to delete the file
                    unlink($filePath);
                  }
                }
                // Update Photo Database
                UpdateData('tt_accounts', 'UserID', $the_writer_id, array("UserAvater" => $customFileName));


                  echo json_encode([
                    'status' => 'success',
                    'aa' => $the_writer['AccountStatus'],
                    'file' => $customFileName
                  ]);


              } else {
                  echo json_encode(['status' => 'error', 'message' => 'File move failed.']);
              }
          } else {
              echo json_encode(['status' => 'error', 'message' => 'File upload failed.', 'error' => $_FILES['avatar']['error']]);
          }
      } else {
        // Error Page
        echo "404";
        exit;
      }
    }else {
      // Error Page
      echo "404";
      exit;
    }
  }

  // Get Location   Upazila, Union
  elseif ($section_1 == 'location_news') {
    // Handle request
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['requestType'])) {
        $requestType = $_POST['requestType'];

        if ($requestType === 'ttDivision' && isset($_POST['division'])) {


            $division_id = $_POST['division'];
            $district_list = showAllttdata("mahamudfutureit_loc_districts","WHERE division_id = '$division_id'");

            echo '<option value="" selected>জেলা</option>';

            foreach($district_list as $this_district){
                ?>
                <option data-url="<?php echo $this_district['slag'] ?>" value="<?php echo $this_district['id'] ?>">  <?php echo $this_district['bn_name'] ?></option>
                <?php
            }



        } elseif ($requestType === 'ttDistrict' && isset($_POST['district'])) {

            $district_id = $_POST['district'];
            $upazila_list = showAllttdata("mahamudfutureit_loc_upazilas","WHERE district_id = '$district_id'");

            echo '<option value="" selected>উপজেলা</option>';

            foreach($upazila_list as $this_upazila){
                ?>
                <option data-url="<?php echo $this_upazila['slag'] ?>" value="<?php echo $this_upazila['id'] ?>"> <?php echo $this_upazila['bn_name'] ?></option>
                <?php
            }
        } else {
            echo '<option value="" selected>স্থান নির্বাচন করুণ </option>';
        }
    }
  }


  // Get Location   Upazila, Union
  elseif ($section_1 == 'location') {
    // Zilla Inputs
    if(!empty($_POST["pZilla"])) {
      ?>
      <label for="exampleFormControlInput1" class="form-label">স্থায়ী উপজেলা </label>
      <select  name="permanentupazilla" id="permanentUpaZillaID" onchange="changePermanentUnion()" class="form-select select2 address_select js-example-responsive" style="width: 100%" required>
          <option value="" selected disabled>উপজেলা নির্বাচন করুণ</option>
          <?php
          $zila = $_POST["pZilla"];
          $get_district = $conn -> prepare("SELECT * from  mahamudfutureit_loc_upazilas WHERE district_id='$zila'");
          $get_district->execute();
          $get_district_data=$get_district->fetchAll(PDO::FETCH_OBJ);
          if($get_district->rowCount() > 0)
          {
            foreach($get_district_data as $district){
            ?>
            <option value="<?php echo $district->id ?>"><?php echo $district->name.' - '.$district->bn_name ?></option>
            <?php
            }
          }
          ?>
      </select>
      <script type="text/javascript">
          $('#parmanentUnionOption').empty();
          $('#parmanentUnionOption').append('<option value="" selected disabled>ইউনিয়ন নির্বাচন করুণ</option>');
      </script>
      <?php
    }elseif (!empty($_POST["pUpazilla"])) {
      ?>
      <label for="exampleFormControlInput1" class="form-label">স্থায়ী ইউনিয়ন</label>
      <select name="permanentunion" id="permanentUnionID" class="form-select select2 address_select js-example-responsive" style="width: 100%" required>
          <option value="" selected disabled>ইউনিয়ন নির্বাচন করুণ</option>
          <?php
          $upazila = $_POST["pUpazilla"];
          $get_district = $conn -> prepare("SELECT * from  mahamudfutureit_loc_unions WHERE upazilla_id = '$upazila'");
          $get_district->execute();
          $get_district_data=$get_district->fetchAll(PDO::FETCH_OBJ);
          if($get_district->rowCount() > 0)
          {
            foreach($get_district_data as $district){
            ?>
            <option value="<?php echo $district->id ?>"><?php echo $district->name.' - '.$district->bn_name ?></option>
            <?php
            }
          }
          ?>
      </select>
      <?php
    }elseif ( !empty($_POST["presentUpazilla"])) {
      ?>
      <label for="exampleFormControlInput1" class="form-label">বর্তমান উপজেলা</label>
      <select name="presentupazilla" class="form-select select2 address_select js-example-responsive" style="width: 100%" required>
          <option value="" selected disabled>উপজেলা নির্বাচন করুণ</option>
          <?php
          $presentdist = $_POST["presentUpazilla"];

          $get_district = $conn -> prepare("SELECT * from  mahamudfutureit_loc_upazilas  WHERE district_id = '$presentdist'");
          $get_district->execute();
          $get_district_data=$get_district->fetchAll(PDO::FETCH_OBJ);
          if($get_district->rowCount() > 0)
          {
            foreach($get_district_data as $district){
            ?>
            <option value="<?php echo $district->id ?>"><?php echo $district->name.' - '.$district->bn_name ?></option>
            <?php
            }
          }
          ?>
      </select>
      <?php
    }
  }

  // Get User Name
  elseif ($section_1 == 'usernamechk') {

    if (isset($_POST['UserUrl']) && isset($_POST['UserId'])) {
        $username = trim($_POST['UserUrl']); // Get and clean username
        $userId = trim($_POST['UserId']); // Get user ID

        $user_id_by_username = getSingleValue('tt_accounts', 'UserUrl', $username , 'UserID');


        if ($user_id_by_username == '' || $user_id_by_username == $userId) {
          ?>
          <span style='color: green;'>Username is available</span>
          <script type="text/javascript">
            document.getElementById("SubmitChange").disabled = false;
          </script>
          <?php
        }else {
          $user_id_by_username_user = getSingleValue('tt_accounts', 'UserUrl', $username , 'UserName');
          ?>
          <script type="text/javascript">
          document.getElementById("SubmitChange").disabled = true;

          </script>
          <?php
          echo "<span style='color: red;'>Username is already given to - <a target='_blank' href='".$site_domain."mfit-admin/writer-profile.php?id=$user_id_by_username'>".$user_id_by_username_user."</a></span>";
        }

        if ($username == '') {
          ?>
          <script type="text/javascript">
            document.getElementById("SubmitChange").disabled = false;
          </script>
          <?php
        }

    }else {
      // code...
      echo "404";
      exit;
    }

  }elseif ($section_1 == 'load-post') {

    if(isset($_POST['page']) && isset($_POST['data_type']) && isset($_POST['data_id'])){

        if($_POST['data_type'] == 'ttcp'){

            $default_load = 9;
            $itemsPerNextLoad = 8;
            $pageNumber = $_POST['page'];
            $item_offset = ($pageNumber - 1) * $itemsPerNextLoad;
            $category_for_this_post = $_POST['data_id'];

            $this_posts= getArticles($category_for_this_post, $itemsPerNextLoad, $item_offset);

            if (!empty($this_posts)) {

              if ($pageNumber == 3) {
                $category_ads_4 = showAds($advancenews['CategoryAds4'], $advancenews['CategoryAdsCtl']);
                if ($category_ads_4 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_4 ?>
                  </div>
                  <?php
                }
              }elseif ($pageNumber == 4) {
                $category_ads_5 = showAds($advancenews['CategoryAds5'], $advancenews['CategoryAdsCtl']);
                if ($category_ads_5 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_5 ?>
                  </div>
                  <?php
                }
              }elseif ($pageNumber == 5) {
                $category_ads_6 = showAds($advancenews['CategoryAds6'], $advancenews['CategoryAdsCtl']);
                if ($category_ads_6 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_6 ?>
                  </div>
                  <?php
                }
              }else {
                $category_ads_7 = showAds($advancenews['CategoryAds7'], $advancenews['CategoryAdsCtl']);
                if ($category_ads_7 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_7 ?>
                  </div>
                  <?php
                }
              }

              foreach ($this_posts as $the_load_post) {
                ?>
                <div class="col-md-3 col-6">
                  <a href="<?php echo articleLink($the_load_post['PostId']) ?>"  class="d-block prothom-songbad-post-style">
                    <div class="align-items-center">
                        <div class="position-relative overflow-hidden rounded-2">
                          <img class="w-100 ps-post-line-img"  src="<?php echo $advancenews['ArticlePreloader'] ?>"  data-src="<?php echo articleImg($the_load_post['PostId']) ?>" alt="<?php echo $the_load_post['PostTitle'] ?>">
                        </div>
                        <div class="p-2">
                          <h1 class=" news-title-2 m-0 tt-text"><?php echo $the_load_post['PostTitle'] ?></h1>
                        </div>
                    </div>
                  </a>
                </div>
                <?php
              }

            }else {
              ?>
              <div class="col-12">
                <?php echo  noPost(); ?>
                <script type="text/javascript">
                document.getElementById("load-more-button").style.display = "none";
                </script>
              </div>
              <?php
            }
        }elseif($_POST['data_type'] == 'ttathp'){

            $default_load = 9;
            $itemsPerNextLoad = 8;
            $pageNumber = $_POST['page'];
            $item_offset = ($pageNumber - 1) * $itemsPerNextLoad;
            $category_for_this_post = $_POST['data_id'];
            $this_posts= getUserArticles($category_for_this_post, $itemsPerNextLoad, $item_offset);

            if (!empty($this_posts)) {

              if ($pageNumber == 3) {
                $category_ads_4 = showAds($advancenews['AuthorAds4'], $advancenews['AuthorAdsCtl']);
                if ($category_ads_4 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_4 ?>
                  </div>
                  <?php
                }
              }elseif ($pageNumber == 4) {
                $category_ads_5 = showAds($advancenews['AuthorAds5'], $advancenews['AuthorAdsCtl']);
                if ($category_ads_5 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_5 ?>
                  </div>
                  <?php
                }
              }elseif ($pageNumber == 5) {
                $category_ads_6 = showAds($advancenews['AuthorAds6'], $advancenews['AuthorAdsCtl']);
                if ($category_ads_6 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_6 ?>
                  </div>
                  <?php
                }
              }else {
                $category_ads_7 = showAds($advancenews['AuthorAds7'], $advancenews['AuthorAdsCtl']);
                if ($category_ads_7 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_7 ?>
                  </div>
                  <?php
                }
              }

              foreach ($this_posts as $the_load_post) {
                ?>
                <div class="col-md-3 col-6">
                  <a href="<?php echo articleLink($the_load_post['PostId']) ?>"  class="d-block prothom-songbad-post-style">
                    <div class="align-items-center">
                        <div class="position-relative overflow-hidden rounded-2">
                          <img class="w-100 ps-post-line-img"  src="<?php echo $advancenews['ArticlePreloader'] ?>"  data-src="<?php echo articleImg($the_load_post['PostId']) ?>" alt="<?php echo $the_load_post['PostTitle'] ?>">
                        </div>
                        <div class="p-2">
                          <h1 class=" news-title-2 m-0 tt-text"><?php echo $the_load_post['PostTitle'] ?></h1>
                        </div>
                    </div>
                  </a>
                </div>
                <?php
              }

            }else {
              ?>
              <div class="col-12">
                <?php echo  noPost(); ?>
                <script type="text/javascript">
                document.getElementById("load-more-button").style.display = "none";
                </script>
              </div>
              <?php
            }
        }elseif($_POST['data_type'] == 'ttarp'){

            $default_load = 8;
            $itemsPerNextLoad = 8;
            $pageNumber = $_POST['page'];
            $item_offset = ($pageNumber - 1) * $itemsPerNextLoad;

            $date_arch = $_POST['data_id'];

            $archive_article = showAllttdata("mahamudfutureit_article", "WHERE PostDayPublic = '$date_arch' AND PostStatus = 'published' AND PostDatePublic <= '$the_time' ORDER BY PostDatePublic DESC LIMIT $itemsPerNextLoad OFFSET $item_offset");
            if (!empty($archive_article)) {
              foreach ($archive_article as $the_article) {
                ?>
                  <div>
                    <a href="<?php echo articleLink($the_article['PostId']) ?>" class="d-block  prothom-songbad-post-style  ">
                       <div class="row g-2 align-items-center">
                           <div class="col-4">
                             <div class="position-relative overflow-hidden rounded-2">
                               <img class="w-100 ps-post-line-img "  src="<?php echo $advancenews['ArticlePreloader'] ?>"  data-src="<?php echo articleImg($the_article['PostId']) ?>" alt="<?php echo $the_article['PostTitle'] ?>">
                             </div>
                           </div>
                           <div class="col-8">
                             <h1 class=" news-title-2 m-0 tt-text"><?php echo $the_article['PostTitle'] ?></h1>
                             <p class="summery-2"><?php echo str_replace('&nbsp;', '&#160;',  articleContentClean(strip_tags($the_article['PostContent']))) ?></p>
                           </div>
                       </div>
                     </a>
                  </div>
                  <hr>
                <?php
              }
            }else {
              ?>
              <div class="col-12">
                <?php echo  noPost(); ?>
                <script type="text/javascript">
                document.getElementById("load-more-button").style.display = "none";
                </script>
              </div>
              <?php
            }

        }elseif($_POST['data_type'] == 'ttpg'){

            $default_load = 9;
            $itemsPerNextLoad = 8;
            $pageNumber = $_POST['page'];
            $item_offset = ($pageNumber - 1) * $itemsPerNextLoad;

            $all_gallery = getGallery($itemsPerNextLoad, $item_offset);
            if (!empty($all_gallery)) {
              foreach ($all_gallery as $the_galllery) {
                $gallery_items = json_decode($the_galllery['gallery_items'], TRUE);
                ?>
                <div class="col-md-6 col-lg-3 col-6">
                  <a  href="<?php echo $site_domain ?>photos/<?php echo $the_galllery['gallery_url'] ?>/" class="d-block prothom-songbad-post-style">
                      <div class="align-items-center">
                          <div class="position-relative  featured-image-photo overflow-hidden rounded-2">
                            <img class="gallery-img-1 ps-post-line-img " src="<?php echo $advancenews['ArticlePreloader'] ?>" data-src="<?php echo $gallery_items[1]['storyimage']; ?>" alt="<?php echo htmlentities($the_galllery['gallery_title'], ENT_QUOTES, 'utf-8'); ?>">
                            <i class="bi bi-image"></i>
                          </div>
                          <h1 class="py-2 news-title-2 m-0"> <?php echo htmlentities($the_galllery['gallery_title'], ENT_QUOTES, 'utf-8'); ?></h1>
                      </div>
                    </a>
                </div>
                <?php
              }
            }else {
              ?>
              <div class="col-12">
                <?php echo  noPost(); ?>
                <script type="text/javascript">
                document.getElementById("load-more-button").style.display = "none";
                </script>
              </div>
              <?php
            }

        }elseif($_POST['data_type'] == 'ttvg'){

            $default_load = 9;
            $itemsPerNextLoad = 8;
            $pageNumber = $_POST['page'];
            $item_offset = ($pageNumber - 1) * $itemsPerNextLoad;

            $tt_recent_video = showAllttdata("mahamudfutureit_video", " ORDER BY  ID  DESC LIMIT $itemsPerNextLoad OFFSET $item_offset");
            if (!empty($tt_recent_video)) {
              foreach ($tt_recent_video as $the_rvideo) {
                ?>
                <div class="col-6">
                  <div class="mahamudfutureit_vwraper rounded-2 overflow-hidden">
                    <a href="<?php echo $site_domain ?>video/<?php echo $the_rvideo['ID'] ?>/" class="position-relative">
                      <i class="bi bi-play-circle"></i>
                      <img class="w-100" src="<?php echo $the_rvideo['FeaturedImage'] ?>" alt="<?php echo $the_rvideo['VideoTitle'] ?>">
                    </a>
                    <h3><a   href="<?php echo $site_domain ?>video/<?php echo $the_rvideo['ID'] ?>/"><?php echo $the_rvideo['VideoTitle'] ?></a></h3>
                  </div>
                </div>
                <?php
              }
            }else {
              ?>
              <div class="col-12">
                <div class="text-center p-2 bg-light rounded-t">
                  কোন ভিডিও নেই !
                </div>
                <script type="text/javascript">
                document.getElementById("load-more-button").style.display = "none";
                </script>
              </div>
              <?php
            }

        }elseif($_POST['data_type'] == 'ttpl'){

            $default_load = 6;
            $itemsPerNextLoad = 6;
            $pageNumber = $_POST['page'];
            $item_offset = ($pageNumber - 1) * $itemsPerNextLoad;

            $all_poll = getPoll($itemsPerNextLoad, $item_offset);
            if (!empty($all_poll)) {
              foreach ($all_poll as $the_poll) {
                // Total votes
                $total_votes = $the_poll['PollOptionOneView'] + $the_poll['PollOptionTwoView'] + $the_poll['PollOptionThreeView'];
                // Avoid division by zero
                if ($total_votes > 0) {
                    $p1 = floor(($the_poll['PollOptionOneView'] / $total_votes) * 100);
                    $p2 = floor(($the_poll['PollOptionTwoView'] / $total_votes) * 100);
                    $p3 = floor(($the_poll['PollOptionThreeView'] / $total_votes) * 100);
                } else {
                    $p1 = $p2 = $p3 = 0;
                }

                $poll_link = pollLink($the_poll['PollID']);
                $poll_title = htmlentities($the_poll['PollTitle'], ENT_QUOTES, 'utf-8');

                ?>
                <div class="col-lg-4 col-12">
                  <div class="border rounded-2 bg-white p-3 mahamudfutureit_poll">
                    <div>
                      <div class="row">
                        <div class="col-10">
                          <span class="mahamudfutureit_poll_date"><?php echo dateTimeShow($the_poll['PollDate']) ?></span>
                        </div>
                        <div class="col-2 text-end">
                          <span class="mahamudfutureit_poll_download" id="poll_download_<?php echo $the_poll['PollID'] ?>"  data-poll-date="<?php echo dateTimeShow($the_poll['PollDate']) ?>"
                                                                  data-poll-image="<?php echo  getPollImage($the_poll['PollID']); ?>"
                                                                  data-poll-title="<?php echo htmlentities($the_poll['PollTitle'], ENT_QUOTES, 'utf-8'); ?>"
                                                                  data-poll-option1="<?php echo htmlentities($the_poll['PollOptionOne'], ENT_QUOTES, 'utf-8'); ?>"
                                                                  data-poll-option2="<?php echo htmlentities($the_poll['PollOptionTwo'], ENT_QUOTES, 'utf-8'); ?>"
                                                                  data-poll-option3="<?php echo htmlentities($the_poll['PollOptionThree'], ENT_QUOTES, 'utf-8'); ?>"
                                                                  data-poll-option1-vote="<?php echo bnNumber($p1) ?> %"
                                                                  data-poll-option2-vote="<?php echo bnNumber($p2) ?> %"
                                                                  data-poll-option3-vote="<?php echo bnNumber($p3) ?> %"
                                                                  data-poll-option1-vote-per="<?php echo $p1 ?>%"
                                                                  data-poll-option2-vote-per="<?php echo $p2 ?>%"
                                                                  data-poll-option3-vote-per="<?php echo $p3 ?>%"
                                                                  data-poll-download="ডাউনলোডঃ <?php echo dateTimeShow(date('Y-m-d H:i:s')); ?>"
                                                                  data-poll-total-votes="মোট ভোটদাতাঃ <?php echo bnNumber($total_votes) ?> জন"><i class="bi bi-download"></i></span>
                        </div>
                      </div>
                    </div>
                    <div class="mahamudfutureit_poll_image mt-2">
                      <img class="rounded-2 w-100"  src="<?php echo $advancenews['Poll_DF_Img'] ?>" data-src="<?php echo  getPollImage($the_poll['PollID']); ?>" alt="<?php echo htmlentities($the_poll['PollTitle'], ENT_QUOTES, 'utf-8'); ?>">
                    </div>
                    <div class="mahamudfutureit_poll_title mt-2">
                      <h2 class="news-title-2"><?php echo htmlentities($the_poll['PollTitle'], ENT_QUOTES, 'utf-8'); ?></h2>
                    </div>
                    <div class="mahamudfutureit_poll_options">
                      <div class="mahamudfutureit_poll_options_item rounded-2 collect_vote collect_vote_<?php echo $the_poll['PollID'] ?>" data-poll-id="<?php echo $the_poll['PollID'] ?>" data-poll-option="1">
                        <div class="mahamudfutureit_poll_item_info fw-bold">
                          <i class="bi bi-circle tt_poll_selector_<?php echo $the_poll['PollID'] ?>" id="ttpoll_icon_<?php echo $the_poll['PollID'] ?>_1"></i>&nbsp;&nbsp;<?php echo htmlentities($the_poll['PollOptionOne'], ENT_QUOTES, 'utf-8'); ?> <span id="ttpoll_perc_<?php echo $the_poll['PollID'] ?>_1" style="display:none" class="float-end fw-bold">৯৭%</span>
                        </div>
                        <div class="mahamudfutureit_poll_item_bar" id="ttpoll_opt_<?php echo $the_poll['PollID'] ?>_1" style="width: 0%; display:none"></div>
                      </div>
                      <div class="mahamudfutureit_poll_options_item rounded-2 collect_vote collect_vote_<?php echo $the_poll['PollID'] ?>" data-poll-id="<?php echo $the_poll['PollID'] ?>" data-poll-option="2">
                        <div class="mahamudfutureit_poll_item_info fw-bold">
                          <i class="bi-circle tt_poll_selector_<?php echo $the_poll['PollID'] ?>" id="ttpoll_icon_<?php echo $the_poll['PollID'] ?>_2"></i>&nbsp;&nbsp;<?php echo htmlentities($the_poll['PollOptionTwo'], ENT_QUOTES, 'utf-8'); ?> <span id="ttpoll_perc_<?php echo $the_poll['PollID'] ?>_2" style="display:none" class="float-end fw-bold">৯৭%</span>
                        </div>
                        <div class="mahamudfutureit_poll_item_bar" id="ttpoll_opt_<?php echo $the_poll['PollID'] ?>_2" style="width: 0%; display:none"></div>
                      </div>
                      <div class="mahamudfutureit_poll_options_item rounded-2 collect_vote collect_vote_<?php echo $the_poll['PollID'] ?>" data-poll-id="<?php echo $the_poll['PollID'] ?>" data-poll-option="3">
                        <div class="mahamudfutureit_poll_item_info fw-bold">
                          <i class="bi bi-circle tt_poll_selector_<?php echo $the_poll['PollID'] ?>" id="ttpoll_icon_<?php echo $the_poll['PollID'] ?>_3"></i>&nbsp;&nbsp;<?php echo htmlentities($the_poll['PollOptionThree'], ENT_QUOTES, 'utf-8'); ?> <span id="ttpoll_perc_<?php echo $the_poll['PollID'] ?>_3" style="display:none" class="float-end fw-bold">৯৭%</span>
                        </div>
                        <div class="mahamudfutureit_poll_item_bar" id="ttpoll_opt_<?php echo $the_poll['PollID'] ?>_3" style="width: 0%; display:none"></div>
                      </div>
                    </div>
                    <div class="mahamudfutureit_poll_total_voter text-center my-3 fw-bold">
                      মোট ভোটদাতাঃ <span id="ttpoll_total_<?php echo $the_poll['PollID'] ?>"><?php echo bnNumber($the_poll['PollOptionOneView']+$the_poll['PollOptionTwoView']+$the_poll['PollOptionThreeView']) ?></span> জন
                    </div>
                    <div class="text-center mb-2">
                      <div class="mahamudfutureit_social_menu  bordered round color ">
                        <a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo $poll_link; ?>" target="_blank" class="facebook">
                            <i class="fa-brands fa-facebook-f"></i>
                        </a>
                        <a href="https://api.whatsapp.com/send?text=<?php echo $poll_title . ' ' . $poll_link; ?>" target="_blank" class="whatsapp">
                            <i class="fa-brands fa-whatsapp"></i>
                        </a>
                        <a href="https://www.messenger.com/t/?link=<?php echo $poll_link; ?>" target="_blank" class="messenger">
                            <i class="bi bi-messenger"></i>
                        </a>
                        <a href="https://twitter.com/intent/tweet?url=<?php echo $poll_link; ?>&text=<?php echo $poll_title; ?>" target="_blank" class="twitter">
                            <i class="fa-brands fa-x-twitter"></i>
                        </a>
                        <a href="https://www.linkedin.com/shareArticle?mini=true&url=<?php echo $poll_link; ?>&title=<?php echo $poll_title; ?>" target="_blank" class="linkedin">
                            <i class="fa-brands fa-linkedin-in"></i>
                        </a>
                      </div>
                    </div>
                  </div>
                </div>
                <?php
              }
            }else {
              ?>
              <div class="col-12">
                <?php echo  noPost(); ?>
                <script type="text/javascript">
                document.getElementById("load-more-button").style.display = "none";
                </script>
              </div>
              <?php
            }

        }elseif($_POST['data_type'] == 'ttlp'){

          $default_load = 9;
          $itemsPerNextLoad = 8;
          $pageNumber = $_POST['page'];
          $item_offset = ($pageNumber - 1) * $itemsPerNextLoad;
          $category_for_this_post = $_POST['data_id'];

          $this_posts= getCountryArticles($_POST['loc_type'], $category_for_this_post, $itemsPerNextLoad, $item_offset);

          if (!empty($this_posts)) {

            if ($pageNumber == 3) {
              $category_ads_4 = showAds($advancenews['CountryAds4'], $advancenews['CountryAdsCtl']);
              if ($category_ads_4 != '') {
                ?>
                <div class=" col-12">
                  <?php echo $category_ads_4 ?>
                </div>
                <?php
              }
            }elseif ($pageNumber == 4) {
              $category_ads_5 = showAds($advancenews['CountryAds5'], $advancenews['CountryAdsCtl']);
              if ($category_ads_5 != '') {
                ?>
                <div class=" col-12">
                  <?php echo $category_ads_5 ?>
                </div>
                <?php
              }
            }elseif ($pageNumber == 5) {
              $category_ads_6 = showAds($advancenews['CountryAds6'], $advancenews['CountryAdsCtl']);
              if ($category_ads_6 != '') {
                ?>
                <div class=" col-12">
                  <?php echo $category_ads_6 ?>
                </div>
                <?php
              }
            }else {
              $category_ads_7 = showAds($advancenews['CountryAds7'], $advancenews['CountryAdsCtl']);
              if ($category_ads_7 != '') {
                ?>
                <div class=" col-12">
                  <?php echo $category_ads_7 ?>
                </div>
                <?php
              }
            }

            foreach ($this_posts as $the_load_post) {
              ?>
              <div class="col-md-3 col-6">
                <a href="<?php echo articleLink($the_load_post['PostId']) ?>"  class="d-block prothom-songbad-post-style">
                  <div class="align-items-center">
                      <div class="position-relative overflow-hidden rounded-2">
                        <img class="w-100 ps-post-line-img"  src="<?php echo $advancenews['ArticlePreloader'] ?>"  data-src="<?php echo articleImg($the_load_post['PostId']) ?>" alt="<?php echo $the_load_post['PostTitle'] ?>">
                      </div>
                      <div class="p-2">
                        <h1 class=" news-title-2 m-0 tt-text"><?php echo $the_load_post['PostTitle'] ?></h1>
                      </div>
                  </div>
                </a>
              </div>
              <?php
            }

          }else {
            ?>
            <div class="col-12">
              <?php echo  noPost(); ?>
              <script type="text/javascript">
              document.getElementById("load-more-button").style.display = "none";
              </script>
            </div>
            <?php
          }
      }
        elseif($_POST['data_type'] == 'tttop'){

            $default_load = 9;
            $itemsPerNextLoad = 8;
            $pageNumber = $_POST['page'];
            $item_offset = ($pageNumber - 1) * $itemsPerNextLoad;

            $category_for_this_post = $_POST['data_id'];
            $this_posts =  getTopicsArticle($category_for_this_post, $itemsPerNextLoad, $item_offset);

            if (!empty($this_posts)) {

              if ($pageNumber == 3) {
                $category_ads_4 = showAds($advancenews['TopicAds4'], $advancenews['TopicAdsCtl']);
                if ($category_ads_4 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_4 ?>
                  </div>
                  <?php
                }
              }elseif ($pageNumber == 4) {
                $category_ads_5 = showAds($advancenews['TopicAds5'], $advancenews['TopicAdsCtl']);
                if ($category_ads_5 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_5 ?>
                  </div>
                  <?php
                }
              }elseif ($pageNumber == 5) {
                $category_ads_6 = showAds($advancenews['TopicAds6'], $advancenews['TopicAdsCtl']);
                if ($category_ads_6 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_6 ?>
                  </div>
                  <?php
                }
              }else {
                $category_ads_7 = showAds($advancenews['TopicAds7'], $advancenews['TopicAdsCtl']);
                if ($category_ads_7 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_7 ?>
                  </div>
                  <?php
                }
              }

              foreach ($this_posts as $the_load_post) {
                ?>
                <div class="col-md-3 col-6">
                  <a href="<?php echo articleLink($the_load_post['PostId']) ?>"  class="d-block prothom-songbad-post-style">
                    <div class="align-items-center">
                        <div class="position-relative overflow-hidden rounded-2">
                          <img class="w-100 ps-post-line-img"  src="<?php echo $advancenews['ArticlePreloader'] ?>"  data-src="<?php echo articleImg($the_load_post['PostId']) ?>" alt="<?php echo $the_load_post['PostTitle'] ?>">
                        </div>
                        <div class="p-2">
                          <h1 class=" news-title-2 m-0 tt-text"><?php echo $the_load_post['PostTitle'] ?></h1>
                        </div>
                    </div>
                  </a>
                </div>
                <?php
              }

            }else {
              ?>
              <div class="col-12">
                <?php echo  noPost(); ?>
                <script type="text/javascript">
                document.getElementById("load-more-button").style.display = "none";
                </script>
              </div>
              <?php
            }
        }elseif($_POST['data_type'] == 'ttsp'){

            $default_load = 12;
            $itemsPerNextLoad = 6;
            $pageNumber = $_POST['page'];
            $item_offset = ($pageNumber - 1) * $itemsPerNextLoad;

            $category_for_this_post = $_POST['data_id'];
            $this_posts =   getSearchArticles($category_for_this_post, $itemsPerNextLoad, $item_offset);

            if (!empty($this_posts)) {

              if ($pageNumber == 3) {
                $category_ads_4 = showAds($advancenews['SearchAds4'], $advancenews['SearchAdsCtl']);
                if ($category_ads_4 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_4 ?>
                  </div>
                  <?php
                }
              }elseif ($pageNumber == 4) {
                $category_ads_5 = showAds($advancenews['SearchAds5'], $advancenews['SearchAdsCtl']);
                if ($category_ads_5 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_5 ?>
                  </div>
                  <?php
                }
              }elseif ($pageNumber == 5) {
                $category_ads_6 = showAds($advancenews['SearchAds6'], $advancenews['SearchAdsCtl']);
                if ($category_ads_6 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_6 ?>
                  </div>
                  <?php
                }
              }else {
                $category_ads_7 = showAds($advancenews['SearchAds7'], $advancenews['SearchAdsCtl']);
                if ($category_ads_7 != '') {
                  ?>
                  <div class=" col-12">
                    <?php echo $category_ads_7 ?>
                  </div>
                  <?php
                }
              }

              foreach ($this_posts as $the_load_post) {
                ?>
                <div class="col-md-12 col-6 ">
                  <div class=" tt-white ">
                    <div class="row g-2 align-items-center ">
                      <div class="col-md-4  col-12 mt-0 ">
                       <a href="<?php echo articleLink($the_load_post['PostId']) ?>"  class="p-2 d-block tt-white prothom-songbad-post-style ">
                         <div class="position-relative  overflow-hidden">
                           <img class="w-100 ps-post-line-img"  src="<?php echo $advancenews['ArticlePreloader'] ?>"  data-src="<?php echo articleImg($the_load_post['PostId']) ?>" alt="<?php echo $the_load_post['PostTitle'] ?>">
                         </div>
                       </a>
                      </div>
                      <div class="col-md-8 col-12 mt-0 ps-md-0 pt-0 p-3 pt-md-3">
                        <a href="<?php echo articleLink($the_load_post['PostId']) ?>"  class=" mb-2 d-block tt-white prothom-songbad-post-style ">
                          <h1 class="news-title-2 m-0 tt-text"><?php echo $the_load_post['PostTitle'] ?></h1>
                          <div class="d-none d-md-block">
                            <p class="summery-2  mb-0 my-1">
                              <?php echo  strip_tags($the_load_post['PostContent']) ?>
                            </p>
                          </div>
                        </a>
                        <a href="<?php echo articleCategory($the_load_post['PostId'],'url') ?>">
                          <h4 class="m-0 p_post_cat1 d-inline fs-6"><?php echo articleCategory($the_load_post['PostId'],'name') ?></h4>
                        </a> | <?php echo dokanDate($the_load_post['PostDatePublic']) ?>
                      </div>
                    </div>
                  </div>
                </div>
                <?php
              }

            }else {
              ?>
              <div class="col-12">
                <?php echo  noPost(); ?>
                <script type="text/javascript">
                document.getElementById("load-more-button").style.display = "none";
                </script>
              </div>
              <?php
            }
        }elseif($_POST['data_type'] == 'ttstp'){

            $default_load = 12;
            $itemsPerNextLoad = 6;
            $pageNumber = $_POST['page'];
            $item_offset = ($pageNumber - 1) * $itemsPerNextLoad;
            $the_item_post = showAllttdata('mahamudfutureit_photostories', "WHERE  story_status = 'Published' AND story_date <= '$the_time' ORDER BY story_date DESC LIMIT $itemsPerNextLoad OFFSET $item_offset");
            if (!empty($the_item_post)) {
              $count_the_item_post = 1;
              echo '<div class="row g-3  mt-3 " id="news-container">';
              foreach ($the_item_post as $the_story) {
                $story_items = json_decode($the_story['story_items'],true);
                ?>
                <div class="col-md-2 col-6  position-relative">
                  <a href="<?php echo $site_domain."photo-stories/".$the_story['story_url'] ?>/" class="story-single-block" style="background-image:url(<?php echo $story_items[1]['storyimage'] ?>)">
                    <h3 class="news-title-4"><?php echo $the_story['story_title'] ?></h3>
                    <span class="photo-stories-icon"><i class="bi bi-files-alt"></i></span>
                  </a>
                </div>
                <?php
              }
              echo '</div>';
              ?>
             <div class="row mb-2">
                  <div id="loader" class="col-md-12 text-center py-3" style="display: none;"><img src="<?php echo $site_domain ?>assets/image/loading-post.gif" style=" height:40px;"></div>
                  <div class="col-12 text-center py-3">
                      <button class="btn btn-more-details tt-white tt-text" id="load-more-button"> আরো দেখুন  </button>
                  </div>
             </div>
              <?php
            }else {
              ?>
              <div class="col-12">
                <?php echo  noPost(); ?>
                <script type="text/javascript">
                document.getElementById("load-more-button").style.display = "none";
                </script>
              </div>
              <?php
            }
        }

      }else{
        // Error Page
        echo "404";
        exit;
      }

  }elseif ($section_1 == 'arcive-calender') {
    if (!empty($_REQUEST['year']) && !empty($_REQUEST['month'])) {
        $year = (int)$_REQUEST['year'];
        $month = (int)$_REQUEST['month'];

        // Validate inputs
        if ($year < 1 || $month < 1 || $month > 12) {
            header('HTTP/1.1 400 Bad Request');
            echo json_encode(['error' => 'Invalid year or month.']);
            exit;
        }

        // Calculate the last day of the month
        $dateTime = new DateTime("$year-$month-01");
        $lastday = (int)$dateTime->format('t'); // 't' gives the number of days in the month

        $data = [];
        for ($day = 1; $day <= $lastday; $day++) {
            $date = sprintf('%04d-%02d-%02d', $year, $month, $day);
            $data[] = [
                'id' => uniqid(str_pad($day, 2, '0', STR_PAD_LEFT), true),
                'date' => $date,
                'markup' => '<a href="' . $site_domain . 'archive/' . $date . '/ ">[day]</a>',
            ];
        }

        header('Content-Type: application/json');
        echo json_encode($data);
    } else {
        header('HTTP/1.1 400 Bad Request');
        echo json_encode(['error' => 'Year and month are required.']);
    }
  }elseif ($section_1 == 'collect-vote') {

    $poll_id = isset($_POST['poll_id']) ? trim($_POST['poll_id']) : '';
    $poll_option = isset($_POST['poll_option']) ? trim($_POST['poll_option']) : '';

    if ($poll_id != '' && $poll_option != '') {

        $pop_1_view = getSingleValue('mahamudfutureit_poll', 'PollID', $poll_id, 'PollOptionOneView');
        $pop_2_view = getSingleValue('mahamudfutureit_poll', 'PollID', $poll_id, 'PollOptionTwoView');
        $pop_3_view = getSingleValue('mahamudfutureit_poll', 'PollID', $poll_id, 'PollOptionThreeView');
        $pop_ip = json_decode(getSingleValue('mahamudfutureit_poll', 'PollID', $poll_id, 'PollIP') ?: '[]', true);

        // Ensure it's an array
        if (!is_array($pop_ip)) {
            $pop_ip = [];
        }

        $current_ip = $userIP;

        // Check if IP exists
        if (in_array($current_ip, $pop_ip)) {
            // Voted Locked
            ?>
            <script type="text/javascript">
              $(".tt_poll_selector_<?php echo $poll_id ?>").removeClass("bi-circle").addClass("bi-slash-circle-fill").addClass("text-muted");
              $(".collect_vote_<?php echo $poll_id ?>").removeClass("collect_vote");
            </script>
            <?php
        } else {
            // Add new IP and Vote
            $pop_ip[] = $current_ip;
            if ($poll_option == '1') {
                // Vote 1
                $pop_1_view = $pop_1_view + 1;
                $post_form_data = array(
                    "PollOptionOneView" => $pop_1_view,
                    "PollIP" => json_encode($pop_ip),
                );
            } elseif ($poll_option == '2') {
                // Vote 2
                $pop_2_view = $pop_2_view + 1;
                $post_form_data = array(
                    "PollOptionTwoView" => $pop_2_view,
                    "PollIP" => json_encode($pop_ip),
                );
            } elseif ($poll_option == '3') {
                // Vote 3
                $pop_3_view = $pop_3_view + 1;
                $post_form_data = array(
                    "PollOptionThreeView" => $pop_3_view,
                    "PollIP" => json_encode($pop_ip),
                );
            }
            UpdateData('mahamudfutureit_poll', 'PollID', $poll_id, $post_form_data);
            ?>
            <script type="text/javascript">
              $("#ttpoll_icon_<?php echo $poll_id ?>_<?php echo $poll_option ?>").removeClass("bi-circle").addClass("bi-record-circle");
            </script>
            <?php
        }

        // Total votes
        $total_votes = $pop_1_view + $pop_2_view + $pop_3_view;

        // Avoid division by zero
        if ($total_votes > 0) {
            $p1 = floor(($pop_1_view / $total_votes) * 100);
            $p2 = floor(($pop_2_view / $total_votes) * 100);
            $p3 = floor(($pop_3_view / $total_votes) * 100);
        } else {
            $p1 = $p2 = $p3 = 0;
        }
        ?>
        <script type="text/javascript">
          $(document).ready(function() {
            var pollId = "<?php echo $poll_id ?>";
            var percentages = [<?php echo $p1 ?>, <?php echo $p2 ?>, <?php echo $p3 ?>];
            var percentagesBn = ["<?php echo bnNumber($p1) ?>", "<?php echo bnNumber($p2) ?>", "<?php echo bnNumber($p3) ?>"];

            $("#ttpoll_total_" + pollId).text("<?php echo bnNumber($total_votes) ?>");
            $(".collect_vote_<?php echo $poll_id ?>").removeClass("collect_vote");
            $.each(percentages, function(index, value) {
              var i = index + 1;
              $("#ttpoll_perc_" + pollId + "_" + i).css("display", "block").text(percentagesBn[index] + " %");
              $("#ttpoll_opt_" + pollId + "_" + i).css({
                "display": "block",
                "width": value + "%"
              });
            });

            $('#poll_download_<?php echo $poll_id ?>')
                .attr('data-poll-option1-vote', '<?php echo bnNumber($p1) ?> %')
                .attr('data-poll-option2-vote', '<?php echo bnNumber($p2) ?> %')
                .attr('data-poll-option3-vote', '<?php echo bnNumber($p3) ?> %')
                .attr('data-poll-option1-vote-per', '<?php echo $p1 ?>%')
                .attr('data-poll-option2-vote-per', '<?php echo $p2 ?>%')
                .attr('data-poll-option3-vote-per', '<?php echo $p3 ?>%')
                .attr('data-poll-total-votes', 'মোট ভোটদাতাঃ <?php echo bnNumber($total_votes) ?> জন');
          });
        </script>
        <?php
    }
  /* =================  REACTIONS  ================= */
  }elseif ($section_1 == 'react') {
      header('Content-Type: application/json; charset=utf-8');
      $post_id  = isset($_POST['post_id'])  ? (int) $_POST['post_id'] : 0;
      $reaction = isset($_POST['reaction']) ? trim($_POST['reaction']) : '';
      $allowed  = ['like','love','haha','wow','sad'];
      if ($post_id <= 0 || !in_array($reaction, $allowed, true)) { echo json_encode(['status'=>'error','message'=>'invalid input']); exit; }
      $exists = getSingleValue('mahamudfutureit_article', 'PostId', $post_id, 'PostId');
      if ($exists == '') { echo json_encode(['status'=>'error','message'=>'post not found']); exit; }
      $ua = substr($_SERVER['HTTP_USER_AGENT'] ?? '', 0, 250);
      $user_key = substr(hash('sha256', $userIP.'|'.$ua), 0, 64);
      $check = $conn->prepare("SELECT ReactionID, ReactionType FROM mahamudfutureit_reactions WHERE PostId = ? AND UserKey = ? LIMIT 1");
      $check->execute([$post_id, $user_key]);
      $existing = $check->fetch(PDO::FETCH_ASSOC);
      $selected = $reaction;
      if ($existing) {
          if ($existing['ReactionType'] === $reaction) {
              $conn->prepare("DELETE FROM mahamudfutureit_reactions WHERE ReactionID = ?")->execute([$existing['ReactionID']]);
              $selected = null;
          } else {
              $conn->prepare("UPDATE mahamudfutureit_reactions SET ReactionType = ?, CreatedAt = NOW() WHERE ReactionID = ?")->execute([$reaction, $existing['ReactionID']]);
          }
      } else {
          $conn->prepare("INSERT INTO mahamudfutureit_reactions (PostId, ReactionType, UserKey, UserIP, UserAgent) VALUES (?,?,?,?,?)")->execute([$post_id, $reaction, $user_key, $userIP, $ua]);
      }
      $cstmt = $conn->prepare("SELECT ReactionType, COUNT(*) c FROM mahamudfutureit_reactions WHERE PostId = ? GROUP BY ReactionType");
      $cstmt->execute([$post_id]);
      $raw = $cstmt->fetchAll(PDO::FETCH_KEY_PAIR);
      $counts = [
        'like' => (int)($raw['like'] ?? 0),
        'love' => (int)($raw['love'] ?? 0),
        'haha' => (int)($raw['haha'] ?? 0),
        'wow'  => (int)($raw['wow']  ?? 0),
        'sad'  => (int)($raw['sad']  ?? 0),
      ];
      echo json_encode(['status'=>'ok','selected'=>$selected,'counts'=>$counts]);
      exit;

  /* =================  STAR RATING  ================= */
  }elseif ($section_1 == 'rate') {
      header('Content-Type: application/json; charset=utf-8');
      $post_id = isset($_POST['post_id']) ? (int) $_POST['post_id'] : 0;
      $stars   = isset($_POST['stars'])   ? (int) $_POST['stars']   : 0;
      if ($post_id <= 0 || $stars < 1 || $stars > 5) { echo json_encode(['status'=>'error','message'=>'invalid input']); exit; }
      $exists = getSingleValue('mahamudfutureit_article', 'PostId', $post_id, 'PostId');
      if ($exists == '') { echo json_encode(['status'=>'error','message'=>'post not found']); exit; }
      $ua = substr($_SERVER['HTTP_USER_AGENT'] ?? '', 0, 250);
      $user_key = substr(hash('sha256', $userIP.'|'.$ua), 0, 64);
      $check = $conn->prepare("SELECT RatingID FROM mahamudfutureit_ratings WHERE PostId = ? AND UserKey = ? LIMIT 1");
      $check->execute([$post_id, $user_key]);
      $row = $check->fetch(PDO::FETCH_ASSOC);
      if ($row) {
          $conn->prepare("UPDATE mahamudfutureit_ratings SET Stars = ?, CreatedAt = NOW() WHERE RatingID = ?")->execute([$stars, $row['RatingID']]);
      } else {
          $conn->prepare("INSERT INTO mahamudfutureit_ratings (PostId, Stars, UserKey, UserIP, UserAgent) VALUES (?,?,?,?,?)")->execute([$post_id, $stars, $user_key, $userIP, $ua]);
      }
      $stat = $conn->prepare("SELECT COUNT(*) c, ROUND(AVG(Stars),1) a FROM mahamudfutureit_ratings WHERE PostId = ?");
      $stat->execute([$post_id]);
      $s = $stat->fetch(PDO::FETCH_ASSOC) ?: ['c'=>0,'a'=>0];
      echo json_encode(['status'=>'ok','votes'=>(int)$s['c'],'avg'=>(float)$s['a'],'mine'=>$stars]);
      exit;

  }elseif($section_1 == 'load_more'){
    $cat_id = $_POST['mahamudfutureit_identity'];
    $page   = $_POST['page'];

    if($page !='' && $cat_id!=''){

      $article_get = getArticles($cat_id, 1, $post_offset = $page);
      $the_post = $article_get[0];


      if(!empty($the_post)){
        // Count View
        UpdateData('mahamudfutureit_article', 'PostId', $the_post['PostId'],   array("ViewCount"=> $the_post['ViewCount'] + 1));


        // Post Writer Information
        if ($the_post['PostWriterName'] !='') {
          $post_writer = $the_post['PostWriterName'];
          $post_writer_profile = "#";
          $post_writer_avater = $advancenews['PostWriterDefaultImage'];
          $post_writer_designation = $the_post['PostWriterDesignation'];
        }else{
          $post_writer = getSingleValue('tt_accounts', 'UserID',  $the_post['PostAuthor'], 'UserName');
          $post_writer_profile = userProfile($the_post['PostAuthor']);
          $post_writer_avater = userAvater($the_post['PostAuthor']);
          $post_writer_designation = getSingleValue('tt_accounts', 'UserID',  $the_post['PostAuthor'], 'UserDesignation');
        }

      ?>
        <div class="auto_load_next py-5 border-top">

          <?php
          $post_ads_1 = showAds($advancenews['PostAds1'], $advancenews['PostAdsCtl']);
          if ($post_ads_1 != '') {
            ?>
            <div class="  mb-3">
              <?php echo $post_ads_1 ?>
            </div>
            <?php
          }
          ?>
          <div class="mb-4">
            <a href="<?php echo articleCategory($the_post['PostId'],'url') ?>">
              <h4 class="m-0 p_post_cat2 d-inline-block  "><?php echo articleCategory($the_post['PostId'],'name') ?></h4>
            </a>
          </div>
          <?php
          $post_ads_2 = showAds($advancenews['PostAds2'], $advancenews['PostAdsCtl']);
          if ($post_ads_2 != '') {
            ?>
            <div class="tt-white my-3">
              <?php echo $post_ads_2 ?>
            </div>
            <?php
          }
          ?>
          <div class="row g-4 ">
            <div class="col-lg-8">
              <div class="">
                <h1 class="m-0 mb-4 h1 fw-bold tt-text" style="font-size:var(--tt_post_title_fs)"><?php echo htmlentities($the_post['PostTitle'], ENT_QUOTES, 'utf-8'); ?></h1>
                <hr class="d-block d-lg-none">
                <div class="row g-4 align-items-center">
                  <div class="col-12 col-md-6">
                    <div class="d-flex align-items-center">
                      <a href="<?php echo $post_writer_profile ?>">
                        <img class="rounded-circle" height="50px" width="auto" src="<?php echo $post_writer_avater ?>" alt="<?php echo htmlentities($post_writer, ENT_QUOTES, 'utf-8'); ?>">
                      </a>
                      <div class="ms-2">
                        <a href="<?php echo $post_writer_profile ?>" class="tt-text">
                          <h5 class="m-0 h5 fw-bold"><?php echo htmlentities($post_writer, ENT_QUOTES, 'utf-8'); ?>
                            <?php
                            if ($post_writer_designation !='') {
                              ?>
                              <small class="opacity-75 fw-normal"><?php echo $post_writer_designation ?></small>
                              <?php
                            }
                            ?>
                          </h5>
                        </a>
                        <span class="tt-text">প্রকাশ : <?php echo dokanDate($the_post['PostDatePublic']) ?></span>
                        <?php
                        if ($advancenews['NewsPrintView'] == 'on') {
                          ?>
                          <span ><small>|</small> <a  style="color:var(--tt_primary_color)" href="<?php echo $site_domain ?>print-post/<?php echo $the_post['PostId'] ?>/">প্রিন্ট সংস্করণ </a></span>
                          <?php
                        }
                        if($c_user !='' || $advancenews['PhotoCardPublic'] == 'on'){
                          ?>
                          <span ><small>|</small> <a  style="color:var(--tt_primary_color)" href="<?php echo $site_domain ?>mahamudfutureit-photocard/<?php echo $the_post['PostId'] ?>/"> ফটো কার্ড  </a></span>
                          <?php
                        }
                        ?>
                      </div>
                    </div>
                  </div>
                  <!-- share -->
                  <div class="col-12 col-md-6">
                    <div class="myshare-mahamudfutureit mb-3 mb-md-0">
                      <div class="sharethis-inline-share-buttons text-start text-lg-end"   data-title="<?php echo $the_post['PostTitle'] ?>" data-url="<?php echo articleLink($the_post['PostId']) ?>"></div>
                    </div>
                  </div>
                </div>
                <hr class="d-none d-lg-block">
                <?php
                if ($the_post['PageContentType'] == 'video') {
                  $videoLink = $the_post['PostVideoLink'];
                  function getVideoId($url) {
                      // Check if the URL matches the short format (youtu.be)
                      if (preg_match('/youtu\.be\/([a-zA-Z0-9_-]+)/', $url, $matches)) {
                          return $matches[1]; // Return the video ID
                      }
                      // For the standard YouTube URL format
                      parse_str(parse_url($url, PHP_URL_QUERY), $query);
                      return $query['v'] ?? null; // Return the video ID or null if not found
                  }
                  $videoId = getVideoId($videoLink);
                  $embedUrl = "https://www.youtube.com/embed/$videoId";
                  ?>
                      <div class="video-container rounded-top">
                          <iframe src="<?php echo $embedUrl; ?>" frameborder="0" allowfullscreen></iframe>
                      </div>
                  <?php
                }else {
                  ?>
                  <div class="story-image popup-gallery">
                    <a  title="<?php echo htmlentities($the_post['PostTitle'], ENT_QUOTES, 'utf-8'); ?>" href="<?php echo articleImg($the_post['PostId'], 'featured') ?>">
                      <img  class="w-100" src="<?php echo articleImg($the_post['PostId'], 'featured') ?>" alt="<?php echo htmlentities($the_post['PostTitle'], ENT_QUOTES, 'utf-8'); ?>">
                    </a>
                    <?php
                    $post_ads_3 = showAds($advancenews['PostAds3'], $advancenews['PostAdsCtl']);
                    if ($post_ads_3 != '') {
                      ?>
                      <div class="story-image-ads">
                        <?php echo $post_ads_3 ?>
                      </div>
                      <?php
                    }
                    ?>
                  </div>
                  <?php
                  if ($the_post['PostImageCaption'] !='') {
                    ?>
                    <div class="p-2 py-1 text-center" style="background:#e9f5f5">
                      <h5 class="m-0 news-title-4"><?php echo $the_post['PostImageCaption'] ?></h5>
                    </div>
                    <?php
                  }
                }
                ?>
                <div class="story-details tt-text">
                  <?php
                  echo articleContent($the_post['PostContent']);
                  ?>
                </div>
                <?php
                if ($the_post['PostAttachment'] !='') {
                  ?>
                  <div class="my-2">
                    <?php renderMediaByType($the_post['PostAttachment']); ?>
                  </div>
                  <?php
                }
                $post_topic = json_decode($the_post['PostTopic'], TRUE);
                if (!empty($post_topic)) {
                  ?>
                  <div class="mt-2 post-tipics">
                    <p class="m-0"> <strong>বিষয় :</strong>
                      <?php
                        foreach ($post_topic as $the_ptopic) {
                          ?>
                          <a class="border" href="<?php echo getTopicsUrl($the_ptopic) ?>"><?php echo $the_ptopic ?></a>
                          <?php
                        }
                      ?>
                    </p>
                  </div>
                  <?php
                }
                ?>
              </div>
              <?php
              $post_ads_5 = showAds($advancenews['PostAds5'], $advancenews['PostAdsCtl']);
              if ($post_ads_5 != '') {
                ?>
                <div class="tt-white my-3">
                  <?php echo $post_ads_5 ?>
                </div>
                <?php
              }
              ?>
              <div class="tt-white fb-comment  mt-3 ">
                <?php
                if ($advancenews['PostFbCommentTitle'] !='') {
                  ?>
                  <h2 class="m-0 p-md-3 p-2"> <?php echo $advancenews['PostFbCommentTitle'] ?></h2>
                  <?php
                }
                 ?>
                <div class="p-md-3 p-2 border">
                  <?php
                  if ($advancenews['PostFbComment'] == 'on' && $advancenews['PostFbCommentAppID'] !='') {
                    ?>
                    <div class="">
                      <div id="fb-root"></div>
                      <script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v22.0&appId=<?php echo $advancenews['PostFbCommentAppID'] ?>"></script>
                      <div class="fb-comments" data-width="100%" data-href="<?php echo articleLink($the_post['PostId']) ?>" data-numposts="5"></div>
                    </div>
                    <?php
                  }
                  ?>
                </div>
              </div>
            </div>
            <div class="col-lg-4 d-lg-block d-none">
              <?php
              $post_ads_6 = showAds($advancenews['PostAds6'], $advancenews['PostAdsCtl']);
              if ($post_ads_6 != '') {
                ?>
                <div class="tt-white mb-3">
                  <?php echo $post_ads_6 ?>
                </div>
                <?php
              }
              ?>
              <div class="sidebar">
                <div class="related-post mb-1">
                  <?php
                    $ps_block_this_post = $the_post['PostCategory'];
                  ?>
                  <div class="row g-2">
                    <div class="col-12">
                      <?php
                        echo getBlockTitle($ps_block_this_post);
                      ?>
                    </div>
                    <?php
                    $ps_block_this_post_article = getArticles($ps_block_this_post, 8);
                    $ps_block_this_post_count = 1;
                    if (!empty($ps_block_this_post_article)) {
                      foreach ($ps_block_this_post_article as $this_post_the_ps) {
                        if ($ps_block_this_post_count == 1) {
                          ?>
                          <div class="col-12">
                            <div class="ps-category-lead-news position-relative rounded-2">
                              <a href="<?php echo articleLink($this_post_the_ps['PostId']) ?>">
                                <div class="ps-clead-img">
                                  <img class="w-100" src="<?php echo articleImg($this_post_the_ps['PostId']) ?>" alt="<?php echo $this_post_the_ps['PostTitle'] ?>">
                                </div>
                                <div class="ps-clead-title">
                                  <h1 class="p-2 news-title-3 m-0 text-white"><?php echo $this_post_the_ps['PostTitle'] ?></h1>
                                </div>
                              </a>
                            </div>
                          </div>
                          <?php
                          $post_ads_7 = showAds($advancenews['PostAds7'], $advancenews['PostAdsCtl']);
                          if ($post_ads_7 != '') {
                            ?>
                            <div class="col-12">
                              <?php echo $post_ads_7 ?>
                            </div>
                            <?php
                          }
                          ?>
                          <?php
                        }else {
                          ?>
                          <div class="col-12">
                            <a href="<?php echo articleLink($this_post_the_ps['PostId']) ?>" class="d-block  prothom-songbad-post-style  py-1">
                              <div class="row g-2 align-items-center">
                                  <div class="col-5">
                                    <div class="position-relative overflow-hidden rounded-2">
                                      <img class="w-100 ps-post-line-img" src="<?php echo articleImg($this_post_the_ps['PostId']) ?>" alt="<?php echo $this_post_the_ps['PostTitle'] ?>">
                                    </div>
                                  </div>
                                  <div class="col-7">
                                    <h1 class="news-title-4 m-0 tt-text"><?php echo $this_post_the_ps['PostTitle'] ?></h1>
                                  </div>
                              </div>
                            </a>
                          </div>
                          <?php
                        }
                        $ps_block_this_post_count++;
                      }
                    }else {
                      ?>
                      <div class="col-12">
                        <?php echo  noPost(); ?>
                      </div>
                      <?php
                    }
                    ?>
                  </div>
                </div>

                <?php
                $post_ads_8 = showAds($advancenews['PostAds8'], $advancenews['PostAdsCtl']);
                if ($post_ads_8 != '') {
                  ?>
                  <div class="my-3">
                    <?php echo $post_ads_8 ?>
                  </div>
                  <?php
                }
                ?>
              </div>
            </div>
          </div>
          <?php
          $post_ads_10 = showAds($advancenews['PostAds10'], $advancenews['PostAdsCtl']);
          if ($post_ads_10 != '') {
            ?>
            <div class="  my-3">
              <?php echo $post_ads_10 ?>
            </div>
            <?php
          }
          ?>
          <div class="load_next"
                    data-page-title="<?php echo htmlentities($the_post['PostTitle'], ENT_QUOTES, 'utf-8'); ?>"
                    data-page-url="<?php echo articleLink($the_post['PostId']) ?>"></div>
          </div>
      </div>
      <?php
      }

    }
  }
  else {
    exit;
  }
 ?>
