<?php 
class Foodapi extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        // $this->load->model('Scheme_m');
       // $this->load->model('Scheme_m');
       date_default_timezone_set("Asia/Kolkata");
    }
    

 public function index()
    {
        echo 'test';
    }
  
   
    
    // User Registration
    public function user_registration()
    {
        $userdata = json_decode(file_get_contents('php://input'),true);
      
        
        $checkuser = $this->db->select('Uid')->where('Mobile_Number',$userdata['mobile'])->where('Deleted_Dt',NULL)->limit(1)->get('reg_users')->result();
      
            if(empty($checkuser)) {
        
                $data=array("Name"=>$userdata['name'],
        					"Mobile_Number"=>$userdata['mobile'],
        					"Flat_Num"=>$userdata['flat'],
        					"Building_Name"=>$userdata['bname'],
        					"Village"=>$userdata['village'],
        					"Taluka"=>$userdata['taluka'],
                            "Inserted_DT"=>date('Y-m-d H:i:s')
        				);
        				
        			
                        if($this->db->insert('reg_users',$data)) 
                        {
                        	$userid = $this->db->select('Uid')->where('Mobile_Number',$userdata['mobile'])->where('Deleted_Dt',NULL)->get('reg_users')->result();
                            echo json_encode(array("success"=>1,"userid"=>$userid[0]->Uid)); 
                        }else{
                            echo json_encode(array("failed"=>0)); 
                        }
            } else {
                foreach ($checkuser as $d) {
                    
                $data=array("Name"=>$userdata['name'],
        					"Mobile_Number"=>$userdata['mobile'],
        					"Flat_Num"=>$userdata['flat'],
        					"Building_Name"=>$userdata['bname'],
        					"Village"=>$userdata['village'],
        					"Taluka"=>$userdata['taluka'],
        					"Modified_Si"=>$d->Uid,
                            "Modified_By"=>$userdata['mobile'],
                            "Modified_Dt"=>date('Y-m-d H:i:s')
        				);
                        if($this->db->where('Mobile_Number',$userdata['mobile'])->update('reg_users',$data)) 
                        {
                            echo json_encode(array("success"=>1,"userid"=>$d->Uid)); 
                        }else{
                            echo json_encode(array("failed"=>0)); 
                        }
                }
            }
    }
    
    
    //Insert Order
    public function insert_order(){
         $userdata = json_decode(file_get_contents('php://input'),true);
      

                $data=array("Type"=>$userdata['ftype'],
        					"User_Name"=>$userdata['userid'],
        					"Food_Name"=>$userdata['fname'],
        					"Farmer_Name"=>$userdata['farmer'],
        					"Order_Uid"=>$userdata['orderid'],
        					"Quantity"=>$userdata['quant'],
        					"Price_Per_Kg"=>$userdata['ppkg'],
        					"Total_Price"=>$userdata['tprice'],
        					"Address_Type "=>$userdata['atype'],
        					"Flat_Num"=>$userdata['flat'],
        					"Building_Name"=>$userdata['bname'],
        					"Village "=>$userdata['village'],
        					"Taluka"=>$userdata['talluka'],
        					"Inserted_Si"=>$userdata['userid'],
        					"Inserted_By"=>$userdata['username'],
                            "Inserted_DT"=>date('Y-m-d H:i:s')
        				);
                        if($this->db->insert('trans_order',$data)) 
                        {
                            echo json_encode(array("success"=>1)); 
                        }else{
                            echo json_encode(array("failed"=>0)); 
                        }
    }
    
    //Get food list api
    public function getfood_list(){
        
         $userdata = json_decode(file_get_contents('php://input'),true);
        
       
        $rs = $this->db->select('Uid,Type,Food_Name,Description')->where('Deleted_DT',NULL)->where('Type',$userdata['type'])->order_by('Uid','DESC')->get('mst_food')->result();
        
        if(count($rs) > 0)
        {
            foreach($rs as $row)
            {
                $foodlist['foodlist'][] = $row;
            }
            echo json_encode($foodlist);
        }else{
           echo json_encode(array("empty"=>0)); 
        }
    }
    
        
    //Get farmer list api
    public function get_farmer_list(){
        
         $userdata = json_decode(file_get_contents('php://input'),true);
       
        $rs = $this->db->select('rf.Uid,rf.Name,rf.Mobile_Number,rf.Village,rf.Taluka,tfwf.Price_Per_Kg')
            ->join('trans_farmer_wise_food tfwf', 'tfwf.Farmer_Name = rf.Uid')
            ->where('tfwf.Food_Name',$userdata['fid'])
            ->where('tfwf.Deleted_DT',NULL)
            ->where('rf.Deleted_DT',NULL)
            ->order_by('tfwf.Price_Per_Kg','ASC')
            ->get('reg_farmer rf')
            ->result();
        
        if(count($rs) > 0)
        {
            foreach($rs as $row)
            {
                $farmerlist['farmerlist'][] = $row;
            }
            echo json_encode($farmerlist);
        }else{
           echo json_encode(array("empty"=>0)); 
        }
    }
    
     //Get previous order list api
    public function get_previous_order(){
        
         $userdata = json_decode(file_get_contents('php://input'),true);
       
        $rs = $this->db->select('rf.Uid,rf.Name,rf.Mobile_Number,rf.Village,rf.Taluka,to.Price_Per_Kg,food.Food_Name,to.Order_Uid,to.Inserted_Dt,to.Total_Price,to.Type')
            ->join('reg_farmer rf', 'rf.Uid = to.Farmer_Name')
            ->join('mst_food food', 'food.Uid = to.Food_Name')
            ->where('to.Inserted_Si',$userdata['userid'])
            ->where('to.Deleted_DT',NULL)
            ->where('rf.Deleted_DT',NULL)
            ->where('food.Deleted_DT',NULL)
            ->order_by('to.Inserted_Dt','DESC')
            ->get('trans_order to')
            ->result();
        
        if(count($rs) > 0)
        {
            foreach($rs as $row)
            {
                $previousorder['previousorder'][] = $row;
            }
            echo json_encode($previousorder);
        }else{
           echo json_encode(array("empty"=>0)); 
        }
    }
    
    
    //Get announcement list api
    public function getannouncement(){
        
         $userdata = json_decode(file_get_contents('php://input'),true);
        
       
        $rs = $this->db->select('Uid,Title,Description,inserted_DT')->where('Deleted_DT',NULL)->order_by('Uid','DESC')->get('mst_announcement')->result();
        
        if(count($rs) > 0)
        {
            foreach($rs as $row)
            {
                $announcement['announcement'][] = $row;
            }
            echo json_encode($announcement);
        }else{
           echo json_encode(array("empty"=>0)); 
        }
    }
    
    
}