<?php include("_config/dbconfig.php");
include("_includes/head.php");
include("_includes/header.php"); 

$all_cats= $obj_category_class->getParentCategories();

$category       = "";
$parent_cat     = "";
$descp          = "";
$status         = '';
$id_cat         = ''; 

if(count($_POST)>0 && isset($_POST['form_submit'])){
        /*echo "<pre>";
        print_r($_POST);
        echo '</pre>';*/
        
        $category       =   trim($_POST['category']);
        $parent_cat     =   trim($_POST['parent_cat']);
        $descp          =   trim($_POST['descp']);
        
        if(isset($_SESSION['user_role_session']) && $_SESSION['user_role_session']=='Admin'){
            $status = '1';                
         } else { 
            $status = '1';    
         }

         $user_id = $_SESSION['user_session'];
        $obj_category_class->insert($category, $descp, $parent_cat,  $status, $user_id);
        echo "<script>window.location='categories.php'</script>";
        /************/
 die();   
}
if(count($_POST)>0 && isset($_POST['update_submit'])){
        /*echo "<pre>";
        print_r($_POST);
        echo '</pre>';*/
        
        $category       =   trim($_POST['category']);
        $parent_cat     =   trim($_POST['parent_cat']);
        $descp          =   trim($_POST['descp']);
        $status         =   trim($_POST['status']);
        $cat_id         =   $_POST['cat_id'];
        
        //update($cat_id, $cat_title, $cat_desc, $cat_parentId, $cat_status) 
        $obj_category_class->update($cat_id, $category, $descp, $parent_cat,  $status);
        echo "<script>window.location='categories.php'</script>";
        /************/
 die();   
}

?>
<?php if(isset($_GET['edit']) && !empty($_GET['edit'])){
              $id_cat     = base64_decode($_GET['edit']);
            $cat_det    = $obj_category_class->getCategories($id_cat);

            $category       = $cat_det['cat_title'];
            $parent_cat     = $cat_det['cat_parentId'];
            $descp          = $cat_det['cat_desc'];
            $status         = $cat_det['cat_status'];

            /*echo "<pre>";
            print_r($cat_det);
            echo '</pre>';
            */
            $page_title = "Edit Category";
            echo '<title>Edit Category</title>';
       }else { 
           $page_title = "Add Category";
           echo '<title>Add Category </title>'; 
       }
?>

<div class="col-sm-12 right_form">
        <div class="title">
            <div class="row">
                <div class="col-md-8 col-sm-12">
                    <h1><?php echo $page_title; ?></h1>
                </div>
            </div>
        </div>
        
    <ol class="breadcrumb white">
      <li><a href="dashboard.php">Dashboard</a></li>
      <li><a href="categories.php">Categories</a></li>
      <li class="active">Add Category</li>
    </ol>

</div>

<div class="col-sm-12 form_inner">
    <form class="col-sm-6 form-box" method="post" id="cat_form">
                <div class="row">
                	<div class="col-sm-12">
                    	<h5> </h5>
                    </div>
					<div class="col-md-6 col-sm-12 form-group">
						<label for="category">Category</label>
                                                <input id="category" type="text" class="form-control" value="<?= $category ?>" name="category" required>
					</div>
					<div class="col-md-6 col-sm-12 form-group">
						<label for="parent_cat">Parent Category</label>
                                                <select id="parent_cat" class="form-control" required name="parent_cat">
                                                    <option value="0">None</option>
                                                    <?php   if(count($all_cats)>0){
                                                            foreach($all_cats as $cats){ $sel = "";
                                                                    if(isset($parent_cat) && $parent_cat == $cats['cat_id']){
                                                                            $sel = "selected='selected'";
                                                                    }
                                                                ?> 
                                                    <option <?= $sel?> value="<?php echo $cats['cat_id']; ?>"><?php echo $cats['cat_title']; ?></option>
                                                    <?php }}?>
                                                </select>
					</div>
                    
                                        <div style="clear:both;"></div>
                                        
					<div class="col-md-12 col-sm-12 form-group">
						<label for="descp">Description </label>
                                                <textarea id="descp" class="form-control" name="descp"><?= $descp ?></textarea>
					</div>
                                        
                                        <div style="clear:both;"></div>

                                        <?php if(isset($_GET['edit']) && !empty($_GET['edit'])){ ?>
                                        <?php if(isset($_SESSION['user_role_session']) && $_SESSION['user_role_session']=='Admin'){?>
                                            <div class="col-md-6 col-sm-12 form-group">
						<label for="Status">Status</label>
                                                <select id="Status" class="form-control" name="status">
                                                    <option value="1" <?php if($status==1) echo 'selected="selected"';?>>Active</option>
                                                    <option value="2" <?php if($status==2) echo 'selected="selected"';?>>Pending</option>
                                                    <option value="0"  <?php if($status==0) echo 'selected="selected"';?>>Disable</option>
                                                </select>
                                            </div>
                                        <?php } else { ?>
                                                    <input type="hidden" name="status" value="<?= $status; ?>" />      
                                        <?php } ?> 
                                        
                                        <div class="col-sm-12 form-group">
                                            <input type="hidden" name="cat_id" value="<?= $id_cat; ?>" />
                                            <button type="submit" class="btn btn-default" name="update_submit"> Update </button>
                                        </div>

                                        
                                        
                                        <?php } else { ?>
                                            <div class="col-md-6 col-sm-12 form-group">
						<label for="Status">Status</label>
                                                <select id="Status" class="form-control" name="status">
                                                 <?php if(isset($_SESSION['user_role_session']) && $_SESSION['user_role_session']=='Admin'){?>
                                                    <option value="1">Active</option>
                                                 <?php } else { ?>
                                                    <option value="2">Pending</option>
                                                <?php } ?>
                                                </select>
                                            </div>
                                        
                                        <div class="col-sm-12 form-group">
                                            <button type="submit" class="btn btn-default" name="form_submit"> Submit </button>
                                        </div>


                                        <?php } ?>
                                        
                    
                    
            	</div>
            </form>
	</div>
<?php 

//$obj_category_class->insert('1','11', '111', '1111','11111');
?>
<?php include("_includes/footer.php"); ?>
<script>
	$(document).ready(function(){
                   $("#cat_form").validate();
               });
</script>