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

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

if(!class_exists('PDAPP_DataFields'))
{
    class PDAPP_DataFields
    {
        public function __construct()
        {
            add_action('woocommerce_init', [&$this, 'pdapp_init']);
        }
        
        public function pdapp_init()
        {
            add_action('woocommerce_product_write_panel_tabs', [$this, 'pdapp_panel_tab']);
            if($this->pdapp_check_version('3.0'))
            {
                add_action('woocommerce_product_data_panels', [$this, 'pdapp_product_panel']);
            }else
            {
                add_action('woocommerce_product_write_panels', [$this, 'pdapp_product_panel']);
            }
            
            add_action('woocommerce_process_product_meta', [$this, 'pdapp_save_data'], 30, 2);
        }

        function pdapp_check_version($version = '3.0')
        {
            global $woocommerce;
            if(version_compare($woocommerce->version, $version, ">="))
            {
                return true;
            }
            
            return false;
        }

        public function pdapp_panel_tab()
        {
            echo '<li class="pdapp_productdata_tab" id="pdapp_productdata_tab"><a href="#pdapp_product_options_panel">'. esc_html(pdapp_lang('product.pdapp_product', 'admin')) .'</a></li>';
        }
        
        public function pdapp_product_panel()
        {
            global $pdapp_post_id, $post;

            $devMode = PDAPP_DEV_MODE ? '' : 'min.';
            $devKey = PDAPP_DEV_MODE ? '_dev' : '';
            wp_enqueue_style('pdapp_css_field'. $devKey, PDAPP_PLUGIN_URL .'admin/assets/css/pdapp-product.'. $devMode .'css', array(), PDAPP_VERSION, 'all');
            wp_enqueue_script('pdapp_js_field'. $devKey, PDAPP_PLUGIN_URL .'admin/assets/js/pdapp-product.'. $devMode .'js', ['jquery'], PDAPP_VERSION, true);
            echo '<div id="pdapp_product_options_panel" class="panel woocommerce_options_panel">';

            echo '<div id="pdapp_product_options_box"><div class="pdapp_product_options_topbox">';
            echo '<button type="button" class="pdapp-btn pdapp-btn-primary tooltip-right" data-tooltip="'. esc_html(pdapp_lang('product.select_product_design', 'admin')) .'" onclick="pdappProduct.getProduct()"><span class="pdapp-btn-spinner pdapp-none" role="status" aria-hidden="true""></span> '. esc_html(pdapp_lang('product.select_pdapp_product', 'admin')) .'</button><button type="button" class="pdapp-btn pdapp-btn-danger pdapp-btn-clearproduct pdapp-none" data-tooltip="'. esc_html(pdapp_lang('product.clear_product_design', 'admin')) .'" onclick="pdappProduct.clearProduct()">'. esc_html(pdapp_lang('clear', 'admin')) .'</button>';
            echo '</div><div class="pdapp_product_options_box pdapp-none"><div class="pdapp_product_options_product"><strong class="pdapp_option_title">'. esc_html(pdapp_lang('product.pdapp_product_info', 'admin')) .'</strong><div class="pdapp_product_info"></div></div><div class="pdapp_product_options_template"><strong class="pdapp_option_title">'. esc_html(pdapp_lang('product.pdapp_design_template', 'admin')) .'</strong><div class="pdapp_product_template"></div></div></div>';
            echo '<div id="pdapp-product-modal" class="pdapp-modal pdapp-fade pdapp-show" role="dialog"><div class="pdapp-modal-dialog pdapp-modal-full-width pdapp-modal-dialog-scrollable"><div class="pdapp-modal-content"><div class="pdapp-modal-header"><h4 class="pdapp-modal-title pdapp-list-products">'. esc_html(pdapp_lang('product.list_products', 'admin')) .'</h4><h4 class="pdapp-modal-title pdapp-list-templates pdapp-none">'. esc_html(pdapp_lang('product.list_templates', 'admin')) .'</h4><button type="button" class="pdapp-btn-close" onclick="pdappProduct.closeModal()"></button></div><div class="pdapp-modal-body pdapp-none"><div id="pdapp-list-products" class="pdapp-list-products"><div class="pdapp-list-topbox"><strong>'. esc_html(pdapp_lang('product.products', 'admin')) .'</strong><input id="pdapp-search-products" type="text" class="pdapp-input-search" placeholder="'. esc_html(pdapp_lang('product.search', 'admin')) .'" onkeyup="pdappProduct.searchProducts()"/></div><div class="pdapp-products pdapp-row"></div></div><div id="pdapp-list-templates" class="pdapp-none pdapp-list-templates"><div class="pdapp-list-topbox"><strong>'. esc_html(pdapp_lang('product.folders', 'admin')) .'</strong><span class="pdapp-none pdapp-templates-text">'. esc_html(pdapp_lang('product.templates', 'admin')) .'</span><ol class="pdapp-breadcrumb"></ol></div><div class="pdapp-list-folder pdapp-row"></div><div class="pdapp-list-topbox pdapp-temps"><strong>'. esc_html(pdapp_lang('product.templates', 'admin')) .'</strong><input id="pdapp-search-templates" type="text" class="pdapp-input-search" placeholder="'. esc_html(pdapp_lang('product.search_templates', 'admin')) .'" onkeyup="pdappProduct.searchTemplates()"/></div><div class="pdapp-list-temps pdapp-temps pdapp-row"></div></div></div><div class="pdapp-loading-box pdapp-none"><div class="pdapp-loading-mask"><div class="pdapp-spinner"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div></div></div></div></div></div>';
            echo '<div class="pdapp-modal-backdrop pdapp-fade pdapp-show pdapp-none" onclick="pdappProduct.closeModal()"></div><div class="pdapp-toast pdapp-show pdapp-fade pdapp-none" role="alert"><div class="pdapp-toast-body"></div></div></div>';

            $script = 'var pdappAdminUrl = "'. esc_url(PDAPP_ADMIN_URL) .'";var pdappNonce = "'. esc_html(wp_create_nonce(PDAPP_ADMIN_NONCE)) .'";var pdappUploadUrl = "'. esc_url(PDAPP_UPLOAD_URL) .'";var pdappPluginUrl = "'. esc_url(PDAPP_PLUGIN_URL) .'";var pdappLangDefault = "'. esc_html(pdapp_lang_code()) .'";';
            wp_enqueue_script('pdapp-script', PDAPP_PLUGIN_URL . 'assets/js/pdapp-inline.js', ['jquery'], PDAPP_VERSION.'-'. time(), true);
            wp_add_inline_script('pdapp-script', wp_kses_post($script), 'before');

            $pdapp_post_id = empty($pdapp_post_id) ? $post->ID : $pdapp_post_id;
            $pDesign = pdapp_woo_get_product(["pid"=>$pdapp_post_id]);
            $productID = isset($pDesign['did']) ? $pDesign['did'] : '';
            $valueID = isset($pDesign['vid']) ? $pDesign['vid'] : '';
            $pTitle = isset($pDesign['title']) ? $pDesign['title'] : '';
            $pThumb = isset($pDesign['thumb']) ? $pDesign['thumb'] : '';
            $pValues = isset($pDesign['values']) ? $pDesign['values'] : [];

            echo '<input type="hidden" name="_pdapp_product_id" id="pdapp_product_design_id" value="'. esc_html($productID) .'"/>';
            echo '<input type="hidden" name="_pdapp_value_id" id="pdapp_product_value_id" value="'. esc_html($valueID) .'"/>';
            echo '<input type="hidden" name="_pdapp_product_title" id="pdapp_product_design_title" value="'. esc_attr(str_replace('"', '',str_replace('\"', '', str_replace('\\', '', $pTitle)))) .'"/>';
            echo '<input type="hidden" name="_pdapp_product_thumb" id="pdapp_product_design_thumb" value="'. esc_html($pThumb) .'"/>';
            echo '<input type="hidden" name="_pdapp_product_design" id="pdapp_product_design_value" value="'. esc_attr(htmlspecialchars(pdapp_json_encode($pValues))) .'"/>';
            echo '</div>';
        }

        public function pdapp_save_data($postID, $post)
        {
            $productID = pdapp_post('_pdapp_product_id', 'key', 0);
            $valueID = pdapp_post('_pdapp_value_id', 'key', 0);
            $title = pdapp_post('_pdapp_product_title', 'text', '');
            $thumb = pdapp_post('_pdapp_product_thumb', 'text', '');
            $design = pdapp_post('_pdapp_product_design', 'text', '');
            $designs = ($design != '') ? json_decode($design, true) : [];
            
            $pData = pdapp_woo_get_product(["pid"=>$postID]);
            if ($productID !== '')
            {
                if(isset($pData['id']))
                {
                    $pData['did'] = $productID;
                    $pData['vid'] = $valueID;
                    $pData['title'] = $title;
                    $pData['thumb'] = $thumb;
                    $pData['values'] = $designs;
                }else
                {
                    $pData = ["id"=>0, "pid"=>$postID, "did"=>$productID, "vid"=>$valueID, "title"=>$title, "thumb"=>$thumb, "type"=>"p", "values"=>$designs];
                }
                pdapp_woo_save_product(["pid"=>$postID], $pData);
            }else
            {
                if(isset($pData['id']))
                {
                    $pData['did'] = $productID;
                    $pData['vid'] = $valueID;
                    $pData['title'] = $title;
                    $pData['thumb'] = $thumb;
                    $pData['values'] = $designs;
                    pdapp_woo_save_product(["pid"=>$postID], $pData);
                }
            }
        }
    }
}

new PDAPP_DataFields();