<?php include('lib/config.php');?>
<?php       
    //===============================================================   
    $toDay = date("Y-m-d");
    $cDateTime = date("mdYHis");
    
	$category_id = isset($_POST['category_id'])?$_POST['category_id']:'';
    $category_title = isset($_POST['category_title'])?$_POST['category_title']:'';
	$category_abbreviation = isset($_POST['category_abbreviation'])?$_POST['category_abbreviation']:'';
	$category_description = isset($_POST['category_description'])?$_POST['category_description']:''; 
	$category_orientation = isset($_POST['category_orientation'])?$_POST['category_orientation']:'';
	$category_influence = isset($_POST['category_influence'])?$_POST['category_influence']:'';
	
    //===============================================================
    //============== Add record =====================================
    //===============================================================
    if(isset($_POST['action_type']) && $_POST['action_type'] == 'add')
    { 
        $insQry = "INSERT INTO ".TBL_CATEGORIES." SET 
        category_abbreviation='".addslashes($category_abbreviation)."',
		category_description='".addslashes($category_description)."',
		category_orientation = '".addslashes($category_orientation)."',
		category_influence = '".addslashes($category_influence)."',
		category_title='".addslashes($category_title)."',
        date_added='".$toDay."' ";
       
        $response = $con->insertRecords($insQry, __FILE__,__LINE__);
        
        if($response===TRUE )
            $return_arr[] = array("message" => 'ok');
        else
            $return_arr[] = array("message" => 'err');
                    
        echo json_encode($return_arr);
    }
	//===============================================================
    //============== Update updateSHCat =============================
    //===============================================================
    elseif(isset($_POST['action']) && $_POST['action'] =='updateSHCat')
    { 
        $shcatid = isset($_POST['shcatid'])?$_POST['shcatid']:'';
	
		$insQry = "UPDATE ".TBL_CATEGORIES." SET 
        category_title='".addslashes($category_title)."',
        category_abbreviation='".addslashes($category_abbreviation)."',
		category_description='".addslashes($category_description)."',
		category_orientation = '".addslashes($category_orientation)."',
		category_influence = '".addslashes($category_influence)."',
		date_modified='".$toDay."' 		
		WHERE category_id = '".$shcatid."' LIMIT 1 ";        
        $response = $con->insertRecords($insQry, __FILE__,__LINE__);
        
        if($response===TRUE )
            $return_arr[] = array("message" => 'ok');
        else
            $return_arr[] = array("message" => 'err');
                    
        echo json_encode($return_arr);
    }
    //===============================================================
    //============== Update record ==================================
    //===============================================================
    elseif(isset($_POST['action_type']) && $_POST['action_type'] == 'edit')
    {           
        $updQry = "UPDATE ".TBL_CATEGORIES." SET 
        category_title='".addslashes($category_title)."',
        date_modified='".$toDay."'
        WHERE category_id = '".$category_id."' LIMIT 1";
        
        $response = $con->updateRecords($updQry, __FILE__,__LINE__);
        
        if($response===TRUE )
            $return_arr[] = array("message" => 'ok');
        else
            $return_arr[] = array("message" => 'err');
                    
        echo json_encode($return_arr);
    }
	//===============================================================
    //============== Update Cat Orientation =========================
    //===============================================================
    elseif(isset($_POST['action']) && $_POST['action'] =='updateCatOrt')
    { 
		$shcatid = isset($_POST['catid'])?$_POST['catid']:'';
	
		$insQry = "UPDATE ".TBL_CATEGORIES." SET 
        category_title='".addslashes($category_title)."',
        category_abbreviation='".addslashes($category_abbreviation)."',
		category_description='".addslashes($category_description)."',
		category_orientation = '".addslashes($category_orientation)."',
		category_influence = '".addslashes($category_influence)."',
		date_modified = '".$toDay."'	
		WHERE category_id = '".$shcatid."' LIMIT 1 ";        
        $response = $con->insertRecords($insQry, __FILE__,__LINE__);
        
        if($response===TRUE )
            $return_arr[] = array("message" => 'ok');
        else
            $return_arr[] = array("message" => 'err');
                    
        echo json_encode($return_arr);
    }
	//===============================================================
    //============== Retrieve Categories List =======================
    //===============================================================
    elseif(isset($_POST['action_type']) && $_POST['action_type'] == 'view')
    { 
		  $tabid = isset($_POST['tabid'])?$_POST['tabid']:'';
		  //--------------- Category Info ----------
		  $category_counter = 0;
		  $selQry_cat = "SELECT * FROM ".TBL_CATEGORIES." ORDER BY category_abbreviation asc";
		  $catInfo =  $con->getMultiRow($selQry_cat, __FILE__,__LINE__);
		  $totSHCatListings = count($catInfo);
			if($totSHCatListings>0)
			{
				for($shcat=0;$shcat<$totSHCatListings;$shcat++)
				{
					$category_counter++;
		  //----------------------------------------
		?>
          <!-- Grid row -->
		  <?php if($category_counter==1){?>
		  <div class="row mt-3" style="margin-bottom:10px;">
			<?php }?>
			<div class="col-md-2 col-lg-2 col-xl-2 mx-auto mb-4" style="text-align:left;">
			  <!-- Links -->
			  <h6 class="text-uppercase font-weight-bold" title="<?php echo $catInfo[$shcat]['category_title'];?>">
              
              <a href="sh-cat-update.php?scatid=<?php echo $catInfo[$shcat]['category_id'];?>" class="popup" title="<?php echo $catInfo[$shcat]['category_title'];?>" style="color:#000000;"><strong><?php echo $catInfo[$shcat]['category_title'];?></strong></a>
              
              <a href="sh-add.php?cid=<?php echo $catInfo[$shcat]['category_id'];?>" style="font-size:12px;cursor: pointer;" class="popup" title="Add Stakeholder"> <i class="fa fa-plus"></i></a></h6>
              
			  <hr class="deep-purple accent-2 mb-4 mt-0 d-inline-block mx-auto" style="width: 60px; margin-top:0px !important; margin-bottom:0px !important;">
			  <?php 
					$shsrno = 0;
					$selQry_sh = "SELECT * FROM ".TBL_STAKEHOLDERS." WHERE category_id='".$catInfo[$shcat]['category_id']."' ORDER BY name";
					$shInfo =  $con->getMultiRow($selQry_sh, __FILE__,__LINE__);
					$totSHListings = count($shInfo);
					if($totSHListings>0)
					{
						?>
			  <ul>
				<?php
				for($shh=0;$shh<$totSHListings;$shh++){										
					$shsrno++;										
				?>
				<li> <a href="sh-update-<?=$tabid?>.php?sid=<?php echo $shInfo[$shh]['id'];?>" class="popup" <?php echo $shInfo[$shh]['name'];?>><span style="color:<?php echo !empty($shInfo[$shh]['sh_current_orientation'])?$shInfo[$shh]['sh_current_orientation']:'#a9b7af';?>; font-size:<?php echo !empty($shInfo[$shh]['orientation_sh_influence'])?$shInfo[$shh]['orientation_sh_influence']:'';?>px;"><?php echo $shInfo[$shh]['abbreviation'];?></span></a> </li>
				<?php }?>
			  </ul>
			  <?php }?>
			</div>
			<?php if($category_counter==6){$category_counter=0;?>
		  </div>

		  <?php }?>
		  <?php }
			}
			?>
            <script type="text/javascript">
				$(document).ready(function()
				{
					$(".popup").colorbox();
				});
			</script>
            <?php 
    }
?>