<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Voucher_game extends CI_Controller
{
    public function index()
    {
        $this->load->model('voucher_game_model');
        $providers = $this->voucher_game_model->get_providers();

        if ($this->input->post()) {
            if ($last_id = $this->voucher_game_model->create()) {
                $this->session->set_flashdata('success', 'Tagihan dibuat.');
                if ($this->input->get_post('json_format')) {
                    $this->output->set_content_type('application/json', 'UTF-8');
                    return $this->output->set_output(json_encode(array(
                        'success' => true,
                        'id' => $last_id,
                        ), JSON_PRETTY_PRINT));
                } else {
                    redirect('history/view/' . $last_id);
                }
            } elseif ($this->input->get_post('json_format')) {
                $this->output->set_content_type('application/json', 'UTF-8');
                return $this->output->set_output(json_encode(array(
                    'success' => false,
                    'errors' => array_values($this->form_validation->error_array()),
                    ), JSON_PRETTY_PRINT));
            }
        }
        redirect(site_url());
    }
}
