<?php
class Product extends Controller
{
    function __construct()
    {
        parent::__construct();

        $this->load->library('session');
        $this->load->library('PaypalExpress');
        $this->load->model('mproduct');

        if ($_SESSION['user_id'] == '' && $this->uri->segment(2) != 'login') {
            //$_SESSION['prod_id'] = $this->uri->segment(3);
            //redirect('product/login');
        }
    }

    function index()
    {
        if ($this->uri->segment(2) != 'index')
            redirect('product/index');

        /*if ($this->uri->segment(3) == '' || !is_numeric($this->uri->segment(3))) {
            $this->load->view('product/404.php');
            return;
        }*/

        $product_id         = $this->uri->segment(3);
        $payData            = array();
        $payData['product'] = $this->mproduct->getProduct(687);

        /*if (empty($payData['product'])) {
            $this->load->view('product/404.php');
            return;
        }*/

        $this->load->view('product/index.php', $payData);
    }

    function login()
    {
        if ($_SESSION['user_id'] != '')
            redirect('product');

        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $payData = array();

        if ($_POST['login-btn']) {
            $user = $this->mpay->login($username, $password);

            if (!$user) {
                $payData['error'] = 'Wrong username or password';
            } else {
                $_SESSION['user_id'] = $user->nUserID;
                redirect('product/index/' . $_SESSION['prod_id']);
            }
        }

        $this->load->view('product/login.php', $payData);
    }

    function success()
    {
        $this->load->view('pay/success.php');
    }
}
