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

class Laporan extends MY_Controller
{
    public $data = array(
        'breadcrumb'    => 'Barcode',
        'modul'         => 'Barcode',
        'pesan'         => '',
        'pagination'    => '',
        'datatable'    => '',
        'main_view'     => 'laporan/laporan', ///This View Form or Table is want to the load
        'form_action'   => '',
        'form_value'    => '',
    );

    public function __construct()
    {	
        parent::__construct();      
        $this->load->model('Laporan_model', 'laporan', TRUE);
        $this->load->model('Login_model', 'userSession', TRUE);
        $this->load->library('session');
        $this->load->helper('encryption');
    }
    public function index()
    {
        $session_data = $this->session->userdata('full_name');
        $session_role = $this->session->userdata('role_id');

        $this->data['full_name'] = $session_data;   
        $this->data['role_id'] = $session_role;
        $this->load->view('template', $this->data);
    }

    public function loadLaporan()
    {
      $list = $this->laporan->get_laporan();
      $data = array();
      $no = $_POST['start'];

      foreach ($list as $itm) 
      {
        $var = $itm->createDate;
        $date = str_replace('/', '-', $var);
        $no++;
        $row = array();
        $row[] = '<p style="text-align: center">'.$no.'</p>';
        $row[] = '<p style="text-align: left">'.$itm->customerCode.'</p>';
        $row[] = '<p style="text-align: left">'.$itm->customerName.'</p>';
        $row[] = '<p style="text-align: left">'.$itm->productCode.'</p>';
        $row[] = '<p style="text-align: left">'.$itm->productName.'</p>';
        $row[] = '<p style="text-align: center">'.date('d-m-Y', strtotime($date)).'</p>';
        $row[] = '<p style="text-align: center">'.$itm->totalPrint.'</p>';
      
        $data[] = $row;
      }
      $output = array(
                "draw" => $_POST['draw'],
                "recordsTotal" => $this->laporan->count_laporan_all(),
                "recordsFiltered" => $this->laporan->count_laporan_filtered(),
                "data" => $data
                );
        
      echo json_encode($output);
    }

    public function loadLaporanSearch()
    {
      $list = $this->laporan->get_laporan();
      $data = array();
      $no = $start = $this->input->post('start');

      foreach ($list as $itm) 
      {

        $var = $itm->createDate;
        $date = str_replace('/', '-', $var);

        $no++;
        $row = array();
        $row[] = '<p style="text-align: center">'.$no.'</p>';
        $row[] = '<p style="text-align: left">'.$itm->customerCode.'</p>';
        $row[] = '<p style="text-align: left">'.$itm->customerName.'</p>';
        $row[] = '<p style="text-align: left">'.$itm->productCode.'</p>';
        $row[] = '<p style="text-align: left">'.$itm->productName.'</p>';
        $row[] = '<p style="text-align: center">'.date('d-m-Y', strtotime($date)).'</p>';
        $row[] = '<p style="text-align: center">'.$itm->totalPrint.'</p>';
         
        $data[] = $row;
      }
      $output = array(
                "draw" => intval($this->input->post('draw')),
                "recordsTotal" => $this->laporan->count_laporan_all(),
                "recordsFiltered" => $this->laporan->count_laporan_filtered(),
                "data" => $data
                );
      
      echo json_encode($output);
    }

    
}