<?php
include_once("../include/main.inc.php");
check_session('MyCpanel', 'index.php');
################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start) < 0 or $start == "") ? 0 : $start;
$pagesize = intval($pagesize) == 0 ? $pagesize = 20 : $pagesize;

$heading = "Manage Before After Gallery";
$record_type = "Gallery";
$script_name = "manage_before_after_gallery.php";
$table_name = "beforAfter_tbl";
####################################################

if ($id == "add_record" && $_FILES['collar']['name'] != "") {
  if ($_FILES['collar']['name'] != "") {
    $file_extension = strtolower(getExtension($_FILES['collar']['name']));
    if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png") {
      $form_check_alert .= '<div class="btn btn-danger">Please upload small image(.jpg,.gif,.png Format Only) </div>';
    }
  }
  if (!strlen($form_check_alert) > 0) {
    if ($_FILES['collar']['name'] != "") {
      $img_name = "gallery_" . date("dhis");
      $img1 = upload_file($img_name, "collar", "uploaded-files/before-after");
      
    }
    
    $maxRes = mysqli_fetch_array(mysqli_query($dbconn, "select max(gallery_order) from $table_name"));
    $gallery_order = $maxRes[0] + 1;
    
    
    $sql = db_query("insert into $table_name set gallery_title='$gallery_title', description='$detail', gallery_image='$img1', image_name='$name', gallery_order='$gallery_order',gallery_cat='$cat'");
    
    $_SESSION['session_message'] = '<div class="btn btn-success">New ' . $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 ($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 gallery_order='$val' where id='$key1'");
    }
  }
  $_SESSION['session_message'] = $record_type . ' order has been updated successfully';

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

if ($id == "update_record") {
  if ($_FILES['collar']['name'] != "") {
    $file_extension = strtolower(getExtension($_FILES['collar']['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['collar']['name'] != "") {
      $res1 = getResult($table_name, " where id='$recordID'");
      $filenm1 = SITE_DIR_PATH . "/uploaded-files/gallery/" . $res1['gallery_image'];
      if (file_exists($filenm1)) {
        unlink($filenm1);
      }

      $img_name = "gallery1" . date("dhis");
      $img1 = upload_file($img_name, "collar", "uploaded-files/before-after");
      updateTable($table_name, " gallery_image='$img1' where id='$recordID'");
    }

    updateTable($table_name, "image_name='$name', description='$detail', gallery_title='$gallery_title',gallery_cat='$cat'  where id='$recordID'");
    $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated 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 == "delete_record") {
  db_query("delete from $table_name where id='$recordID'");
  $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been deleted successfully  </div>';
  header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&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 gallery_image !=''");
$reccnt = NumRows($qry1);

$qry = db_query("select * from $table_name where gallery_image!='' order by id desc limit $start, $pagesize");
//echo pageNavigation($reccnt, $pagesize, $start);
print_message();
?>
<table width="99%" border="0" cellpadding="2" cellspacing="2" class="lightBorder table table-bordered table-hover">
  <form action="" method="POST">
    <?php
    if ($reccnt > 0) {
      ?>
      <tr height="25" bgcolor="#EAEAEA">
        <td width="5%" align="center"><strong>S.No.</strong></td>
        <td width="20%" align="left"><strong>Name</strong></td>
        <td width="20%" align="left"><strong>Detail</strong></td>
        <td width="10%" align="center"><strong>Image</strong></td>
        <td width="5%" align="center"><strong>Order</strong></td>
        <td width="10%" align="left"><strong>Options</strong></td>
      </tr>
      <?php
      $sl = $start + 1;
      while ($arr = mysqli_fetch_array($qry)) {
        //  $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['image_name']; ?></td>
          <td align="left"><?= $arr['description']; ?></td>
          <td align="center">
            <?php
            if ($arr['gallery_image'] != "") {
              $filenm = SITE_DIR_PATH . "/uploaded-files/before-after/" . $arr['gallery_image'];
              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_new.php?img=before-after/<?= $arr['gallery_image']; ?>', '<?= $dimens[0]; ?>', '<?= $dimens[1]; ?>')" class="linkgray fontbold"><strong>View <?= $record_type; ?></strong></a>
                      <?php
                    }
                  } else {
                    ?>
                    <strong>No Image Uploaded</strong>
                    <?php
                  }
                  ?>
                  </td>
                  <td align="center"><input name="order_<?= $arr['id']; ?>" class="txtfieldadmin" type="text" size="2" value="<?= $arr['gallery_order']; ?>"></td>
                    <td align="center">&nbsp;<a href="<?= $script_name; ?>?id=edit_record&start=<?= $start; ?>&pagesize=<?= $pagesize ?>&recordID=<?= $arr['id']; ?>#bot"><img src="<?= SITE_ADMIN_URL ?>/images/edit.gif" alt="Edit This <?= $record_type; ?>" title="Edit This <?= $record_type; ?>" border="0"></a>&nbsp;<a href="<?= $script_name; ?>?id=delete_record&start=<?= $start; ?>&pagesize=<?= $pagesize ?>&recordID=<?= $arr['id']; ?>" onClick="return confirm('Are you sure to delete this record...');"><img src="<?= SITE_ADMIN_URL ?>/images/cancel.gif" alt="Delete This <?= $record_type; ?>" border="0"></a></td>
                      </tr>

                      <?php
                      $sl++;
                    }
                    ?>
                    <tr>
                      <td class="lightBlueBg" colspan=3></td>
                      <td class="lightBlueBg" align="center">
                        <input type="submit" name="Submit" value="Update" class="button">
                        <input type="hidden" name="action" value="Update" class="button">
                      </td>
                      <td class="lightBlueBg"></td>
                      <td class="lightBlueBg"></td>
                    </tr>
                    <?php
                  } else {
                    ?>
                    <tr><td><b>No Record Found........</b></td></tr>
                    <?php
                  }
                  ?>

                  </form>
                  </table><br />

                  <form name="bannerFrm" method=post action="" enctype="multipart/form-data" onSubmit="return validate('bannerFrm') && chk_valid_date();">
             
                      <?php
                      if ($_GET['recordID'] == "" || $id != "edit_record") {
                        ?>
                    <h4 class="myTitle">Add New <?= $record_type; ?></h4>
                        <?php
                      } else {
                        ?>
                      <div class="text-left myTitle">Edit <?= $record_type; ?></div>
                      <div class="text-right"><a href="<?= $script_name; ?>" class="btn btn-primary text-light"><b>Add New <?= $record_type; ?></b></a></div>
                        <?php
                      }
                      $res = getResult($table_name, " where id='$_GET[recordID]'");
                      ?>
                      <div class="form-group">
                        <label for="email">Gallery Image *:</label>
                   

                        <?php
                        if ($res['gallery_image'] != "") {
                          ?>
                          <input name="collar" type="file" size="40"  class="form-control">
                          <strong>Image Size: Please upload square image for better view.</strong> <br>
                          <?php
                          $banner = SITE_DIR_PATH . "/uploaded-files/before-after/" . $res['gallery_image'];
                          if (file_exists($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 "<br>&nbsp;<a href=\"javascript:void(0);\" onClick=\"openwin('" . SITE_ADMIN_URL . "/popup_new.php?img=gallery/" . $res[gallery_image] . "', '$dimens[0]', '$dimens[1]')\" class=\"linkgray fontbold\"><b>View Image</b></a>";
                          }
                        }
                          else{
                              ?>
                               <input type="file" class="form-control" name="collar">
                                <strong>Image Size: Please upload square image for better view.</strong>
                          <?php
                          }
                        ?>
                        <br/>
                      </div> 

                      <div class="form-group">
                        <label for="name">Name *:</label>
                        <input type="text" class="form-control txtfieldadmin"  value="<?= $res['image_name']; ?>" placeholder="Enter Gallery Name" name="name">
                      </div>
                      <div class="form-group">
                        <label for="name">Gallery Title *:</label>
                        <input type="text" class="form-control txtfieldadmin"  value="<?= $res['gallery_title']; ?>" placeholder="Enter Gallery Name" name="gallery_title">
                      </div>
                      <div class="form-group">
                        <label for="name">Gallery Title : (If Any)</label>
                        <input type="text" class="form-control txtfieldadmin"  value="<?= $res['description']; ?>" placeholder="Enter Gallery Detail" name="detail">
                      </div>
                      <div class="form-group">
      <label for="cate">Select Category<font color="red">*</font></label>
        <select class="form-control" name="cat" id="quickCity">
            <option>Select Category</option>
            <?php
$sql_cat1=mysqli_query($dbconn,"select * from category_tbl where parentID='0'");
while ($res_cat=mysqli_fetch_array($sql_cat1))
{
?>
<option value="<?php echo $res_cat['catID']; ?>" <? if ($res['gallery_cat']==$res_cat['catID']) { echo "selected"; }?>><?php echo $res_cat['catName']; ?></option>
<?php
}
?>
    </select>
    </div>

                 
                        <?php
                        if ($recordID != "") {
                          ?>
                          <input type="submit" name="submit" value="Update <?=$record_type; ?>" class="button btn btn-success">
                          <input type="hidden" name="recordID" value="<?=$res['id']; ?>">
                          <input type="hidden" name="id" value="update_record">
                          <?php
                        } else {
                          ?>
                          <input type="submit" name="submit" value="Add New <?= $record_type; ?>" class="button btn btn-success">
                          <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>
                 
                  <?php
                  admin_footer();
                  exit;
                  ?>