<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

class Register extends CI_Controller {

    
    public function __construct()
    {
        parent::__construct();
        if($this->session->userdata('id_users') != null)
        {
            
            redirect('Home','refresh');
            
        }
    }
    

    public function index()
    {
        $this->load->view('Header');
        $this->load->view('Register');
        $this->load->view('Footer');
    }

    public function submit()
    {
        $this->load->helper('string');
        $username = $this->input->post("username");
        $password = $this->input->post("pass");
        $cf = $this->input->post("cfpassword");
        $email = $this->input->post("email");
        $ref = $this->input->post('ref');

        $this->db->or_where('username', $username);
        $this->db->or_where('email', $email);
        $query = $this->db->get('users', 1);
        if($username != null && $password != null && $email != null)
        {
            if($password == $cf)
            {
                if($query->num_rows() == 0)
                {
                    $object = array(
                        'username'  =>  $username,
                        'password'  =>  sha1($password.'fbgray'),
                        'email'     =>  $email,
                        'type'      =>  'Reg',
                    );
                    $this->db->insert('users', $object);
                    echo json_encode(['status' => 1, 'msg' => 'Success']);
                }else{
                    echo json_encode(['status' => 0, 'msg' => 'Faill']);
                }
            }else{
                echo json_encode(['status' => 3, 'msg' => 'Faill']);
            }
        }else{
            echo json_encode(['status' => 2, 'msg' => 'Faill']);
        }
    }

    public function fb()
    {
        $this->load->helper('string');
        $name = $this->input->post("name");
        $email = $this->input->post("email");
        $fbid = $this->input->post("fbid");
        if($fbid != null)
        {
            $this->db->where('fbid', $fbid);
            $query = $this->db->get('users', 1);
        if($query->num_rows() == 0)
        {
            $object = array(
                'fbid' => $fbid,
                'username' => $name,
                'email' => $email,
                'type'  => 'Facebook',
                'date'  => date("Y-m-d")
            );
            $this->db->insert('users', $object);
            $this->db->where('id_users', $this->db->insert_id());
            $query = $this->db->get('users', 1);
            $data = $query->row_array();
            $this->session->set_userdata($data);
            echo json_encode(['status' => 1, 'msg' => 'Success']);
        }else{
            $data = $query->row_array();
            $this->session->set_userdata($data);
            echo json_encode(['status' => 2, 'msg' => 'Success']);
        }
        }
    }
}

/* End of file Register.php */
