<?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 Life at Webpulse Photos";
$record_type = "Life at Webpulse Photos";
$script_name = "addeditwebpulselife.php";
$redirect_script_name ="manage-webpulse-life.php";
$table_name = "life_at_webpulse_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/about-webpulse");

           //Thumb Cut
      $width="311";
      $height="208";

      $source_folder=SITE_DIR_PATH."/uploaded-files/about-webpulse/";
      $destination_folder=SITE_DIR_PATH."/uploaded-files/about-webpulse/thumbs/";
      saveResizedPhoto($img1, $width, $height, $source_folder, $destination_folder);
      }
      $bannerUrl = $bannerUrl;

      $maxRes=db_fetch_array(db_query("select max(bannerorder) from $table_name"));
			$bannerorder=$maxRes[0]+1;

      $sql = db_query("insert into $table_name set personName='$personName', bannerImage='$img1',alt='$alt',title='$title', bannerorder='$bannerorder', 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 (!strlen($form_check_alert) > 0) {
    if ($_FILES['banner']['name'] != "") {
      $res1 = getResult($table_name, " where slno='$sl'");
      $filenm1 = SITE_DIR_PATH . "/uploaded-files/about-webpulse/" . $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/about-webpulse");

      $width="311";
      $height="208";

      $source_folder=SITE_DIR_PATH."/uploaded-files/about-webpulse/";
      $destination_folder=SITE_DIR_PATH."/uploaded-files/about-webpulse/thumbs/";
      saveResizedPhoto($img1, $width, $height, $source_folder, $destination_folder);
      
      updateTable($table_name, " bannerImage='$img1' where slno='$slno'");
    }

    $bannerUrl = $bannerUrl;
    updateTable($table_name, " personName='$personName',alt='$alt',title='$title', recvDate=now() where slno='$slno'");
    // $_SESSION['session_message'] = $record_type . ' has been updated successfully';
    header("Location: " . SITE_ADMIN_URL . "/" . $redirect_script_name . "?start=" . $start . "&pagesize=" . $pagesize);
    exit;
  } else {

    $msg = html_head();
    $msg .= $form_check_alert;
    $msg .= html_foot();
  }
}
########################################################
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>Image Title *</label>
                                <div id="the-basics">
                                    <input class="typeahead" type="text" name="personName"
                                        value="<?=$res['personName']?>" placeholder="Image Title" required>
                                </div>
                            </div>

                        </div>


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

                        <div class="form-group row">
             <div class="col">
                <label>Alt</label>
           
                <input class="typeahead" type="text"  name="alt" value="<?=$res['alt']?>" placeholder="Image ALT">
      
             </div>
             <div class="col">
                <label>Title</label>
             
                <input class="typeahead" type="text"  name="title" value="<?=$res['title']?>" placeholder="Image Title">
        </div>
        </div>
                        <?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;
?>