<?php

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

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

        if ($this->input->post()) {
            if ($last_id = $this->token_pln_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());
    }
}
