<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Tag extends CI_Controller {

	public function __construct() {
		parent::__construct();
		date_default_timezone_set("Asia/Kolkata");
        $this->load->model('Tag_model');
		$this->load->model('Dashboard_model');
		
		if(!$this->session->userdata('logged_in')) {
			redirect(base_url('Login'));
		}
 	}
	
	function listTags(){
		$template['page'] = 'Tag/viewTagList';
        $template['menu'] = 'Tag Management';
        $template['smenu'] = 'View Tag List';
        $template['pTitle'] = "Tag Management";
        $template['pDescription'] = "View Tag List";

        $template['tag_data'] = $this->Tag_model->getTagData();
		$this->load->view('template',$template);
	}

	function addTags(){
		$template['page'] = 'Tag/tagAddForm';
        $template['menu'] = 'Tag Management';
        $template['smenu'] = 'Add Tag';
        $template['pTitle'] = "Add Tag";
        $template['pDescription'] = "Create New Tag";

		$this->load->view('template',$template);
	}

	function createTags(){
		$flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');

		if(!isset($_POST) || empty($_POST)){
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Tag/addTags'));
        }

		$err = 0;
        $errMsg = '';
        if($err == 0 && (!isset($_POST['tag_name']) || empty($_POST['tag_name']))){
            $err = 1;
            $errMsg = 'Provide a Tag Name';
        }

        $status = $this->Tag_model->createTag($_POST);
        if($status == 1){
            $flashMsg['class'] = 'success';
            $flashMsg['message'] = 'Tag Created';

            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Tag/listTags'));
        }
        $this->session->set_flashdata('message',$flashMsg);
        redirect(base_url('Tag/addTags'));
	}

	function editTags($tag_id=''){
        $flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
		if(empty($tag_id) || empty(decode_param($tag_id))){
			$this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Tag/listTags'));
		}
		$template['page'] = 'Tag/tagAddForm';
        $template['menu'] = 'Tag Management';
        $template['smenu'] = 'Edit Tag';
        $template['pTitle'] = "Edit Tag";
        $template['pDescription'] = "Update Tag Data";

        $template['tag_id'] = $tag_id;
        $template['tag_data'] = $this->Tag_model->getTagData(decode_param($tag_id));

		$this->load->view('template',$template);
	}

	function updateTags($tag_id=''){
		$flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');

		if(!isset($_POST) || empty($_POST) || empty($tag_id)){
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Tag/listTags'));
        }

        $err = 0;
        $errMsg = '';
        if($err == 0 && (!isset($_POST['tag_name']) || empty($_POST['tag_name']))){
            $err = 1;
            $errMsg = 'Provide a Tag Name';
        } 
        
        $status = $this->Tag_model->updateTags(decode_param($tag_id),$_POST);
        if($status == 1){
            $flashMsg['class'] = 'success';
            $flashMsg['message'] = 'Tag Created';

            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Tag/listTags'));
        }
        $this->session->set_flashdata('message',$flashMsg);
        redirect(base_url('Tag/editTags/'.$tag_id));
	}

    function changeStatus($tag_id = '',$status = '1'){
        $flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
        if(empty($tag_id)){
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Tag/listTags'));
        }
        $tag_id = decode_param($tag_id);
        $status = $this->Tag_model->changeStatus($tag_id,$status);
        if(!$status){
            $this->session->set_flashdata('message',$flashMsg);
        }
        redirect(base_url('Tag/listTags'));
    }

    function getTagData(){
        $resArr = array('status'=>0);
        if(!isset($_POST) || empty($_POST) || !isset($_POST['tag_id']) || empty($_POST['tag_id']) || 
           empty($tag_id = decode_param($_POST['tag_id']))){
            echo json_encode($resArr);exit;
        }
        $tagData = $this->Tag_model->getTagData($tag_id);
        if(!empty($tagData)){
            $resArr['status'] = 1;
            $resArr['data'] = $tagData;
        }
        echo json_encode($resArr);exit;
    }
}
?>