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

require APPPATH . '/libraries/REST_Controller.php';
require APPPATH . '/libraries/Format.php';

use Restserver\Libraries\REST_Controller;

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");

class Api_pln_activity_masukkan extends REST_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->helper(['jwt', 'authorization']);
	}


	public function index_post()
	{
		//PLN
		$trx_pln_kode =  "PLN-" . strtoupper(substr(sha1(uniqid()), 0, 10)) . "-" . rand(0, 999) . "-" . date('Hms');
		$trx_pln_nomor = $this->trx_pln_nomor();

		//DIGIFLAZZ
		$url = $this->config->item('digiflazz_api_url');
		$header = array(
			'Content-Type: text/plain',
		);
		$params = array(
			'commands' => 'inq-pasca',
			'username' => $this->config->item('digiflazz_api_username'),
			'buyer_sku_code' => 'pln_pascabayar',
			'customer_no' => $trx_pln_nomor,
			'ref_id' => $trx_pln_kode,
			'testing' => $this->config->item('digiflazz_api_testing'),
			'sign' => md5($this->config->item('digiflazz_api_username') . $this->config->item('digiflazz_api_key') . $trx_pln_kode)
		);
		$json = json_encode($params);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
		curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		$result = curl_exec($ch);
		curl_close($ch);
		$response = json_decode($result, true)['data'];

		if (!empty($response['rc'])) {
			//DIGIFLAZZ
			$rc = $response['rc'];
			$message = $response['message'];
			$customer_no = $response['customer_no'];
			$customer_name = ucwords(strtolower($response['customer_name']));
			$admin = $this->config->item('admin_pln');
			$selling_price = $response['selling_price'];
			$total = $admin + $selling_price;
			$desc = $response['desc'];
			$tarif = $desc['tarif'];
			$daya = $desc['daya'];
			$lembar_tagihan = $desc['lembar_tagihan'];


			if ($lembar_tagihan == "1") {
				$detail = $desc['detail'][0];
				$periode_tahun = substr($detail['periode'], 0, 4);
				$periode_bulan = substr($detail['periode'], -2);
				$periode = $this->bulan($periode_bulan) . " " . $periode_tahun;
			} else {
				//TAGIHAN 1
				$tagihan_satu = $desc['detail'][0];
				$periode_satu_bulan = substr($tagihan_satu['periode'], -2);
				$periode_satu = $this->bulan($periode_satu_bulan);

				//TAGIHAN 2
				$tagihan_dua = $desc['detail'][1];
				$periode_dua_bulan = substr($tagihan_dua['periode'], -2);
				$periode_dua_tahun = substr($tagihan_dua['periode'], 0, 4);
				$periode_dua = $this->bulan($periode_dua_bulan);

				//PERIODE
				$periode = $periode_satu . " - " . $periode_dua . " " . $periode_dua_tahun;
			}



			//SUKSES
			if ($rc == "00") {
				$data['pln'][] = array(
					'trx_pln_kode' =>  encrypt($trx_pln_kode, $this->config->item('key')),
					'trx_pln_nomor' =>  encrypt($customer_no, $this->config->item('key')),
					'trx_pln_nama' =>  encrypt($customer_name, $this->config->item('key')),
					'trx_pln_segment' =>  encrypt($tarif, $this->config->item('key')),
					'trx_pln_daya' =>  encrypt($daya, $this->config->item('key')),
					'trx_pln_admin' =>  encrypt(number_format($admin, 0, ',', '.'), $this->config->item('key')),
					'trx_pln_harga' =>  encrypt(number_format($selling_price, 0, ',', '.'), $this->config->item('key')),
					'trx_pln_total' =>  encrypt(number_format($total, 0, ',', '.'), $this->config->item('key')),
					'trx_pln_lembar_tagihan' =>  encrypt($lembar_tagihan, $this->config->item('key')),
					'trx_pln_periode' =>  encrypt($periode, $this->config->item('key')),
					'trx_pln_keterangan' =>  encrypt("-", $this->config->item('key')),
					'trx_pln_notasi' =>  encrypt("-", $this->config->item('key')),
				);

				//BERHASIL
				$sess["status"] = "11";
				$sess["pesan"] = "Transaksi berhasil dan ditemukan";
				$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
				$sess['data'] = array(
					'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
					'value' =>  encrypt(json_encode($data), $keytoken)
				);
				echo json_encode($sess);
			} else if ($rc == "60") {
				//TAGIHAN BELUM ADA
				$sess["status"] = "33";
				$sess["pesan"] = $rc . " Tagihan belum tersedia untuk nomor pelanggan " . $trx_pln_nomor;
				$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
				$sess['data'] = array(
					'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
					'value' =>  encrypt(json_encode(array()), $keytoken)
				);
				echo json_encode($sess);
			} else {
				//GAGAL
				$sess["status"] = "33";
				$sess["pesan"] = $rc . " " . $message . ". Silahkan laporkan pesan tersebut ke customer service jika ada pertanyaan lebih lanjut.";
				$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
				$sess['data'] = array(
					'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
					'value' =>  encrypt(json_encode(array()), $keytoken)
				);
				echo json_encode($sess);
			}
		} else {
			//GAGAL
			$sess["status"] = "34";
			$sess["pesan"] = "Transaksi gagal. Silahkan laporkan pesan error tersebut ke customer service untuk menyelesaikan masalah kamu.";
			$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
			$sess['data'] = array(
				'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
				'value' =>  encrypt(json_encode(array()), $keytoken)
			);
			echo json_encode($sess);
		}
	}


	private function trx_pln_nomor()
	{
		$input = decrypt($this->input->post("trx_pln_nomor", true), $this->config->item('key'));
		$token = AUTHORIZATION::generateToken($input);
		try {
			$data = AUTHORIZATION::validateToken($token);
			if ($data === false) {
				$sess["status"] = "004";
				$sess["pesan"] = $this->config->item('error_parameter');
				$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
				$sess['data'] = array(
					'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
					'value' =>  encrypt(json_encode(array()), $keytoken)
				);
				echo json_encode($sess);
				exit();
			} else {
				return $data;
			}
		} catch (Exception $e) {
			$sess["status"] = "004";
			$sess["pesan"] = $this->config->item('error_parameter');
			$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
			$sess['data'] = array(
				'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
				'value' =>  encrypt(json_encode(array()), $keytoken)
			);
			echo json_encode($sess);
			exit();
		}
	}

	private function bulan($bulan)
	{
		if ($bulan == "01") {
			$bulan = "Jan";
		} else if ($bulan == "02") {
			$bulan = "Feb";
		} else if ($bulan == "03") {
			$bulan = "Mar";
		} else if ($bulan == "04") {
			$bulan = "Apr";
		} else if ($bulan == "05") {
			$bulan = "Mei";
		} else if ($bulan == "06") {
			$bulan = "Jun";
		} else if ($bulan == "07") {
			$bulan = "Jul";
		} else if ($bulan == "08") {
			$bulan = "Agu";
		} else if ($bulan == "09") {
			$bulan = "Sep";
		} else if ($bulan == "10") {
			$bulan = "Okt";
		} else if ($bulan == "11") {
			$bulan = "Nov";
		} else if ($bulan == "12") {
			$bulan = "Des";
		}
		return $bulan;
	}
}
