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


class MarketingTaskCtrl extends CI_Controller
{
	public function __construct($config = 'rest')
	{
		header('Access-Control-Allow-Origin: *');
		header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
		parent::__construct();
		$this->load->model('Common_m');
	}

	public function task_view()
	{
		chk_login();
		$data['tasks']=$this->Common_m->single_join_query(
			'mt.id,mt.task,mc.category_name,mc.logo,mc.note',
			'marketing_tasks as mt',
			'marketing_category as mc','mc.id=mt.category_id',
			'mt.status=1'
		);
		$data['setting']=$this->Common_m->getRow('marketing_setting','*','id',1);

		$data['task']=$data['tasks']['0'];

		$this->load->view('templates/1_head.php');

		$this->load->view('templates/2_nav.php');

		$this->load->view('templates/3_sidebar_menu_left.php');

		$this->load->view('marketing/marchant_task',$data);

		$this->load->view('templates/5_footer.php');

		$this->load->view('templates/6_script_end.php');

	}

	public function task_store()
	{
		$data = $this->input->post();
		$data['user_id']=$_SESSION['user_id'];

		$setting = $this->Common_m->getRow('marketing_setting','task_limit','id',1);
		$taskLimit =  $setting->task_limit;
		
		$query = $this->db->query('SELECT count(id) as total_task FROM task_submit WHERE DATE(created_at)=CURRENT_DATE and user_id='.$data['user_id']);
		$totalTask = intval($query->row()->total_task);
		
		if($totalTask <= $taskLimit-1){
			$result = $this->Common_m->insert('task_submit',$data);
			if($result === true)
			{
				$this->session->set_flashdata(array("type" => "success", "message" => "Your Task Submit Successfully done!!"));
				echo json_encode(array('success'=>1));
			}
			else{
				echo json_encode(array('success'=>0));
			}
		}else{
			$this->session->set_flashdata(array("type" => "danger", "message" => "your maximum ".$taskLimit." task submitted per day !!"));
				echo json_encode(array('success'=>2));
		}
	}

	public function task_history()
	{
		chk_login();
		
		$user_id=$_SESSION['user_id'];
		
		$data['pending_tasks']=$this->Common_m->single_join_query(
			'mt.task,ts.url,ts.created_at,ts.status',
			'task_submit as ts',
			'marketing_tasks as mt','mt.id=ts.task_id',
			'ts.status=0 AND ts.user_id='.$user_id
		);

		$data['approve_tasks']=$this->Common_m->single_join_query(
			'mt.task,ts.url,ts.created_at,ts.income,ts.status,ts.updated_at',
			'task_submit as ts',
			'marketing_tasks as mt','mt.id=ts.task_id',
			'ts.status=1 AND ts.user_id='.$user_id
		);

		$this->load->view('templates/1_head.php');

		$this->load->view('templates/2_nav.php');

		$this->load->view('templates/3_sidebar_menu_left.php');

		$this->load->view('marketing/task_list',$data);

		$this->load->view('templates/5_footer.php');

		$this->load->view('templates/6_script_end.php');

	}
}
