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

class Snap extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		if (!$this->session->userdata('email')) {
         redirect('auth/login');
      	}
		$params = array('server_key' => 'SB-Mid-server-3A5B5YOsnQECZFv-9ErWiEP9', 'production' => false);
		$this->load->library('midtrans');
		$this->midtrans->config($params);
		$this->load->helper('url');
		$this->load->model('Model_snap');
	}

	public function beranda()
	{
		$data['title'] = 'beranda';
		$this->load->view('home/header', $data);
		$this->load->view('home/topbar');
		$this->load->view('home/beranda', $data);
		$this->load->view('home/footer');
	}

	public function simpan_reservasi()
    {
    	$id=$this->session->userdata('id_user');
        $this->db->where('id_user', $id);
		$this->db->delete('reservasi');
        $tglin = $this->input->post('tglin');
		$tglout = $this->input->post('tglout');
		

	      $tgl1 = new DateTime($tglin);
	      $tgl2 = new DateTime($tglout);
	      $jarak = $tgl2->diff($tgl1);
	      $hari=$jarak->d;

		$query_str="SELECT * FROM `area_camping`";
        $query=$this->db->query($query_str);
          foreach ($query->result_array() as $row){
          	$idbrg=$row['id_area'];

          	$sql = $this->db->query("SELECT * FROM `tb_pesanan` INNER JOIN tb_pesanan_detail on tb_pesanan.id_pesanan=tb_pesanan_detail.id_pesanan WHERE id_barang='$idbrg' AND status='Sudah Dibayar'");
			if($sql->num_rows()>0){
			   	$data = $sql->row();
			   	$tgin=$data->tgl_in;
			   	$tgout=$data->tgl_out;
			   	$tgl2    = date('Y-m-d', strtotime('+1 days', strtotime($tgout)));
			   	$period = new DatePeriod(
				     new DateTime($tgin),
				     new DateInterval('P1D'),
				     new DateTime($tgl2)
				);
				$sv='';
				$data_array = array();
				foreach ($period as $key => $value) {
					$data_array[$value->format('Y-m-d')] ='2';
					$sv.=$value->format('Y-m-d');
				}

				$tg=$data_array[$tglin];
				if (!empty($tg)) {
				    $inst='2';
				} else {
				    $inst='0';
				}

			 	
			} else {
			 $inst='0'; 
			}
	          	$tot=$row['harga']*$hari;
	            $data = array(
	        		'id_area' 	=> $row['id_area'],
	        		'id_user' 	=> $id,
	        		'blok' 		=> $row['blok'],
	        		'harga_hari' 	=> $row['harga'],
	        		'tot_harga' 	=> $tot,
	        		'latlng' 	=> $row['latlng'],
	        		'tgl_in' 	=> $tglin,
	        		'tgl_out' 	=> $tglout,
	        		'hari' 	=> $hari,
	        		'status' 	=> $inst

				);
				$insert=$this->db->insert('reservasi', $data);
          }
        if ($insert) {
            redirect("snap/reservasi");
        }
    }


    public function pilih_ok()
    {
    	$id=$this->uri->segment(3);
            $data = array(
        		'status' 	=> '1'
			);
			$update=$this->db->update('reservasi', $data, "id_reservasi = '$id'");
        if ($update) {
            redirect("snap/reservasi");
        }
    }

    public function pilih_batal()
    {
    	$id=$this->uri->segment(3);
            $data = array(
        		'status' 	=> '0'
			);
			$update=$this->db->update('reservasi', $data, "id_reservasi = '$id'");
        if ($update) {
            redirect("snap/reservasi");
        }
    }

	public function reservasi()
	{
		$data['title'] = 'reservasi';
		$this->load->view('home/header', $data);
		$this->load->view('home/topbar');
		$this->load->view('home/reservasi', $data);
		$this->load->view('home/footer');
	}


	public function index()
	{
		$data['title'] = 'Cekout';
		$this->load->view('home/header', $data);
		$this->load->view('home/cekout');
	}

	public function token()
	{
		$id=$this->session->userdata('id_user');
  		$data=$this->db->get_where('user',array('id_user'=>$id))->row_array();  
		//menampilkan pesanan
		$this->db->select("
			tb_pesanan_detail.id_pesanan as id,
			tb_pesanan_detail.qty as quantity,
			area_camping.harga as price,
			area_camping.blok as name
		");
		$this->db->from('tb_pesanan_detail');
		$this->db->join('area_camping', 'area_camping.id_area=tb_pesanan_detail.id_barang');
		$this->db->where('id_pesanan', $this->session->userdata('id_pesanan'));
		$item = $this->db->get();



		$gross_amount = 0;
		foreach ($item->result() as $r) {
			$gross_amount += $r->price * $r->quantity; //menghitung gross amount
		}
		// Required
		$transaction_details = array(
			'order_id' => rand(),
			'gross_amount' => $gross_amount , // no decimal allowed for creditcard
		);

		$data_ongkir = array(
			'id' => 'Ongkir',
			'price' => '0',
			'quantity' => 1,
			'name' => "Ongkir"
		);

		$item_details = array_merge($item->result_array(), array($data_ongkir));

		//menampilkan data customer
		$cust = $this->db->query("select * from tb_pesanan where id_pesanan='" . $this->session->userdata('id_pesanan') . "'")->row();
		// Optional
		$tlp=rand(11, 11);
		$kdpos=rand(6, 6);
		$billing_address = array(
			'first_name'    => "CAMP",
			'last_name'     => "DLUNDUNG",
			'address'       => "Jl.xx xx",
			'city'          => "Pandeglang",
			'postal_code'   => $kdpos,
			'phone'         => $tlp,
			'country_code'  => 'IDN'
		);

		// Optional
		$shipping_address = array(
			'first_name'    => "E",
			'last_name'     => "JASA",
			'address'       => "Jl.xx xx",
			'city'          => "Pandeglang",
			'postal_code'   => $kdpos,
			'phone'         => $tlp,
			'country_code'  => 'IDN'
		);

		// Optional
		$customer_details = array(
			'first_name'    => "",
			'last_name'     => "",
			'email'         => $data['email'],
			'phone'         => $data['telepon'],
			'billing_address'  => $billing_address,
			'shipping_address' => $shipping_address
		);

		// Data yang akan dikirim untuk request redirect_url.
		$credit_card['secure'] = true;
		//ser save_card true to enable oneclick or 2click
		//$credit_card['save_card'] = true;

		$time = time();
		$custom_expiry = array(
			'start_time' => date("Y-m-d H:i:s O", $time),
			'unit' => 'minute',
			'duration'  => 2
		);

		$transaction_data = array(
			'transaction_details' => $transaction_details,
			'item_details'       => $item_details,
			'customer_details'   => $customer_details,
			'credit_card'        => $credit_card,
			'expiry'             => $custom_expiry
		);

		// echo json_encode($transaction_data);

		error_log(json_encode($transaction_data));
		$snapToken = $this->midtrans->getSnapToken($transaction_data);
		error_log($snapToken);
		echo $snapToken;
	}

	public function finish()
	{

		$result = json_decode($this->input->post('result_data'));

		// echo "<pre>";
		// print_r($result);
		// echo "</pre>";
		// die;

		if (isset($result->va_numbers[0]->bank)) {
			$bank = $result->va_numbers[0]->bank;
		} else {
			$bank = '-';
		}

		if (isset($result->va_numbers[0]->va_number)) {
			$va_number = $result->va_numbers[0]->va_number;
		} else {
			$va_number = '-';
		}

		if (isset($result->bca_va_number)) {
			$bca_va_number = $result->bca_va_number;
		} else {
			$bca_va_number = '-';
		}

		if (isset($result->bill_key)) {
			$bill_key = $result->bill_key;
		} else {
			$bill_key = '-';
		}

		if (isset($result->biller_code)) {
			$biller_code = $result->biller_code;
		} else {
			$biller_code = '-';
		}

		if (isset($result->permata_va_number)) {
			$permata_va_number = $result->permata_va_number;
		} else {
			$permata_va_number = '-';
		}

		if (isset($result->fraud_status)) {
			$fraud_status = $result->fraud_status;
		} else {
			$fraud_status = '-';
		}

		if (isset($result->pdf_url)) {
			$pdf_url = $result->pdf_url;
		} else {
			$pdf_url = '-';
		}
		if (isset($result->payment_code)) {
			$payment_code = $result->payment_code;
		} else {
			$payment_code = '-';
		}

		$data = [
			'status_code' => $result->status_code,
			'status_message' => $result->status_message,
			'transaction_id' => $result->transaction_id,
			'order_id' => $result->order_id,
			'gross_amount' => $result->gross_amount,
			'payment_type' => $result->payment_type,
			'payment_code' => $payment_code,
			'transaction_time' => $result->transaction_time,
			'transaction_status' => $result->transaction_status,
			'fraud_status' => $fraud_status,
			'bill_key' => $bill_key,
			'biller_code' => $biller_code,
			'pdf_url' => $pdf_url,
			'finish_redirect_url' => $result->finish_redirect_url,
			'bank' => $bank,
			'va_number' => $va_number,
			'bca_va_number' => $bca_va_number,
			'permata_va_number' => $permata_va_number
		];

		// $this->session->set_userdata($data);
		// echo "<pre>";
		// var_dump($data);
		// echo "</pre>";
		// die;

		$id_pesanan =  $this->session->userdata('id_pesanan');
		$proses = $this->Model_snap->insert($data, $id_pesanan);
		if ($proses) {
			$id_transaksi = $proses;
			$data['finish'] = $id_pesanan;
			$data['title'] = 'Finish';
			$data['result'] = $this->Model_snap->get_data($id_transaksi);
			$this->load->view('home/header', $data);
			$this->load->view('home/topbar');
			$this->load->view('home/finish', $data);
			$this->load->view('home/footer');
		} else {
			echo "Request pembayaran gagal";
		}
	}

	public function status_transaksi()
	{
		$data['title'] = 'Status Transaksi';
		$id_user = $this->session->userdata('id_user');
		$data['barang'] = $this->Model_snap->get_barang_pesanan($id_user);
		$this->load->view('home/header', $data);
		$this->load->view('home/topbar');
		$this->load->view('home/status_transaksi', $data);
		$this->load->view('home/footer');
	}

	public function detail_area()
	{
		$data['title'] = 'Detail Area';
		$id_area=$this->uri->segment(3); 
		$data['area'] = $this->db->get_where('area_camping', ['id_area' => $id_area])->row_array();
		$this->load->view('home/header', $data);
		$this->load->view('home/topbar');
		$this->load->view('home/detail', $data);
		$this->load->view('home/footer');
	}

	public function detail($id_pesanan)
	{
		$data['title'] = 'Detail';
		$data['detail_pesanan'] = $this->Model_snap->detail_pesanan($id_pesanan);
		$data['detail_pelanggan'] = $this->Model_snap->detail_pelanggan($id_pesanan);
		$this->load->view('home/header', $data);
		$this->load->view('home/topbar');
		$this->load->view('home/detail_pesanan', $data);
		$this->load->view('home/footer');
	}

	public function transaksi()
	{
		$data['title'] = 'Transaksi';
		$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
		$data['transaksi'] = $this->Model_snap->get_transaksi();
		$this->load->view('admin_templates/header', $data);
		$this->load->view('admin_templates/sidebar');
		$this->load->view('admin_templates/topbar', $data);
		$this->load->view('admin/transaksi', $data);
		$this->load->view('admin_templates/footer');
	}

	public function transaksi_detail($id_transaksi)
	{
		$data['title'] = 'Transaksi Detail';
		$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
		$data['detail'] = $this->Model_snap->detail($id_transaksi);
		$data['pembeli'] = $this->Model_snap->pembeli($id_transaksi);

		$this->load->view('admin_templates/header', $data);
		$this->load->view('admin_templates/sidebar');
		$this->load->view('admin_templates/topbar', $data);
		$this->load->view('admin/transaksi_detail', $data);
		$this->load->view('admin_templates/footer');
	}
}
