<?php
include_once("../include/main.inc.php");
check_session('MyCpanel', 'index.php');
?>
<script type="text/javascript">
  function chk_valid_date() {
    if (document.bannerFrm.validFrom.value == 0) {
      alert("Please select Validity Period");
      return false;
    }
    if (document.bannerFrm.validTo.value == 0) {
      alert("Please select Validity Period");
      return false;
    }
  }
</script>
<?php
################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start) < 0 or $start == "") ? 0 : $start;
$pagesize = intval($pagesize) == 0 ? $pagesize = 20 : $pagesize;
$res = getResult("admin_details", " where user_type='admin'");

$heading = "Manage Banners";
$record_type = "Banner";
$script_name = "addeditbanner.php";
$redirect_script_name ="manage-banners.php";
$table_name = "banner_tbl";
####################################################


if ($action == "add_record" && $_FILES['banner']['name'] != "") {
  // $count_cat = mysqli_query($GLOBALS['dbconn'], "SELECT * FROM $table_name");
  // if (mysqli_num_rows($count_cat) >= $res['banner_count']) {
  //   $_SESSION['session_message'] = ' <div class="btn btn-danger"> ' . $record_type . ' count exceeds the allowed limit [' . $res[banner_count] . ']  </div>';
  // } else {
    if ($_FILES['banner']['name'] != "") {
      $file_extension = strtolower(getExtension($_FILES['banner']['name']));
      if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png" && $file_extension != "webp") {
        $form_check_alert .= "<li>Please upload small image(.jpg,.gif,.png,.webp Format Only)</li>";
      }
    }
      if ($_FILES['banner']['name'] != "") {
        $img_name = "$personName";
        $img_name = str_replace(" ", "-", $img_name);
        $img1 = upload_file($img_name, "banner", "uploaded-files/banner-image");

        $width="526";
        $height="542";
  
        $source_folder=SITE_DIR_PATH."/uploaded-files/banner-image";
        $destination_folder=SITE_DIR_PATH."/uploaded-files/banner-image/thumbs/";
        saveResizedPhoto($img1, $width, $height, $source_folder, $destination_folder);
      }
      //mobile banner
      if ($_FILES['mobilebanner']['name'] != "") {
        $file_extension = strtolower(getExtension($_FILES['mobilebanner']['name']));
        if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png" && $file_extension != "webp") {
          $form_check_alert .= "<li>Please upload small image(.jpg,.gif,.png,.webp Format Only)</li>";
        }
      }
        if ($_FILES['mobilebanner']['name'] != "") {
          $img_name1 = "$personName";
          $img_name1 = str_replace(" ", "-", $img_name);
          $img_name1=$img_name1.'-mobilebanner-'.date('his');
          $img2 = upload_file($img_name1, "mobilebanner", "uploaded-files/banner-image");
  
          $width="526";
          $height="542";
    
          $source_folder=SITE_DIR_PATH."/uploaded-files/banner-image";
          $destination_folder=SITE_DIR_PATH."/uploaded-files/banner-image/thumbs/";
          saveResizedPhoto($img2, $width, $height, $source_folder, $destination_folder);
        }
      $bannerUrl = $bannerUrl;

      $personName =  htmlentitiesDecode($personName);
      $tagline =  htmlentitiesDecode($tagline);
      $description =  htmlentitiesDecode($description);
      $marqueeContent =  htmlentitiesDecode($marqueeContent);

      $sql = db_query("insert into $table_name set personName='$personName', bannerUrl='$bannerUrl',bannerImage='$img1', tagline='$tagline', description='$description',marqueeContent='$marqueeContent', btn1='$btn1',bl1='$bl1',btn2='$btn2',bl2='$bl2',alt='$alt',title='$title',mobilebanner='$img2', recvDate=now()");
      $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been added successfully  </div>';

      header("Location: " . SITE_ADMIN_URL . "/" . $redirect_script_name);
      exit;
  
}
################ Update Record ##################

if ($action == "update_record") {
  if ($_FILES['banner']['name'] != "") {
    $file_extension = strtolower(getExtension($_FILES['banner']['name']));
    if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png" && $file_extension != "webp") {
      $form_check_alert .= "<li>Please upload small image(.jpg,.gif,.png,.webp Format Only)</li>";
    }
  }

    if ($_FILES['banner']['name'] != "") {
      $res1 = getResult($table_name, " where slno='$sl'");
      $filenm1 = SITE_DIR_PATH . "/uploaded-files/banner-image/" . $res1['bannerImage'];
      if (file_exists($filenm1)) {
        unlink($filenm1);
      }

      $img_name = "$personName";
        $img_name = str_replace(" ", "-", $img_name);
      $img1 = upload_file($img_name, "banner", "uploaded-files/banner-image");

      $width="526";
      $height="542";

      $source_folder=SITE_DIR_PATH."/uploaded-files/banner-image";
      $destination_folder=SITE_DIR_PATH."/uploaded-files/banner-image/thumbs/";
      saveResizedPhoto($img1, $width, $height, $source_folder, $destination_folder);


      updateTable($table_name, " bannerImage='$img1' where slno='$slno'");
    }

    //mobile banner update 
    if ($_FILES['mobilebanner']['name'] != "") {
      $file_extension = strtolower(getExtension($_FILES['mobilebanner']['name']));
      if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png" && $file_extension != "webp") {
        $form_check_alert .= "<li>Please upload small image(.jpg,.gif,.png,.webp Format Only)</li>";
      }
    }
  
      if ($_FILES['mobilebanner']['name'] != "") {
        $res1 = getResult($table_name, " where slno='$sl'");
        $filenm2 = SITE_DIR_PATH . "/uploaded-files/banner-image/" . $res1['mobilebanner'];
        if (file_exists($filenm2)) {
          unlink($filenm2);
        }
  
        $img_name1 = "$personName";
          $img_name1 = str_replace(" ", "-", $img_name);
          $img_name1=$img_name1.'-mobilebanner-'.date('his');
          $img2 = upload_file($img_name1, "mobilebanner", "uploaded-files/banner-image");
  
  
        $width="526";
        $height="542";
  
        $source_folder=SITE_DIR_PATH."/uploaded-files/banner-image";
        $destination_folder=SITE_DIR_PATH."/uploaded-files/banner-image/thumbs/";
        saveResizedPhoto($img2, $width, $height, $source_folder, $destination_folder);
  
  
        updateTable($table_name, "mobilebanner='$img2' where slno='$slno'");
      }

    $bannerUrl = $bannerUrl;

    $personName =  htmlentitiesDecode($personName);
    $tagline =  htmlentitiesDecode($tagline);
    $description =  htmlentitiesDecode($description);
    $marqueeContent =  htmlentitiesDecode($marqueeContent);

    updateTable($table_name, " personName='$personName', bannerUrl='$bannerUrl', tagline='$tagline', description='$description',marqueeContent='$marqueeContent',btn1='$btn1',bl1='$bl1',btn2='$btn2',bl2='$bl2',alt='$alt',title='$title', recvDate=now() where slno='$slno'");
    $_SESSION['session_message'] = $record_type . ' has been updated successfully';
    // header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?slno=" . $slno);
    header("Location: " . SITE_ADMIN_URL . "/" . $redirect_script_name);
    exit;
  
}
########################################################
admin_header();
$res = getResult($table_name, " where slno='$slno'");
?>
           <div class="content-wrapper">
          <div class="page-header">
            <h3 class="page-title">
			<?=$heading;?>
      <?=$_SESSION['session_message'];?>
            </h3>
            <nav aria-label="breadcrumb">
              <ol class="breadcrumb">
			  <li class="nav-item d-none d-lg-flex">
            <a class="nav-link" href="<?=$redirect_script_name?>">
              <span class="btn btn-primary">Go Back</span>
            </a>
          </li>
              </ol>
            </nav>
          </div>
          <div class="row">
          
            <div class="col-12 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
                  <form class="forms-sample" method="POST" action=""  enctype="multipart/form-data">

                  <div class="form-group row">
                    <div class="col">
                      <label>Banner Title *</label>
                      <div id="the-basics">
                        <input class="typeahead" type="text" name="personName" value="<?=$res['personName']?>" placeholder="Banner Title" required>
                      </div>
                    </div>
</div>
<div class="form-group row">
                    <div class="col">
                      <label>Banner Tagline</label>
                      <div id="bloodhound">
                        <input class="typeahead" type="text"  name="tagline" value="<?=$res['tagline']?>" placeholder="Banner tagline">
                      </div>
                    </div>
                  </div>

                  <!-- <div class="form-group row">
<div class="col">
    <label>Marquee Content</label>
    <div id="bloodhound">
      <input class="typeahead" type="text" name="marqueeContent" value="<?=$res['marqueeContent']?>" placeholder="Marquee Content">
    </div>
  </div>
</div>

                  <div class="form-group row">             
<div class="col">
    <label>Banner Tagline 2</label>
    <div id="the-basics">
    <input class="typeahead" type="text" name="description" value="<?=$res['description']?>" placeholder="Banner Tagline 2">
    </div>
  </div> 

 
 
</div> -->



                  <!-- <div class="form-group row">
                      <div class="col">
                          <label>Button Name 1</label>
                          <div id="the-basics">
                          <input class="typeahead" type="text" name="btn1" value="<?=$res['btn1']?>" placeholder="Button Name 1">
                          </div>
                        </div> 

                        <div class="col">
                          <label>Button Link 1</label>
                          <div id="bloodhound">
                            <input class="typeahead" type="text" name="bl1" value="<?=$res['bl1']?>" placeholder="Button Link1">
                          </div>
                        </div>
                   
                  </div> -->

                  <!-- <div class="form-group row">
                      <div class="col">
                          <label>Button Name 2</label>
                          <div id="the-basics">
                          <input class="typeahead" type="text" name="btn2" value="<?=$res['btn2']?>" placeholder="Button Name 2">
                          </div>
                        </div> 

                        <div class="col">
                          <label>Button Link2</label>
                          <div id="bloodhound">
                            <input class="typeahead" type="text" name="bl2" value="<?=$res['bl2']?>" placeholder="Button Link2">
                          </div>
                        </div>
                   
                  </div> -->

                 
                  <?php
          if ($res['slno'] == "") { ?>
                  <div class="form-group">
                     <label for="exampleTextarea1">Banner Image * (700 X 721 Px)</label>
                      <input type="file" class="dropify" name="banner" required>
                  </div>
          <?php } else{ ?>
            <div class="form-group row">
              <div class="col">
                  <label>Banner Image (700 X 721 Px)</label>
                      <input type="file" class="dropify" name="banner">
             </div>
            <div class="col">
                <img src="<?=site_url."/uploaded-files/banner-image/".$res['bannerImage']?>" alt="image" style="width: 60%;">
            </div>
          </div>
       <?php   } ?>


       <div class="form-group row">
                  <div class="col">
                      <label>Image Alt</label>
                      <div id="the-basics">
                      <input class="typeahead" type="text" name="alt" value="<?=$res['alt']?>" placeholder="Alt">
                      </div>
                    </div> 

                    <div class="col">
                      <label>Image Title</label>
                      <div id="bloodhound">
                        <input class="typeahead" type="text" name="title" value="<?=$res['title']?>" placeholder="Title">
                      </div>
                    </div>
                   
                  </div>

                  <!-- mobile banner  -->
                  <?php
          if ($res['mobilebanner'] == "") { ?>
                  <div class="form-group">
                     <label for="exampleTextarea1">Mobile Banner Image </label>
                      <input type="file" class="dropify" name="mobilebanner" required>
                  </div>
          <?php } else{ ?>
            <div class="form-group row">
              <div class="col">
                  <label>Mobile Banner Image </label>
                      <input type="file" class="dropify" name="mobilebanner">
             </div>
            <div class="col">
                <img src="<?=site_url."/uploaded-files/banner-image/".$res['mobilebanner']?>" alt="image" style="width: 60%;">
            </div>
          </div>
       <?php   } ?>


       
                  <?php
          if ($res['slno'] == "") {
            ?>
            <button type="submit" class="btn btn-primary mr-2" name="add_record">Submit</button>
            <input type="hidden" name="action" value="add_record">
    <?php
          } else {
            ?>
              <button type="submit" class="btn btn-primary mr-2" name="update_record">Update</button>
              <input type="hidden" name="action" value="update_record">
    <?php
          }
          ?>
          <button class="btn btn-light" onclick="javascript: window.location.href = '<?=SITE_ADMIN_URL;?>/<?= $redirect_script_name; ?>?parentID=<?= $parentID; ?>&catID=<?= $catID; ?>'">Cancel</button>
                 
                  </form>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- content-wrapper ends -->
<?php
admin_footer();
exit;
?>

