<?php

$keyword=$_REQUEST['keyword'];
$cl=new MY_HTML();


error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

if (PHP_SAPI == 'cli')
    die('This example should only be run from a Web Browser');

/** Include PHPExcel */

require_once 'Classes/PHPExcel.php';
if(preg_match("/^windows/i",strtolower(php_uname()))){PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);}

ini_set('memory_limit', '1024M');

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// My data
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1','TELKOMCEL')
            ->setCellValue('A2','ANC REGISTRATION REPORTING')
            ->setCellValue('A3','Reported By : '.$_SESSION['nik']);

//  set format cell
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);


// Print Header
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A6','No')
            ->setCellValue('B6','MSISDN')
            ->setCellValue('C6','Customer Name')
            ->setCellValue('D6','Customer Address')
            ->setCellValue('E6','ID Card')
            ->setCellValue('F6','Register Date')
            ->setCellValue('G6','Type')
            ->setCellValue('H6','CSO NIK')
            ;

// === Merge Cells
//$objPHPExcel->getActiveSheet()->mergeCells('G5:H5');
//$objPHPExcel->getActiveSheet()->mergeCells('I5:J5');

$objPHPExcel->getActiveSheet()->getStyle('A6:H6')->getFont()->getColor()->setRGB('ffffff');
// Worksheet Name
$objPHPExcel->getActiveSheet()->setTitle('ANCREGISTER_'.date("Ym"));

// === Set Background Cell
$objPHPExcel->cellColor("A6:H6","938e8e");

// === set Column Width A
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(6);
// === Set auto COlumn width
foreach(range('B','H') as $columnID)
{
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
}

$swhere="";

$qry="select id,msisdn,customername,customeraddress,idcard,registerdate,idtypes,types,iby
      from ".DB_APORTIL.".tbl_marsal_simcardreg
      where (msisdn like '%$keyword%' or customername like '%$keyword%') and isdeleted=0 
      order by id asc";
                  
$rsrpt=$cl->selectquery($qry);
if($rsrpt)
{
    $i=7;$j=1;
    while($rw=$cl->data_row_array($rsrpt))
    {
        $registerdate=date("Y-m-d H:i:s",strtotime($rw["registerdate"]));
                        
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$i,$j)
                                            ->setCellValue('B'.$i,$rw["msisdn"])
                                            ->setCellValue('C'.$i,$rw["customername"])
                                            ->setCellValue('D'.$i,$rw["customeraddress"])
                                            ->setCellValue('E'.$i,$rw["idcard"])
                                            ->setCellValue('F'.$i,$registerdate)
                                            ->setCellValue('G'.$i,$rw["types"])
                                            ->setCellValue('H'.$i,$rw["iby"])
                                            ;
        $i+=1;$j+=1;
    }
}
$cl->clearrecordset($rsrpt);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a web browser (Excel2007)
//header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="ANCRegistration.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>
