<?php
if (is_admin()) {
    global $wpdb;

    // Export in csv
    $filename = "voucher_orders.csv";
    header('Content-Encoding: UTF-8');
    header('Content-type: text/csv; charset=UTF-8');
    header('Content-Disposition: attachment; filename=' . $filename);
    header('Pragma: no-cache');
    header('Expires: 0');
    ob_clean();

    $file = fopen('php://output', 'w');

    // Ghi BOM UTF-8
    fwrite($file, chr(0xEF) . chr(0xBB) . chr(0xBF));

    fputcsv($file, array('Id', 'Order Type', 'Template Id', 'Template Name', 'Itemcat Id', 'ItemCat Name', 'Item Id', 'Item Name', 'Buying For', 'From Name', 'To Name', 'Amount', 'Message', 'First Name', 'Last Name', 'Email', 'Address', 'Postcode', 'Payment Method', 'Shipping Type', 'Shipping Email', 'Shipping Method', 'Expiry Date', 'Voucher Code', 'Voucher Pdf Link', 'Order Date', 'Status', 'Payment Status'));

    $voucher_orders = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}giftvouchers_list");

    foreach ($voucher_orders as $voucher_order) {
        $id = $voucher_order->id ?? '';
        $order_type = $voucher_order->order_type ?? '';
        $template_id = $voucher_order->template_id ?? '';

        $template_name_arr = $wpdb->get_row("SELECT title FROM {$wpdb->prefix}giftvouchers_template WHERE id='$template_id' ", ARRAY_A);
        $template_name = $template_name_arr['title'] ?? '';

        $itemcat_id = $voucher_order->itemcat_id ?? '';
        $itemcat_name_arr = get_term_by('id', $itemcat_id, 'wpgv_voucher_category', ARRAY_A);
        $itemcat_name = $itemcat_name_arr['name'] ?? '';

        $item_id = $voucher_order->item_id ?? '';
        $item_name = get_the_title($item_id);

        $buying_for = $voucher_order->buying_for ?? '';
        $from_name = $voucher_order->from_name ?? '';
        $to_name = $voucher_order->to_name ?? '';

        $setting_options = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}giftvouchers_setting WHERE id = 1");

        $amount = $voucher_order->amount ?? '';
        $currency = $setting_options->currency ?? '';
        $amount_with_currency = $currency . ' ' . $amount;

        $message = $voucher_order->message ?? '';
        $firstname = $voucher_order->firstname ?? '';
        $lastname = $voucher_order->lastname ?? '';
        $email = $voucher_order->email ?? '';
        $address = $voucher_order->address ?? '';
        $postcode = $voucher_order->postcode ?? '';
        $pay_method = $voucher_order->pay_method ?? '';
        $shipping_type = $voucher_order->shipping_type ?? '';
        $shipping_email = $voucher_order->shipping_email ?? '';
        $shipping_method = ($voucher_order->shipping_method === 'undefined') ? '' : $voucher_order->shipping_method;

        $expiry = $voucher_order->expiry ?? '';
        $couponcode = $voucher_order->couponcode ?? '';
        $couponcode = '="' . $couponcode . '"';
        $voucherpdf_link = $voucher_order->voucherpdf_link ?? '';

        $uploads = wp_upload_dir();
        $upload_path = $uploads['baseurl'];
        $full_path_voucherpdf_link = $upload_path . "/voucherpdfuploads/" . $voucherpdf_link . ".pdf";

        $voucheradd_time = $voucher_order->voucheradd_time ?? '';
        $status = $voucher_order->status ?? '';
        $payment_status = $voucher_order->payment_status ?? '';

        fputcsv($file, array(
            $id,
            $order_type,
            $template_id,
            $template_name,
            $itemcat_id,
            $itemcat_name,
            $item_id,
            $item_name,
            $buying_for,
            $from_name,
            $to_name,
            $amount_with_currency,
            $message,
            $firstname,
            $lastname,
            $email,
            $address,
            $postcode,
            $pay_method,
            $shipping_type,
            $shipping_email,
            $shipping_method,
            $expiry,
            $couponcode,
            $full_path_voucherpdf_link,
            $voucheradd_time,
            $status,
            $payment_status
        ));
    }
    exit();
}
