<?php
namespace Elementor\LearnDash;

defined('ABSPATH') || exit;

class WKFE_PRO_LD_CPT_Modal {

    public function __construct() {
        $this->dir = dirname(__FILE__) . '/';
        $this->url = plugin_dir_url(__FILE__);

        add_action('admin_footer',[$this,'wkfe_ld_template_modal']);

        add_action('wp_ajax_wkfe_ld_template_save_data',[$this,'wkfe_ld_template_save_data']);
        add_action('wp_ajax_wkfe_ld_template_get_data',[$this,'wkfe_ld_template_get_data']);
        add_action('admin_enqueue_scripts',[$this,'wkfe_ld_template_admin_essential_scripts']);
        add_filter( 'elementor/editor/localize_settings',[$this,'elementor_settings_localize']);
    }

    public function elementor_settings_localize( $settings ) {
        $id = $settings['initial_document']['id'];
        if( get_post_type($id) === 'wkfe-ld-template' ) {
            unset($settings['initial_document']['panel']['elements_categories']['widgetkit_pro_learndash_elements']);
            $categories = [
                'widgetkit_pro_learndash_elements' => [
                    'title' => __( 'Learndash Elements', 'widgetkit-pro' ),
                ]
            ];
            $categories = $categories + $settings['initial_document']['panel']['elements_categories'];
            $settings['initial_document']['panel']['elements_categories'] = $categories;
        } else {
            unset($settings['initial_document']['panel']['elements_categories']['widgetkit_pro_learndash_elements']);
        }
        return $settings;
    } 

    public function wkfe_ld_template_get_data() {
        $formid = $_POST['formid'];

        $ldpost = get_post($formid);

        if( $ldpost ) {
            $data =  array(
                'title' =>  $ldpost->post_title,
                'course_id' => get_post_meta($ldpost->ID,'_wkfe_ld_template_selected_post_id',true),
                'activation' => get_post_meta($ldpost->ID,'_wkfe_ld_template_activation',true) 
            );

            wp_send_json_success( $data );
        }


        wp_die();
    }

    public function wkfe_ld_template_save_data() {
        $formid = $_POST['formid'];
        $title = ($_POST['title'] == '') ? ('Learndash Template #' . time()) : $_POST['title'];
        $activation = $_POST['activation'];
        $course_id = $_POST['course_id'];
        $inelementor = $_POST['inelementor'];
        $postdata = array(
            'post_title' => $title,
            'post_status'   => 'publish',
            'post_type' => 'wkfe-ld-template',
        );

        $txpost = get_post($formid);
        if( null == $txpost ) {
            $formid = wp_insert_post($postdata);
        } else {
            $postdata['ID'] = $formid;
            wp_update_post( $postdata );
        }

        update_post_meta( $formid, '_wp_page_template', 'elementor_header_footer' );
        update_post_meta( $formid, '_wkfe_ld_template_activation', $activation );
        update_post_meta( $formid, '_wkfe_ld_template_selected_post_id', $course_id );

        $data =  array(
            'postid' =>  $formid,
            'inelementor'   => $inelementor
        );

        wp_send_json_success( $data );
        
        wp_die();
    }

    public function wkfe_ld_template_modal() {
        $screen = get_current_screen();
		if( isset($screen) &&  $screen->id == 'edit-wkfe-ld-template' ){
			include_once $this->dir. 'templates/modal.php';
        }
    }

    public function wkfe_ld_template_admin_essential_scripts( $screen ) {
        $screen = get_current_screen();
		if( isset($screen) && $screen->id == 'edit-wkfe-ld-template'){

            wp_enqueue_script('wkfe-ld-template-modal',$this->url.'assets/js/modal.js',array('jquery','jquery-ui-dialog'),'1.0',true);
            wp_localize_script('wkfe-ld-template-modal','wkfe_ld_template_modal_obj',array(
                'admin_url' => admin_url( 'admin-ajax.php' ),
                'security'  => wp_create_nonce('wkfe_ld_template_modal_nonce')
            ));

            wp_enqueue_style('jquery-ui',$this->url. 'assets/css/jquery-ui.min.css',array(),'1.12.1');
        }
    }
}

new WKFE_PRO_LD_CPT_Modal();