<?php


namespace Elex\MultiVendor\Controller;

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

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



class VendorInstallerController {
	public static $nonce_error_msg = 'Wrong Nonce';
	public static $settings        = null;
	const VERSION                  = '2.0.0';


	public static function init() {
		$self = new self();
		add_action( 'wp_ajax_elex-multi-vendor-for-woocommerce-vendor-installation-setup', array( $self, 'elex_mv_vendor_installation_setup' ) );


		add_action( 'wp_ajax_elex-multi-vendor-for-woocommerce-vendor-onboard-setup', array( $self, 'elex_mv_vendor_onboard_setup' ) );
		add_action( 'elex_mv_vendor_setup_page_store-setup', array( $self, 'elex_mv_vendor_setup_page_store_details' ) );
		add_action( 'elex_mv_vendor_setup_page_payment-setup', array( $self, 'elex_mv_vendor_setup_page_pay_settle' ) );
		add_action( 'elex_mv_vendor_setup_page_ready-setup', array( $self, 'elex_mv_vendor_setup_page_ready_to_use' ) );
	}

  
	public function elex_mv_vendor_installation_setup() {
		
		$current_user = wp_get_current_user();

		$vendor_settings = Vendor::load( $current_user );

		

		$store = new Store();
		$store->set_vendor( $vendor_settings );

		if ( $vendor_settings->has_store() ) {

			$store = $vendor_settings->get_store();

		}
	

	
		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, 403 );
			die();      
		}

		global $wpdb;

		if ( isset( $_POST['store_url'] ) ) {
			$store->set_title( sanitize_text_field( wp_unslash( $_POST['store_url'] ) ) );
		}
		
		if ( isset( $_POST['store_full_addr1'] ) ) {
			$store->set_address1( sanitize_text_field( wp_unslash( $_POST['store_full_addr1'] ) ) );
		}
		if ( isset( $_POST['store_full_addr2'] ) ) {
			$store->set_address2( sanitize_text_field( wp_unslash( $_POST['store_full_addr2'] ) ) );
		}
		if ( isset( $_POST['city'] ) ) {
			$store->set_city( sanitize_text_field( wp_unslash( $_POST['city'] ) ) );
		}
		if ( isset( $_POST['state'] ) ) {
			$store->set_state( sanitize_text_field( wp_unslash( $_POST['state'] ) ) );
		}
		if ( isset( $_POST['postal_code'] ) ) {
			$store->set_postal_code( sanitize_text_field( wp_unslash( $_POST['postal_code'] ) ) );
		}
		if ( isset( $_POST['country'] ) ) {
			$store->set_country( sanitize_text_field( wp_unslash( $_POST['country'] ) ) );
		}
		if ( isset( $_POST['phone_number'] ) ) {
			$store->set_phone_number( sanitize_text_field( wp_unslash( $_POST['phone_number'] ) ) );
		}
		if ( isset( $_POST['paypal_email'] ) ) {
			$store->set_paypal_email( sanitize_text_field( wp_unslash( $_POST['paypal_email'] ) ) );
		}     
		if ( isset( $_POST['email'] ) ) {
			$store->set_email( sanitize_text_field( wp_unslash( $_POST['email'] ) ) );
		}        
		if ( isset( $_POST['is_setup_finished'] ) ) {
			$store->set_is_setup_finished( sanitize_text_field( wp_unslash( $_POST['is_setup_finished'] ) ) );
		}
		if ( isset( $_POST['status'] ) ) {
			$store->set_status( sanitize_text_field( wp_unslash( $_POST['status'] ) ) );
		}
		if ( isset( $_POST['dial_code'] ) ) {
			$store->set_dial_code( sanitize_text_field( wp_unslash( $_POST['dial_code'] ) ) );
		}
		if ( isset( $_POST['country_iso'] ) ) {
			$store->set_country_iso( sanitize_text_field( wp_unslash( $_POST['country_iso'] ) ) );
		}

		$store->create();
	}
	
	public function elex_mv_vendor_setup_page_store_details() {
		$current_user    = wp_get_current_user();
		$vendor          = Vendor::load( $current_user );
		$vendor_settings = $vendor->get_store();
		include ELEX_MULTI_VENDOR_PLUGIN_PATH . 'templates/vendor-onboarding/store-details.php';
	}

	public function elex_mv_vendor_setup_page_pay_settle() {
		$current_user    = wp_get_current_user();
		$vendor          = Vendor::load( $current_user );
		$vendor_settings = $vendor->get_store();     
		include ELEX_MULTI_VENDOR_PLUGIN_PATH . 'templates/vendor-onboarding/payment-settlement.php';
	}

	public function elex_mv_vendor_setup_page_ready_to_use() {
		$current_user    = wp_get_current_user();
		$vendor          = Vendor::load( $current_user );
		$vendor_settings = $vendor->get_store();
		include ELEX_MULTI_VENDOR_PLUGIN_PATH . 'templates/vendor-onboarding/thank-you.php';
	}
}
