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

<div style="margin-top: -20px;">
    <table class="widefat" disabled="disabled">
    <tr style="background: #EAF2FA;">
        <th align="left"><strong>Suppliers</strong></th>
        <th align="left"><strong>Estimated date</strong></th>
        <th align="left"><strong>Confirmed date</strong></th>
    </tr>
    <?php
        foreach($suppliers as $supplier){
            echo '<tr style="background: #f5f5f5;"><td><strong>' . $supplier['name'] . '</strong></td>';

            $estimated_date = $supplier['estimated_date'] ? date("Y-m-d", $supplier['estimated_date']) : "";
            $confirmed_date = $supplier['confirmed_date'] ? date("Y-m-d", $supplier['confirmed_date']) : "";

            ?>
            <td>
                <input
                        id=<?php echo 'estimate-date-' . $supplier['id'] ?>
                        <?php disabled($is_disabled, true, true) ?>
                        type="date"
                        onChange="onEstimatedDeliveryDateChange(<?php echo $supplier['id'] ?>, this.value)"
                        value="<?php echo $estimated_date ?>" />
            </td>

            <td>
                <input
                        id=<?php echo 'confirmed-date-' . $supplier['id'] ?>
                        <?php disabled($is_disabled, true, true) ?>
                        type="date" onChange="onConfirmedDeliveryDateChange(<?php echo $supplier['id'] ?>, this.value)"
                        value="<?php echo $confirmed_date ?>" />
            </td>
            </tr>

            <tr>
                <td colspan="3">
                    <span>Tracking:</span>
                </td>
            </tr>

            <?php
                foreach($supplier['tracking_info'] as $index => $tracking_info){
                    ?>
                    <tr>
                        <td colspan="3">
                            <input  id=<?php echo 'supplier_' . $supplier['id'] . '_tracking_num_' . $index ?>
                                    <?php disabled($is_disabled, true, true) ?>
                                   placeholder="Tracking number" value="<?php echo $tracking_info['number'] ?>" />
                            <input id=<?php echo 'supplier_' . $supplier['id'] . '_tracking_url_' . $index ?>
                                   <?php disabled($is_disabled, true, true) ?>
                                   placeholder="Tracking URL" value="<?php echo $tracking_info['url'] ?>" />
                            <input
                                    type="button"
	                                <?php disabled($is_disabled, true, true) ?>
                                    class="button"
                                    onClick="saveTrackingInfo(event, <?php echo $supplier['id'] . ',' . $index ?>)"
                                    value="SAVE">
                            <input
                                    type="button"
	                                <?php disabled($is_disabled, true, true) ?>
                                    class="button"
                                    onClick="deleteTrackingInfo(event, <?php echo $supplier['id'] . ',' . $index ?>)" value="REMOVE"
                            >
                        </td>
                        
                    </tr>

                    <?php
                }
            ?>
            <tr>
                <td colspan="4">
                    <input
                            type="button"
	                        <?php disabled($is_disabled, true, true) ?>
                            class="button"
                            id=<?php echo $supplier['id'] ?> onClick="addAdditionalTrackingInfo(event)"
                            value="Add new tracking info">
                </td>
            </tr>

            <?php
        }
    ?>

    <tr>
        <td><h4>Final dates : </h4></td>
        <td><h4><?php echo $final_estimated_date ? date('Y-m-d', $final_estimated_date) : '-' ?></h4></td>
        <td><h4><?php echo $final_confirmed_date ? date('Y-m-d', $final_confirmed_date) : '-' ?></h4></td>
    </tr>

    <tfoot>
        <tr>
            <td><h4>Earliest installation date : </h4></td>
            <td colspan="2">
                <h4><?php echo $earliest_installation_date ? date('Y-m-d', $earliest_installation_date) : '-' ?></h4>
            </td>
        </tr>
    </tfoot>

    </table>    
</div>

<script type="text/javascript">
function onEstimatedDeliveryDateChange(supplierID, date) {
    var timestamp = parseInt((new Date(date).getTime() / 1000).toFixed(0));
    
    if (isNaN(timestamp)){
        timestamp = '';
    }

    var data = {
            action: 'wiaas_save_estimated_date_for_supplier',
            _ajax_nonce: '<?php echo wp_create_nonce( "wiaas_save_estimated_date_for_supplier" ) ?>',
            order: '<?php echo $order_id ?>',
            supplier: supplierID,
            date: timestamp

        };

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

function onConfirmedDeliveryDateChange(supplierID, date) {
    var timestamp = parseInt((new Date(date).getTime() / 1000).toFixed(0));
    
    if (isNaN(timestamp)){
        timestamp = '';
    }

    var data = {
            action: 'wiaas_save_confirmed_date_for_supplier',
            _ajax_nonce: '<?php echo wp_create_nonce( "wiaas_save_confirmed_date_for_supplier" ) ?>',
            order: '<?php echo $order_id ?>',
            supplier: supplierID,
            date: timestamp
        };

    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    jQuery.post(ajaxurl, data, function(response) {
        if (response.success){
            location.reload();
        }else{
            alert(response.data[0].message);
        }
    });
}
function addAdditionalTrackingInfo(e){
    e.preventDefault();
    
    var data = {
            action: 'wiaas_add_additional_tracking_info_for_supplier_in_order',
            _ajax_nonce: '<?php echo wp_create_nonce( "wiaas_add_additional_tracking_info_for_supplier_in_order" ) ?>',
            order: '<?php echo $order_id ?>',
            supplier: e.target.id

        };

    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    jQuery.post(ajaxurl, data, function(response) {
        if (response.success){
            location.reload();
        }else{
            alert(response.data[0].message);
        }
    });
}
function saveTrackingInfo(e, supplierID, index){
    e.preventDefault();

    var tracking_num = document.getElementById('supplier_' + supplierID + '_tracking_num_' + index).value;
    var tracking_url = document.getElementById('supplier_' + supplierID + '_tracking_url_' + index).value;

    var data = {
            action: 'wiaas_save_tracking_info',
            _ajax_nonce: '<?php echo wp_create_nonce( "wiaas_save_tracking_info" ) ?>',
            order: '<?php echo $order_id ?>',
            supplier: supplierID,
            index: index,
            tracking_num: tracking_num,
            tracking_url: tracking_url
        };

    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    jQuery.post(ajaxurl, data, function(response) {
        if (response.success){
            location.reload();
        }else{
            alert(response.data[0].message);
        }
    });
}
function deleteTrackingInfo(e, supplierID, index){
    e.preventDefault();

    var data = {
            action: 'wiaas_delete_tracking_info',
            _ajax_nonce: '<?php echo wp_create_nonce( "wiaas_delete_tracking_info" ) ?>',
            order: '<?php echo $order_id ?>',
            supplier: supplierID,
            index: index
        };

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