<?php 
    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
    
    $report             = isset($_POST["report"]) ? $_POST["report"] : "";

    $spreadsheet = new Spreadsheet();
    
    switch ($report){
        case "":
        case "prices":
            $cimri = new Cimri($db, $utils, $product);
            $products         = $cimri->getProducts();

            $spreadsheet->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold( true );
            $spreadsheet->getActiveSheet()->setAutoFilter($spreadsheet->getActiveSheet()->calculateWorksheetDimension());
            $header_row_array = ['DURUM', 'ÜRÜN ADI', 'CİMRİ SATICI', 'CİMRİ FİYAT', 'SİTE FİYAT', 'FARK', 'SON GÜNCELLEME', 'CİMRİ LİNK'];
            $sheet = $spreadsheet->getActiveSheet();
            $sheet->fromArray( $header_row_array, NULL, 'A1' );
            $sheet->getColumnDimension('A')->setAutoSize(true);
            $sheet->getColumnDimension('B')->setAutoSize(true);
            $sheet->getColumnDimension('C')->setAutoSize(true);
            $sheet->getColumnDimension('D')->setAutoSize(true);
            $sheet->getColumnDimension('E')->setAutoSize(true);
            $sheet->getColumnDimension('F')->setAutoSize(true);
            $sheet->getColumnDimension('G')->setAutoSize(true);
            $sheet->getColumnDimension('H')->setAutoSize(true);
            

            $i     = 2;
            if(count($products)>0) {
                foreach($products as $row) {
                    $sheet->setCellValue('A'.$i, $row['status'] == 1 ? "Var" : "Yok");
                    $sheet->setCellValue('B'.$i, $row['title']); 
                    $sheet->setCellValue('C'.$i, $row['cimri_seller']); 
                    $sheet->setCellValue('D'.$i, $utils->moneyFormatter($row['cimri_price'])); 
                    $sheet->setCellValue('E'.$i, $utils->moneyFormatter($row['selling_price'])); 
                    $sheet->setCellValue('F'.$i, round($row['cimri_price']-$row['selling_price'], 2));
                    $sheet->setCellValue('G'.$i, $row['date_modified']);
                    $sheet->setCellValue('H'.$i, $row['cimri_url']); 

                    if($row['selling_price'] > $row['cimri_price']) {
                        $sheet->getStyle('E'.$i)->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
                    }else if($row['selling_price'] < $row['cimri_price']) {
                        $sheet->getStyle('E'.$i)->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
                    }

                    $i++;
                }
            }

            $filename = "Cimri Ürün Listesi.xlsx";
        break;
    }

    
    try {
        $writer = new Xlsx($spreadsheet);
        $writer->save($filename);
        $content = file_get_contents($filename);
    } catch(Exception $e) {
        exit($e->getMessage());
    }

    header("Content-Disposition: attachment; filename=".$filename);

    unlink($filename);
    exit($content);