<?php
include_once("../include/main.inc.php");
check_session('MyCpanel', 'index.php');

@extract($_REQUEST);
if (empty($catID)) {
  $heading = "Add New Category";
} else {
  $heading = "Edit Category";
}
$res = getResult("admin_details", " where user_type='admin'");

$record_type = "New Category";
$script_name = "addeditcategory.php";
$redirect_script_name = "manage_categories.php";
$table_name = "category_tbl";
#################### Add Record ##################
if ($id == 'remove_image') {
  mysqli_query($dbconn, "UPDATE $table_name SET $field='' where catID = '$catID'");
  $_SESSION['session_message'] = $record_type . ' has been updated successfully';
  header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
  exit;
}


if ($id == 'remove_pdf') {
  mysqli_query($dbconn, "delete from catalogue_tbl where id='$del_id'");
  $_SESSION['session_message'] = $record_type . ' has been updated successfully';
  header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
  //header('Location: '.$_SERVER['REQUEST_URI']);
  exit;
}

if ($_REQUEST['action'] == "add_record" && $catName != "") {

  $catDesc = $_POST['catDesc'];
  $catDesc1 = $_POST['catDesc1'];
  $fcontent = $_POST['fcontent'];

  $numCnt = getCount($table_name, " where catName='$catName' and parentID='$parentID' and catID!='$catID' and catgType='$catgType' ");
  $count_cat = mysqli_query($dbconn, "SELECT * FROM $table_name");
  if (mysqli_num_rows($count_cat) >= $res['category_count']) {
    $_SESSION['session_message'] = '<div class="btn btn-danger"> ' . $record_type . ' count exceeds the allowed limit ['. $res[category_count] .']  </div>';
  } else {
    if ($numCnt > 0) {
      $_SESSION['session_message'] = '<div class="btn btn-warning">' . $record_type . ' already exist  </div>';
    } else {
      if ($_FILES['image']['name']!=" ") {
        $file_extension = strtolower(getExtension($_FILES['image']['name']));
        if ($_FILES['image']['name'] != "") {
          $cat = str_replace(" ", "-", $catName);
          $img_name = $cat . date("s");
          $img = upload_file($img_name, "image", "uploaded-files/category/images");
        }
      }

      if ($_FILES['banner']['name'] != "") {
        $file_extension = strtolower(getExtension($_FILES['banner']['name']));
        if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png") {
          $_SESSION['session_message'] = '<div class="btn btn-warning"> Please upload image (.jpg,.gif,.png Format Only) </div>';
          header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
          exit;
        }
        if ($_FILES['banner']['name'] != "") {
          $cat = str_replace(" ", "-", $catName);
          $banner_name = $cat . "-banner" . date("s");
          $banner = upload_file($banner_name, "banner", "uploaded-files/category/banners");
        }
      }
      if ($_FILES['icon']['name'] != "") {
        $file_extension = strtolower(getExtension($_FILES['icon']['name']));
        if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png") {
          $_SESSION['session_message'] = '<div class="btn btn-warning"> Please upload image (.jpg,.gif,.png Format Only) </div>';
          header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
          exit;
        }
        if ($_FILES['icon']['name'] != "") {
          $cat = str_replace(" ", "-", $catName);
          $banner_icon_name = $cat . "-banner-icon" . date("s");
          $banner_icon = upload_file($banner_icon_name, "icon", "uploaded-files/category/icons");
        }
      }
      if ($_FILES['faq_icon']['name'] != "") {
        $file_extension = strtolower(getExtension($_FILES['faq_icon']['name']));
        if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png") {
          $_SESSION['session_message'] = '<div class="btn btn-warning"> Please upload image (.jpg,.gif,.png Format Only) </div>';
         
          header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
          exit;
        }
        if ($_FILES['faq_icon']['name'] != "") {
          $cat = str_replace(" ", "-", $catName);
          $faq_icon_name = $cat . "-faq-icon" . date("s");
          $faq_icon = upload_file($faq_icon_name, "faq_icon", "uploaded-files/category/icons");
        }
      }
      $maxRes = mysqli_fetch_array(mysqli_query($dbconn, "select max(catOrder) from `$table_name` where parentID='$parentID'"));
      $catOrder = $maxRes[0] + 1;
      $catalogname = $_FILES['catalog']['name'];
      $cimg = upload_file($catalogname, "catalog", "uploaded_files/catalog_images");
      $sql = mysqli_query($GLOBALS['dbconn'], "insert into `$table_name` set parentID='$parentID', catVideo='$catVideo', catName='$catName', displayName='$displayName', catgType='$catgType', catDesc='$catDesc', catDesc1='$catDesc1', catDesc2='$catDesc2', catImage='$img', catBanner='$banner', catIcon='$banner_icon',faq_icon='$faq_icon',catOrder='$catOrder', pageTitle='$pageTitle', pageKeywords='$pageKeywords', pageDescription='$pageDescription', footerContent='$fcontent', catalogImage='$cimg', catalogLink='$catlink',keyword1='$keyword1',keyword2='$keyword2',keyword3='$keyword3'");
      $_SESSION['session_message'] ='<div class="btn btn-success">' . $catName . ' has been added successfully  </div> ';

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

if ($_REQUEST['action'] == "update_record" && $catName != "") {
  $catDesc = $_POST['catDesc'];
  $catDesc1 = $_POST['catDesc1'];
  $numCnt = getCount($table_name, " where catName='$catName' and parentID='$parentID' and catID!='$catID' and catgType='$catgType'");
  if ($numCnt > 0) {
    $_SESSION['session_message'] = '<div class="btn btn-warning">' . $record_type . ' already exist  </div>';
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
  }

  if ($_FILES['image']['name'] != "") {
    $file_extension = strtolower(getExtension($_FILES['image']['name']));
    if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png") {
      $_SESSION['session_message'] = '<div class="btn btn-warning"> Please upload image (.jpg,.gif,.png Format Only) </div>';
      header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
      exit;
    }
    if ($_FILES['image']['name'] != "") {
      $categoryRes = getResult($table_name, " where catID='$catID'", "catImage");
      $filename = $categoryRes['catImage'];
      if ($filename != "") {
        $file = SITE_DIR_PATH . "/uploaded-files/category/images/" . $filename;
        if (file_exists($file)) {
          unlink($file);
        }

        $file_thumb = SITE_DIR_PATH . "/uploaded-files/category/images/thumbs/" . $filename;
        if (file_exists($file_thumb)) {
          unlink($file_thumb);
        }
      }
      $cat = str_replace(" ", "-", $catName);
      $img_name = $cat . date("s");
      $img = upload_file($img_name, "image", "uploaded-files/category/images");
      updateTable($table_name, " catImage='$img' where catID='$catID'");
    }
  }
  if ($_FILES['banner']['name'] != "") {
    $categoryRes = getResult($table_name, " where catID='$catID'", "catImage");
    $banner_name = $categoryRes['catBanner'];
    if ($banner_name != "") {
      $banner = SITE_DIR_PATH . "/uploaded-files/category/banners/" . $banner_name;
      if (file_exists($banner)) {
        unlink($banner);
      }
    }
    $cat = str_replace(" ", "-", $catName);
    $banner_name = $cat . "-banner" . date("s");
    $banner = upload_file($banner_name, "banner", "uploaded-files/category/banners");
    updateTable($table_name, " catBanner='$banner' where catID='$catID'");
  }
  if ($_FILES['icon']['name'] != "") {
    $categoryRes = getResult($table_name, " where catID='$catID'", "catIcon");
    $icon_name = $categoryRes['catIcon'];
    if ($icon_name != "") {
      $banner = SITE_DIR_PATH . "/uploaded-files/category/banners/icons/" . $icon_name;
      if (file_exists($banner)) {
        unlink($banner);
      }
    }
    $cat = str_replace(" ", "-", $catName);
    $icon = $cat . "-banner-icon" . date("s");
    $cat_icon = upload_file($icon, "icon", "uploaded-files/category/icons");
    updateTable($table_name, " catIcon='$cat_icon' where catID='$catID'");
  }
  if ($_FILES['faq_icon']['name'] != "") {
    $categoryRes = getResult($table_name, " where catID='$catID'", "faq_icon");
    $icon_name = $categoryRes['faq_icon'];
    if ($icon_name != "") {
      $banner = SITE_DIR_PATH . "/uploaded-files/category/banners/icons/" . $icon_name;
      if (file_exists($banner)) {
        unlink($banner);
      }
    }
    $cat = str_replace(" ", "-", $catName);
    $faq_icon1 = $cat . "-faq-icon" . date("s");
    $faq_icon = upload_file($faq_icon1, "faq_icon", "uploaded-files/category/icons");
    updateTable($table_name, " faq_icon='$faq_icon' where catID='$catID'");
  }

$countArr = count($c_name);
if($c_name[0]!='')
{
for($i=0;$i<$countArr;$i++)
{

  $uploads_dir = SITE_DIR_PATH.'/uploaded-files/catalogue';
  $pdf_file =  $_FILES['c_pdf']['name'][$i];
  $extension = pathinfo($pdf_file, PATHINFO_EXTENSION );
  $file_name = str_replace(" ", "-", $c_name[$i]);
  $pdf_final = $file_name . $i . date("is").'.'.$extension;

  $sql_s = mysqli_query($GLOBALS['dbconn'], "insert into catalogue_tbl set catelogue_title='$c_name[$i]',catelogue_image='$pdf_final', categoryID='$catID'");

         $tmp_name = $_FILES["c_pdf"]["tmp_name"][$i];
         
         move_uploaded_file($tmp_name, "$uploads_dir/$pdf_final");

 }
}
   

  updateTable($table_name, " catName='$catName', displayName='$displayName', catgType='$catgType', catVideo='$catVideo', catDesc='$catDesc', catDesc1='$catDesc1', catDesc2='$catDesc2', pageTitle='$pageTitle', pageKeywords='$pageKeywords', pageDescription='$pageDescription', footerContent='$fcontent', catalogLink='$catlink',keyword1='$keyword1',keyword2='$keyword2',keyword3='$keyword3' where catID='$catID'");
  $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been added successfully  </div>';
   
  header("Location: " . SITE_ADMIN_URL . "/" . $redirect_script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
  exit;
}
########################################################
admin_header();
?>

<div align="left" class="myTitle"><?= $heading; ?></div>
<hr size="2" width="100%" color="#92B2D6">
<?php
print_message();
$res = getResult($table_name, " where catID='$catID'");
//$res_pdf = getResult('catalogue_tbl', " where catID='$catID'");

$catName = $res['catName'];
$displayName = $res['displayName'];
$catDesc = $res['catDesc'];
$catDesc1 = $res['catDesc1'];
$catgType = $res['catgType'];
$pageTitle = $res['pageTitle'];
$catVideo = $res['catVideo'];
$pageKeywords = $res['pageKeywords'];
$pageDescription = $res['pageDescription'];
$footerContent = $res['footerContent'];
$catalogLink = $res['catalogLink'];
$catDesc2 = $res['catDesc2'];
$keyword1 = $res['keyword1'];
$keyword2 = $res['keyword2'];
$keyword3 = $res['keyword3'];
?>
<input type="button" class="button btn btn-warning" value="Go Back <?= $record_type; ?>" onclick="window.location.href = '<?= SITE_ADMIN_URL ?>/<?= $redirect_script_name; ?>?parentID=<?= $parentID; ?>&catID=<?= $catID; ?>&start=<?= $start; ?>&pagesize=<?= $pagesize; ?>';" style="float: right;
    margin-bottom: 25px;">
   
      <br />

<form name="formn" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this);">
  <?= $msg; ?>
  <div class="form-group">
    <label for="email">Category Name *:</label>
    <input type="text" class="form-control txtfieldadmin" value="<?= $catName ?>" placeholder="Enter Category Name" name="catName">
  </div>
  <div class="form-group">
    <label for="email">Display Name *:</label>
    <input type="text" class="form-control txtfieldadmin" value="<?= $displayName ?>" placeholder="Enter Category Display Name" name="displayName">
  </div>
    <div class="form-group">
    <label for="email">Video Link *:</label>
    <input type="text" class="form-control txtfieldadmin" value="<?= $catVideo ?>" placeholder="Enter Category Video Link" name="catVideo">
  </div>
  <div class="form-group">
    <label for="email">Meta Title *:</label>
    <input type="text" class="form-control txtfieldadmin"  value="<?= $pageTitle ?>" placeholder="Enter Meta Title" name="pageTitle">
  </div>
  <div class="form-group">
    <label for="email">Meta Keyword *:</label>
    <input type="text" class="form-control txtfieldadmin" value="<?= $pageKeywords ?>" placeholder="Enter Meta Title" name="pageKeywords">
  </div>

  <div class="form-group">
    <label for="comment">Meta Description:</label>
    <textarea name="pageDescription" class="form-control" rows="5" placeholder="Enter Meta Description"><?= $pageDescription ?></textarea>
  </div>
  
  <div class="row">
      <div class="col-md-4">
           <div class="form-group">
               <label for="comment" class="text-danger">KeyWord 1:</label>
     <input type="text" class="form-control txtfieldadmin" value="<?=$keyword1;?>" placeholder="Enter Keyword1" name="keyword1">
  </div>
      </div>
       <div class="col-md-4">
           <div class="form-group">
    <label for="comment" class="text-danger">KeyWord 2:</label>
     <input type="text" class="form-control txtfieldadmin" value="<?=$keyword2;?>" placeholder="Enter Keyword2" name="keyword2">
  </div>
      </div> <div class="col-md-4">
           <div class="form-group">
   <label for="comment" class="text-danger">KeyWord 3:</label>
     <input type="text" class="form-control txtfieldadmin" value="<?=$keyword3;?>" placeholder="Enter Keyword3" name="keyword3">
  </div>
      </div>
  </div>
  <div class="form-group">
    <label for="comment">Short Description:</label>
    <textarea name="catDesc"  class="form-control" rows="5"><?= $catDesc ?></textarea>
  </div>
  <div class="form-group">
    <label for="comment">Description:</label>
    <textarea name="catDesc1" id="catDesc1" class="form-control" rows="5"><?= $catDesc1 ?></textarea>
  </div>
  <div class="form-group">
    <label for="comment">Description 2:</label>
    <textarea name="catDesc2" id="catDesc2" class="form-control" rows="5"><?= $catDesc2 ?></textarea>
  </div>
  <div class="form-group">
    <label for="email">Category Image *:</label>
    <input type="file" class="form-control txtfieldadmin" name="image">
    <?php
    if ($res['catImage'] != "") {
      $file1 = SITE_DIR_PATH . "/uploaded-files/category/images/" . $res['catImage'];

      $dimens = GetImageSize($file1);
      if ($dimens[0] > 130) {
        $dimens[0] = $dimens[0] + 30;
        $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=category/images/<?= $res['catImage']; ?>', '<?= $dimens[0]; ?>', '<?= $dimens[1]; ?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong></a>&nbsp; <strong><a href="<?= SITE_ADMIN_URL ?>/addeditcategory.php?catID=<?= $catID ?>&id=remove_image&field=catImage">Remove</a></strong>
      <?php
    }
    ?>
    <br/><strong>Image Size: Please upload square image for better view.</strong>
  </div>
  <div class="form-group">
    <label for="email">Category Banner *:</label>
    <input type="file" class="form-control txtfieldadmin" name="banner">
    <?php
    if ($res['catBanner'] != "") {
      $file1 = SITE_DIR_PATH . "/uploaded-files/category/banners/" . $res['catBanner'];
      if (file_exists($file1)) {
        $dimens = GetImageSize($file1);
        if ($dimens[0] > 130) {
          $dimens[0] = $dimens[0] + 30;
          $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=category/banners/<?= $res['catBanner']; ?>', '<?= $dimens[0]; ?>', '<?= $dimens[1]; ?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong></a>&nbsp; <strong><a href="<?= SITE_ADMIN_URL ?>/addeditcategory.php?catID=<?= $catID ?>&id=remove_image&field=catBanner">Remove</a></strong>
        <?php
      }
    }
    ?>

  </div>
  <div class="form-group">
    <!--<label for="email">Category Banner *:</label>-->
    <!--<input type="file" class="form-control txtfieldadmin" name="icon">-->
    <?php
    if ($res['catIcon'] != "") {
      $file2 = SITE_DIR_PATH . "/uploaded-files/category/icons/" . $res['catIcon'];
      if (file_exists($file2)) {
        $dimens = GetImageSize($file2);
        if ($dimens[0] > 130) {
          $dimens[0] = $dimens[0] + 30;
          $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=category/icons/<?= $res['catIcon']; ?>', '<?= $dimens[0]; ?>', '<?= $dimens[1]; ?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong></a>&nbsp; <strong><a href="<?= SITE_ADMIN_URL ?>/addeditcategory.php?catID=<?= $catID ?>&id=remove_image&field=catIcon">Remove</a></strong>
        <?php
      }
    }
    ?>
  </div>
  <div class="form-group">
    <!--<label for="email">Category Banner *:</label>-->
    <!--<input type="file" class="form-control txtfieldadmin" name="faq_icon">-->
    <?php
    if ($res['faq_icon'] != "") {
      $file2 = SITE_DIR_PATH . "/uploaded-files/category/icons/" . $res['faq_icon'];
      if (file_exists($file2)) {
        $dimens = GetImageSize($file2);
        if ($dimens[0] > 130) {
          $dimens[0] = $dimens[0] + 30;
          $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=category/icons/<?= $res['faq_icon']; ?>', '<?= $dimens[0]; ?>', '<?= $dimens[1]; ?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong></a>&nbsp; <strong><a href="<?= SITE_ADMIN_URL ?>/addeditcategory.php?catID=<?= $catID ?>&id=remove_image&field=faq_icon">Remove</a></strong>
        <?php
      }
    }
    ?>
  </div>
  <?php
          if ($res['catID']!='') {
            ?>
  <div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<label for="Catelogue">Catelogue File :</label>
<tr>
<td><input type="text" name="c_name[]" placeholder="Catalogue Title" class="form-control name_list" /></td>
<td><input type="file" name="c_pdf[]" class="form-control name_list" /></td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
</tr>

<?php }?>
</table>


<table class="table table-bordered">
<?php $sql_pdf = mysqli_query($GLOBALS['dbconn'],"select * from catalogue_tbl where categoryID='$catID'");
       if(mysqli_num_rows($sql_pdf) > 0){
      while($res_pdf = mysqli_fetch_assoc($sql_pdf))
      {?>
<tr>

        <td><input type="text" value="<?=$res_pdf['catelogue_title'];?>" class="form-control name_list" /></td>
        <td><a href="<?=site_url;?>/uploaded-files/catalogue/<?=$res_pdf['catelogue_image'];?>" download><span class="glyphicon glyphicon-download-alt"></span></a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="<?=SITE_ADMIN_URL;?>/addeditcategory.php?del_id=<?=$res_pdf['id'];?>&id=remove_pdf&catID=<?= $catID; ?>&start=<?= $start; ?>&pagesize=<?= $pagesize; ?>"><span class="glyphicon glyphicon-trash"></span></a></td>
</tr>
<?php }}?>
</table>


    <div class="col-md-12 submitbtntop"> 
          <?php
          if ($res['catID'] == "") {
            ?>
            <input type="submit" name="add_record" class="button btn btn-success" value="Add <?= $record_type; ?>"><input type="hidden" name="action" value="add_record">
            <?php
          } else {
            ?>
            <input type="submit" name="update_record" class="button btn btn-success" value="Update <?= $record_type; ?>"><input type="hidden" name="action" value="update_record">
            <?php
          }
          ?>
          &nbsp;&nbsp;<input type="button" name="cancel" class="button btn btn-dark" value="Cancel" onclick="javascript: window.location.href = '<?= SITE_ADMIN_URL; ?>/<?= $redirect_script_name; ?>?parentID=<?= $parentID; ?>&catID=<?= $catID; ?>&start=<?= $start; ?>&pagesize=<?= $pagesize; ?>'">   
       
</div>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
	<script>
	        var i = 1;
	        $('#add').click(function() {
	            i++;
	            $('#dynamic_field').append('<tr id="row' + i + '"><td><input type="text" name="c_name[]" placeholder="Catalogue Title" class="form-control name_list" /></td><td><input type="file" name="c_pdf[]" placeholder="Enter your Skill" class="form-control name_list" /></td><td><button type="button" name="remove" id="' + i + '" class="btn btn-danger btn_remove">X</button></td></tr>');
	        });

	        $(document).on('click', '.btn_remove', function() {
	            var button_id = $(this).attr("id");
	            $('#row' + button_id + '').remove();
	        });

         
	</script>


<?php
admin_footer();
exit;
?>