<?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 = "manage_banners.php";
$table_name = "banner_tbl";
####################################################

if ($id == "add_record" && $_FILES['banner']['name'] != "") {
  $count_cat = mysqli_query($GLOBALS['dbcon'], "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") {
        $form_check_alert .= "<li>Please upload small image(.jpg,.gif,.png Format Only)</li>";
      }
    }
    if (!strlen($form_check_alert) > 0) {
      if ($_FILES['banner']['name'] != "") {
        $img_name = "$personName" . date("s");
        $img_name = str_replace(" ", "-", $img_name);
        $img1 = upload_file($img_name, "banner", "uploaded-files/banner-image");
      }
      $bannerUrl = "" . $bannerUrl;
      $sql = db_query("insert into $table_name set personName='$personName', bannerUrl='$bannerUrl', bannerImage='$img1', bannerCat='$cat', bannerSubcat='$subcat', status='Y', recvDate=now()");
      $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been added successfully  </div>';

      header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&pagesize=" . $pagesize);
      exit;
    } else {
      $msg = html_head();
      $msg .= $form_check_alert;
      $msg .= html_foot();
    }
  }
}

if ($id == "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") {
      $form_check_alert .= "<li>Please upload small image(.jpg,.gif,.png Format Only)</li>";
    }
  }
  if (!strlen($form_check_alert) > 0) {
    if ($_FILES['banner']['name'] != "") {
      $res1 = getResult($table_name, " where slno='$recordID'");
      $filenm1 = SITE_DIR_PATH . "/uploaded-files/banner-image/" . $res1['bannerImage'];
      if (file_exists($filenm1)) {
        unlink($filenm1);
      }

      $img_name = "$personName" . date("s");
      $img_name = str_replace(" ", "-", $img_name);
      $img1 = upload_file($img_name, "banner", "uploaded-files/banner-image");
      updateTable($table_name, " bannerImage='$img1' where slno='$recordID'");
    }

    $bannerUrl = $bannerUrl;
    updateTable($table_name, " personName='$personName', bannerUrl='$bannerUrl', bannerCat='$cat', bannerSubcat='$subcat' where slno='$recordID'");
    $_SESSION['session_message'] = $record_type . ' has been updated successfully';
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&pagesize=" . $pagesize);
    exit;
  } else {

    $msg = html_head();
    $msg .= $form_check_alert;
    $msg .= html_foot();
  }
}

if ($id == "delete_record") {
  db_query("delete from $table_name where slno='$recordID'");
  $_SESSION['session_message'] = $record_type . ' has been deleted successfully';
  header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&pagesize=" . $pagesize);
  exit;
}


if ($action == "Update") {


  while (list($key, $val) = each($_POST)) {

    if (substr($key, 0, 6) == "order_") {

      $key1 = substr($key, 6, strlen($key) - 6);

      $qry1 = db_query("update $table_name set bannerorder='$val' where slno='$key1'");
    }
  }

  $_SESSION['session_message'] = $record_type . ' order has been updated successfully';

  header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&slno=" . $slno . "&pagesize=" . $pagesize);

  exit;
}

admin_header("");
?>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
  <tr>
    <td align="left" width="50%" class="myTitle"><?= $heading; ?></td>
    <td align="right">
      <form name="pgimg_form" action="<?= $PHP_SELF ?>" method="post" style="margin:0px;padding:0px;">
        <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
          <tr>
            <td align="right">Records Per Page:
              <select name="pagesize" onchange="this.form.submit();">
                <option value="0">--Select--</option>
                <?php
                foreach ($_GLOBALS['pgsize_arr'] as $val) {
                  if (trim($pagesize) == trim($val)) {
                    echo '<option value="' . $val . '" selected>' . $val . '</option>';
                  } else {
                    echo '<option value="' . $val . '">' . $val . '</option>';
                  }
                }
                ?>
              </select>	    
            </td>
          </tr>
        </table>
      </form>
    </td>
  </tr>
</table>
<hr size="2" width="99%" color="#DA251C">
<?php
$qry1 = db_query("select * from $table_name where bannerImage !=''");
$reccnt = NumRows($qry1);
$qry = db_query("select * from $table_name where bannerImage!='' order by slno desc limit $start, $pagesize");
echo pageNavigation($reccnt, $pagesize, $start);
print_message();
?>
<form name="formn" method="post" action="" enctype="multipart/form-data">

  <table width="99%" border="0" cellpadding="2" cellspacing="2" class="lightBorder table table-bordered table-hover">
    <?php
    if ($reccnt > 0) {
      ?>
      <tr height="25" bgcolor="#EAEAEA">
        <td width="5%" align="center"><strong style="
                                              color: #468cd2;">S.No.</strong></td>
        <td width="30%" align="left"><strong style="
                                             color: #468cd2;">Banner Name</strong></td>
        <td width="70%" align="center"><strong style="
                                               color: #468cd2;">Banner Image</strong></td>
        <td width="70%" align="center"><strong style="
                                               color: #468cd2;">Banner Order</strong></td>
        <td width="10%" align="left"><strong style="
                                             color: #468cd2;">Options</strong></td>
      </tr>
      <?php
      $sl = $start + 1;
      while ($arr = mysqli_fetch_array($qry)) {
        //trace($arr);
        //  $arr=ms_htmlentities_decode($arr);
        if ($sl % 2 != 0) {
          $color = "#f9f9f9";
        } else {
          $color = "#f0f0f0";
        }
        ?>		 
        <tr bgcolor="<?= $color; ?>">
          <td align="center"><?= $sl; ?></td>
          <td align="left"><?= $arr['personName']; ?></td>


          <td align="center">
            <?php
            if ($arr['bannerImage'] != "") {
              $filenm = SITE_DIR_PATH . "/uploaded-files/banner-image/" . $arr['bannerImage'];
              if (file_exists($filenm)) {
                $dimens = GetImageSize($filenm);
                if ($dimens[0] > 130) {
                  $dimens[0] = $dimens[0] + 50;
                  $dimens[1] = $dimens[1] + 80;
                } else {
                  $dimens[0] = 200;
                  $dimens[1] = 200;
                }
                ?>
              <a href="javascript:void(0);" onClick="openwin('<?= SITE_ADMIN_URL; ?>/popup.php?img=banner-image/<?= $arr['bannerImage']; ?>', '<?= $dimens[0]; ?>', '<?= $dimens[1]; ?>')" class="linkgray fontbold"><strong>Click Here to View Uploaded <?= $record_type; ?></strong></a>
                      <?php
                    }
                  } else {
                    ?>
                    <strong>No Image Uploaded</strong>
                    <?php
                  }
                  ?>
                  </td>
                  <td class="<?= $class ?>" align="center"><input name="order_<?= $arr['slno']; ?>" class="txtfieldadmin" type="text" size="2" value="<?= $arr['bannerorder']; ?>"></td>

                  <td align="center">&nbsp;<a href="<?= $script_name; ?>?id=edit_record&start=<?= $start; ?>&pagesize=<?= $pagesize ?>&recordID=<?= $arr['slno']; ?>#bot"><i class="fa fa-edit"></i></a>&nbsp;<a href="<?= $script_name; ?>?id=delete_record&start=<?= $start; ?>&pagesize=<?= $pagesize ?>&recordID=<?= $arr['slno']; ?>" onClick="return confirm('Are you sure to delete this record...');"><i class="fa fa-trash"></i></td>
                  </tr>
                  <?php
                  $sl++;
                }
                ?>
                <tr>

                  <td class="lightBlueBg" colspan=3></td>

                  <td class="lightBlueBg" align="center">

                    <input type="submit" name="Submit" value="Update Order" class="button">

                    <input type="hidden" name="action" value="Update" class="button">

                  </td>

                  <td class="lightBlueBg"></td>

                </tr>

                <?
              } else {
                ?>
                <tr><td><b>No Record Found........</b></td></tr>
                <?php
              }
              ?>
              </table></form><br />

              <form name="bannerFrm" method=post action="" enctype="multipart/form-data" onsubmit="return validate('bannerFrm') && chk_valid_date();">
                <a href="<?= $script_name; ?>"><button type="button" class="btn btn-info" style="float: right;
                                                       margin-bottom: 25px;">Add <?= $record_type; ?></button></a>

                <?php
                if ($_GET['recordID'] == "" || $id != "edit_record") {
                  ?>
                  <div bgcolor="#EAEAEA">
                    <h3><b><a name="bot">Add New <?= $record_type; ?></a></b></h3>
                    <?php
                  } else {
                    ?>

                    <h3><a name="bot"></a><b>Edit <?= $record_type; ?></b></h3>



                    <?php
                  }
                  $res = getResult($table_name, " where slno='$_GET[recordID]'");
                  ?>
                  <div class="form-group">
                    <label for="email">Banner Name *:</label>
                    <input type="text" class="form-control txtfieldadmin" value="<?= $res['personName']; ?>" placeholder="Enter Banner Name" name="personName">
                  </div>

                  <div class="form-group">
                    <label for="email">Image *:</label>
                    <input type="file" class="form-control txtfieldadmin" name="banner">
                    <?php
                    if ($res['banner'] != "") {
                      ?>
                      <input name="banner" type="file" size="40"  class="button">
                      <?php
                      $banner = SITE_DIR_PATH . "/uploaded-files/banner-image/" . $res['banner'];

                      $dimens = GetImageSize($banner);
                      if ($dimens[0] > 130) {
                        $dimens[0] = $dimens[0] + 50;
                        $dimens[1] = $dimens[1] + 80;
                      } else {
                        $dimens[0] = 200;
                        $dimens[1] = 200;
                      }
                      echo "&nbsp;<a href=\"javascript:void(0);\" onClick=\"openwin('" . SITE_ADMIN_URL . "/popup.php?img=banner_images/" . $res['banner'] . "', '$dimens[0]', '$dimens[1]')\" class=\"linkgray fontbold\"><b>View Image</b></a>";
                    }
                    ?>
                  </div>

                  <div class="form-group">
                    <label for="email">Banner URLs *:</label>
<input type="text" class="form-control txtfieldadmin" value="<?=$res['bannerUrl']; ?>" placeholder="Enter Banner URLs" name="bannerUrl">
                  </div>



                  <?php
                  if ($recordID != "") {
                    ?>
                    <input type="submit" name="submit" class="button btn btn-success" value="Update <?= $record_type; ?>" class="button">
                    <input type="hidden" name="recordID" value="<?= $res['slno']; ?>">
                    <input type="hidden" name="id" value="update_record">
                    <?php
                  } else {
                    ?>
                    <input type="submit" name="submit" class="button btn btn-success" value="Add New <?= $record_type; ?>" class="button">
                    <input type="hidden" name="id" value="add_record">
                    <?php
                  }
                  ?>
                  <input type="hidden" name="start" value="<?= $start; ?>">
                  <input type="hidden" name="pagesize" value="<?= $pagesize; ?>">

                  </form>
                  <script src="jquery.min.js"></script>
                  <script type="text/javascript">

                $("#quickCity").change(function () {

                  var cityid = $('#quickCity').val();

                  $.ajax({

                    url: "ajax99.php", //The url where the server req would we made.

                    async: false,

                    type: "POST", //The type which you want to use: GET/POST

                    data: "cityid=" + cityid, //The variables which are going.

                    dataType: "html", //Return data type (what we expect).

                    //This is the function which will be called if ajax call is successful.

                    success: function (data) {

                      //data is the html of the page where the request is made.

                      $('#locatity').html(data);

                    }

                  })

                });

                  </script>
                  <iframe width=174 height=189 name="gToday:normal:agenda.js" id="gToday:normal:agenda.js" src="<?= SITE_URL_PATH; ?>/js/calendar/themes/Normal/ipopeng.htm" scrolling="no" frameborder="0" style="visibility:visible; z-index:999; position:absolute; top:-500px; left:-500px;">
                  </iframe>
                  <?php
                  admin_footer();
                  exit;
                  ?>