<?php

class Currency extends CI_Controller {

    private $response;

    public function __construct(){
        parent::__construct();
        $this->response = array(
            'draw' => 0,
            'recordsTotal' => 0,
            'recordsFiltered' => 0,
            'data' => array()
        );
    }

    public function index(){
        if($this->input->is_ajax_request() && $this->session->has_userdata('logged')) {
            $currencies = $this->currency_model->getTable();
            $rows = array();
            foreach ($currencies as $currency) {
                $row = array();
                $row[0] = $this->currency_model->currencyLib(true, $currency->currency).' ('.strtoupper($currency->currency).')';
                $row[0] .= $currency->def=='1'?' <span class="label label-success">Default</span>':'';
                $row[1] = $currency->rate;
                $row[2] = '';
                $row[2] .= $currency->def=='0'?'<a href="'.base_url('lbadmin/currency/default/'.$currency->currency).'" title="Make Default" class="btn btn-acl-table btn-default"><i class="acl-icon acl-icon-zoom acl-icon-money"></i></a> ':'';
                $row[2] .= '<a href="'.base_url('lbadmin/currency/edit/'.$currency->currency).'" class="btn btn-acl-table btn-primary"><i class="acl-icon acl-icon-zoom acl-icon-edit-alt"></i></a> <a href="'.base_url('lbadmin/currency/delete/'.$currency->currency).'" title="'.strtoupper($currency->currency).'" class="btn btn-acl-table btn-danger acl-remove-currency"><i class="acl-icon acl-icon-zoom acl-icon-trash-5"></i></a>';
                $rows[] = $row;
            }
            $this->response['data'] = $rows;
            $this->response['draw'] = $this->input->post('draw', true);
            $this->response['recordsTotal'] = $this->currency_model->count_all();
            $this->response['recordsFiltered'] = $this->currency_model->count_filtered();
        }
        $this->sendResponse();
    }

    private function sendResponse(){
        header('Content-Type: application/json');
        echo json_encode($this->response);
        exit;
    }
}