<?php
include "../config/conn.php";
if($_GET['tipe']=="PDA") {
////AWAL DOWNLOAD MUKA AIR=======================	
$namalokasi=$_GET['pos'];
$cekdetaillokasi=mysql_query("select * from m_lokasi where nama_lokasi='".$namalokasi."'");
$rowcek=mysql_fetch_array($cekdetaillokasi);

$bulanini=$_GET['bln'];
if($bulanini==1) { $bln="Januari";}
else if($bulanini==2) { $bln="Februari";}
else if($bulanini==3) { $bln="Maret";}
else if($bulanini==4) { $bln="April";}
else if($bulanini==5) { $bln="Mei";}
else if($bulanini==6) { $bln="Juni";}
else if($bulanini==7) { $bln="Juli";}
else if($bulanini==8) { $bln="Agustus";}
else if($bulanini==9) { $bln="September";}
else if($bulanini==10) { $bln="Oktober";}
else if($bulanini==11) { $bln="November";}
else if($bulanini==12) { $bln="Desember";}
$thn=date('Y');

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
if (PHP_SAPI == 'cli')
	die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/../lib/PHPExcel/IOFactory.php';
// Create new PHPExcel object
$objPHPExcel = PHPExcel_IOFactory::load("../file/templateDownloadTMAmanual.xls");
$baseRow = 10;
$startRow = $baseRow;
$endRow = $baseRow;
	$objPHPExcel->setActiveSheetIndex(0)
          ->setCellValue('E'.'4', $bln)
          ->setCellValue('G'.'4', $thn)
          ->setCellValue('C'.'5', $rowcek['ws'])
          ->setCellValue('C'.'45', $rowcek['nama_pengamat'])
          ->setCellValue('C'.'6', $rowcek['nama_alat']);
	$i=1;	
	//for($i=1; $i<=31; $i++)
	while($i <= 31)
	{
		//echo "select WLevel from ".$namalokasi." WHERE ReceivedDate LIKE '".date('Y')."-".$bulanini."-".$i."' AND ML='50' AND ReceivedTime='07:00:00'";
$select_data7=mysql_query("select WLevel from ".$namalokasi." WHERE ReceivedDate = '".date('Y')."-".$bulanini."-".$i."' AND ML='50' AND ReceivedTime='07:00:00'"); 				$rowdata7=mysql_fetch_array($select_data7);
$select_data12=mysql_query("select WLevel from ".$namalokasi." WHERE ReceivedDate = '".date('Y')."-".$bulanini."-".$i."' AND ML='50' AND ReceivedTime='12:00:00'");
$rowdata12=mysql_fetch_array($select_data12);
$select_data17=mysql_query("select WLevel from ".$namalokasi." WHERE ReceivedDate = '".date('Y')."-".$bulanini."-".$i."' AND ML='50' AND ReceivedTime='17:00:00'");
$rowdata17=mysql_fetch_array($select_data17);							
							$jml7=mysql_num_rows($select_data7);
							$jml12=mysql_num_rows($select_data12);
							$jml17=mysql_num_rows($select_data17);

		$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('B'.$baseRow, $i)
            ->setCellValue('C'.$baseRow, $rowdata7['WLevel'])
            ->setCellValue('D'.$baseRow, $rowdata12['WLevel'])
            ->setCellValue('E'.$baseRow, $rowdata17['WLevel'])
            ->setCellValue('F'.$baseRow, ($rowdata7['WLevel']+$rowdata12['WLevel']+$rowdata17['WLevel'])/3)
            ->setCellValue('G'.$baseRow, max($rowdata7['WLevel'],$rowdata12['WLevel'],$rowdata17['WLevel']))
            ->setCellValue('H'.$baseRow, '');
        $baseRow++;
        $i++;
    }
//die();
$objPHPExcel->setActiveSheetIndex(0);
$nvr=$_GET['pos'];
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="DataTMAmanual'.$nvr.'.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit; 
} //AKHIR DOWNLOAD PDA================
else if($_GET['tipe']=="PCH")
{
////AWAL DOWNLOAD CURAH HUJAN=======================	
$namalokasi=$_GET['pos'];
$cekdetaillokasi=mysql_query("select * from m_lokasi where nama_lokasi='".$namalokasi."'");
$rowcek=mysql_fetch_array($cekdetaillokasi);

$bulanini=$_GET['bln'];
if($bulanini==1) { $bln="Januari";}
else if($bulanini==2) { $bln="Februari";}
else if($bulanini==3) { $bln="Maret";}
else if($bulanini==4) { $bln="April";}
else if($bulanini==5) { $bln="Mei";}
else if($bulanini==6) { $bln="Juni";}
else if($bulanini==7) { $bln="Juli";}
else if($bulanini==8) { $bln="Agustus";}
else if($bulanini==9) { $bln="September";}
else if($bulanini==10) { $bln="Oktober";}
else if($bulanini==11) { $bln="November";}
else if($bulanini==12) { $bln="Desember";}
$thn=date('Y');

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
if (PHP_SAPI == 'cli')
	die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/../lib/PHPExcel/IOFactory.php';
// Create new PHPExcel object
$objPHPExcel = PHPExcel_IOFactory::load("../file/templateDownloadCHmanual.xls");
$baseRow = 9;
$startRow = $baseRow;
$endRow = $baseRow;
	$objPHPExcel->setActiveSheetIndex(0)
          ->setCellValue('C'.'6', $bln."-".$thn)
          ->setCellValue('C'.'45', $rowcek['nama_pengamat'])
          ->setCellValue('C'.'5', $rowcek['nama_alat']);
	$i=1;	
	//for($i=1; $i<=31; $i++)
	while($i <= 31)
	{
		//echo "select WLevel from ".$namalokasi." WHERE ReceivedDate LIKE '".date('Y')."-".$bulanini."-".$i."' AND ML='50' AND ReceivedTime='07:00:00'";
$select_data7=mysql_query("select Rain from ".$namalokasi." WHERE ReceivedDate = '".date('Y')."-".$bulanini."-".$i."' AND ML='50' AND ReceivedTime='07:00:00'"); 				$rowdata7=mysql_fetch_array($select_data7);
							$jml7=mysql_num_rows($select_data7);

		$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('B'.$baseRow, $i)
            ->setCellValue('C'.$baseRow, $rowdata7['Rain'])
            ->setCellValue('D'.$baseRow, '');
        $baseRow++;
        $i++;
    }
//die();
$objPHPExcel->setActiveSheetIndex(0);
$nvr=$_GET['pos'];
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="DataCHmanual'.$nvr.'.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;	
} // AKHIR DOWNLOAD CURAH HUJAN
?>