jQuery( document ).ready( function ( $ ) {

	jQuery( '#admin-dashboard-from-date' ).val( elexMvDashboardParams.date_to_display );

	jQuery( '#apply_filter_admin_dashboard_btn' ).click( function () {

		var formdata = new FormData( document.getElementById( 'apply_filter_admin_dashboard_form' ) );
		formdata.append( '_ajax_nonce', elexMvDashboardParams.nonce );

		// Rebuild bar chart.
		var link_chart = elexMvDashboardParams.ajax_url + '?action=elex_mv_admin_product_data_last_thirty_days';

		jQuery.ajax( {
			url:         link_chart,
			type:        'POST',
			processData: false,
			contentType: false,
			data:        formdata,
			success: function ( result ) {
				var resu = JSON.parse( result.data );

				var total_vendor_earning = [];
				var order_date           = [];
				var total_sales_product  = [];

				resu.forEach( function ( curr ) {
					total_vendor_earning.push( curr['total_vendor_earning'] );
					order_date.push( curr['order_date'] );
					total_sales_product.push( curr['total_sales_product'] );
				} );

				adminChart.destroy();
				dashboard_chart( total_vendor_earning, order_date, total_sales_product );
			}
		} );

		// Rebuild world map.
		var link_map = elexMvDashboardParams.ajax_url + '?action=elex_mv_admin_country_data';

		jQuery.ajax( {
			url:         link_map,
			type:        'POST',
			processData: false,
			contentType: false,
			data:        formdata,
			success: function ( result ) {
				if ( adminroot ) {
					adminroot.remove();
					adminroot = null;
				}
				map_chart_dashboard( result.data );
			}
		} );

		// Refresh summary stats.
		var link_dash = elexMvDashboardParams.ajax_url + '?action=elex_mv_admin_dashboard_page';

		jQuery.ajax( {
			url:         link_dash,
			type:        'POST',
			processData: false,
			contentType: false,
			data:        formdata,
			success: function ( result ) {
				map_admin_dashboard_details( result.data );
			}
		} );
	} );
} );
