<?php


namespace Elex\MultiVendor\Controller;

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

use Elex\MultiVendor\Model\Product;

use Elex\MultiVendor\Model\Vendor;


class CategoryController {

	public static $nonce_error_msg = 'Wrong Nonce';

	public static function init() {

		$self = new self();
		add_action( 'wp_ajax_elex_mv_delete_category', array( $self, 'elex_mv_delete_category' ) );
		
		add_action( 'wp_ajax_elex_mv_rename_display', array( $self, 'elex_mv_rename_display' ) );
		
		add_action( 'wp_ajax_elex_mv_category_rename_form' , array( $self, 'elex_mv_category_rename_form' ) );

		add_action( 'wp_ajax_elex_mv_add_category_name' , array( $self, 'elex_mv_add_category_name' ) );
	}

	public function elex_mv_delete_category() {
	
		if ( ! isset( $_POST['nonce'] ) || 
		! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'elex_mv_prod_page_nonce' ) 
		) {
			wp_send_json_error( self::$nonce_error_msg, 200 );
		}

		if ( isset( $_POST['id'] ) ) {
			wp_delete_term( sanitize_text_field( wp_unslash( $_POST['id'] ) ) , 'product_cat' );
		
		}
	}

	public function elex_mv_rename_display() {

		if ( ! isset( $_POST['nonce'] ) || 
		! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'elex_mv_prod_page_nonce' ) 
		) {
			wp_send_json_error( self::$nonce_error_msg, 200 );
		}

		if ( isset( $_POST['id'] ) ) {

			$category_details           = get_the_category_by_ID( sanitize_text_field( wp_unslash( $_POST['id'] ) ) );
			$data_edit                  = array();
			$data_edit['category_id']   = sanitize_text_field( wp_unslash( $_POST['id'] ) );
			$data_edit['category_name'] = $category_details;

		}

		wp_send_json_success( $data_edit );
	}

	public function elex_mv_add_category_name() {

		if ( ! isset( $_POST['elex-nonce'] ) || 
			! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['elex-nonce'] ) ), 'elex_mv_add_cat' ) 
			) {
			return;
		}
		$cat_array['cat_add_name'] = isset( $_POST['category_name'] ) ? sanitize_text_field( wp_unslash( $_POST['category_name'] ) ) : '';
		$cat_array['cat_parent']   = isset( $_POST['category_parent'] ) ? sanitize_text_field( wp_unslash( $_POST['category_parent'] ) ) : '';
		$cat_array['vendor_id']    = isset( $_POST['cat_vendor_id'] ) ? sanitize_text_field( wp_unslash( $_POST['cat_vendor_id'] ) ) : '';


		$cat_array_arr = array( 
			'parent' => $cat_array['cat_parent'], 
			'slug'   => strtolower( $cat_array['cat_add_name'] ),
		);

	

		$vendor = Vendor::load( get_user_by( 'ID', $cat_array['vendor_id'] ) );
		$store  = $vendor->get_store();

		$inserted_cat_id = wp_insert_term( $cat_array['cat_add_name'], 'product_cat', $cat_array_arr );
	

		update_term_meta( $inserted_cat_id['term_id'], 'created_by_vendor', $store->get_id() );

		wp_send_json_success( $cat_array, 200 );    
	}

	public function elex_mv_category_rename_form() {

		if ( ! isset( $_POST['elex-nonce'] ) || 
		! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['elex-nonce'] ) ), 'elex_mv_rename_cat' ) 
		) {
		return;
		}
		$cat_array['cat_ID']   = isset( $_POST['category-current-id'] ) ? sanitize_text_field( wp_unslash( $_POST['category-current-id'] ) ) : '';
		$cat_array['cat_name'] = isset( $_POST['category-new-name'] ) ? sanitize_text_field( wp_unslash( $_POST['category-new-name'] ) ) : '';

		$cat_array_arr = array(
			'name'        => $cat_array['cat_name'],
			'alias_of'    => $cat_array['cat_name'],
			'description' => '',
			'slug'        => strtolower( $cat_array['cat_name'] ),
		);

		wp_update_term( $cat_array['cat_ID'], 'product_cat', $cat_array_arr );
	
		wp_send_json_success( $cat_array, 200 );
	}
}
