<?php

add_action( 'wp_ajax_wiaas_add_additional_tracking_info_for_supplier_in_order', 'wiaas_ajax_add_additional_tracking_info_for_supplier_in_order' );
add_action( 'wp_ajax_wiaas_save_tracking_info', 'wiaas_ajax_save_tracking_info');
add_action( 'wp_ajax_wiaas_delete_tracking_info', 'wiaas_ajax_delete_tracking_info');
add_action( 'wp_ajax_wiaas_save_estimated_date_for_supplier', 'wiaas_ajax_save_estimated_date_for_supplier');
add_action( 'wp_ajax_wiaas_save_confirmed_date_for_supplier', 'wiaas_ajax_save_confirmed_date_for_supplier');
add_action( 'wp_ajax_wiaas_save_estimated_date_for_order', 'wiaas_ajax_save_estimated_date_for_order');

/**
 * Adds additional tracking info for supplier in order
 */
function wiaas_ajax_add_additional_tracking_info_for_supplier_in_order(){
	check_ajax_referer('wiaas_add_additional_tracking_info_for_supplier_in_order');
	$error = new WP_Error('-1', 'Failed to add additional tracking info');
	
	if (!isset($_POST['order']) || !isset($_POST['supplier'])){
		wp_send_json_error($error);
	}
	$order_id = intval( $_POST['order'] );
	$supplier_id = intval( $_POST['supplier'] );

	if (Wiaas_Order::add_additional_tracking_info($order_id, $supplier_id)){
		wp_send_json_success();
	}
	
	wp_send_json_error($error);
}

function wiaas_ajax_save_tracking_info(){
	check_ajax_referer('wiaas_save_tracking_info');
	$error = new WP_Error('-1', 'Failed to save tracking info');
	
	if (!isset($_POST['order']) || !isset($_POST['supplier']) || !isset($_POST['index']) 
								|| !isset($_POST['tracking_num']) || !isset($_POST['tracking_url'])){
		wp_send_json_error($error);
	}
	$order_id = intval( $_POST['order'] );
	$supplier_id = intval( $_POST['supplier'] );
	$index = intval($_POST['index']);
	$tracking_num = $_POST['tracking_num'];
	$tracking_url = $_POST['tracking_url'];

	if (Wiaas_Order::save_tracking_info($order_id, $supplier_id, $index, $tracking_num, $tracking_url)){
		wp_send_json_success();
	}
	
	wp_send_json_error($error);
}

function wiaas_ajax_delete_tracking_info(){
	check_ajax_referer('wiaas_delete_tracking_info');
	$error = new WP_Error('-1', 'Failed to save tracking info');
	
	if (!isset($_POST['order']) || !isset($_POST['supplier']) || !isset($_POST['index'])){
		wp_send_json_error($error);
	}
	$order_id = intval( $_POST['order'] );
	$supplier_id = intval( $_POST['supplier'] );
	$index = intval($_POST['index']);

	if (Wiaas_Order::delete_tracking_info($order_id, $supplier_id, $index)){
		wp_send_json_success();
	}
	
	wp_send_json_error($error);
}

function wiaas_ajax_save_estimated_date_for_supplier(){
	check_ajax_referer('wiaas_save_estimated_date_for_supplier');
	$error = new WP_Error('-1', 'Failed to save estimated date');
	
	if (!isset($_POST['order']) || !isset($_POST['supplier']) || !isset($_POST['date'])){
		wp_send_json_error($error);
	}
	$order_id = intval( $_POST['order'] );
	$supplier_id = intval( $_POST['supplier'] );
	$date = intval($_POST['date']);

	if (Wiaas_Order::save_estimated_date($order_id, $supplier_id, $date)){
		wp_send_json_success();
	}
	
	wp_send_json_error($error);
}

function wiaas_ajax_save_confirmed_date_for_supplier(){
	check_ajax_referer('wiaas_save_confirmed_date_for_supplier');
	$error = new WP_Error('-1', 'Failed to save confirmed date');
	
	if (!isset($_POST['order']) || !isset($_POST['supplier']) || !isset($_POST['date'])){
		wp_send_json_error($error);
	}
	$order_id = intval( $_POST['order'] );
	$supplier_id = intval( $_POST['supplier'] );
	$date = intval($_POST['date']);

	if (Wiaas_Order::save_confirmed_date($order_id, $supplier_id, $date)){
		wp_send_json_success();
	}
	
	wp_send_json_error($error);
}

function wiaas_ajax_save_estimated_date_for_order(){
	check_ajax_referer('wiaas_save_estimated_date_for_order');
	$error = new WP_Error('-1', 'Failed to save global estimated date for order');
	
	if (!isset($_POST['order']) || !isset($_POST['date'])){
		wp_send_json_error($error);
	}
	$order_id = intval( $_POST['order'] );
	$date = intval($_POST['date']);

	if (Wiaas_Order::save_order_estimated_date($order_id, $date)){
		wp_send_json_success();
	}
	
	wp_send_json_error($error);
}
