jQuery( document ).ready( function ( $ ) {

	/**
	 * Checkout
	 */
	auto_listings_checkout_total();
	if ( $( '#auto-listing-checkout select' ).length ) {
		$( '#auto-listing-checkout select' ).SumoSelect();
	}


	/**
	 * Dashboard
	 */
	auto_listings_end_listing();

	// Add a container div into rwmb-confirmation
	if ( $( 'body' ).hasClass( 'submit-listing-template' ) && $( '.rwmb-confirmation' ) ) {
		$html = '<div class="container">' + $( '.rwmb-confirmation' ).text() + '</div>';
		$( '.rwmb-confirmation' ).html( $html );
	}

	/**
	 * ================================= FUNCTIONS =======================================
	 */

	/**
	 * Stop listing
	 */
	function auto_listings_end_listing() {
		$( '.auto-listings-end' ).on( 'click', function ( e ) {
			e.preventDefault();
			var r = confirm( "Are you sure you want to end listing? This can\'t be undone!" );
			if ( r == true ) {
				window.location = $( this ).attr( 'href' ) + '&js=true';
			}
		} );
	}


	/**
	 * checkout total
	 */
	function auto_listings_checkout_total() {
		var price = $( '#auto-listings-package' ).find( ":selected" ).data( 'price' );
		$( '.purchase-total' ).html( price );
		$( '#auto-listings-package' ).on( 'change', function () {
			var price = $( this ).find( ":selected" ).data( 'price' );
			$( '.purchase-total' ).html( price );
		} );
	}

	var requiredFields = ALFrontend.requiredFields;
	requiredFields.forEach( function ( field ) {
		$( '#' + field ).attr( 'required', true );
	} );

	$( 'form.rwmb-form' ).append( $( '#form' ) );
	$( '#title-field' ).append( $( '#post_title' ) );
	$( '#description-field' ).append( $( '<textarea cols="60" rows="3" id="post_content" class="rwmb-textarea large-text" name="post_content" spellcheck="false">' + $( '.rwmb-wysiwyg#post_content' ).html() + '</textarea>' ) );
	$( '#excerpt-field' ).append( $( '#post_excerpt' ) );
	$( '#make-field' ).append( $( '#car-makes' ) );
	$( '#model-field' ).append( $( '#car-models' ) );
	$( '#year-field' ).append( $( '#car-years' ) );
	$( '#trims-field' ).append( $( '#car-model-trims' ) );
	$( '#bodytype-field' ).append( $( '#_al_listing_body_type' ) );
	$( '#condition-field' ).append( $( '#_al_listing_condition' ) );
	$( '#transmission-field' ).append( $( '#_al_listing_model_transmission_type' ) );
	$( '#seats-field' ).append( $( '#_al_listing_model_seats' ) );
	$( '#doors-field' ).append( $( '#_al_listing_model_doors' ) );
	$( '#drive-type-field' ).append( $( '#_al_listing_model_drive' ) );
	$( '#fuel-field' ).append( $( '#_al_listing_model_engine_fuel' ) );
	$( '#engine-field' ).append( $( '#_al_listing_model_engine_l' ) );
	$( '#exterior-color-field' ).append( $( '#_al_listing_exterior_color' ) );
	$( '#interior-color-field' ).append( $( '#_al_listing_interior_color' ) );
	$( '#address-field' ).append( $( '#_al_listing_displayed_address' ) );
	$( '#zip-field' ).append( $( '#_al_listing_zip' ) );
	$( '#mileage-field' ).append( $( '#_al_listing_odometer' ) );
	$( '#vin-field' ).append( $( '#_al_listing_vin' ) );
	$( '#registered-field' ).append( $( '#_al_listing_registration' ) );

	$( '#car-review-stamps' ).append( $( 'input#nonce__al_listing_certificate' ).siblings( '.rwmb-checkbox_list-wrapper' ).find( '.rwmb-input-list' ) );
	$( '#car-images' ).append( $( '.rwmb-image_upload-wrapper .rwmb-input' )[ 0 ] );
	$( '#car-video' ).append( $( '.rwmb-oembed-wrapper .rwmb-input' )[ 0 ] );
	$( '#car-brochure' ).append( $( '.rwmb-file_input-wrapper .rwmb-input' )[ 0 ] );
	$( '#car-notes' ).append( $( '#_al_listing_seller_note' ) );
	$( '#car-regular-price' ).append( $( '#_al_listing_price' ) );
	$( '#car-sale-price' ).append( $( '#_al_listing_sale_price' ) );

	$( '#car-feature-comfort' ).append( $( $( 'input#nonce__al_listing_extra_features' ).siblings( '.rwmb-checkbox_list-wrapper' ).find( '.rwmb-input-list' )[ 0 ] ).clone() );
	$( '#car-feature-safety' ).append( $( $( 'input#nonce__al_listing_extra_features' ).siblings( '.rwmb-checkbox_list-wrapper' ).find( '.rwmb-input-list' )[ 1 ] ).clone() );
	$( '#car-feature-plus' ).append( $( $( 'input#nonce__al_listing_extra_features' ).siblings( '.rwmb-checkbox_list-wrapper' ).find( '.rwmb-input-list' )[ 2 ] ).clone() );
	$( '#car-feature-others' ).append( $( $( 'input#nonce__al_listing_extra_features' ).siblings( '.rwmb-checkbox_list-wrapper' ).find( '.rwmb-input-list' )[ 3 ] ).clone() );

	$( '#car-submit' ).append( $( '.rwmb-form-submit' )[ 0 ] );

	var submitListing = {
		init: function () {
			submitListing.$trimFields = $( '#car-model-trims' );

			submitListing.loadSelect2();
			submitListing.insertPlaceHolder();
			submitListing.trigger();
			submitListing.populateData();
		},

		loadSelect2: function () {
			$( '.auto-listings-submit-listing select' ).select2( {} );
		},

		trigger: function () {
			setTimeout( () => {
				$( '#cq-select-model' ).click();
			}, 3000 );
		},

		insertPlaceHolder: function () {
			$( 'input[id^=_al]' ).each( function ( i, val ) {
				$( $( 'input[id^=_al]' )[ i ] ).attr( 'placeholder', $( $( 'input[id^=_al]' )[ i ] ).parent().attr( 'placeholder' ) );
			} );
		},

		populateData: function () {
			$( '#car-years' ).on( 'change', function () {
				$( '#_al_listing_model_year' ).val( $( this ).val() );
			} );
			$( '#car-makes' ).on( 'change', function () {
				$( '#_al_listing_make_display' ).val( $( this ).find( 'option:selected' ).text() );
			} );
			$( '#car-models' ).on( 'change', function () {
				$( '#_al_listing_model_name' ).val( $( this ).val() );
			} );
			$( '#car-model-trims' ).on( 'change', function () {
				$( '#_al_listing_model_vehicle' ).val( $( this ).find( 'option:selected' ).text() );
			} );
		},

		insertListingTitle: function () {
			setTimeout( function () {
				var title = '';
				title += $( '#select2-car-years-container' ).text();
				title += ' ' + $( '#select2-car-makes-container' ).text();
				title += ' ' + $( '#select2-car-models-container' ).text();
				$( '#post_title' ).val( title );
			}, 3000 );
		}

	};
	if ( $( 'body' ).hasClass( 'submit-listing-template' ) ) {
		submitListing.init();
	}
} );
