<?php

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



class ProductSellCtrl 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 index()
	{
		chk_login();

		$this->load->model('Affiliate_m');

		$data['all_income'] = $this->Affiliate_m->all_income_history($this->session->user_id);
		$data['user_id'] = $this->session->userdata('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('pdt_sell', $data); //4_dynamic

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

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

	public function set_income()
	{
		$cashback = $_GET['cashback'];
		$user_id = $_GET['user_id'];
		$blance_cost = $_GET['blance_cost'];

		$msg = "Dear User,Your " . $cashback . "tk product cashback added in your wallet.Thank You for Mali-Mart";

		$user = $this->db->query("SELECT mobile_num FROM `users_login` WHERE user_id=$user_id");

		if ($cashback != 0) {
			$this->load->library('sms_send');
			$this->db->query("UPDATE `total_balance` SET `cashblnc`=cashblnc+$cashback WHERE user_id=$user_id");
			$this->sms_send->send_sms($user->row()->mobile_num, $msg);
		}
		if ($blance_cost != 0) {
			$this->db->query("UPDATE `total_balance` SET `cashblnc`=cashblnc-$blance_cost WHERE user_id=$user_id");
		}

		$this->load->model('Affiliate_m');
		$this->Affiliate_m->add_affiliate_income($_GET['ui'], $_GET['pc'], $_GET['price'],  0, $_GET['income']);
		$this->Affiliate_m->add_affiliate_income_intotal($_GET['ui'], $_GET['income']);
		echo json_encode("success");
	}

	public function pdt_sell_history()
	{
		chk_login();

		$this->load->model('Affiliate_m');

		$data['all_income'] = $this->Affiliate_m->all_income_history($this->session->user_id);
		$data['user_id'] = $this->session->userdata('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('pdt_history', $data); //4_dynamic

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

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

	public function sell_income($marchant_id, $product_income)
	{
		$exist_blance = $this->Common_m->getRow('total_balance', 'product_sell', 'user_id', $marchant_id - 1000);
		$user = $this->Common_m->getRow('users_login', 'mobile_num', 'user_id', $marchant_id - 1000);

		$this->db->trans_start();

		$new_blance = $exist_blance->product_sell + $product_income;
		$this->Common_m->updateData('total_balance', 'user_id', $marchant_id - 1000, array('product_sell' => $new_blance));
		$this->db->trans_complete();

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			echo json_encode(['msg' => 0]);
		} else {
			$this->load->library('sms_send');
			$this->db->trans_commit();
			$msg = "Dear Merchant your product sell added Successfully " . number_format($product_income, 2) . " Tk.please check your product sell income.  Thank You for Mali-Mart";
			$this->sms_send->send_sms($user->mobile_num, $msg);
			echo json_encode(['msg' => 1]);
		}
	}
}
