<?php 
define('DOCUMENT_ROOT_CLASS','../../');
define('DOCUMENT_ROOT_INC_FOLDER','inc/');
require "../../inc/header.php";
Session::CheckSession(); 
$msg = Session::get('msg');
Session::set("msg", NULL);?>
    
    <!-- Page-Title -->
    <div class="row">
        <div class="col-sm-12">
            <div class="page-title-box">
                <div class="row">
                    <div class="col">
                        <h4 class="page-title">Services Categories / Tags Area</h4>
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Products & Services</a></li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Services</a></li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Services Categories / Tags Area</a></li>
                        </ol>
                    </div><!--end col-->
                    <!-- <div class="col-auto align-self-center">
                        <a href="#" class="btn btn-sm btn-outline-primary" id="Dash_Date">
                            <span class="day-name" id="Day_Name">Today:</span>&nbsp;
                            <span class="" id="Select_date">Jan 11</span>
                            <i data-feather="calendar" class="align-self-center icon-xs ml-1"></i>
                        </a>
                        <a href="#" class="btn btn-sm btn-outline-primary">
                            <i data-feather="download" class="align-self-center icon-xs"></i>
                        </a>
                    </div> --><!--end col-->  
                </div><!--end row-->                                                              
            </div><!--end page-title-box-->
        </div><!--end col-->
    </div><!--end row-->
    <?php if($_GET['state'] == 'list'){ ?>
        <div class="row">
            <div class=" col-sm-12">
                <div class="card">
                    <div class="card-header">
                        <h4 class="card-title">Services Categories / Tags Area 
                            <a class="btn btn-sm btn-soft-primary" href="services-category.php?state=add" role="button" style="float: right;"><i class="fas fa-plus mr-2"></i> Add New</a>
                        </h4>
                    </div><!--end card-header-->
                    <div class="card-body">
                        <div id="jstree">
                            <!-- in this example the tree is populated from inline HTML -->
                            <ul>
                                <!-- <li class="jstree-open"  data-jstree='{"icon":"fa fa-folder text-warning font-18"}'>Root node 1
                                    <ul>
                                        <li  data-jstree='{"icon":"fa fa-folder text-warning font-18"}' class="jstree-open">Child node 3
                                            <ul>
                                                <li data-jstree='{"icon":"fa fa-folder text-warning font-18"}'>Child node 2
                                                    <ul>
                                                        <li data-jstree='{"icon":"fa fa-folder text-warning font-18"}'>Child node 1</li>
                                                    </ul>
                                                </li>
                                            </ul>
                                        </li>
                                    </ul>
                                </li> -->
                                <?php $categoryData = $commonFunctions->getData('invty_svs_tag',' WHERE parent_id = 0'); 
                                $f=1;$qqq = 0;
                                foreach ($categoryData as $rw) {
                                    $qqq++;
                                    $class_m = "";
                                    if(!empty($rw['colour'])){ 
                                        $class_m = "c-folder-".$f++;
                                        ?>
                                        <style type="text/css">
                                            .c-folder-<?php echo $f;?>{
                                                background: "<?php echo $rw['colour'];?>";
                                            }
                                        </style>
                                    <?php }
                                 ?>
                                    <li data-jstree='{"icon":"fa fa-folder text-warning <?php echo $class_m;?> font-18"}'>
                                        <?php echo $rw['label']; ?>
                                        <ul>
                                            <?php $categorySubData = $commonFunctions->getData('invty_svs_tag',' WHERE parent_id = "'.$rw['id'].'" '); 
                                            $i=1;
                                            foreach ($categorySubData as $rw1) { 
                                                $class = "";
                                                if(!empty($rw1['colour'])){ 
                                                    $class = "c-sub-folder-".$i++;
                                                } ?>
                                                <li data-jstree='{"icon":"fa fa-folder text-warning <?php echo $class;?> font-18"}'><?php echo $rw1['label']; ?></li>
                                            <?php } ?>                                     
                                        </ul>
                                    </li>
                                <?php } 
                                if($qqq == 0){ ?>
                                    <li data-jstree='{"icon":"text-danger"}'>
                                        No Tag/Category of this type Created
                                    </li>
                                <?php }
                                ?>
                            </ul>
                        </div>
                    </div><!--end card-body-->
                </div><!--end card-->
            </div><!--end col-->
                       
        </div><!-- end row -->
    <?php }else{ 

        if(!empty($_POST)){
            if(empty($_POST['label'])){
                Session::set('msg', '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Error !</strong> Please enter ref. !</div>');
            }
            if(!empty($_POST['label'])){
                $parent_id = (!empty($_POST['add_in']))?$_POST['add_in']:0;
                $commonFunctions->addData('invty_svs_tag'," (label,parent_id,description,colour) VALUES ('".$_POST['label']."','".$parent_id."','".$_POST['description']."','".$_POST['colour']."' ) ",'services-category.php?state=add');
            }
        }?>
        <div class="row">
            <div class=" col-sm-12">
                <div class="card">
                    <div class="card-header">
                        <h4 class="card-title">Services Tags / Categories Area </h4>
                        <?php echo !empty($msg)?$msg:''; ?>        
                        <?php echo !empty($msg1)?$msg1:''; ?>
                    </div>
                    <!--end card-header-->                                
                    <div class="card-body">
                        <form enctype="multipart/form-data" action="services-category.php?state=add" method="post" name="formsoc" autocomplete="off">      
                            <div class="form-row">
                                <div class="position-relative form-group col-md-4">
                                    <label for="code" class="">Ref. <span class="error">*</span></label>
                                    <input type="text" maxlength="128" name="label" id="label" value="" autofocus="autofocus" class="form-control">
                                </div>
                                <div class="position-relative form-group col-md-4">
                                    <label for="code" class="">Colour<span class="error">*</span></label>
                                    
                                    <div id="b_color-default" class="input-group colorpicker-element" title="Using input value" data-colorpicker-id="1">
                                        <input type="text" class="form-control input-lg" value="" name="colour">
                                        <span class="input-group-append">
                                            <span class="input-group-text colorpicker-input-addon" data-original-title="" title="" tabindex="0"><i style="background: rgb(245, 164, 22);"></i></span>
                                        </span>
                                    </div>

                                </div>
                                <div class="position-relative form-group col-md-4">
                                    <label for="code" class="">Add In </label>
                                    <?php $categoryData = $commonFunctions->getData('invty_svs_tag',' WHERE parent_id = 0'); ?>
                                    <select name="add_in" class="form-control">
                                        <option value="0">Select</option>
                                        <?php foreach ($categoryData as $rw) { ?>
                                            <option value="<?php echo $rw['id'];?>" ><?php echo $rw['label'];?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                                <div class="position-relative form-group col-md-4">
                                    <label for="code" class="">Description</label>
                                    <textarea name="description" id="elm1" class="form-control"></textarea>
                                </div>
                                <div class="position-relative form-group col-md-12">
                                    <button type="submit" class="btn btn-success">Create</button>
                                    <a href="services-category.php?state=list"><button type="button" class="btn btn-danger">Back to Customers Tags / Categories Area List</button></a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    <?php } ?>    

<?php require "../../inc/footer.php"; ?>