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

class Laporanbiayaformulir extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('formulir_model');
		$this->load->model('biayaformulir_model');
	    $this->simple_login->check_login();
	    //proteksi admin dan peserta
		if($this->session->userdata('level') !='Administrator') 
		{
			if($this->session->userdata('level') !='Operator') 
			{
			//kalau bukan adamin, lempar ke login
			$this->session->set_flashdata('warning', 'Hak akses anda tidak diijinkan mengakses menu penyerahan berkas');
			redirect(base_url('login'),'refresh');		
			}
		}

	}

	public function index()
	{
		
        $dataku= $this->db->get("v_rpt_trans_formulir")->result();
		$data = array(	 'title'		=> 'Laporan Transaksi Biaya Tes Per Periode',
						 'formulir'		=> $dataku,
						 'content' 		=> 'laporanformulir/index'						 
						);
		$this->load->view('layout/wrapper', $data, FALSE);	
	}

    
    public function get_data_transaksi() {
        // Ambil parameter filter dari request
        $tanggal_awal = $this->input->post('tanggal_awal');
        $tanggal_akhir = $this->input->post('tanggal_akhir');
        $operator = $this->input->post('operator');

        // Query SQL untuk mendapatkan data transaksi
        $sql = "SELECT * FROM v_rpt_trans_formulir WHERE tgl_trans BETWEEN ? AND ? AND operator = ?";
        $query = $this->db->query($sql, array($tanggal_awal, $tanggal_akhir, $operator));
        $data = $query->result_array();

        // Kirim data sebagai JSON ke view
        echo json_encode($data);
    }


    public function getdata()
    {
        $draw = intval($this->input->get("draw"));
        $start = intval($this->input->get("start"));
        $length = intval($this->input->get("length"));
    
        $tanggal_awal = $this->input->get("tanggal_awal");
        $tanggal_akhir = $this->input->get("tanggal_akhir");
        $operator = $this->input->get("operator");
    
        $this->db->select('*');
        $this->db->from('v_rpt_trans_formulir');
        if (!empty($tanggal_awal) && !empty($tanggal_akhir)) {
            $this->db->where('tgl_trans >=', $tanggal_awal);
            $this->db->where('tgl_trans <=', $tanggal_akhir);
        }
        if (!empty($operator)) {
            $this->db->where('operator', $operator);
        }
        $this->db->order_by('no_trans', 'DESC');
        $query = $this->db->get();
        
        $data = [];
        $no = 0;
        foreach ($query->result() as $key => $lists) {
            $no++;
            $data[$key][]  = $no;
            $data[$key][]  = date('d-M-Y', strtotime($lists->tgl_trans));
            $data[$key][]  = $lists->no_trans;
            $data[$key][]  = $lists->no_pendaftaran;
            $data[$key][]  = $lists->nama_lengkap;
            $data[$key][]  = "Rp. ".number_format($lists->biaya);
            $data[$key][]  = "Rp. ".number_format($lists->diskon);
            $data[$key][]  = "Rp. ".number_format($lists->bayar);
            $data[$key][]  = "Rp. ".number_format($lists->cash);
            $data[$key][]  = "Rp. ".number_format($lists->kembali);
        }
    
        $result = array(
            "draw" => $draw,
            "recordsTotal" => $query->num_rows(),
            "recordsFiltered" => $query->num_rows(),
            "data" => $data
        );
    
        echo json_encode($result);
        exit();
    }

    public function print_pdf($start = null, $end = null) {
        // Mengambil data dari model berdasarkan rentang tanggal dan metode yang diberikan
       
        $tanggal_awal = $this->input->get("start_date");
        $tanggal_akhir = $this->input->get("end_date");
        $operator = $this->input->get("operator");
    
        $this->db->select('*');
        $this->db->from('v_rpt_trans_formulir');
        if (!empty($tanggal_awal) && !empty($tanggal_akhir)) {
            $this->db->where('tgl_trans >=', $tanggal_awal);
            $this->db->where('tgl_trans <=', $tanggal_akhir);
        }
        if (!empty($operator)) {
            $this->db->where('operator', $operator);
        }
        $this->db->order_by('no_trans', 'DESC');
        $query = $this->db->get();
        $data["tp"] = $this->db->get("setting")->row()->tp;
        $data["data"] = $query;
    
        
        $this->load->view('laporanformulir/printpdf', $data);
    }
    
}
