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

class Afiliados extends CI_Controller {

	public function index(){
        $this->login->checkSession();
        $userID = $this->session->id;
        $data['nomepagina'] = 'Painel do Afiliado';
		$data['config'] = $this->app->config();

        $this->db->where('id', $userID );
		$data['usuario'] = $this->db->get('usuarios')->result();

        $query = $this->db->query("SELECT COUNT(*) as total_rows FROM usuarios WHERE afiliado = " . $data['usuario'][0]->usuario);
        if ($query) {
            if ($query->num_rows() > 0) {
                $result = $query->row();
                $data['total_indicados'] = $result->total_rows;
            } else {
                $data['total_indicados'] = 0;
            }
        } else {
            $data['total_indicados'] = 0;
        }
        
        $data['configAfiliado'] = $this->cpa->checkAfiliadoCustomCPA($userID);
        $data['geralCPA'] = $this->cpa->geralCPA();

        $this->db->where('usuario', $userID);
		$data['afiliado'] = $this->db->get('afiliado')->result();

        $historico_ids = array('Crédito REV', 'Crédito CPA');

        $this->db->select('*');
        $this->db->from('historico');
        $this->db->where('usuario', $userID);
        $this->db->where_in('pagina', $historico_ids);

        $data['historico'] = $this->db->get()->result();

        $data['configRef']  = $this->app->configRef();
        $data['bonus'] = $this->app->configFin('deposit-bonus-text');
		$data['minSaque'] = $this->app->configFin('min-saque');
        $data['EnableCashback'] = $this->app->configFin('cashback-enable-text');

        $this->load->view('pages/layout/header', $data);
        $this->load->view('pages/layout/modals', $data);
        $this->load->view('pages/minhaconta/painel_afiliado', $data);
		$this->load->view('pages/layout/footer', $data);
    }

}