<?php 
include "bank.php";


//AND uptodate > '".time()."'
$result = $conn->query("SELECT * FROM `users` WHERE token = '".$_GET['token']."'  ");
		 if ($result->num_rows > 0) {
				// output data of each row
				$row = $result->fetch_assoc();
				
				
				
					
						
						
						$row['mood'] = 1;
					
					// print_r($row );
					$uid = $row['id'];
					$packdata =  packim($row['level']);
					$noom = json_decode($row['numbers']);
					$price = json_decode($packdata['smsprice'] , true);
					//print_r($noom);
					//print_r($price);
					$smsprice = array();
					$options = json_decode($packdata['options'] , true);
					foreach($noom as $val){
						$nval = $price[$val];
						array_push($smsprice,$nval);

					}
					$news = $conn->query("SELECT * FROM news ")->num_rows;
					//print_r($smsprice);
					$exp = floor(($row['exp']-time())/86400);
					if($exp < 0){
						$exp = 0;
					}
					
					if($row['credit'] < 1){
						$row['credit'] = 1;
					}
					
					
					$udata = array
					(
						'id'=> $row['id'],
						'userstats' => '1',
						'name'=> $row['name'],
						'mobile'=>$row['mobile'],
						'username'=>$row['mobile'],
						'mood'=>$row['mood'],
						'numbers'=> $noom ,
						'options'=>$options,
						'smsprice'=>$smsprice,
						'st'=>$row['st'],
						'exp'=>$exp,'expDate'=>date("Y/n/d", $row['exp']),
						'level'=>$row['level'],
						'token'=>$row['token'],
						'credit'=>$row['credit'],
						'newsnum'=>$news,
						'sync'=>$row['sync'],
						'sendsim'=>$row['sendsim'],
						'simMessage'=>'متاسفانه امکان استفاده از ارسال سیم کارت برای شما میسر نیست'
					);
					
					if(isset($_POST['gettype']) && $_POST['gettype'] == "userinfo" ){
						
						echo json_encode($udata);
						
					}elseif(isset($_POST['gettype']) && $_POST['gettype'] == "credit" ){
						
						echo json_encode(array('credit'=>$row['credit']));
						
					}elseif(isset($_POST['gettype']) && $_POST['gettype'] == "changepass" ){
						
						$pass = sanitize($_POST['password']);
						
						
						
					}elseif(isset($_POST['gettype']) && $_POST['gettype'] == "checkForUpdate" ){
						
						echo json_encode(array('versionNum'=>$vnum,'versionLink'=>$vlink), JSON_UNESCAPED_UNICODE);
						
					}elseif(isset($_POST['gettype']) && $_POST['gettype'] == "sync" ){
						
						
						
						
						if($_POST['syncType'] == "list"){
							$res = $conn->query("SELECT * FROM backup WHERE uid=".$uid);
							$newss = array();
									while($row = $res->fetch_assoc()) {
										$data = array
											(
												'id'=> $row['id'],
												'date'=>pdate("Y/n/d H:i:s",$row['date'])
											);
											
										array_push($newss,$data);
									}
									
									echo json_encode($newss, JSON_UNESCAPED_UNICODE);
							
						}elseif($_POST['syncType'] == "up"){
							
							$ndata = $_POST['syncData'];
							$p = json_encode($_POST, JSON_UNESCAPED_UNICODE);
							
							
							$sql = "INSERT INTO backup (uid, data, date, p) VALUES ('".$uid."', '".$ndata."', '".time()."', '".$p."')";

								if ($conn->query($sql) === TRUE) {
									echo "اطلاعات نرم افزار پشتیبان گیری شد";
								} else {
									echo "خطای دیتا بیس : " . $conn->error;
								}
						}elseif($_POST['syncType'] == "get"){
							
							$res = $conn->query("SELECT * FROM backup WHERE uid=".$uid." AND id=".$_POST['id']);
							if ($res->num_rows > 0) {
								$row = $res->fetch_assoc();
								echo $row['data'];
							}else{
								echo "data not found!";
							}
						}
						
					}elseif(isset($_POST['gettype']) && $_POST['gettype'] == "sendTicket" ){
						if($_POST['ticketTitle'] !== "" && $_POST['ticketText'] !== ""){
							
							$ticket['title'] = sanitize($_POST['ticketTitle']);
							$ticket['text'] = sanitize($_POST['ticketText']);
							$ticket['text'] = str_replace(array("\r","\n"),"/*/",$ticket['text']);
							$tID = sanitize($_POST['ticketId']);
							
							echo json_encode($ticket, JSON_UNESCAPED_UNICODE);
							//exit();
							$sql = "INSERT INTO tickets (uid, data, date, ansfor, mood) VALUES ('".$uid."', '".json_encode($ticket, JSON_UNESCAPED_UNICODE)."', '".time()."', '".$tID."', '0')";
							$conn->query("UPDATE tickets SET mood='0' WHERE id=$tID");
							if ($conn->query($sql) === TRUE) {
								echo "تیکت با موفقیت  ثبت شد";
							} else {
								echo "Error: " . $sql . "<br>" . $conn->error;
							}
							
						}else{
							echo "فیلد های عنوان و متن تیکت نباید خالی باشد";
						}
					}elseif(isset($_POST['gettype']) && $_POST['gettype'] == "accesslist" ){
						$numbers = array(
							array('number'=>'985000145','price'=>'123'),
							array('number'=>'985000949','price'=>'123'),
							array('number'=>'98100009','price'=>'133'),
							array('number'=>'98sim','price'=>'143'),
							array('number'=>'985000415','price'=>'123'),
							array('number'=>'98100020400','price'=>'133'),
							array('number'=>'985000125475','price'=>'123')
						);
						echo json_encode($numbers);
					}
					elseif(isset($_POST['gettype']) && $_POST['gettype'] == "tickets" ){
					$sql = "SELECT * FROM tickets WHERE ansfor=0 AND uid=".$uid." ORDER BY id DESC";
						$result = $conn->query($sql);

						if($result->num_rows > 0){
							// output data of each row
							$tikets = array();
							while($row = $result->fetch_assoc()) {
								$row['ansmood'] = ansmood($row['mood']);
								$indata = json_decode($row['data'] , true);
								$indata['text'] = str_replace("/*/","\n",$indata['text']);
								$indata['date'] = pdate("Y/n/d H:i",$row['date']);
								if($row['ansby'] == 0){ $indata['from'] = "شما"; }else{ $indata['from'] = "مدیر"; }
								//$row['saed'] =  $indata;
									$sql2 = "SELECT * FROM tickets WHERE ansfor='".$row['id']."' AND uid=".$uid." ORDER BY id ASC";
									$row['inin'] = array();
									array_push($row['inin'],$indata);
									$res = $conn->query($sql2);
									while($subrow = $res->fetch_assoc()) {
										$indata2 = json_decode($subrow['data'] , true);
										$indata2['text'] = str_replace("/*/","\n",$indata2['text']);
										//print_r($indata2);
										$indata2['date'] = pdate("Y/n/d H:i",$subrow['date']);
										if($subrow['ansby'] == 0){ $indata2['from'] = "شما"; }else{ $indata2['from'] = "مدیر"; }
										array_push($row['inin'],$indata2);
										//
										//echo $subrow['data'];
									}
									
									unset($row['mood']);
									unset($row['data']);
									unset($row['uid']);
									unset($row['date']);
									unset($row['ansby']);
									unset($row['ansfor']);
								//print_r($indata);
								array_push($tikets,$row);
							}
							echo json_encode($tikets, JSON_UNESCAPED_UNICODE);
						} else {
							echo "0 results";
						}
					
					
					}
					elseif(isset($_POST['gettype']) && $_POST['gettype'] == "viweTicket" ){
					$id = sanitize($_POST['ticketId']);
					$sql = "SELECT * FROM tickets WHERE id=".$id;
						$result = $conn->query($sql);

						if($result->num_rows > 0){
							// output data of each row
							
							$row = $result->fetch_assoc();
								$row['ansmood'] = ansmood($row['mood']);
								$indata = json_decode($row['data'] , true);
								$indata['date'] = pdate("Y/n/d H:i",$row['date']);
								if($row['ansby'] == 0){ $indata['from'] = "شما"; }else{ $indata['from'] = "مدیر"; }
								//$row['saed'] =  $indata;
									$sql2 = "SELECT * FROM tickets WHERE ansfor='".$row['id']."' AND uid=".$uid;
									$row['inin'] = array();
									array_push($row['inin'],$indata);
									$res = $conn->query($sql2);
									while($subrow = $res->fetch_assoc()) {
										$indata2 = json_decode($subrow['data'] , true);
										$indata2['date'] = pdate("Y/n/d H:i",$subrow['date']);
										if($subrow['ansby'] == 0){ $indata2['from'] = "شما"; }else{ $indata2['from'] = "مدیر"; }
										array_push($row['inin'],$indata2);
									}
									unset($row['mood']);
									unset($row['data']);
									unset($row['uid']);
									unset($row['date']);
									unset($row['ansby']);
									unset($row['ansfor']);
								//print_r($row);
								//array_push($tikets,$row);
							
							echo json_encode($row, JSON_UNESCAPED_UNICODE);
						} else {
							echo "0 results";
						}
					}elseif(isset($_POST['gettype']) && $_POST['gettype'] == "news" ){
						//echo 1;
						$res = $conn->query("SELECT * FROM news ");
						$newss = array();
									while($row = $res->fetch_assoc()) {
										$data = array
											(
												'id'=> $row['id'],
												'title'=> $row['title'],
												'content'=>$row['con'],
												'mood'=>pdate("Y/n/d H:i",$row['date'])
											);
											
										array_push($newss,$data);
									}
									
									echo json_encode($newss, JSON_UNESCAPED_UNICODE);
					
					}else{
						include "home.php";
					}
			} else {
				//echo "0 results";
				$data = array("userstats" => "0" ,"msg" => "اعتبار ورود منقضی شده است با خروج از پنل نرم افزار دوباره وارد شوید" );
				echo json_encode($data );
			}

	




?>