<?php
    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }
    
    $kirilkirkovwipdf_i = 0; // default - one
    if(isset($invoice[$Config::INPUTS_PREFIX.'items']) && count($invoice[$Config::INPUTS_PREFIX.'items'])) {
        $kirilkirkovwipdf_i = count($invoice[$Config::INPUTS_PREFIX.'items']['item_from_list']) - 1;
    }
    $kirilkirkovwipdf_loop = 0;
    while($kirilkirkovwipdf_loop <= $kirilkirkovwipdf_i) {
?>
<tr>
    <td class="align-middle--important">
        <!-- ID of the item if is selected from the list. Used to decrease its quantity -->
        <input type="hidden" class="item_from_list-input" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'items[item_from_list][]'; ?>" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'items']['item_from_list'][$kirilkirkovwipdf_loop]) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'items']['item_from_list'][$kirilkirkovwipdf_loop])) : '0'; ?>">
        <div class="actions hidden">
            <div class="p-1">
                <a href="javascript:void(0);" class="delete-item">
                    <span class="dashicons dashicons-no-alt" title="<?php esc_attr_e( 'Delete Item', 'kirilkirkov-pdf-invoice-manager' ); ?>"></span>
                </a>
            </div>
            <div class="p-1">
                <a href="javascript:void(0);" class="sort-item">
                    <span class="dashicons dashicons-sort" title="<?php esc_attr_e( 'Sort Items', 'kirilkirkov-pdf-invoice-manager' ); ?>"></span>
                </a>
            </div>
        </div>
    </td>
    <td class="align-middle--important">
        <div class="selectable">
            <div class="flex items-center justify-between">

                <input type="hidden" class="item-available-quantity" name="item-available-quantity" value="" />
 
                <input type="text" class="item-name" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'items[items_names][]'; ?>" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'items']['items_names'][$kirilkirkovwipdf_loop]) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'items']['items_names'][$kirilkirkovwipdf_loop])) : ''; ?>" />
                <a class="ajax-select kkwi-show-select-item-modal" href="javascript:;">
                    <span class="dashicons dashicons-menu-alt" title="<?php esc_attr_e( 'Select from list', 'kirilkirkov-pdf-invoice-manager' ); ?>"></span>
                </a>
            </div>
        </div>
    </td>
    <td class="quantity relative align-middle--important">
        <div class="absolute td-col-warning-msg none"></div>
        <div class="flex items-center">
            <input type="text" class="inuse quantity-input-field calc-field is-nan-validation" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'items']['items_quantities'][$kirilkirkovwipdf_loop]) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'items']['items_quantities'][$kirilkirkovwipdf_loop])) : '1.00'; ?>" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'items[items_quantities][]'; ?>">
            <select name="<?php echo esc_attr($Config::INPUTS_PREFIX).'items[items_quantity_types][]'; ?>">
            <?php 
                $kirilkirkovwipdf_quantity_types = get_option($Config::INPUTS_PREFIX.'quantity_types');
                $kirilkirkovwipdf_selected_quantity_type = esc_html__('Kilogram', 'kirilkirkov-pdf-invoice-manager');
                if($kirilkirkovwipdf_quantity_types && is_array($kirilkirkovwipdf_quantity_types) && count($kirilkirkovwipdf_quantity_types)) {
                    $kirilkirkovwipdf_selected_quantity_type = reset($kirilkirkovwipdf_quantity_types);
                }
                if(isset($invoice[$Config::INPUTS_PREFIX.'items']['items_quantity_types'][$kirilkirkovwipdf_loop])) {
                    $kirilkirkovwipdf_selected_quantity_type = $invoice[$Config::INPUTS_PREFIX.'items']['items_quantity_types'][$kirilkirkovwipdf_loop];
                }
                if($kirilkirkovwipdf_quantity_types && is_array($kirilkirkovwipdf_quantity_types) && count($kirilkirkovwipdf_quantity_types)) {
                    $kirilkirkovwipdf_has_selection = false;
                    foreach($kirilkirkovwipdf_quantity_types as $kirilkirkovwipdf_q) {
                        $kirilkirkovwipdf_selected = false;
                        if((isset($invoice[$Config::INPUTS_PREFIX.'items']['items_quantity_types'][$kirilkirkovwipdf_loop]) 
                            && $invoice[$Config::INPUTS_PREFIX.'items']['items_quantity_types'][$kirilkirkovwipdf_loop] === $kirilkirkovwipdf_q) 
                            || $kirilkirkovwipdf_selected_quantity_type === $kirilkirkovwipdf_q) {
                            $kirilkirkovwipdf_has_selection = true;
                            $kirilkirkovwipdf_selected = true;
                        }
            ?>
                    <option <?php selected($kirilkirkovwipdf_selected, true); ?> value="<?php echo esc_attr(sanitize_text_field($kirilkirkovwipdf_q)); ?>"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_q)); ?></option>
            <?php  }
                }
                // if selected value is not in the array
                if($kirilkirkovwipdf_has_selection === false) {
            ?>
                    <option selected="selected" value="<?php echo esc_attr(sanitize_text_field($kirilkirkovwipdf_selected_quantity_type)); ?>"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_selected_quantity_type)); ?></option>
            <?php } ?>           
            </select>
            <span class="dashicons dashicons-no-alt px-2"></span>
        </div>
    </td>
    <td class="min-w-180 align-middle--important">
        <div class="flex items-center">
            <input type="text" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'items']['items_prices'][$kirilkirkovwipdf_loop]) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'items']['items_prices'][$kirilkirkovwipdf_loop])) : '0.00'; ?>" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'items[items_prices][]'; ?>" class="price-input-field calc-field is-nan-validation">
            <span class="px-2">=</span>
        </div>
    </td>
    <td class="text-right align-middle--important">
        <div class="flex items-center">
            <span class="item-txt-total <?php echo $disabled_js_calc && (int)$disabled_js_calc === 1 ? 'hidden' : ''; ?>"><?php echo isset($invoice[$Config::INPUTS_PREFIX.'items']['items_totals'][$kirilkirkovwipdf_loop]) ? esc_attr($invoice[$Config::INPUTS_PREFIX.'items']['items_totals'][$kirilkirkovwipdf_loop]) : '0.00'; ?></span>
            <input type="<?php echo $disabled_js_calc && (int)$disabled_js_calc === 1 ? 'text' : 'hidden'; ?>" class="item-input-total" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'items']['items_totals'][$kirilkirkovwipdf_loop]) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'items']['items_totals'][$kirilkirkovwipdf_loop])) : '0.00'; ?>" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'items[items_totals][]'; ?>">
            <span class="currency-text"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_selected_currency)); ?></span>
        </div>
    </td>
</tr>
<?php 
    $kirilkirkovwipdf_loop++;
    }
?>