<?php  
	
	include 'aksi/koneksi.php';
	require 'vendor/export-import/autoload.php';

	use PhpOffice\PhpSpreadsheet\Spreadsheet;
	use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
	use PhpOffice\PhpSpreadsheet\Writer\Xls;
	use PhpOffice\PhpSpreadsheet\Writer\Csv;


	if(isset($_POST['export_btn']))
	{
		// $ext 		= "xls";
		$sessionCabang = $_POST['barang_cabang'];
		$fileName 	= "data-barang-export-tanggal-".date("Y-m-d");

		$query 		= "SELECT * FROM barang WHERE barang_cabang = $sessionCabang ORDER BY barang_id DESC ";
		$query_run 	= mysqli_query($conn, $query);

		if( mysqli_num_rows($query_run) > 0 ) {

			$spreadsheet = new Spreadsheet();
			$sheet 		 = $spreadsheet->getActiveSheet();
			$sheet->setCellValue('A1', 'No.');
			$sheet->setCellValue('B1', 'Kode Barang/Barcode');
			$sheet->setCellValue('C1', 'Nama Barang');
			$sheet->setCellValue('D1', 'Deskripsi');
			$sheet->setCellValue('E1', 'Kategori');
			$sheet->setCellValue('F1', 'Satuan Utama');
			$sheet->setCellValue('G1', 'Harga Beli');
			$sheet->setCellValue('H1', 'Harga Umum Satuan Utama');
			$sheet->setCellValue('I1', 'Harga Grosir 1 Satuan Utama');
			$sheet->setCellValue('J1', 'Harga Grosir 2 Satuan Utama');
			$sheet->setCellValue('K1', 'SN/Non SN');
			$sheet->setCellValue('L1', 'Stok');
			$sheet->setCellValue('M1', 'Satuan Kedua');
			$sheet->setCellValue('N1', 'Konversi Isi Dari Satuan Utama');
			$sheet->setCellValue('O1', 'Harga Umum Satuan Kedua');
			$sheet->setCellValue('P1', 'Harga Grosir 1 Satuan Kedua');
			$sheet->setCellValue('Q1', 'Harga Grosir 2 Satuan Kedua');
			$sheet->setCellValue('R1', 'Satuan Ketiga');
			$sheet->setCellValue('S1', 'Konversi Isi Dari Satuan Utama');
			$sheet->setCellValue('T1', 'Harga Umum Satuan Ketiga');
			$sheet->setCellValue('U1', 'Harga Grosir 1 Satuan Ketiga');
			$sheet->setCellValue('V1', 'Harga Grosir 2 Satuan Ketiga');
			$sheet->setCellValue('W1', 'Tanggal Input');
			$sheet->setCellValue('X1', 'Terjual');
			$sheet->setCellValue('Y1', 'Cabang');	

			$i = 1;
			$rowCount = 2;
			foreach ( $query_run as $data ) {
				$sheet->setCellValue('A' . $rowCount, $i);
				$sheet->setCellValue('B' . $rowCount, $data['barang_kode']);
				$sheet->setCellValue('C' . $rowCount, $data['barang_nama']);
				$sheet->setCellValue('D' . $rowCount, htmlspecialchars_decode($data['barang_deskripsi']));

				// Mencari Nama Kategori
				$kategori 		= $data['kategori_id'];
				$dataKategori 	= mysqli_query($conn, "SELECT kategori_nama FROM kategori WHERE kategori_id = $kategori ");
				$dataKategori 	= mysqli_fetch_array($dataKategori);
				$kategori_nama 	= $dataKategori['kategori_nama'];
				$sheet->setCellValue('E' . $rowCount, $kategori_nama);

				// Mencari Nama Satuan
				$satuan 		= $data['satuan_id'];
				$dataSatuan 	= mysqli_query($conn, "SELECT satuan_nama FROM satuan WHERE satuan_id = $satuan ");
				$dataSatuan 	= mysqli_fetch_array($dataSatuan);
				$satuan_nama 	= $dataSatuan['satuan_nama'];
				$sheet->setCellValue('F' . $rowCount, $satuan_nama);

				$sheet->setCellValue('G' . $rowCount, $data['barang_harga_beli']);
				$sheet->setCellValue('H' . $rowCount, $data['barang_harga']);
				$sheet->setCellValue('I' . $rowCount, $data['barang_harga_grosir_1']);
				$sheet->setCellValue('J' . $rowCount, $data['barang_harga_grosir_2']);

				// Mencari Data SN Non SN
				$snNonSn = $data['barang_option_sn'];
				if ( $snNonSn < 1 ) {
					$namesnNonSn = 'Non SN';
				} else {
					$namesnNonSn = 'SN';
				}
				$sheet->setCellValue('K' . $rowCount, $namesnNonSn);
				$sheet->setCellValue('L' . $rowCount, $data['barang_stock']);

				// Mencari Nama Satuan
				$satuan2 		= $data['satuan_id_2'];
				if ( $satuan2 < 1 ) {
					$satuan_nama2 = "";
				} else {
					$dataSatuan2 	= mysqli_query($conn, "SELECT satuan_nama FROM satuan WHERE satuan_id = $satuan2 ");
					$dataSatuan2 	= mysqli_fetch_array($dataSatuan2);
					$satuan_nama2 	= $dataSatuan2['satuan_nama'];
				}
				$sheet->setCellValue('M' . $rowCount, $satuan_nama2);
		
				$sheet->setCellValue('N' . $rowCount, $data['satuan_isi_2']);
				$sheet->setCellValue('O' . $rowCount, $data['barang_harga_s2']);
				$sheet->setCellValue('P' . $rowCount, $data['barang_harga_grosir_1_s2']);
				$sheet->setCellValue('Q' . $rowCount, $data['barang_harga_grosir_2_s2']);

				// Mencari Nama Satuan
				$satuan3 		= $data['satuan_id_3'];
				if ( $satuan3 < 1 ) {
					$satuan_nama3 = "";
				} else {
					$dataSatuan3 	= mysqli_query($conn, "SELECT satuan_nama FROM satuan WHERE satuan_id = $satuan3 ");
					$dataSatuan3 	= mysqli_fetch_array($dataSatuan3);
					$satuan_nama3 	= $dataSatuan3['satuan_nama'];
				}
				$sheet->setCellValue('R' . $rowCount, $satuan_nama3);

				$sheet->setCellValue('S' . $rowCount, $data['satuan_isi_3']);
				$sheet->setCellValue('T' . $rowCount, $data['barang_harga_s3']);
				$sheet->setCellValue('U' . $rowCount, $data['barang_harga_grosir_1_s3']);
				$sheet->setCellValue('V' . $rowCount, $data['barang_harga_grosir_2_s3']);

				$sheet->setCellValue('W' . $rowCount, $data['barang_tanggal']);
				$sheet->setCellValue('X' . $rowCount, $data['barang_terjual']);

				// Mencari Data Cabang
				$cabang = $data['barang_cabang'];
				if ( $cabang < 1 ) {
					$nameCabang = 'Pusat';
				} else {
					$nameCabang = 'Cabang ke '.$cabang;
				}
				$sheet->setCellValue('Y' . $rowCount, $nameCabang);
				$i++;
				$rowCount++;
			}

			// if ( $ext == 'xlsx' ) {
			// 	$writer 		= new Xlsx($spreadsheet);
			// 	$final_fileName = $fileName.'.xlsx';
			// } elseif ( $ext == 'xls' ) {
			// 	$writer 		= new Xls($spreadsheet);
			// 	$final_fileName = $fileName.'.xls';
			// } elseif ( $ext == 'csv' ) {
			// 	$writer 		= new Csv($spreadsheet);
			// 	$final_fileName = $fileName.'.csv';
			// }
			
			$writer 		= new Xlsx($spreadsheet);
			$final_fileName = $fileName.'.xlsx';

			// $writer->save($final_fileName);
			header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
			header('Content-Disposition: attachment; filename="'.urlencode($final_fileName).'" ');
			$writer->save('php://output');


		} else {
			echo "
				<script>
					alert('Data Gagal Export');
					document.location.href = 'barang';
				</script>
			";
		}
	}