<?php
require "../../config/koneksi.php";
require "../../vendor/autoload.php";
require "../../config/function.php";
session_start();
 
use PhpOffice\PhpSpreadsheet\Helper\Sample;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

  $id=$_GET['id'];
  $edit2=$koneksi->query("select * from jadwal_mapel where id_jadwal='$id'");
  $ms=mysqli_fetch_array($edit2);
  $mapel=$ms['kode'];
  $klas=$ms['kelas'];
   $tahun=date('Y');
  $semester=$setting['semester'];
  
$spreadsheet = new Spreadsheet();
$spreadsheet->getProperties()->setCreator('Edi Sukarna')
->setLastModifiedBy('Edi Sukarna')
->setTitle('Office 2007 XLSX')
->setSubject('Office 2007 XLSX')
->setDescription('Test document for Office 2007 XLSX.')
->setKeywords('office 2007 openxml php')
->setCategory('Test result file');

$spreadsheet->getActiveSheet()->getStyle('A1:L1')
    ->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);

    $spreadsheet->getActiveSheet()->getStyle('A1:L1')->getFill()
    ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
    ->getStartColor()->setARGB('FFFF0000');



$spreadsheet->setActiveSheetIndex(0)
->setCellValue('A1', "No")
->setCellValue('B1', "NIS")
->setCellValue('C1', "Nama Siswa")
->setCellValue('D1', "Mapel") 
->setCellValue('E1', "PK1")
->setCellValue('F1', "PK2")
->setCellValue('G1', "PK3")
->setCellValue('H1', "PK4")
->setCellValue('I1', "PK5")
->setCellValue('J1', "PK6")
->setCellValue('K1', "PK7")
->setCellValue('L1', "PK8")
->setCellValue('M1', "Kelas")
->setCellValue('N1', "KI")
->setCellValue('O1', "SMT")
->setCellValue('P1', "THN"); 

$i=2; 
$no=1; 
$sql = mysqli_query($koneksi, "SELECT * FROM siswa WHERE id_kelas='$klas'");

while($row = mysqli_fetch_array($sql)){ 
$spreadsheet->setActiveSheetIndex(0)
	->setCellValue('A'.$i, $no)
	->setCellValue('B'.$i, $row['nis'])
	->setCellValue('C'.$i, $row['nama'])
	->setCellValue('D'.$i, $mapel)
	->setCellValue('E'.$i, '')
	->setCellValue('F'.$i, '')
	->setCellValue('G'.$i, '')
	->setCellValue('H'.$i, '')
	->setCellValue('I'.$i, '')
	->setCellValue('J'.$i, '')
	->setCellValue('K'.$i, '')
	->setCellValue('L'.$i, '')
	->setCellValue('M'.$i, $row['id_kelas'])
	->setCellValue('N'.$i, 'KI4')
	->setCellValue('O'.$i, $semester)
	->setCellValue('P'.$i, $tahun);
	$i++; $no++;
}

$spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(5); 
$spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(15); 
$spreadsheet->getActiveSheet()->getColumnDimension('C')->setWidth(30); 
$spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(10); 
$spreadsheet->getActiveSheet()->getColumnDimension('E')->setWidth(5); 
$spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(5);
$spreadsheet->getActiveSheet()->getColumnDimension('G')->setWidth(5); 
$spreadsheet->getActiveSheet()->getColumnDimension('H')->setWidth(5);
$spreadsheet->getActiveSheet()->getColumnDimension('I')->setWidth(5); 
$spreadsheet->getActiveSheet()->getColumnDimension('J')->setWidth(5);
$spreadsheet->getActiveSheet()->getColumnDimension('K')->setWidth(5); 
$spreadsheet->getActiveSheet()->getColumnDimension('L')->setWidth(5);
$spreadsheet->getActiveSheet()->getColumnDimension('M')->setWidth(0); 
$spreadsheet->getActiveSheet()->getColumnDimension('N')->setWidth(0);
$spreadsheet->getActiveSheet()->getColumnDimension('O')->setWidth(0);
$spreadsheet->getActiveSheet()->getColumnDimension('P')->setWidth(0);

$spreadsheet->getActiveSheet()->setTitle('KI-4-'.$mapel);

$spreadsheet->setActiveSheetIndex(0);


header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

header("Content-Disposition: attachment; filename=$ms[kode] KI-4 kelas $klas.xlsx");
header('Cache-Control: max-age=0');

header('Cache-Control: max-age=1');


header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); 
header('Cache-Control: cache, must-revalidate'); 
header('Pragma: public');

$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');

?>
