<?php
include("../db.php");
class send_data
{
	public $db;
	public $txt;
	public function send($db,$txt)
	{
		mysqli_query($db,$txt);
		$data['a'] = 1;
		echo json_encode($data);
	}
	public function request($db,$txt)
	{
		$query = mysqli_query($db,$txt);
		while($row = mysqli_fetch_assoc($query))
		{
			$data[] = $row;
		}
		echo json_encode($data);
	}
	public function get_num($db,$txt)
	{
		$query = mysqli_query($db,$txt);
		$num = mysqli_num_rows($query);
		$data['num'] = $num;
		echo json_encode($data);
	}
	public function get_end($db,$txt)
	{
		$date_page = date('Y-m-d');
		$q = "select * from `cus` left join `member` on `member`.`id_member` = `cus`.`id_member` where `cus`.`id_cus` = '$txt'";
		$query = mysqli_query($db,$q);
		$row = mysqli_fetch_assoc($query);
		$data['type'] = $row['type_member'];
		$data['id_cus'] = $row['id_cus'];
		$data['count_game'] = $row['count_game'];
		$data['count_s'] = $row['count_s'];
		$data['price_game'] = $row['price_game'] + $row['mony_cus'];
		$id_cus = $row['id_cus'];
		$date_today = $date_page;
		$query_1 = mysqli_query($db,"select * from `table_day` where `id_cus` = '$id_cus' and `date_table` = '$date_page'");
		$count_query_1 = mysqli_num_rows($query_1);
		$data['count_rev_today'] = $count_query_1;
		$data['type_rev_cus'] = $row['type_stop'];
		if($row['date_game'] == 0)
		{
			$day = 1;
			$data['day'] = 1;
		}
		if($row['date_game'] == 1)
		{
			$data['day'] = 7;
		}
		if($row['date_game'] == 2)
		{
			$data['day'] = 30;
		}
		if($row['date_game'] == 3)
		{
			$data['day'] = 365;
		}
		if($row['date_game'] == 4)
		{
			$data['day'] = 90;
		}
		if($row['date_game'] == 5)
		{
			$data['day'] = 180;
		}
		if($row['date_game'] == 6)
		{
			$data['day'] = 270;
		}
				if($row['date_game'] == 7)
		{
			$data['day'] = 15;
		}
		if($row['date_game'] == 8)
		{
			$data['day'] = 60;
		}


		if($row['date_game'] == 9)
		{
			$data['day'] = 21;
		}
		if($row['date_game'] == 10)
		{
			$data['day'] = 45;
		}
		if($row['date_game'] == 11)
		{
			$data['day'] = 120;
		}
		if($row['date_game'] == 12)
		{
			$data['day'] = 150;
		}
		if($row['date_game'] == 13)
		{
			$data['day'] = 210;
		}
		if($row['date_game'] == 14)
		{
			$data['day'] = 240;
		}
		if($row['date_game'] == 15)
		{
			$data['day'] = 300;
		}
		if($row['date_game'] == 16)
		{
			$data['day'] = 330;
		}

		if($data['type'] == 0)
		{
			$data['end_c'] = $row['count_s'];
			$data['end_c_1'] = $row['count_s'] + $row['count_game'];
		}
		if($data['type'] == 1)
		{
			$start_date = $row['member_date'];
			$day = $data['day'];
			$data['end_d']  = date('Y-m-d', strtotime($start_date. ' + '.$day.' days'));
			$data['end_d_1']  = date('Y-m-d', strtotime($date_page. ' + '.$day.' days'));
			if($data['end_d'] > date('Y-m-d'))
			{
				$data['end_date'] = 1;
			}else{
				$data['end_date'] = 0;
			}
		}
		if($data['type'] == 2)
		{
			$start_date = $row['member_date'];
			$day = $data['day'];
			$data['end_d']  = date('Y-m-d', strtotime($start_date. ' + '.$day.' days'));
			$data['end_d_1']  = date('Y-m-d', strtotime($date_page. ' + '.$day.' days'));
			$data['end_c'] = $row['count_s'];
			$data['end_c_1'] = $row['count_s'] + $row['count_game'];
			if($data['end_d'] > date('Y-m-d'))
			{
				$data['end_date'] = 1;
			}else{
				$data['end_date'] = 0;
			}
		}
		echo json_encode($data);

	}
	public function update_mony_cus($db,$txt)
	{
		$query = mysqli_query($db,"select * from `stock_mony` left join `cus` on `cus`.`id_cus` = `stock_mony`.`id_cus` where `stock_mony`.`id_stock_mony` = '$txt'");
		$row = mysqli_fetch_assoc($query);
		$id_cus = $row['id_cus'];
		$data['paid'] = $row['paid'];
		$new_stay = $data['paid'] + $row['mony_cus'];
		$query = mysqli_query($db,"update `cus` set `mony_cus` = '$new_stay' where `id_cus` = '$id_cus'");
		echo json_encode($data);
	}
	public function update_stock_pro($db,$txt)
	{
		$query = mysqli_query($db,"select * from `sales` left join `pro` on `pro`.`id_pro` = `sales`.`id_pro` where `sales`.`id_sales` = '$txt'");
		$row = mysqli_fetch_assoc($query);
		$id_pro = $row['id_pro'];
		$data['q'] = $row['q'];
		$new_stay = $data['q'] + $row['stock_pro'];
		$query = mysqli_query($db,"update `pro` set `stock_pro` = '$new_stay' where `id_pro` = '$id_pro'");
		echo json_encode($data);
	}
	public function active_page($db,$txt)
	{
			$myfile = fopen("vendor/jquery/css/jquery/vendor/t.txt", "r") or die($data['a'] = 2);
			$t =  fread($myfile,filesize("vendor/jquery/css/jquery/vendor/t.txt"));

			$txt1 = "select * from `active` ";
			$query = mysqli_query($db,$txt1);
			$row = mysqli_fetch_assoc($query);
			$date = $row['active_end'];

			if($date == $t)
			{
				$d = date("Y-m-d");
				if($t >= $d)
				{
					$data['a'] = 1;
				}else{
					$data['a'] = 0;
				}
				
			}else{
				$data['a'] = 2;
			}
	}
	public function get_date()
	{
		$data['today'] = date("Y-m-d");
		//echo $data['today'];
		echo json_encode($data);
	}

}
$type = $_POST['type'];
$txt = $_POST['txt'];
if($type == 0)
{
	$send = new send_data();
	$send->db = $db;
	$send->txt = $txt;
	$send->send($db,$send->txt);
}
if($type == 1)
{
	$request = new send_data();
	$request->db = $db;
	$request->txt = $txt;
	$request->request($db,$request->txt);
}
if($type == 2)
{
	$num = new send_data();
	$num->db = $db;
	$num->txt = $txt;
	$num->get_num($db,$num->txt);
}
if($type == 3)
{
	$get_end_rev = new send_data();
	$get_end_rev ->db = $db;
	$get_end_rev ->txt = $txt;
	$get_end_rev ->get_end($db,$get_end_rev->txt);
}
if($type == 4)
{
	$update_mony = new send_data();
	$update_mony ->db = $db;
	$update_mony ->txt = $txt;
	$update_mony ->update_mony_cus($db,$update_mony->txt);
}
if($type == 5)
{
	$update_stock = new send_data();
	$update_stock ->db = $db;
	$update_stock ->txt = $txt;
	$update_stock ->update_stock_pro($db,$update_stock->txt);
}
if($type == 6)
{
	$active_class = new send_data();
	$active_class ->db = $db;
	$active_class ->txt = $txt;
	$active_class ->active_page($db,$active_class->txt);
}

if($type == 7)
{
	$date_t = new send_data();
	$date_t ->db = $db;
	$date_t ->txt = $txt;
	$date_t ->get_date();
}
?>