<?php 
    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

    $spreadsheet = new Spreadsheet();

    
    $search_keywords    = isset($_POST['q']) ? htmlspecialchars($_POST['q'], ENT_QUOTES, 'UTF-8') : "";
    $date_start         = isset($_POST['date_start']) ? htmlspecialchars($_POST['date_start'], ENT_QUOTES, 'UTF-8') : "";
    $date_end           = isset($_POST['date_end']) ? htmlspecialchars($_POST['date_end'], ENT_QUOTES, 'UTF-8') : "";
    $status             = isset($_POST["status"]) ? $_POST["status"] : "";
    $selected_orders    = isset($_POST['selected_orders']) && !empty($_POST['selected_orders']) ? $_POST['selected_orders'] : null;
    $sort               = isset($_POST["sort"]) ? $_POST["sort"] : "id";
    $sort_order         = isset($_POST["order"]) ? $_POST["order"] : "DESC";

    $page               = !isset($_POST["page"]) || $_POST["page"] < 1 ? 1 : (int)$_POST["page"];
    $limit	            = isset($_POST["limit"]) ? (int)$_POST["limit"] : 25;
    $offset             = ($page-1)*$limit;
    $filter_data = array(
        'search_keywords'    => $search_keywords,
        'status'             => $status,
        'in'                 => $selected_orders,
        'date_start'         => $date_start,
        'date_end'           => $date_end,
        'sort'               => $sort,
        'order'              => $sort_order
    );
    
    $orders             = $order->getOrders($filter_data);
    $total_orders 	    = $order->getTotalOrders($filter_data);

    $spreadsheet->getActiveSheet()->getStyle('A1:L1')->getFont()->setBold( true );
    $spreadsheet->getActiveSheet()->setAutoFilter($spreadsheet->getActiveSheet()->calculateWorksheetDimension());
    $header_row_array = ['TARİH', 'SİPARİŞ NO', 'DURUM', 'ALICI ADI', 'TELEFON', 'ADRES', 'ŞEHİR', 'TUTAR', 'SANAL POS', 'HEDİYE NOTU', 'SİPARİŞ NOTU', 'ÜRÜNLER'];
    $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);
    $sheet->getColumnDimension('I')->setAutoSize(true);
    $sheet->getColumnDimension('J')->setAutoSize(true);
    $sheet->getColumnDimension('K')->setAutoSize(true);
    $sheet->getColumnDimension('L')->setAutoSize(true);
    

    $i     = 2;
    if($total_orders>0) {
        foreach($orders as $row) {
            $order_items         = $order->getOrderItems($row["id"]);
            $items = "";
            foreach ($order_items as $item) {
                $items .= $item["quantity"] ." x " . $item["product_title"] . ",\n";
            }
            $items    = rtrim($items);
            $items    = rtrim($items, ',');

            $sheet->setCellValue('A'.$i, $row['date_added']);
            $sheet->setCellValue('B'.$i, $row['order_id']); 
            $sheet->setCellValue('C'.$i, $row['status_text']); 
            $sheet->setCellValue('D'.$i, $row['name']." ".$row['surname']); 
            $sheet->setCellValue('E'.$i, $row['phone']); 
            $sheet->setCellValue('F'.$i, $row['shipping_address']." ".$row['shipping_town']."/".$row['shipping_city']); 
            $sheet->setCellValue('G'.$i, $row['shipping_city']); 
            $sheet->setCellValue('H'.$i, $row['order_total']); 
            $sheet->setCellValue('I'.$i, $row['payment_gateway']); 
            $sheet->setCellValue('J'.$i, !empty($row['gift_note']) ? "VAR" : "YOK"); 
            $sheet->setCellValue('K'.$i, !empty($row['order_note']) ? "VAR" : "YOK");
            $sheet->setCellValue('L'.$i, $items);

            $sheet->getStyle('F'.$i)->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);

            $i++;
        }
    }

    $filename = "Sipariş Listesi.xlsx";
    
    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);