<?php

// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) {
    exit;
}

// Whols - WooCommerce Wholesale Prices
// https://wordpress.org/plugins/whols/

if ( !class_exists( 'Whols_Lite' ) ) {
    return;
}

if ( ! class_exists( 'PWBE_Whols_Lite' ) ) :

final class PWBE_Whols_Lite {

    function __construct() {
        add_filter( 'pwbe_product_columns', array( $this, 'pwbe_product_columns' ) );
        add_filter( 'pwbe_results_product', array( $this, 'pwbe_results_product' ), 10, 2 );
        add_filter( 'pwbe_save_field_value', array( $this, 'pwbe_save_field_value' ), 10, 2 );
    }

    function pwbe_product_columns( $columns ) {
        $new_columns = array();

        $new_columns[] = array(
            'name' => __( 'Wholesale Price', 'whols' ) . ' (' . get_woocommerce_currency_symbol() . ')',
            'type' => 'currency',
            'table' => 'meta',
            'field' => '_whols_price_type_1_properties',
            'readonly' => 'false',
            'visibility' => 'both',
            'sortable' => 'false',
            'views' => array( 'all' ),
        );

        $new_columns[] = array(
            'name' => __( 'Min. Quantity', 'whols '),
            'type' => 'number',
            'table' => 'meta',
            'field' => '_whols_price_type_1_properties',
            'readonly' => 'false',
            'visibility' => 'both',
            'sortable' => 'false',
            'views' => array( 'all' ),
        );

        // Insert after the "Sale End Date" column.
        $start_index = 1;
        foreach ( $columns as $index => $column ) {
            if ( $column['field'] === '_sale_price_dates_to' ) {
                $start_index = $index + 1;
                break;
            }
        }

        array_splice( $columns, $start_index, 0, $new_columns );

        return $columns;
    }

    function pwbe_results_product( $pwbe_product, $column ) {
        $field = $column['field'];
        if ( $field == '_whols_price_type_1_properties' && isset( $pwbe_product->{$field} ) ) {
            // Keep the unaltered value from the database.
            if ( !isset( $pwbe_product->{$field . '_orig'} ) ) {
                $pwbe_product->{$field . '_orig'} = $pwbe_product->{$field};
            }

            $price_type_1_properties_arr = explode(':', $pwbe_product->{$field . '_orig'});
            $price_type_1_price = isset($price_type_1_properties_arr[0]) ? $price_type_1_properties_arr[0] : '';
            $price_type_1_min_quantity = isset($price_type_1_properties_arr[1]) ? $price_type_1_properties_arr[1] : '';

            if ( $column['type'] == 'currency' ) {
                $pwbe_product->{$field} = $price_type_1_price;
            } else {
                $pwbe_product->{$field} = $price_type_1_min_quantity;
            }
        }

        return $pwbe_product;
    }

    function pwbe_save_field_value( $field_value, $field ) {
        if ( $field['field'] == '_whols_price_type_1_properties' ) {
            $price_type_1_properties = get_post_meta( $field['post_id'], '_whols_price_type_1_properties', true );

            if ( !empty( $price_type_1_properties ) ) {
                $price_type_1_properties_arr = explode(':', $price_type_1_properties);
                $price_type_1_price = isset($price_type_1_properties_arr[0]) ? $price_type_1_properties_arr[0] : '';
                $price_type_1_min_quantity = isset($price_type_1_properties_arr[1]) ? $price_type_1_properties_arr[1] : '';
            }


            if ( $field['type'] == 'currency' ) {
                $price_type_1_price = wc_format_decimal( $field_value );
            } else {
                $price_type_1_min_quantity = absint( $field_value );
            }

            if ( !empty( $price_type_1_price ) ){
                $field_value = $price_type_1_price . ':' . $price_type_1_min_quantity;
            } else {
                $field_value = '';
            }

            update_post_meta( $field['post_id'], '_whols_price_type_1_properties', $field_value );

        }

        return $field_value;
    }
}

new PWBE_Whols_Lite();

endif;
