<?php
require_once('config.php');

// Delete
if (isset($_GET['delid'])) {
    $id_for_delete = $_GET['delid'] ;
    if (deleteData('mahamudfutureit_photocard', 'ID', $id_for_delete)){
       header("Location:photocard.php");
      exit;
    }else {
      $tt_alert = ttAlert('danger', 'Something went wrong to delete !');
    }
}

if (isset($_GET['active'])) {
  $tt_all_cd = showAllttdata("mahamudfutureit_photocard");
    foreach ($tt_all_cd as $the_cardss) {
      $post_form_data = array(
        "Status"             => NULL,
      );
      UpdateData('mahamudfutureit_photocard', 'ID', $the_cardss['ID'] , $post_form_data);
    }

    $id_for_active = $_GET['active'] ;
    $post_form_data = array(
      "Status"             => 'Active',
    );
    if (UpdateData('mahamudfutureit_photocard', 'ID', $id_for_active , $post_form_data)){
      $tt_alert = ttAlert('success', 'Activated Template !');
    }else {
      $tt_alert = ttAlert('danger', 'Something went wrong to Active !');
    }
}

// Include Heaser File
require_once('inc/header.php');
?>
<style>
  @import url('https://fonts.googleapis.com/css2?family=Noto+Serif+Bengali:wght@100..900&display=swap');
  .photocard_shortcode{
    user-select: all;
  }
  #MahamudFutureITPhotocard {
      width: 360px;
      min-height: 360px;
      font-family: "Noto Serif Bengali", serif;
      position: relative;
      background-color: white;
      overflow: hidden;
  }
  #mahamudfutureitCtrl{
      width: 360px;
      margin: auto;
  }
</style>
<main id="main" class="main ">
  <div class="row">
    <div class="col-6 align-items-center">
      <div class="pagetitle mb-0">
        <h1><?php echo $page_title ?></h1>
      </div>
    </div>
    <div class="col-6 text-end">
        <a href="photocard-new.php" class="btn btn-primary btn-sm"><i class="bi bi-node-plus"></i> New Photocard </a>
    </div>
  </div>
  <?php echo $tt_alert ?? ''; ?>
  <section class="section mt-4">
    <div class="row justify-content-center">

          <?php
          $tt_all_cd = showAllttdata("mahamudfutureit_photocard");
          foreach ($tt_all_cd as $the_pc) {
            // Define replacement values
            $shortcode_map = [
                '[MFIT-PHOTO]'    => 'assets/img/placeholder/mahamudfutureit-photocard-featured.jpg',
                '[MFIT-TITLE]'    => 'আধুনিক নিউজ পোর্টাল ও উন্নত মানের ফিচারে অঙ্গীকারবদ্ধ ডেভেলপার টিম',
                '[MFIT-DATE]'     => '১ সেপ্টেম্বর ২০২৪',
                '[MFIT-CATEGORY]' => 'প্রযুক্তি',
                '[MFIT-QRIMG]'    => 'assets/img/placeholder/qr.png',
                '[MFIT-DOMAIN]'    =>'WWW.mf1t.com',
            ];
            $photocard_html_final = str_replace(array_keys($shortcode_map), array_values($shortcode_map), $the_pc['PC_HTML']);
            $photocard_css_final = str_replace(array_keys($shortcode_map), array_values($shortcode_map), $the_pc['PC_CSS']);
            ?>
            <div class="col-lg-4">
              <style id="MahamudFutureITPhotocardCSS"><?php echo $photocard_css_final ?></style>
              <div id="MahamudFutureITPhotocard" class="m-auto mahamudfutureit-photo-card border"><?php echo $photocard_html_final ?></div>
              <div id="mahamudfutureitCtrl" class="p-2 border">
                  <div class="row g-2">
                    <div class="col-5">
                      <a href="photocard-edit.php?photocard_id=<?php echo $the_pc['ID'] ?>" class="btn btn-outline-primary btn-sm  d-block"><i class="bi bi-pencil-square"></i> Customize </a>
                    </div>
                    <div class="col-2">
                      <a  data-href="photocard.php?delid=<?php echo $the_pc['ID'] ?>" class="btn btn-danger btn-sm  d-block actiion_to_delete"><i class="bi bi-trash"></i></a>
                    </div>
                    <div class="col-5 text-end">
                      <?php
                      if ($the_pc['Status'] == 'Active') {
                        ?>
                        <a href="javascript:void(0);" class="btn btn-success btn-sm  d-block">Active</a>

                        <?php
                      }else {
                        ?>
                        <a href="photocard.php?active=<?php echo $the_pc['ID'] ?>" class="btn  btn-secondary  btn-sm  d-block">Active</a>
                        <?php
                      }
                      ?>
                    </div>
                  </div>
              </div>
            </div>
            <?php
          }
          ?>
    </div>
  </section>
</main>
<script>
  document.addEventListener("click", function(event) {
    // Check if the clicked element or its parent has the class 'actiion_to_delete'
    const target = event.target.closest(".actiion_to_delete");

    if (target) {
      event.preventDefault(); // Prevent default link behavior
      const url = target.getAttribute("data-href");

      if (confirm("Are you sure you want to delete this item?")) {
        window.location.href = url;
      }
    }
  });
</script>
<?php require_once('inc/footer.php'); ?>
