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

class MarketingCtrl extends CI_Controller
{



	public function __construct()
	{
		parent::__construct();
		$this->load->model('Common_m');
		isset($_SESSION['sec_rf_admin']) ? $_SESSION['sec_rf_admin'] == true ? null : redirect('/') : redirect('/');
	}

	public function task_create()
	{
		$data['categories'] = $this->Common_m->select_with_where('marketing_category', '*', 'status=1');

		$data['tasks'] = $this->Common_m->single_join_query(
			'mc.category_name,mt.*',
			'marketing_tasks as mt',
			'marketing_category as mc',
			'mc.id=mt.category_id',
			'mt.status=1 OR mt.status=0',
			'mt.created_at',
			'desc'
		);


		$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_create.php', $data); //4_dynamic

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

		$this->load->view('templates/6_script_end.php');
	}
	public function task_store()
	{
		$data = $this->input->post();

		$result = $this->Common_m->insert('marketing_tasks', $data);

		if ($result !== false) {
			$this->session->set_flashdata(array("title" => "Success", "text" => "Task added Successfully !!"));
			redirect('MarketingCtrl/task_create');
		}
	}
	public function task_update()
	{
		$data = $this->input->post();

		$id = $this->input->post('id');

		$this->Common_m->updateData('marketing_tasks', 'id', $id, $data);

		echo json_encode(array('success' => true));
	}

	public function task_delete()
	{
		$id = $this->input->get('id');

		$result = $this->Common_m->delete('marketing_tasks', 'id', $id);
		if ($result !== false) {
			$this->session->set_flashdata(array("title" => "Success", "text" => "Task Delete Successfully !!"));
			redirect('MarketingCtrl/task_create');
		}
	}

	public function task_status()
	{
		$id = $this->input->post('id');
		$data['status'] = ($this->input->post('status') == 1) ? 0 : (1);

		$result = $this->Common_m->updateData('marketing_tasks', 'id', $id, $data);
		$this->session->set_flashdata(array("title" => "Success", "text" => "Status Change Successfully !!"));
		if ($result !== false) {
			echo json_encode(array('success' => true));
		} else {
			echo json_encode(array('success' => false));
		}
	}



	public function category_add()
	{
		$data['categories'] = $this->Common_m->select_with_where('marketing_category', '*', 'status=1 OR status=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/category_add.php', $data); //4_dynamic

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

		$this->load->view('templates/6_script_end.php');
	}
	public function category_store($id = '')
	{

		$data = $this->input->post();

		if (!empty($_FILES["logo"]["name"])) {
			$tmp_name = $_FILES["logo"]["tmp_name"];
			$name = $_FILES["logo"]["name"];
			move_uploaded_file($tmp_name, 'uploads/social/' . $name);
			$data['logo'] = $name;
		}

		$result = $this->Common_m->insert('marketing_category', $data);

		if ($result !== false) {
			$this->session->set_flashdata(array("title" => "Success", "text" => "Category added Successfully !!"));
			redirect('MarketingCtrl/category_add');
		}
	}

	public function category_update()
	{
		$data['note'] = $this->input->post('note');
		$data['category_name'] = $this->input->post('category_name');

		$id = $this->input->post('id');
		if (!empty($_FILES["logo"]["name"])) {
			$tmp_name = $_FILES["logo"]["tmp_name"];
			$name = $_FILES["logo"]["name"];
			move_uploaded_file($tmp_name, 'uploads/social/' . $name);
			$data['logo'] = $name;
		} else {
			$data['logo'] = $this->input->post('old_image');
		}

		$this->Common_m->updateData('marketing_category', 'id', $id, $data);
		echo json_encode(array('success' => true));
	}
	public function category_delete()
	{
		$id = $this->input->get('id');

		$result = $this->Common_m->delete('marketing_category', 'id', $id);
		if ($result !== false) {
			$this->session->set_flashdata(array("title" => "Success", "text" => "Category Delete Successfully !!"));
			redirect('MarketingCtrl/category_add');
		}
	}


	public function update_status()
	{
		$id = $this->input->post('id');
		$data['status'] = ($this->input->post('status') == 1) ? 0 : (1);

		$result = $this->Common_m->updateData('marketing_category', 'id', $id, $data);
		$this->session->set_flashdata(array("title" => "Success", "text" => "Status Change Successfully !!"));
		if ($result !== false) {
			echo json_encode(array('success' => true));
		} else {
			echo json_encode(array('success' => false));
		}
	}

	public function task_req()
	{

		$data['pending_tasks'] = $this->Common_m->single_join_query(
			'ts.id,mt.task,ts.url,ts.created_at,ts.status,ts.user_id',
			'task_submit as ts',
			'marketing_tasks as mt',
			'mt.id=ts.task_id',
			'ts.status=0 OR ts.status=2'
		);

		$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'
		);


		$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_req', $data);

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

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

	public function task_accepted()
	{
		$this->load->library('sms_send');

		$id = $this->input->post('id');

		$user_id = $this->input->post('user_id');

		$user = $this->Common_m->getRow('users_login', 'mobile_num', 'user_id', $user_id);

		$t_commision = $this->Common_m->getRow('marketing_setting', 'commision', 'id', 1);

		$income_blance = $this->Common_m->getRow('total_balance', 'marketing_earn', 'user_id', $user_id);
		if ($income_blance !== false) {
			$data['marketing_earn'] = $income_blance->marketing_earn + $t_commision->commision;
			$this->Common_m->updateData('total_balance', 'user_id', $user_id, $data);
		} else {
			$data['user_id'] = $_SESSION['user_id'];
			$this->Common_m->insert('total_balance', $data);
		}

		$result = $this->Common_m->updateData('task_submit', 'id', $id, array('status' => 1, 'income' => $t_commision->commision, 'updated_at' => date('y-m-d h:i:a')));

		if ($result !== false) {
			echo json_encode(array('success' => true));
			$msg = "Dear User Your Account have added " . number_format($t_commision->commision, 2) . " taka please check your blance. Thank you for Mali-Martr";
			$this->session->set_flashdata(array("title" => "Success", "text" => "Taks Accepted  Successfully !!"));
			$this->sms_send->send_sms($user->mobile_num, $msg);
		} else {
			echo json_encode(array('success' => false));
		}
	}
	public function task_rejected()
	{


		$id = $this->input->post('id');

		$result = $this->Common_m->delete('task_submit', 'id', $id);

		if ($result !== false) {
			echo json_encode(array('success' => true));
			$this->session->set_flashdata(array("title" => "Success", "text" => "Taks Rejected  Successfully !!"));
		} else {
			echo json_encode(array('success' => false));
		}
	}
}
