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

class Laporan extends CI_Controller {

	function __construct(){
		parent::__construct();
		if(!$this->session->userdata('login')){
			redirect('login');
		}
		$this->load->model('Peminjaman_model');
	}

	public function peminjaman(){

        $q       = $this->input->get('q');
        $status  = $this->input->get('status');
        $filter  = $this->input->get('filter');
        $tanggal = $this->input->get('tanggal');

        $data['data'] = $this->Peminjaman_model
            ->get($q, $status, $filter, $tanggal);

        $data['total'] = count($data['data']);

        // simpan parameter agar bisa dipakai di PDF
        $data['q'] = $q;
        $data['status'] = $status;
        $data['filter'] = $filter;
        $data['tanggal'] = $tanggal;

        $this->load->view('laporan/peminjaman',$data);
	}

	public function peminjaman_pdf(){

        $q       = $this->input->get('q');
        $status  = $this->input->get('status');
        $filter  = $this->input->get('filter');
        $tanggal = $this->input->get('tanggal');

        $data['data'] = $this->Peminjaman_model
            ->get($q, $status, $filter, $tanggal);

		require_once APPPATH.'third_party/dompdf/autoload.inc.php';

		$dompdf = new Dompdf\Dompdf();

		$html = $this->load->view('laporan/peminjaman_pdf',$data,true);

		$dompdf->loadHtml($html);
		$dompdf->setPaper('A4','landscape');
		$dompdf->render();
		$dompdf->stream("laporan_peminjaman.pdf",array("Attachment"=>0));
	}
}
