<?php
/*
* Plugin-Name: Product Designer App
* Plugin-URI: https://productdesignerapp.com
* Author: productdesignerapp
* Backend function
*/

if (!defined('ABSPATH'))
{
    exit;
}

/*
* Add customize data in order item
*/
add_action('woocommerce_before_order_itemmeta', 'pdapp_display_customize_in_order_admin', 1, 3);
if(!function_exists('pdapp_display_customize_in_order_admin'))
{
    function pdapp_display_customize_in_order_admin($item_id, $item, $product)
    {
        $pdappDesign = wc_get_order_item_meta($item_id, "pdappDesign", true);

        if(isset($pdappDesign['designID']))
        {
            $thumbs = $pdappDesign['thumb'];
            $html = '<div style="display: flex; column-gap: 20px; flex-wrap: wrap;">';
            $html .= '<div style="display: flex; column-gap: 10px; flex-wrap: wrap; align-items: center;">';
            foreach($thumbs as $thumb)
            {
                $img = PDAPP_UPLOAD_URL .'orders/'. $pdappDesign['folder'] .'/thumb/'. $thumb;
                $html .= '<a href="'. esc_url($img) .'" target="_blank"><img src="'. esc_url($img) .'" style="border: 1px solid #ddd;max-width: 80px;" alt="" class="pdapp-cart-thumb wp-post-image"></a>';
            }
            $html .= '</div>';
            $html .= '<div style="display: flex; flex-direction: column; flex-wrap: wrap; row-gap: 5px; line-height: 14px;">';
            
            foreach($pdappDesign['options'] as $key=>$val)
            {
                $html .= '<p style="margin: 0px;">'. $key .': '.$val.'</p>';
            }
            if($pdappDesign['note'] != '') $html .= '<p style="margin: 0px;">'. pdapp_lang('order.note', 'system') .': '.$pdappDesign['note'].'</p>';

            $downloadURL = PDAPP_ADMIN_URL .'admin.php?page='. PDAPP_PLUGIN_SLUG .'-orders&detail='. $pdappDesign['orderID'] .'&item='. $pdappDesign['detailID'];
            $html .= '<p style="margin: 5px 0px;"><a target="_blank" href="'. esc_url($downloadURL).'" class="button button-primary"><strong>'. pdapp_lang('order.download_design', 'system') .'</strong></a></p>';
            
            $html .= '</div>';
            $html .= '</div>';
            echo wp_kses_post($html);
        }
    }
}