<?php


namespace Elex\MultiVendor\Controller;

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

use Elex\MultiVendor\Model\Vendor;
use Elex\MultiVendor\Model\Store;

class VendorSettings {

	public static function init() {
		$self = new self();
		add_action( 'elex_mv_store_profile_page', array( $self, 'mv_store_profile_tab_content' ) );
		add_action( 'elex_mv_general_settings_page', array( $self, 'mv_general_settings_tab_content' ) );
		add_action( 'elex_mv_store_page_settings_page', array( $self, 'mv_store_page_settings_tab_content' ) );

		add_action( 'wp_ajax_elex_mv_vendor_settings_details', array( $self, 'elex_mv_vendor_settings_details' ) );
		add_action( 'wp_ajax_elex_mv_vendor_general_settings_details', array( $self, 'elex_mv_vendor_general_settings_details' ) );
		add_action( 'wp_ajax_elex_mv_store_settings_details', array( $self, 'elex_mv_store_settings_details' ) );
	}


	public static function mv_store_profile_tab_content() {
		$current_user = wp_get_current_user();
		$vendor       = Vendor::load( $current_user );
		$store        = $vendor->get_store();
		$user_id      = $current_user->ID;
		$store_id     = $store->get_id();
		$image        = $store->get_image();


		$thumbnail_url = get_the_post_thumbnail_url( $store_id );

		include ELEX_MULTI_VENDOR_PLUGIN_PATH . 'templates/vendor-settings/store-profile.php';
	}

	public static function mv_general_settings_tab_content() {
		$current_user = wp_get_current_user();
		$vendor       = Vendor::load( $current_user );
		$store        = $vendor->get_store();
		$store_id     = $store->get_id();
		include ELEX_MULTI_VENDOR_PLUGIN_PATH . 'templates/vendor-settings/settings.php';
	}

	public static function mv_store_page_settings_tab_content() {

		$current_user   = wp_get_current_user();
		$vendor         = Vendor::load( $current_user );
		$store          = $vendor->get_store();
		$store_id       = $store->get_id();
		$ven_image_name = '';


		$post_thumbnail_id = get_post_meta( $store_id, 'elex_mv_post_banner_id', true );
		$thumbnail_url     = wp_get_attachment_image_url( $post_thumbnail_id, 'thumbnail' );
		$ven_image_info    = wp_get_attachment_image_src( $post_thumbnail_id, 'full' );

		if ( $ven_image_info ) {
			$ven_image_url  = $ven_image_info[0];
			$ven_image_name = basename( $ven_image_url ); // Extracting the file name from the URL
		
		} 

		$name_to_show_on_product  = get_post_meta( $store_id, 'elex_mv_name_to_show_on_product', true );
		$price_to_show_on_product = get_post_meta( $store_id, 'elex_mv_price_to_show_on_product', true );
		$show_all_categories      = get_post_meta( $store_id, 'elex_mv_show_all_categories', true );
		$banner_image_text        = get_post_meta( $store_id, 'elex_mv_banner_image_text', true );

		include ELEX_MULTI_VENDOR_PLUGIN_PATH . 'templates/vendor-settings/store_page_setting.php';
	}

	public function elex_mv_vendor_general_settings_details() {
	
		check_ajax_referer( 'elex_mv_general_setting', 'settings_nonce' );

		if ( ! current_user_can( 'elex_mv_vendor' ) && ! current_user_can( 'manage_woocommerce' ) ) {
			return;
		}
	
		$settings_data_array                        = array();
		$store_id                                   = isset( $_POST['user_id'] ) ? sanitize_text_field( wp_unslash( $_POST['user_id'] ) ) : '';
		$settings_data_array['flat_service_enable'] = isset( $_POST['flat-rate_service'] ) ? sanitize_text_field( wp_unslash( $_POST['flat-rate_service'] ) ) : '';
		$settings_data_array['coupon_notification'] = isset( $_POST['notify_coupon'] ) ? sanitize_text_field( wp_unslash( $_POST['notify_coupon'] ) ) : '';
		$settings_data_array['verify_user_review']  = isset( $_POST['verify_user'] ) ? sanitize_text_field( wp_unslash( $_POST['verify_user'] ) ) : '';

		update_post_meta( $store_id, 'elex_mv_coupon_notification', $settings_data_array['coupon_notification'] );
		update_post_meta( $store_id, 'elex_mv_verify_user_review', $settings_data_array['verify_user_review'] );

	

		wp_send_json_success( $settings_data_array );
	}

	public function elex_mv_vendor_settings_details() {

		$self = new self();
		check_ajax_referer( 'elex_mv_settings_page_nonce', 'vendor_settings_nonce' );


		$new_vendor_array                     = array();
		$new_vendor_array['store_name']       = isset( $_POST['store_name'] ) ? sanitize_text_field( wp_unslash( $_POST['store_name'] ) ) : '';
		$new_vendor_array['email_id']         = isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '';
		$new_vendor_array['phone_number']     = isset( $_POST['phone_number'] ) ? sanitize_text_field( wp_unslash( $_POST['phone_number'] ) ) : '';
		$new_vendor_array['country_iso']      = isset( $_POST['country_iso'] ) ? sanitize_text_field( wp_unslash( $_POST['country_iso'] ) ) : '';
		$new_vendor_array['dial_code']        = isset( $_POST['dial_code'] ) ? sanitize_text_field( wp_unslash( $_POST['dial_code'] ) ) : '';
		$new_vendor_array['user_id']          = isset( $_POST['user_id'] ) ? sanitize_text_field( wp_unslash( $_POST['user_id'] ) ) : '';
		$new_vendor_array['store_full_addr1'] = isset( $_POST['address1'] ) ? sanitize_text_field( wp_unslash( $_POST['address1'] ) ) : '';
		$new_vendor_array['store_full_addr2'] = isset( $_POST['address2'] ) ? sanitize_text_field( wp_unslash( $_POST['address2'] ) ) : '';

		$new_vendor_array['city']          = isset( $_POST['city'] ) ? sanitize_text_field( wp_unslash( $_POST['city'] ) ) : '';
		$new_vendor_array['postal_code']   = isset( $_POST['postal_code'] ) ? sanitize_text_field( wp_unslash( $_POST['postal_code'] ) ) : '';
		$new_vendor_array['state']         = isset( $_POST['state'] ) ? sanitize_text_field( wp_unslash( $_POST['state'] ) ) : '';
		$new_vendor_array['country']       = isset( $_POST['country'] ) ? sanitize_text_field( wp_unslash( $_POST['country'] ) ) : '';
		$new_vendor_array['image_name']    = isset( $_FILES['elex-multi-vendor-for-woocommerce-vendor-setting-filetag']['name'] ) ? sanitize_text_field( $_FILES['elex-multi-vendor-for-woocommerce-vendor-setting-filetag']['name'] ) : '';
		$new_vendor_array['image_path']    = isset( $_FILES['elex-multi-vendor-for-woocommerce-vendor-setting-filetag']['tmp_name'] ) ? sanitize_text_field( $_FILES['elex-multi-vendor-for-woocommerce-vendor-setting-filetag']['tmp_name'] ) : '';
		$new_vendor_array['image_type']    = isset( $_FILES['elex-multi-vendor-for-woocommerce-vendor-setting-filetag']['type'] ) ? sanitize_text_field( $_FILES['elex-multi-vendor-for-woocommerce-vendor-setting-filetag']['type'] ) : '';

		// check vendor store image is allowed format
		if ( ! empty( $new_vendor_array['image_name'] ) ) {
			if ( ! elex_mv_is_valid_image_file( $new_vendor_array['image_name'], $new_vendor_array['image_path'] ) ) {
				wp_send_json_error( array( 'vendor_image' => array( __( 'Unsupported image format.', 'elex-multi-vendor-for-woocommerce' ) ) ), 422 );
			}
		}

		$new_vendor_array['deleted_image'] = isset( $_POST['deleted_image'] ) && 'true' === $_POST['deleted_image'];

		$validation_errors = $self->elex_mv_form_vendor_validation( $new_vendor_array );
	 
		if ( isset( $validation_errors['errors'] ) && count( $validation_errors['errors'] ) ) {
			wp_send_json_error( $validation_errors['errors'], 422 );
		}
		$store_id     = $new_vendor_array['user_id'] ; 
		$user_id      = get_post_field( 'post_author', $store_id );
		$current_user = get_user_by( 'id', $user_id );
		$vendor       = Vendor::load( $current_user );
		$store        = $vendor->get_store();
		if ( isset( $new_vendor_array['image_name'] ) && ! empty( $new_vendor_array['image_name'] ) ) {
			$thumbnail_id = $store->save_image( $new_vendor_array['image_name'], $new_vendor_array['image_path'], $new_vendor_array['image_type'], $store_id );
			set_post_thumbnail( $store_id, $thumbnail_id );
		}
	 
		if ( $new_vendor_array['deleted_image'] ) {
			$thumbnail_id = $store->save_image( $new_vendor_array['image_name'], $new_vendor_array['image_path'], $new_vendor_array['image_type'], $store_id );
			set_post_thumbnail( $store_id, $thumbnail_id );
			delete_post_thumbnail( $store_id );
		}
		update_post_meta( $store_id, 'elex_mv_title', $new_vendor_array['store_name'] );
		update_post_meta( $store_id, 'elex_mv_address1', $new_vendor_array['store_full_addr1'] );
		update_post_meta( $store_id, 'elex_mv_address2', $new_vendor_array['store_full_addr2'] );
		update_post_meta( $store_id, 'elex_mv_email', $new_vendor_array['email_id'] );
		update_post_meta( $store_id, 'elex_mv_city', $new_vendor_array['city'] );
		update_post_meta( $store_id, 'elex_mv_state', $new_vendor_array['state'] );
		update_post_meta( $store_id, 'elex_mv_postal_code', $new_vendor_array['postal_code'] );
		update_post_meta( $store_id, 'elex_mv_country', $new_vendor_array['country'] );
		update_post_meta( $store_id, 'elex_mv_phone_number', $new_vendor_array['phone_number'] );
		update_post_meta( $store_id, 'elex_mv_dial_code', $new_vendor_array['dial_code'] );
		update_post_meta( $store_id, 'elex_mv_country_iso', $new_vendor_array['country_iso'] );

		wp_send_json_success();
	}
	
	public function elex_mv_store_settings_details() {
		

		$self = new self();
		check_ajax_referer( 'elex_mv_settings_page_nonce', 'vendor_settings_nonce' );
		$new_vendor_array                             = array();
		$new_vendor_array['user_id']                  = isset( $_POST['user_id'] ) ? sanitize_text_field( wp_unslash( $_POST['user_id'] ) ) : '';
		$new_vendor_array['name_to_show_on_product']  = isset( $_POST['name_to_show_on_product'] ) ? sanitize_text_field( wp_unslash( $_POST['name_to_show_on_product'] ) ) : '';
		$new_vendor_array['price_to_show_on_product'] = isset( $_POST['price_to_show_on_product'] ) ? sanitize_text_field( wp_unslash( $_POST['price_to_show_on_product'] ) ) : '';
		$new_vendor_array['show_all_categories']      = isset( $_POST['show_all_categories'] ) ? sanitize_text_field( wp_unslash( $_POST['show_all_categories'] ) ) : '';
	  
		$new_vendor_array['banner_image_text'] = isset( $_POST['banner_image_text'] ) ? sanitize_text_field( wp_unslash( $_POST['banner_image_text'] ) ) : '';

		$new_vendor_array['image_name']         = isset( $_FILES['elex-multi-vendor-for-woocommerce-store-setting-filetag']['name'] ) ? sanitize_text_field( $_FILES['elex-multi-vendor-for-woocommerce-store-setting-filetag']['name'] ) : '';
		$new_vendor_array['image_path']         = isset( $_FILES['elex-multi-vendor-for-woocommerce-store-setting-filetag']['tmp_name'] ) ? sanitize_text_field( $_FILES['elex-multi-vendor-for-woocommerce-store-setting-filetag']['tmp_name'] ) : '';
		$new_vendor_array['image_type']         = isset( $_FILES['elex-multi-vendor-for-woocommerce-store-setting-filetag']['type'] ) ? sanitize_text_field( $_FILES['elex-multi-vendor-for-woocommerce-store-setting-filetag']['type'] ) : '';

		// check store banner image is allowed format
		if ( ! empty( $new_vendor_array['image_name'] ) ) {
			if ( ! elex_mv_is_valid_image_file( $new_vendor_array['image_name'], $new_vendor_array['image_path'] ) ) {
				wp_send_json_error( array( 'vendor_image' => array( __( 'Unsupported image format.', 'elex-multi-vendor-for-woocommerce' ) ) ), 422 );
			}
		}

		$new_vendor_array['verify_user_review'] = isset( $_POST['verify_user'] ) ? sanitize_text_field( wp_unslash( $_POST['verify_user'] ) ) : '';

		$new_vendor_array['deleted_image'] = isset( $_POST['deleted_image'] ) && 'true' === $_POST['deleted_image'];

	

		$store_id     = $new_vendor_array['user_id'] ; 
		$user_id      = get_post_field( 'post_author', $store_id );
		$current_user = get_user_by( 'id', $user_id );
		$vendor       = Vendor::load( $current_user );
		$store        = $vendor->get_store();
		$store_post   = get_post( $store_id );

		if ( isset( $new_vendor_array['image_name'] ) && ! empty( $new_vendor_array['image_name'] ) ) {
			
			$thumbnail_id = $store->save_image( $new_vendor_array['image_name'], $new_vendor_array['image_path'], $new_vendor_array['image_type'], $store_id );
			
		

			$thumbnail_id = absint( $thumbnail_id );
			set_post_thumbnail( $store_id, $thumbnail_id );
			if ( $store_post && $thumbnail_id && get_post( $thumbnail_id ) ) {
				if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) {
					update_post_meta( $store_post->ID, 'elex_mv_post_banner_id', $thumbnail_id );
				} else {
					delete_post_meta( $store_post->ID, 'elex_mv_post_banner_id' );
				}
			}
		}
	
		if ( $new_vendor_array['deleted_image'] ) {
				$thumbnail_id = $store->save_image( $new_vendor_array['image_name'], $new_vendor_array['image_path'], $new_vendor_array['image_type'], $store_id );
				set_post_thumbnail( $store_id, $thumbnail_id );
				delete_post_thumbnail( $store_id );
		}
	
		update_post_meta( $store_id, 'elex_mv_name_to_show_on_product', $new_vendor_array['name_to_show_on_product'] );
		update_post_meta( $store_id, 'elex_mv_price_to_show_on_product', $new_vendor_array['price_to_show_on_product'] );
		update_post_meta( $store_id, 'elex_mv_show_all_categories', $new_vendor_array['show_all_categories'] );
		update_post_meta( $store_id, 'elex_mv_banner_image_text', $new_vendor_array['banner_image_text'] );

		update_post_meta( $store_id, 'elex_mv_verify_user_review', $new_vendor_array['verify_user_review'] );


		wp_send_json_success();
	}

	
	public static function elex_mv_form_vendor_validation( $new_product_array ) {
	
		$data = array(
			'message' => __( 'Empty Fields' , 'elex-multi-vendor-for-woocommerce' ),
			'errors'  => array(),
		);
	
		if ( ! isset( $new_product_array['store_name'] ) || empty( $new_product_array['store_name'] ) ) {
			$data['errors']['store_name'][] = __( 'Please enter store name', 'elex-multi-vendor-for-woocommerce' );
		}
		  
		if ( ! isset( $new_product_array['email_id'] ) || empty( $new_product_array['email_id'] ) ) {
  
			$data['errors']['email_id'][] = __( 'Please enter email id', 'elex-multi-vendor-for-woocommerce' );
  
		} elseif ( ! filter_var( $new_product_array['email_id'], FILTER_VALIDATE_EMAIL ) ) {
			$data['errors']['email_id'][] = __( 'Please enter valid email address', 'elex-multi-vendor-for-woocommerce' );
  
		}   
		if ( ! isset( $new_product_array['phone_number'] ) || empty( $new_product_array['phone_number'] ) ) {
			$data['errors']['phone_number'][] = __( 'Please enter phone number', 'elex-multi-vendor-for-woocommerce' );
		}
  
			/**
			* For form validation parameter.  
			*
			* @since 1.0.0
			*/
		$data = apply_filters( 'elex_mv_form_vendor_validation', $data );
		return $data;
	}
}
