<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Team extends CI_Controller
{

    public function __construct()
    {
        
        parent::__construct();
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method');
        header('Content-Type: application/json');
        header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE');
        $method = $_SERVER['REQUEST_METHOD'];
        if ($method == "OPTIONS") {
            die();
        }

        $this->load->helper('form');
        $this->load->helper(['jwt', 'authorization']);
       
    }

    

  
    public function add_team()
    {
        
        $json_data = json_decode(@file_get_contents('php://input'), true);
        
        // check if email exists first
       
        $email = $json_data['email'];
        

        $check_email =   $this->db->get_where('team', array("email" => $email))->row();
        
        if (($check_email != null)) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'error' => "An account with $email exists, please use another",
                    
                )));
            
        } else {

            $fullname =  $json_data['fullname'];
         
            $email = $json_data['email'];

            $user_info = array(

                'fullname' => $fullname,
                'email' => $email,
                'responsibility' => $json_data['responsibility'],
                'password' => $json_data['password'],
                'date_created' => time(),
                'phone_number' => $json_data['phone_number']

            );


            if ($this->db->insert('team', $user_info)) {
                
                return $this->output
                        ->set_content_type('application/json')
                        ->set_status_header(200)
                        ->set_output(json_encode(array(
                            'success' => true,
                            'id'=>$this->db->insert_id(),
                            'message' => 'account created successfully',
                            'fullname'=>$json_data['fullname'],
                            'date_created'=>$user_info['date_created'],
                            'email'=>$json_data['email']
                            )));
                            
                
            }else{
                  return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => false,
                        'message' => 'Account not created'
                    )));  
                   
                
            }
        }
                
           
    }


    public function login(){

        $json_data = json_decode(@file_get_contents('php://input'), true);
        $email = $json_data['email'];
        $password = $json_data['password'];

        // check if email exists first

        $check_email =  $result = $this->db->query('SELECT * FROM team WHERE email = "' . $email . '"  ');

        if ($check_email->num_rows() == 0) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => "$email not found"
                )));
        } else {

            // if we have an email
            
            $user =  $this->db->get_where('team', array('email' => $email))->row();
            
        
            if ($password == $user->password) {
                $token = AUTHORIZATION::generateToken(['timestamp'=>time(),'data'=>$user]);

                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => true,
                        'id'=>$user->id,
                        'fullname'=>$user->fullname,
                        'responsibility'=>$user->responsibility,
                        'email'=>$user->email,
                        'token'=>$token,
                        'message' => 'User login successful'
                    )));
            } else {
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => false,
                        'message' => 'Wrong  password'
                    )));
            }
        }
    }


public function get_team(){
    $this->db->select('id,fullname,responsibility,email,phone_number');
        $this->db->from('team');
        $query = $this->db->get();

        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'team' => $query->result_array()
            )));
}

public function delete_team($id){
      if ($this->input->method() != 'delete') {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'wrong request method used'
                )));
        }

        $product = $this->db->get_where('team', array('id' => $id))->row();
        if ($product == null) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'team member not found'
                )));
        }
        
         // else delete
        $this->db->where('id', $id);
        if ($this->db->delete('team')) {
            
        return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'Team member deleted successfully'
                )));
        } else {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'Team member not deleted'
                )));
        }
}


public function edit_team($id){
      $json_data = json_decode(@file_get_contents('php://input'), true);
      
            $fullname =  $json_data['fullname'];
         
            $email = $json_data['email'];

            $user_info = array(

                'fullname' => $fullname,
                'email' => $email,
                'responsibility' => $json_data['responsibility'],
                'password' => $json_data['password'],
                'phone_number' => $json_data['phone_number']

            );

            
            $this->db->where('id',$id);
            if($this->db->update('team',$user_info)){
                
                    return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => true,
                        'message' => 'Account edited successfully'
                    )));
            }else{
                 return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => false,
                        'message' => 'Account not edited '
                    )));
            }

    
}

    
}
