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

    // require_once "vendor/autoload.php";
    // use CodeItNow\BarcodeBundle\Utils\BarcodeGenerator;
    // use Zend\Barcode\Barcode;
class Barcode extends MY_Controller
{
     public $data = array(
        'breadcrumb'    => 'Barcode',
        'modul'         => 'Barcode',
        'pesan'         => '',
        'pagination'    => '',
        'datatable'    => '',
        'main_view'     => 'barcode/barcode', ///This View Form or Table is want to the load
        'form_action'   => '',
        'form_value'    => '',
        );
	
    public function __construct()
    {	
        parent::__construct();		
        $this->load->model('Barcode_model', 'barcode', 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 getDataCustomer()
    {
        if(!empty($_POST["keyword"])) {
            $result = $this->barcode->customer_search($_POST["keyword"]);
            if(!empty($result)) {
                echo "<ul id='customer-list' style='z-index: 10;'>";
                    foreach($result as $data) {
                        echo "<li onclick='selectCustomer(this);' data-id='".$data->customer_code."'>".$data->customer_name."</li>"; 
                    }
                echo "</ul>";

            }
        }
    }

    public function getDataCustomerLap()
    {
        if(!empty($_POST["keyword"])) {
            $result = $this->barcode->customer_search_lap($_POST["keyword"]);
            if(!empty($result)) {
                echo "<ul id='customer-list' style='z-index: 10;'>";
                    foreach($result as $data) {
                        echo "<li onclick='selectCustomer(this);' data-id='".$data->customer_code."'>".$data->customer_name."</li>"; 
                    }
                echo "</ul>";

            }
        }
    }

    public function getDataProduct()
    {
        if(!empty($_POST["keyword"])) {
            $result = $this->barcode->product_search($_POST["keyword"]);
            if(!empty($result)) {
                echo "<ul id='product-list' style='z-index: 10;'>";
                    foreach($result as $data) {
                        echo "<li onclick='selectProduct(this);' data-id='".$data->product_code."'>".$data->product_name."</li>"; 
                    }
                echo "</ul>";

            }
        }
    }

    public function getDataProductLap()
    {
        if(!empty($_POST["keyword"])) {
            $result = $this->barcode->product_search_lap($_POST["keyword"]);
            if(!empty($result)) {
                echo "<ul id='product-list' style='z-index: 10;'>";
                    foreach($result as $data) {
                        echo "<li onclick='selectProduct(this);' data-id='".$data->product_code."'>".$data->product_name."</li>"; 
                    }
                echo "</ul>";

            }
        }
    }

    public function InsertCustomer()
    {
        $date = date('Y-m-d G:i:s');
        //$birthDate = date('Y-m-d',strtotime($this->input->post('txtAddCustomerBirthDate')));
        //$password = random_password(8);
        $data = array(
                            'customer_code' => $this->input->post('txtAddCustomerCode'), 
                            'customer_name' => $this->input->post('txtAddCustomerName'), 
                            'email'         => $this->input->post('txtAddCustomerMail'), 
                            'phone'         => $this->input->post('txtAddCustomerPhone'), 
                            'create_date'      => $date,
                            'address'       => $this->input->post('txtAddCustomerAddrees'), 
                            'isactive'      => 'Y',
                            'create_by'     => $this->session->userdata('username')
                    );

        $simpan = $this->customer->InsertCustomer($data);
      echo 'ok';
    }

    public function UpdateCustomer()
    {
        $date = date('Y-m-d G:i:s');
        $id = $this->input->post('txtIdCustomer');
        $data = array(
                        'customer_name' => $this->input->post('txtEditCustomerName'), 
                        'email'         => $this->input->post('txtEditCustomerMail'), 
                        'phone'         => $this->input->post('txtEditCustomerPhone'), 
                        'address'       => $this->input->post('txtEditCustomerAddrees'), 
                        'isactive' => $this->input->post('rbactive'), 
                        'update_date'   => $date,
                        'update_by'     => $this->session->userdata('username')
                    );

      $simpan = $this->customer->UpdateCustomer($id,$data);
      echo $simpan;
    }

    // public function CreateBarcode()
    // {
    //     $barcode = new BarcodeGenerator();
    //     $barcode->setText("0123456789");
    //     $barcode->setType(BarcodeGenerator::Code128);
    //     $barcode->setScale(2);
    //     $barcode->setThickness(25);
    //     $barcode->setFontSize(10);
    //     $code = $barcode->generate();
    //     echo '<img src="data:image/png;base64,'.$code.'" />';
    // }

    public function CreateBarcode($code)
    {
        //load library
        // $this->load->library('zend');
        // //load in folder Zend
        // $this->zend->load('Zend/Barcode');
        // //generate barcode
        // echo Zend_Barcode::render('code128', 'image', array('text'=>$code,'barHeight'=>40), array());
        $this->load->library('ciqrcode'); //pemanggilan library QR CODE
 
        $config['cacheable']    = true; //boolean, the default is true
        $config['cachedir']     = './asset/'; //string, the default is application/cache/
        $config['errorlog']     = './asset/'; //string, the default is application/logs/
        $config['imagedir']     = './asset/images/'; //direktori penyimpanan qr code
        $config['quality']      = true; //boolean, the default is true
        $config['size']         = '1024'; //interger, the default is 1024
        $config['black']        = array(224,255,255); // array, default is array(255,255,255)
        $config['white']        = array(70,130,180); // array, default is array(0,0,0)
        $this->ciqrcode->initialize($config);
        $image_name=$code.'.png'; //buat name dari qr code sesuai dengan 
        $params['data'] = $code;
        echo $this->ciqrcode->generate($params);
 
    }

    public function PrintBarcode(){
        // $userID = $this->session->userdata['user_id'];
        $userID = 'olieq';
        $parts = explode('-', $this->input->post('txtCustomer'));
        $cutomerCode = $parts[0];
        $parts = explode('-', $this->input->post('txtProduct'));
        $productCode = $parts[0];
        $parts = explode(' ', $this->input->post('createDate'));
        $createDate = $parts[0];
        $createDate = $this->cDate($createDate);
        $totalPrint = $this->input->post('txtJumlah');
        $imgBarcode = $cutomerCode.$productCode.$createDate;
        // echo $createDate;exit();

        $filename="barcode.pdf";
        $this->load->library('Pdf');
        // $this->load->view('view_file');
        try
        {
            $pdf = new TCPDF('L', 'cm', array(6, 4), true, 'UTF-8', false);

            //remove header and footer
            $pdf->setPrintHeader(false);
            $pdf->setPrintFooter(false);

            // set default monospaced font
            $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

            // set margins
            $pdf->setTopMargin(1);
            $pdf->SetLeftMargin(0);
            $pdf->SetRightMargin(0);

            // set auto page breaks
            $pdf->SetAutoPageBreak(TRUE);
            // $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

            // set image scale factor
            $pdf->setImageScale(TRUE, PDF_IMAGE_SCALE_RATIO);

            // set a barcode on the page footer
            //$pdf->setBarcode(date('Y-m-d H:i:s'));

            // add a page
            $pdf->AddPage();

            // $pdf->setEqualColumns(3, 32);

            $pdf->SetFont('helvetica', '', 8);

            $style['position'] = 'C';
            $style['text'] = true;

            $idx = 1;
            for($i = 0 ; $i < $totalPrint ; $i++)
            {
                $x = $pdf->GetX();
                $y = $pdf->GetY();
                $pdf->setCellMargins(0,0,0,0);
                $pdf->write2DBarcode($imgBarcode, 'QRCODE,Q', '', '', 4, 2, $style, 'N');
                // $pdf->Text(5, 32, 'QRCODE Q');
                
                $pdf->Ln(1);
            }

            $date = date('Y-m-d G:i:s');
            $data = array(
                'product_code' => $productCode,
                'customer_code' => $cutomerCode,
                'create_date' => $date,
                'total_print' => $totalPrint
            );
            $this->barcode->Insertbarcode($data);
            if (ob_get_length()) ob_end_clean();
            // ob_end_clean();
            //Close and output PDF document
            $pdf->Output('barcodes.pdf', 'I');
        }
        catch(HTML2PDF_exception $e)
        { 
            echo 'a'; 
        }
    }


    private function cDate($date)
    {
        $thn=substr($date,0,4);
        $bln=substr($date,5,2);
        $tgl=substr($date,8,2);
        $tanggal=$tgl.$bln.$thn;
        return $tanggal;
    }

}

/* End of file user.php */
/* Location: ./application/controllers/user.php */