<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>

    <p class="form-field form-field-wide">
    <label for="estimated-delivery-date">Estimated delivery date:</label>
    <input id="estimated-delivery-date" name="estimated-delivery-date" 
           type="date" value="<?php echo $order_estimated_delivery_date ? date("Y-m-d", $order_estimated_delivery_date) : ""; ?>"
           onChange="onOrderEstimatedDeliveryDateChange(this.value)"/>
	</p>

<script type="text/javascript">
function onOrderEstimatedDeliveryDateChange(date) {
    var timestamp = parseInt((new Date(date).getTime() / 1000).toFixed(0));
    
    if (isNaN(timestamp)){
        timestamp = '';
    }
    
    var data = {
            action: 'wiaas_save_estimated_date_for_order',
            _ajax_nonce: '<?php echo wp_create_nonce( "wiaas_save_estimated_date_for_order" ) ?>',
            order: '<?php echo $order_id ?>',
            date: timestamp
        };

    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    $.post(ajaxurl, data, function(response) {
        if (response.success){
            location.reload();
        }else{
            alert(response.data[0].message);
        }
    });
}
</script>