<?php 

class Applist extends baseController
{
	
	
	  function add()
	  {
          
			$data = $this->getDataRequest();
			if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') :
			$name = $data->name;				
			$username = $data->username;			
			$password = $data->password;	
			$options = ['cost' => 10];			
			$pass = password_hash($password, PASSWORD_BCRYPT, $options);           
			$get = $this->db->query("select * from users where username ='$username'");	$get->execute();            
			$count = $get->rowCount();
			if($count > 0) :                 
			echo "Username alredy exist";                 
			else : 					
			$query = $this->db->query("insert into users (name,username,password)values ('$name','$username','$pass')"); 
			echo $query ? 'success' : 'fail';
        endif;
        endif;

	  }
     
        function Remove()
          {
			  
              $data = $this->getDataRequest();
              $qry = $this->db->query("delete from applist where id =".$data->id);
              echo $qry ? "success" :  "fail";
          }

          function Update()
          {
			   $data = $this->getDataRequest();
               $folderPath = "../uploads/banner/";
			    if(!empty($_FILES['cover_image']['name'])){
				   $imgfile=rand(0,99999)."_".$_FILES['cover_image']['name'];
				   $get = $this->getBy("applist","id =".$data->edit_id);
				   $datas = $get[0];
				   
				   if($datas['cover_image']!="")
					{
					unlink('../'.$datas['cover_image']);
					}
				 $tpath = $folderPath . $imgfile;
				 $dbfile = 'uploads/banner/'.$imgfile;
				 $pic=$this->compress_image($_FILES["cover_image"]["tmp_name"], $tpath,80);
			   }else{
			      if($data->old_cover ==""){
		            $get = $this->getBy("applist","id =".$data->edit_id);
				    $datas = $get[0];
				   
				   if($datas['cover_image']!="")
					{
					unlink('../'.$datas['cover_image']);
					}
			       }
				  $dbfile = $data->old_cover;
			   } 
			   /*if(!empty($_FILES['background_image']['name'])){
				   $imgfile1=rand(0,99999)."_".$_FILES['background_image']['name'];
				   $get = $this->getBy("applist","id =".$data->edit_id);
				   $datas = $get[0];
				   
				   if($datas['background_image']!="")
					{
					unlink('../'.$datas['background_image']);
					}
				 $tpath1 = $folderPath . $imgfile1;
				 $dbfile1 = 'uploads/banner/'.$imgfile1;
				 $pic1=$this->compress_image($_FILES["background_image"]["tmp_name"], $tpath1,80);
			   }else{
			       if($data->old_background ==""){
		            $get = $this->getBy("applist","id =".$data->edit_id);
				    $datas = $get[0];
				   
				   if($datas['background_image']!="")
					{
					unlink('../'.$datas['background_image']);
					}
			       }
				  $dbfile1 = $data->old_background;
			   }*/
               $appname = !empty($data->appname) ? $data->appname : "";
              
               $tv_stream = !empty($data->tv_stream) ? $data->tv_stream : "";
               $website = !empty($data->website) ? $data->website : "";
               $facebook = !empty($data->facebook) ? $data->facebook : "";
               $whatsapp = !empty($data->whatsapp) ? $data->whatsapp : "";
               $youtube = !empty($data->youtube) ? $data->youtube : "";
               $twitter = !empty($data->twitter) ? $data->twitter : "";
               $instagram = !empty($data->instagram) ? $data->instagram : "";
               $tiktok = !empty($data->tiktok) ? $data->tiktok : "";
               $facebook_id = !empty($data->facebook_id) ? $data->facebook_id : "";
               $admob_id = !empty($data->admob_id) ? $data->admob_id : "";
               $admob_banner_id = !empty($data->admob_banner_id) ? $data->admob_banner_id : "";
               $admob_interstitial_id = !empty($data->admob_interstitial_id) ? $data->admob_interstitial_id : "";
               $admob_native_id = !empty($data->admob_native_id) ? $data->admob_native_id : "";
               $start_color = !empty($data->start_color) ? $data->start_color : "";
               $end_color = !empty($data->end_color) ? $data->end_color : "";
               $about = !empty($data->aboutus) ? $data->aboutus : "";
               $service = !empty($data->service) ? $data->service : "";
                $privacy_policy = !empty($data->privacy_policy) ? $data->privacy_policy : "";
               
               
                $sql = "UPDATE applist SET app_name = :app_name,tv_stream = :tv_stream,website = :website,facebook = :facebook,whatsapp = :whatsapp,youtube = :youtube,twitter= :twitter,instagram = :instagram,tiktok = :tiktok,facebook_id = :facebook_id,admob_id= :admob_id,admob_banner_id = :admob_banner_id,admob_interstitial_id = :admob_interstitial_id,admob_native_id = :admob_native_id,start_color = :start_color,end_color = :end_color,cover_image =:cover_image,about_us = :about_us,our_service = :our_service,privacy_policy =:privacy_policy  WHERE id = :id";
                 $query = $this->db->prepare($sql);  
                 $query->bindParam(':app_name',$appname);
                 $query->bindParam(':tv_stream',$tv_stream);
                 $query->bindParam(':website',$website);
                 $query->bindParam(':facebook',$facebook);
                 $query->bindParam(':whatsapp',$whatsapp);
                 $query->bindParam(':youtube',$youtube);
                 $query->bindParam(':twitter',$twitter);
                 $query->bindParam(':instagram',$instagram);
                 $query->bindParam(':tiktok',$tiktok);
                 $query->bindParam(':facebook_id',$facebook_id);
                 $query->bindParam(':admob_id',$admob_id);
                 $query->bindParam(':admob_banner_id',$admob_banner_id);
                 $query->bindParam(':admob_interstitial_id',$admob_interstitial_id);
                 $query->bindParam(':admob_native_id',$admob_native_id);
                 $query->bindParam(':start_color',$start_color);
                 $query->bindParam(':end_color',$end_color);
                 $query->bindParam(':cover_image',$dbfile);
                 $query->bindParam(':about_us',$about);
                 $query->bindParam(':our_service',$service);
                 $query->bindParam(':privacy_policy',$privacy_policy);
                 $query->bindParam(':id',$data->edit_id);
                 $query->execute();
				 
				echo $query ? "success" : "fail";
            
          }
		  function Upstatus()
          {
			 
			 $data = $this->getDataRequest();
			 
             $get = $this->getBy("applist","id =".$data->id);
			 $data = $get[0];
			 
             if($data['status'] == "0" ){
				 $val = "1";
			 }else{
				$val = "0";
			 }
			$query = $this->db->query("update applist set status = '".$val."' where id = ".$data['id']);
			echo $query ? "success" :  "fail";
			
			//print_r($query);
          }

}
?>