<?php

/*
 * Copyright (c) 2019 Earworks.
 * Codeigniter Project Template Modification
 * Please contact our email for more information :
 *  contact@earworks.co.id
 */
defined('BASEPATH') OR exit('No direct script access allowed');
include_once APPPATH . '/third_party/phpexcel/PHPExcel.php';

/**
 * Description of Export
 *
 * @author Andhy Taslin <andhy@earworks.co.id>
 * @since 1.0
 */
class Export extends Base_controller {

    public function index() {
        // slient is golden
    }

    public function allregister()
    {
        $this->load->model('register_model');
        $register = $this->register_model->get();
        $dataArray = array(
            array(
                'Nama Tamu', 'Nama Perusahaan', 'Nama Dealer', 'Tipe Kartu', 'Kategori', 'Test Offroad', 'Test Onroad', 'Test Drive', 'Waktu Daftar'
            )
        );
        $category = array(
            1 => 'Coorporate',
            2 => 'Media',
            3 => 'Internal'
        );
        foreach($register as $res)
        {
            $dataArray[] = array(
                $res['name'],
                $res['company'],
                $res['dealer'],
                $res['cardname'],
                $category[$res['type']],
                $res['offroad'],
                $res['onroad'],
                $res['testdrive'],
                date('d/m/y H:i', $res['created'])
            );
        }
        $doc = new PHPExcel();
        $doc->setActiveSheetIndex(0);
        $doc->getActiveSheet()->fromArray($dataArray);
        $filename = 'daftartamu.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($doc, 'Excel2007');
        $objWriter->save('php://output');
    }

    public function register($cardid = null) {
        $this->load->model(array('register_model', 'card_model'));
        $register = $this->register_model->get(array('cardid' => $cardid));
        $card = $this->card_model->detail(array('id' => $cardid));
        $dataArray = array(
            array(
                'Nama', 'Perusahaan'
            )
        );
        foreach($register as $res){
            $dataArray[] = array(
                $res['name'],
                $res['company']
            );
        }
        $doc = new PHPExcel();
        $doc->setActiveSheetIndex(0);
        $doc->getActiveSheet()->fromArray($dataArray);
        $filename = 'register-'.$card['name'].'.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($doc, 'Excel2007');
        $objWriter->save('php://output');
    }

}
