<?php

namespace App\Controllers;

use App\Models\Md_laptop;
$session = \Config\Services::session();

class Laptop extends BaseController
{
    
    
    public function __construct() {
         $this->Md_laptop = new Md_laptop();
    }
   
    public function PendingList() 
    {
        header("Access-Control-Allow-Origin: *");
        // If you need to allow specific methods
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
        // If you need to allow specific headers
        header("Access-Control-Allow-Headers: Content-Type, Authorization");
        $datas = json_decode(file_get_contents("php://input"), true);
      
    
        $userid=$datas['userid'];
        $module=$datas['module'];
        
        $final = array();
        $ArrayData = array();
        
        if (!empty($userid) && !empty($module))
        {
            $userResult=$this->Md_laptop->PendingStd($userid);
            
            if(empty($userResult))
            {
                $final = array('status' => 2,'res_result'=>'','msg'=>"Record list is empty.");
            }else {
            $final = array('status' => 1,'res_result'=>$userResult, 'msg'=>"Pending Student list fetch successfully.");
            }
        } else {
            $final = array('status' => 2,'res_result'=>'','msg'=>"User does not perform this action.");
        }
        echo json_encode($final);
        exit();
    }
    
    public function ConfirmList() 
    {
        header("Access-Control-Allow-Origin: *");
        // If you need to allow specific methods
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
        // If you need to allow specific headers
        header("Access-Control-Allow-Headers: Content-Type, Authorization");
        $datas = json_decode(file_get_contents("php://input"), true);
      
    
        $userid=$datas['userid'];
        $module=$datas['module'];
        
        $final = array();
        $ArrayData = array();
        
        if (!empty($userid) && !empty($module))
        {
            $userResult=$this->Md_laptop->ConfirmStd($userid);
            
            if(empty($userResult))
            {
                $final = array('status' => 2,'res_result'=>'','msg'=>"Record list is empty.");
            }else {
            $final = array('status' => 1,'res_result'=>$userResult, 'msg'=>"Student list fetch successfully.");
            }
        } else {
            $final = array('status' => 2,'res_result'=>'','msg'=>"User does not perform this action.");
        }
        echo json_encode($final);
        exit();
    }
    
    public function RejectedList() 
    {
        header("Access-Control-Allow-Origin: *");
        // If you need to allow specific methods
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
        // If you need to allow specific headers
        header("Access-Control-Allow-Headers: Content-Type, Authorization");
        $datas = json_decode(file_get_contents("php://input"), true);
      
    
        $userid=$datas['userid'];
        $module=$datas['module'];
        
        $final = array();
        $ArrayData = array();
        
        if (!empty($userid) && !empty($module))
        {
            $userResult=$this->Md_laptop->RejectedStd($userid);
            
            if(empty($userResult))
            {
                $final = array('status' => 2,'res_result'=>'','msg'=>"Record list is empty.");
            }else {
            $final = array('status' => 1,'res_result'=>$userResult, 'msg'=>"Student list fetch successfully.");
            }
        } else {
            $final = array('status' => 2,'res_result'=>'','msg'=>"User does not perform this action.");
        }
        echo json_encode($final);
        exit();
    }
    
       public function StudentDetails() 
    {
        header("Access-Control-Allow-Origin: *");
        // If you need to allow specific methods
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
        // If you need to allow specific headers
        header("Access-Control-Allow-Headers: Content-Type, Authorization");
        $datas = json_decode(file_get_contents("php://input"), true);
      
    
        $userid=$datas['userid'];
        $studentid=$datas['studentid'];
        $module=$datas['module'];
        
        $final = array();
        $ArrayData = array();
        
        if (!empty($userid) && !empty($studentid) && !empty($module))
        {
            $userResult=$this->Md_laptop->StudentDetails($studentid);
            
            if(empty($userResult))
            {
                $final = array('status' => 2,'res_result'=>'','msg'=>"Record details is empty.");
            }else {
            $final = array('status' => 1,'res_result'=>$userResult, 'msg'=>"Student details fetch successfully.");
            }
        } else {
            $final = array('status' => 2,'res_result'=>'','msg'=>"User does not perform this action.");
        }
        echo json_encode($final);
        exit();
    }
    
    
     public function StudentAction() 
    {
        header("Access-Control-Allow-Origin: *");
        // If you need to allow specific methods
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
        // If you need to allow specific headers
        header("Access-Control-Allow-Headers: Content-Type, Authorization");
        $datas = json_decode(file_get_contents("php://input"), true);
      
    
        $userid=$datas['userid'];
        $studentid=$datas['studentid'];
        $module=$datas['module'];
        $laptopconfig=$datas['laptopconfig'];
        $remark=$datas['remark'];
        $status=$datas['status'];

        
        $final = array();
        $ArrayData = array();
        
        if (!empty($userid) && !empty($studentid) && !empty($status) && !empty($module))
        {
            $updatestatus=null;
            if($status=="Approved")
            {
                $updatestatus=2;
            }
             if($status=="Rejected")
            {
                $updatestatus=3;
            }
            
                
            $table = "mvpm_laptop";
            $condition = array('pk_id' =>$studentid);
             $updareData= array('assign_laptop_confg'=>$laptopconfig,
                    'assign_remark'=>$remark,
                    'assign_status'=>$updatestatus,
                    'assign_id'=>$userid,
                    'updated_date'=>date('Y-m-d H:i:s')
                    );
            $this->Md_laptop->UpdateData($table, $condition, $updareData);
        
            $final = array('status' => 1,'res_result'=>$studentid, 'msg'=>"Student details updated successfully.");
            
        } else {
            $final = array('status' => 2,'res_result'=>'','msg'=>"User does not perform this action.");
        }
        echo json_encode($final);
        exit();
    }
    
}
