/**
 * View Preview for Voucher PDF
 *
 * Handles to view preview for voucher pdf
 *
 * @package WooCommerce - PDF Vouchers
 * @since   1.0.0
 */
function woo_vou_preview_pdf()
{
    global $woo_vou_model;
    $model = $woo_vou_model;
    $pdf_args = array();
    if (isset($_GET['voucher_id']) && !empty($_GET['voucher_id']) && isset($_GET['woo_vou_pdf_action']) && $_GET['woo_vou_pdf_action'] == 'preview') {
        // Getting voucher character support
        $voucher_char_support = get_option('vou_char_support');
        $voucher_template_id = $_GET['voucher_id'];
        $pdf_args['vou_template_id'] = $voucher_template_id;
        //site logo
        $vousitelogohtml = '';
        $vou_site_url = get_option('vou_site_logo');
        if (!empty($vou_site_url)) {
            $vousitelogohtml = '<img src="' . $vou_site_url . '" alt="" />';
        }
        //vendor's logo
        $vou_url = WOO_VOU_IMG_URL . '/vendor-logo.png';
        $voulogohtml = '<img src="' . $vou_url . '" alt="" />';
        $vendor_address = __('Infiniti Mall Malad', 'woovoucher') . "\n\r" . __('GF 9 & 10, Link Road, Mindspace, Malad West', 'woovoucher') . "\n\r" . __('Mumbai, Maharashtra 400064', 'woovoucher');
        $vendor_address = nl2br($vendor_address);
        $nextmonth = mktime(date("H"), date("i"), date("s"), date("m") + 1, date("d"), date("Y"));
        $previousmonth = mktime(date("H"), date("i"), date("s"), date("m") - 1, date("d"), date("Y"));
        $redeem_instruction = __('Redeem instructions :', 'woovoucher');
        $redeem_instruction .= __('Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s.', 'woovoucher');
        $locations = '<strong>' . __('DELHI:', 'woovoucher') . '</strong> ' . __('Dlf Promenade Mall & Pacific Mall', 'woovoucher');
        $locations .= ' <strong>' . __('MUMBAI:', 'woovoucher') . '</strong> ' . __('Infiniti Mall, Malad & Phoenix MarketCity', 'woovoucher');
        $locations .= ' <strong>' . __('BANGALORE:', 'woovoucher') . '</strong> ' . __('Phoenix MarketCity Mall', 'woovoucher');
        $locations .= ' <strong>' . __('PUNE:', 'woovoucher') . '</strong> ' . __('Phoenix MarketCity Mall', 'woovoucher');
        $buyer_fullname = __('WpWeb', 'woovoucher');
        $buyer_email = '*****@*****.**';
        $orderid = '101';
        $orderdate = date("d-m-Y");
        $productname = __('Test Product', 'woovoucher');
        $productprice = '$' . number_format('10', 2);
        $codes = __('[The voucher code will be inserted automatically here]', 'woovoucher');
        $voucher_rec_name = 'Test Name';
        $voucher_rec_email = '*****@*****.**';
        $voucher_rec_message = 'Test message';
        $payment_method = 'Test Payment Method';
        $content_post = get_post($voucher_template_id);
        $content = isset($content_post->post_content) ? $content_post->post_content : '';
        $post_title = isset($content_post->post_title) ? $content_post->post_title : '';
        $voucher_template_html = do_shortcode($content);
        $voucher_template_html = str_replace('{redeem}', $redeem_instruction, $voucher_template_html);
        $voucher_template_html = str_replace('{vendorlogo}', $voulogohtml, $voucher_template_html);
        $voucher_template_html = str_replace('{sitelogo}', $vousitelogohtml, $voucher_template_html);
        $voucher_template_html = str_replace('{startdate}', $model->woo_vou_get_date_format(date('d-m-Y', $previousmonth)), $voucher_template_html);
        $voucher_template_html = str_replace('{startdatetime}', $model->woo_vou_get_date_format(date('d-m-Y H:i:s', $previousmonth), true), $voucher_template_html);
        $voucher_template_html = str_replace('{expiredate}', $model->woo_vou_get_date_format(date('d-m-Y', $nextmonth)), $voucher_template_html);
        $voucher_template_html = str_replace('{expiredatetime}', $model->woo_vou_get_date_format(date('d-m-Y H:i:s', $nextmonth), true), $voucher_template_html);
        $voucher_template_html = str_replace('{vendoraddress}', $vendor_address, $voucher_template_html);
        $voucher_template_html = str_replace('{siteurl}', 'www.bebe.com', $voucher_template_html);
        $voucher_template_html = str_replace('{location}', $locations, $voucher_template_html);
        $voucher_template_html = str_replace('{buyername}', $buyer_fullname, $voucher_template_html);
        $voucher_template_html = str_replace('{buyeremail}', $buyer_email, $voucher_template_html);
        $voucher_template_html = str_replace('{orderid}', $orderid, $voucher_template_html);
        $voucher_template_html = str_replace('{orderdate}', $model->woo_vou_get_date_format($orderdate), $voucher_template_html);
        $voucher_template_html = str_replace('{productname}', $productname, $voucher_template_html);
        $voucher_template_html = str_replace('{productprice}', $productprice, $voucher_template_html);
        $voucher_template_html = str_replace('{codes}', $codes, $voucher_template_html);
        $voucher_template_html = str_replace('{recipientname}', $voucher_rec_name, $voucher_template_html);
        $voucher_template_html = str_replace('{recipientemail}', $voucher_rec_email, $voucher_template_html);
        $voucher_template_html = str_replace('{recipientmessage}', $voucher_rec_message, $voucher_template_html);
        $voucher_template_html = str_replace('{payment_method}', $payment_method, $voucher_template_html);
        //Set pdf name
        $post_title = str_replace(' ', '-', strtolower($post_title));
        $pdf_args['pdf_name'] = $post_title . __('-preview-', 'woovoucher') . $voucher_template_id;
        $pdf_args['vou_codes'] = $codes;
        $pdf_args['char_support'] = !empty($voucher_char_support) && $voucher_char_support == 'yes' ? 1 : 0;
        // Character support
        woo_vou_generate_pdf_by_html($voucher_template_html, $pdf_args);
    }
}
        $voucher_template_html .= '<tr>
									<td colspan="4"><h4>' . __('Locations where you can redeem the Voucher', 'woovoucher') . '</h4></td>
								</tr>';
        foreach ($locations as $key => $value) {
            /*$key = $key + 1;
            		$location = '(' . $key . ') ' . $value[$prefix.'locations'];
            		
            		if( !empty( $value[$prefix.'map_link'] ) ) { 
            			$location .= ' - ' . $value[$prefix.'map_link'];
            		}*/
            $location = '';
            if (isset($value[$prefix . 'locations']) && !empty($value[$prefix . 'locations'])) {
                if (isset($value[$prefix . 'map_link']) && !empty($value[$prefix . 'map_link'])) {
                    $location .= '<a style="text-decoration: none;" href="' . $value[$prefix . 'map_link'] . '">' . $value[$prefix . 'locations'] . '</a> ';
                } else {
                    $location .= $value[$prefix . 'locations'] . ' ';
                }
            }
            $voucher_template_html .= '<tr>
									<td colspan="4">' . $location . '</td>
								</tr>';
        }
    }
    $voucher_template_html .= '</table>';
}
$pdf_args['vou_codes'] = $voucodes;
// Taking voucher codes
$pdf_args['char_support'] = !empty($voucher_char_support) && $voucher_char_support == 'yes' ? 1 : 0;
// Character support
woo_vou_generate_pdf_by_html($voucher_template_html, $pdf_args);