<?php
/**
 * Cost & Reports
 *
 * @package CostReports/includes
 */

defined( 'ABSPATH' ) || exit;

require_once __DIR__ . '/class-cyp-functions.php';
require_once __DIR__ . '/class-cyp-report.php';

class Cyp_Templates {

	/**
	 * Functions
	 */
	public $functions;

	/**
	 * Logger
	 */
	public $wc_logger;

	/**
	 * Context
	 */
	public $cyp_context;

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->functions   = new Cyp_Functions();
		$this->wc_logger   = wc_get_logger();
		$this->cyp_context = array( 'source' => 'woo_cost_reports' );
	}

	/**
	 * Get view
	 */
	public function cyp_get_selected_view() {
		
		$view = ( strlen( filter_input( 1, 'view', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) > 0 ) ? filter_input( 1, 'view', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : 'main';

		switch ( $view ) {
			case 'main':
				$this->cyp_get_main_page();
				break;
			case 'rep':
				$report_view = new Cyp_Report();
				$report_view->cyp_get_main_page();
				break;
		}
		
	}

	/**
	 * Get main page
	 */
	public function cyp_get_main_page() {
		//Get products
		$product_obj   = $this->functions->cyp_get_products();
		$max_num_pages = $product_obj['max_num_pages'];
		?>
		<div class="cyp-main">
			<?php
			$this->cyp_get_top_header();
			$this->cyp_get_table_paging_top( $max_num_pages );
			$this->cyp_get_table( $product_obj['products'] );
			$this->cyp_get_sticky_footer();
			$this->cyp_get_foot( $max_num_pages );
			?>
		</div>
		<?php
	}
	
	/**
	 * Get foot
	 */
	public function cyp_get_foot( $max_num_pages ) {
		
		$html = '';
		
		apply_filters( 'cyp_before_footer_close', $html );
		
		?>
		<div id="cyp-paging-bottom" class="div-paging" style="margin-top: 6px;">
			<?php $this->cyp_get_product_paging( $max_num_pages ); ?>
		</div>
		<?php
	}

	/**
	 * Sticky footer
	 */
	public function cyp_get_sticky_footer() {
		
		$view = filter_input( 1, 'view', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
		$page = filter_input( 1, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
		
		if ( 'crw' === $page || 'main' === $view ) {
			?>
			<footer class="cyp-footer" style="display: flex;">
				<div style="flex: 0 0 37%;text-align:left;padding:4px;color:whitesmoke;font-size: smaller;">
				<?php 
				$img_file = plugins_url( '../consortia-100.png', __FILE__ );

				/* translators: %s: url to vendor and logo */ 
				printf( esc_html__( '- developed by %1$s%2$s%3$s', 'woo-calculate-your-price' ), '<a href="https://www.consortia.no/en/" target="_blank">', '<img src="' . esc_attr( $img_file ) . '" class="cas-logo">', '</a>' );
				?>
				<a href="https://woocommerce.com/products/cost-reports-woocommerce/#reviews" target="_blank" title="<?php esc_attr_e( 'Rate this plugin', 'woo-calculate-your-price' ); ?>" style="padding-left:4px; text-decoration: none;">
					<?php esc_html_e( 'Love it? Rate us ★★★★★', 'woo-calculate-your-price' ); ?>
				</a>
				</div>
				<div style="flex: 1;text-align:left;">
				<button class="button" id="reset-values-footer" onclick="cyp_reset_table_values();" style="width:150px;"><i class="fas fa-undo"></i> <?php esc_attr_e( 'Undo changes', 'woo-calculate-your-price' ); ?></button>
				<button type="button" id="saveall-foot" class="button action" onclick="saveAll();" style="width:150px;"><i class="fas fa-database"></i> <?php esc_html_e( 'Save all', 'woo-calculate-your-price' ); ?></button>
				</div>
			</footer>
			<?php
		}
	}

	/**
	 * Get header and search
	 */
	public function cyp_get_top_header() {

		$get_prod_search = filter_input( 1, 'product_search', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
		$stype           = filter_input( 1, 'stype', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
		$cyp_options     = get_option( 'cyp_options' );
		$shipping_cost   = $cyp_options['cyp_show_shipping_cost_tbl'];
		?>
		<div class="wrap">
			<h1 class="wp-heading-inline"><?php esc_html_e( 'Cost & Reports', 'woo-calculate-your-price' ); ?></h1>
		</div>
		<div>
			<?php $this->functions->cyp_get_page_tab_menu(); ?>
		</div>
	
		<div class="row-container" style="margin-bottom:-14px;">
			<div class="cyp-button-row">
			
			<div class="cas-range-box">
				<span style="width:50%;">
				<select id="cas-range-type" onchange="casRangeTypeChanged('','');">
					<option value="price"><?php esc_attr_e( 'Price ( Range )', 'woo-calculate-your-price' ); ?></option>
					<option value="stock"><?php esc_attr_e( 'Stock ( Range )', 'woo-calculate-your-price' ); ?></option>
					<option value="purchase_price"><?php esc_attr_e( 'Purchase Price ( Range )', 'woo-calculate-your-price' ); ?></option>
					<option value="other_cost"><?php esc_attr_e( 'Other Cost ( Range )', 'woo-calculate-your-price' ); ?></option>
					<?php if ( 'yes' === $shipping_cost ) : ?>
					<option value="shipping_cost"><?php esc_attr_e( 'Shipping Cost ( Range )', 'woo-calculate-your-price' ); ?></option>
					<?php endif; ?>
					<option value="cost_of_goods"><?php esc_attr_e( 'Cost of Goods ( Range )', 'woo-calculate-your-price' ); ?></option>
					<option value="markup"><?php esc_attr_e( 'Markup ( Range )', 'woo-calculate-your-price' ); ?></option>
					<option value="stock-value-cost"><?php esc_attr_e( 'Value at Cost ( Range )', 'woo-calculate-your-price' ); ?></option>
					<option value="sales-value"><?php esc_attr_e( 'Retail Value ( Range )', 'woo-calculate-your-price' ); ?></option>
					<option value="margin"><?php esc_attr_e( 'Profit ( Range )', 'woo-calculate-your-price' ); ?></option>
				</select>
				<button class="button" onclick="casResetFilter();" style="margin-left:2px;margin-top:2px;"><i class="fas fa-filter"></i> <?php esc_attr_e( 'Reset', 'woo-calculate-your-price' ); ?></button>
				<input type="text" id="cas-amount" readonly class="cas-range-amount" style="width: 194px; border-color: white;margin-top: 2px;">
				</span>
				<span id="cas-slider-range" style="margin-bottom:10px;margin-top:10px;width:50%;"></span>
			</div>
			
			</div>
			<div class="col-search display-desktop">
				<div>
				<i class="fas fa-info-circle" style="padding-left: 2px;"></i><span id="cyp-prod-count" style="padding-left:5px;"></span>
				<form name="gs" id="gsSearch" type="get" onsubmit="casSearchSubmit(this);" autocomplete="off" style="padding-top:4px;">
					<input type="hidden" name="page" value="crw">
					<input type="search" class="input-txt-search" onmouseover="<?php echo esc_js( $this->functions->cyp_get_autofocus_value( '' ) ); ?>" placeholder="<?php esc_html_e( 'search', 'woo-calculate-your-price' ); ?><?php echo ' ' . esc_attr( $get_prod_search ); ?>" id="product_search" name="product_search" >
					<button type="submit" id="btnsearch" class="button" title="<?php esc_attr_e( 'Search or Reset', 'woo-calculate-your-price' ); ?>"><i class="fas fa-search"></i></button><br/>
					<div class="frm-radio">
					<?php esc_attr_e( 'Search in', 'woo-calculate-your-price' ); ?>
					<input type="radio" name="stype" value="s" <?php echo ( 's' === $stype ) ? 'checked="checked"' : ''; ?>><?php esc_attr_e( 'Text', 'woo-calculate-your-price' ); ?>
					<input type="radio" name="stype" value="sku" <?php echo ( 'sku' === $stype ) ? 'checked="checked"' : ''; ?>><?php esc_attr_e( 'SKU', 'woo-calculate-your-price' ); ?>
					<input type="radio" name="stype" value="rows" <?php echo ( 'rows' === $stype || !isset( $stype ) ) ? 'checked="checked"' : ''; ?>><?php esc_attr_e( 'Rows', 'woo-calculate-your-price' ); ?>
					</div>           
				</form>
				</div>
			</div>
		</div>
		
	
		<?php
	}

	/**
	 * Get table
	 */
	public function cyp_get_table( $products ) {
		
		?>
		<div class="display-desktop"> <!-- Table -->
			<table class="wp-list-table widefat fixed striped posts" id="cyptable" style="width:100%;">
				<thead>
					<?php $this->cyp_get_table_head(); ?>
				</thead>
				<tbody id="the-list">   
					<?php $this->cyp_loop_products( $products ); ?>
				</tbody>
					<?php $this->cyp_get_table_footer(); ?>
			</table>
			
		</div>
		
		<?php
	}

	/**
	 * Loop products
	 */
	private function cyp_loop_products( $products ) {

		$subscriptions_exists = class_exists( 'WC_Product_Subscription' );
		
		foreach ( $products as $product ) {
			if ( $product instanceof WC_Product ) {
				// Determine if the product is a variation or simple product
				$is_variation = $product instanceof WC_Product_Variation;
		
				// Process subscriptions or simple products
				if ( ! $is_variation && ( $product->is_type( 'simple' || $product->is_type( 'bundle' ) ) || ( $subscriptions_exists && $product->is_type( 'subscription' ) ) ) ) {
					$this->cyp_get_table_row( $product, false, 0 );
				// Process variations (including subscription variations)
				} elseif ( $is_variation || ( $subscriptions_exists && $product->is_type( 'variable-subscription' ) ) ) {
					$this->cyp_get_table_row( $product, true, 0 );
				} else {
					// Other cases e.g. grouped products
					$this->cyp_get_table_row( $product, false, 0 );
				}
			} 
		}
		
	}

	/**
	 * Get paging for products
	 */
	private function cyp_get_product_paging( $max_num_pages ) {
		
		$page_num = ( ! empty( filter_input( 1, 'paged', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) ) ? filter_input( 1, 'paged', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : 1;
		$this->functions->cyp_get_pagination( $page_num, $max_num_pages );

	}

	/**
	 * Get paging above table
	 */
	public function cyp_get_table_paging_top( $max_num_pages ) {

		?>
		<div class="cas-action-box" style="margin-bottom:10px;">
			<div class="col-category"> 
				<div style="width:360px;">
					<!-- Filter by categories -->
					<?php $this->functions->cyp_get_categories_select(); ?>
				</div>
				<div style="width:180px;">
					<!-- Filter by tags -->
					<?php $this->functions->cyp_get_tags_filter(); ?>
				</div>
				<div style="width:180px;">
					<!-- Filter by statuses -->
					<?php $this->functions->cyp_get_stock_status_filter(); ?>
				</div>

				<div style="width:fit-content;padding-top:5px;"> 
					<a type="button" href="#" class="button cas-btn-filters" id="reset-values" onclick="cyp_reset_multi_filters();" title="<?php esc_attr_e( 'Reset all filters', 'woo-calculate-your-price' ); ?>"><i class="fas fa-filter"></i> <?php esc_attr_e( 'Reset', 'woo-calculate-your-price' ); ?></a>	
				</div>

				<div class="cas-mini-buttons-div">
					<?php $this->functions->cyp_get_view_buttons(); ?>
				</div>

				<div style="text-align:end;flex-grow:2;padding-top:10px;">
					<?php $this->cyp_get_product_paging( $max_num_pages ); ?>
				</div>
			</div>	
		</div>

		<div class="cas-action-box">
			<div style="display: flex; justify-content: flex-end; align-items: center; gap: 8px; flex-wrap: wrap;">
				
				<a href="#" class="button" id="a-delete-rows" onclick="deleteRows();" title="<?php esc_attr_e( 'Delete checked rows (products)', 'woo-calculate-your-price' ); ?>">
					<i class="fas fa-trash-alt"></i> <?php esc_html_e( 'Delete', 'woo-calculate-your-price' ); ?>
				</a>

				<a href="#" class="button" id="a-clear-calc" onclick="clearCalc();" title="<?php esc_attr_e( 'Clear all calculations in the table', 'woo-calculate-your-price' ); ?>">
					<i class="fas fa-eraser"></i> <?php esc_html_e( 'Clear', 'woo-calculate-your-price' ); ?>
				</a>

				<button class="button" id="reset-values-footer" onclick="cyp_reset_table_values();" title="<?php esc_attr_e( 'Undo changes (prior to save)', 'woo-calculate-your-price' ); ?>">
					<i class="fas fa-undo"></i> <?php esc_html_e( 'Undo', 'woo-calculate-your-price' ); ?>
				</button>

				<div class="cas-switch-col">
					<label class="cas-switch-row" style="display: flex; align-items: center; gap: 6px;">
						<span class="cas-switch-label"><?php esc_html_e( 'Exclude price on save', 'woo-calculate-your-price' ); ?></span>
						<input type="checkbox" name="exclude_price_save" id="exclude_price_save" class="cas-toggle-mode" data-target="exclude_price_save">
						<span class="cas-slider round"></span>
					</label>
				</div>

				<button type="button" id="saveall-top" class="button action" onclick="saveAll();" style="width: 150px;">
					<i class="fas fa-database"></i> <?php esc_html_e( 'Save all', 'woo-calculate-your-price' ); ?>
				</button>
			</div>
		</div>


		<div class="cyp-saving" id="cyp-saving" style="max-width:100%;height:15px;margin-bottom:4px;">
			<progress id="pbar-saving" class="pbar-saving" value="0" max="100" style="height: 14px;"></progress>
		</div>
		<?php
	}

	/**
	 * Get table head
	 */
	public function cyp_get_table_head() {
		
		$suggested_price_lbl = __( 'The new suggested price, calculated based on markup or cost changes. This price will be saved as the new price on the product unless the checkbox is ticked to exclude it.', 'woo-calculate-your-price' );
		$markup_lbl          = __( 'Set the markup or profit percentage for your products. For example, 150 means a 50% increase over the cost.', 'woo-calculate-your-price' );
		$price_lbl           = __( 'Price with orange border means that product is on sale (current price), use the dropdown select to view prices inc / ex. VAT.', 'woo-calculate-your-price' );
		$retail_lbl          = __(
			'If a New Price is entered and it differs from the current Price, this value shows the updated retail value based on the New Price. (Note: the CSV export will still use the current saved Price)',
			'woo-calculate-your-price'
		);
		$profit_lbl          = __(
			'If a New Price is entered and it differs from the current Price, this value shows the updated profit based on the New Price. (Note: the CSV export will still use the current saved Price)',
			'woo-calculate-your-price'
		);

		?>
		<tr>
			<th class="th-first"><input type="checkbox" id="checkall" class="input-chk" onclick="cypCheckRows();"></th>
			<th style="width:20%;"><a href="#" onclick="sortMain(1,'text','s','cyptable');">
			<i id="s1" class="fas fa-sort" style="padding-left:2px;padding-right:2px;"></i><?php esc_html_e( 'Name', 'woo-calculate-your-price' ); ?></a>
				<input type="checkbox" id="cyp-chk-hide-info" onclick="cyp_show_hide_info();"> <?php esc_html_e( 'Hide ID and SKU', 'woo-calculate-your-price' ); ?>
			</th>
			<th class="th-center"><a href="#" onclick="sortMain(2,'number','s','cyptable');"><i id="s2" class="fas fa-sort"></i> <?php echo esc_attr( $this->functions->cyp_get_label_name( 'purchase_price' ) ); ?></a></th>
			<th class="th-center"><a href="#" onclick="sortMain(3,'number','s','cyptable');"><i id="s3" class="fas fa-sort"></i> <?php echo esc_attr( $this->functions->cyp_get_label_name( 'other_cost' ) ); ?></a></th>
			<?php
			if ( $this->functions->cyp_show_shipping_cost() ) {
				?>
				<th class="th-center"><a href="#" onclick="sortMain(4,'number','s','cyptable');"><i id="s4" class="fas fa-sort"></i> <?php echo esc_attr( $this->functions->cyp_get_label_name( 'shipping_cost' ) ); ?></a></th>
				<th class="th-center"><a href="#" onclick="sortMain(5,'number','s','cyptable');"><i id="s5" class="fas fa-sort"></i> <?php esc_html_e( 'Cost of goods', 'woo-calculate-your-price' ); ?></a></th>
				<th class="th-center" style="width:6%;">
					<a href="#" onclick="sortMain(5,'number','s','cyptable');" title="<?php echo esc_attr( $markup_lbl ); ?>"><i id="s6" class="fas fa-sort"></i> <?php esc_html_e( 'Markup', 'woo-calculate-your-price' ); ?> <i class="fa-regular fa-circle-question" style="color:forestgreen;"></i></a>
				</th>
				<th class="th-center">
					<a href="#" onclick="sortMain(7,'number','s','cyptable');" title="<?php echo esc_attr( $price_lbl ); ?>"><i id="s7" class="fas fa-sort"></i> <?php esc_html_e( 'Price', 'woo-calculate-your-price' ); ?> <i class="fa-regular fa-circle-question" style="color:forestgreen;"></i></a>
				</th>
				<th class="th-center">
					<a href="#" onclick="sortMain(8,'number','s','cyptable');" title="<?php echo esc_attr( $suggested_price_lbl ); ?>"><i id="s8" class="fas fa-sort"></i> <?php esc_html_e( 'New price', 'woo-calculate-your-price' ); ?> <i class="fa-regular fa-circle-question" style="color:forestgreen;"></i></a>
				</th>
				<th class="th-center" style="width:75px;"><a href="#" onclick="sortMain(9,'number','s','cyptable');"><i id="s9" class="fas fa-sort"></i> <?php esc_html_e( 'Stock', 'woo-calculate-your-price' ); ?></a></th>
				<th class="th-center" style="width:7%;"><a href="#" onclick="sortMain(10,'number','s','cyptable');"><i id="s10" class="fas fa-sort"></i> <?php esc_html_e( 'Value at cost', 'woo-calculate-your-price' ); ?></a></th>
				<th class="th-center">
					<a href="#" onclick="sortMain(11,'number','s','cyptable');" title="<?php echo esc_attr( $retail_lbl ); ?>"><i id="s11" class="fas fa-sort"></i> <?php esc_html_e( 'Retail value', 'woo-calculate-your-price' ); ?> <i class="fa-regular fa-circle-question" style="color:forestgreen;"></i></a></th>
				<th class="th-center">
					<a href="#" onclick="sortMain(12,'number','s','cyptable');" title="<?php echo esc_attr( $profit_lbl ); ?>"><i id="s12" class="fas fa-sort"></i> <?php esc_html_e( 'Profit', 'woo-calculate-your-price' ); ?> <i class="fa-regular fa-circle-question" style="color:forestgreen;"></i></a></th>
				<?php
			} else {
				?>
				<th class="th-center"><a href="#" onclick="sortMain(4,'number','s','cyptable');"><i id="s4" class="fas fa-sort"></i> <?php esc_html_e( 'Cost of goods', 'woo-calculate-your-price' ); ?></a></th>
				<th class="th-center" style="width:6%;">
					<a href="#" onclick="sortMain(5,'number','s','cyptable');" title="<?php echo esc_attr( $markup_lbl ); ?>"><i id="s5" class="fas fa-sort"></i> <?php esc_html_e( 'Markup', 'woo-calculate-your-price' ); ?> <i class="fa-regular fa-circle-question" style="color:forestgreen;"></i></a>
				</th>
				<th class="th-center">
					<a href="#" onclick="sortMain(6,'number','s','cyptable');" title="<?php echo esc_attr( $price_lbl ); ?>"><i id="s6" class="fas fa-sort"></i> <?php esc_html_e( 'Price', 'woo-calculate-your-price' ); ?> <i class="fa-regular fa-circle-question" style="color:forestgreen;"></i></a>
				</th>
				<th class="th-center">
					<a href="#" onclick="sortMain(7,'number','s','cyptable');" title="<?php echo esc_attr( $suggested_price_lbl ); ?>"><i id="s7" class="fas fa-sort"></i> <?php esc_html_e( 'New price', 'woo-calculate-your-price' ); ?> <i class="fa-regular fa-circle-question" style="color:forestgreen;"></i></a>
				</th>
				<th class="th-center"><a href="#" onclick="sortMain(8,'number','s','cyptable');"><i id="s8" class="fas fa-sort"></i> <?php esc_html_e( 'Stock', 'woo-calculate-your-price' ); ?></a></th>
				<th class="th-center"><a href="#" onclick="sortMain(9,'number','s','cyptable');"><i id="s9" class="fas fa-sort"></i> <?php esc_html_e( 'Value at cost', 'woo-calculate-your-price' ); ?></a></th>
				<th class="th-center">
					<a href="#" onclick="sortMain(10,'number','s','cyptable');" title="<?php echo esc_attr( $retail_lbl ); ?>"><i id="s10" class="fas fa-sort"></i> <?php esc_html_e( 'Retail value', 'woo-calculate-your-price' ); ?> <i class="fa-regular fa-circle-question" style="color:forestgreen;"></i></a></th>
				<th class="th-center">
					<a href="#" onclick="sortMain(11,'number','s','cyptable');" title="<?php echo esc_attr( $profit_lbl ); ?>"><i id="s11" class="fas fa-sort"></i> <?php esc_html_e( 'Profit', 'woo-calculate-your-price' ); ?> <i class="fa-regular fa-circle-question" style="color:forestgreen;"></i></a></th>
				<?php
			}
			?>
		</tr>
		<?php
			$this->cyp_get_table_head_info();
	}

	/**
	 * Get the js function for rows if shipping cost
	 *
	 * @param var $name name of field.
	 */
	public function cyp_get_js_calc_function( $name ) {
		$out = '';
		if ( $this->functions->cyp_show_shipping_cost() ) {
			switch ( $name ) {
				case 'cost_of_goods':
					$out = "calcSpecial(3,5,'cost_of_goods_select');";
					break;
				case 'markup':
					$out = "calcSpecial(4,6,'markup_select');";
					break;
				case 'new_price':
					$out = "calcSpecial(6,8,'new_price_select');";
					break;
				case 'stock':
					$out = "calcSpecial(7,9,'stock_select');";
					break;
			}
		} else {
			switch ( $name ) {
				case 'cost_of_goods':
					$out = "calcSpecial(2,4,'cost_of_goods_select');";
					break;
				case 'markup':
					$out = "calcSpecial(3,5,'markup_select');";
					break;
				case 'new_price':
					$out = "calcSpecial(5,7,'new_price_select');";
					break;
				case 'stock':
					$out = "calcSpecial(6,8,'stock_select');";
					break;
			}
		}
		return $out;
	}

	/**
	 * Get table head info
	 */
	public function cyp_get_table_head_info() {

		?>
			<tr class="th-info">
				<th></th>
				<th style="vertical-align: middle;"><strong><?php esc_html_e( 'Bulk Editor', 'woo-calculate-your-price' ); ?></strong><br /><span class="th-desc"><?php esc_html_e( 'Fill in values and/or select option to apply to all visible products', 'woo-calculate-your-price' ); ?></span></th>
				<th style="vertical-align: top;"><input type="number" name="purchase_price_select" id="purchase_price_select" class="input-txt" onmouseover="<?php echo esc_js( $this->functions->cyp_get_autofocus_value( '' ) ); ?>"><br/>
					<select name="purchase_price_select_type" id="purchase_price_select_type" class="input-select" onchange="calcSpecial(0,2,'purchase_price_select');">
						<?php $this->cyp_get_bulk_options( '' ); ?>
					</select>
				</th>
				<th style="vertical-align: top;"><input type="number" name="other_cost_select" id="other_cost_select" class="input-txt" onmouseover="<?php echo esc_js( $this->functions->cyp_get_autofocus_value( '' ) ); ?>">
				<br/>
					<select name="other_cost_select_type" id="other_cost_select_type" class="input-select" onchange="calcSpecial(1,3,'other_cost_select');">
						<?php $this->cyp_get_bulk_options( 'other_cost' ); ?>
					</select>
				</th>
				<?php
				if ( $this->functions->cyp_show_shipping_cost() ) {
					?>
					<th style="vertical-align: top;"><input type="number" name="shipping_cost_select" id="shipping_cost_select" class="input-txt" onmouseover="<?php echo esc_js( $this->functions->cyp_get_autofocus_value( '' ) ); ?>">
					<br/>
						<select name="shipping_cost_select_type" id="shipping_cost_select_type" class="input-select" onchange="calcSpecial(2,4,'shipping_cost_select');">
							<?php $this->cyp_get_bulk_options( 'shipping' ); ?>
						</select>
					</th>
					<?php
				}
				?>
				<th style="vertical-align: top;"><input type="number" name="cost_of_goods_select" id="cost_of_goods_select" class="input-txt" onmouseover="<?php echo esc_js( $this->functions->cyp_get_autofocus_value( '' ) ); ?>">
				<br/>
					<select name="cost_of_goods_select_type" id="cost_of_goods_select_type" class="input-select" onchange="<?php echo esc_attr( $this->cyp_get_js_calc_function( 'cost_of_goods' ) ); ?>">
						<?php $this->cyp_get_bulk_options( 'cost_of_goods' ); ?>
					</select>
				</th>
				<th style="vertical-align: top;"><input type="number" name="markup_select" id="markup_select" class="input-txt" onmouseover="<?php echo esc_js( $this->functions->cyp_get_autofocus_value( '' ) ); ?>">
				<br/>
					<select name="markup_select_type" id="markup_select_type" class="input-select" onchange="<?php echo esc_attr( $this->cyp_get_js_calc_function( 'markup' ) ); ?>">
						<?php $this->cyp_get_bulk_options( 'markup' ); ?>
					</select>
				</th>
				<th style="vertical-align:bottom;text-align:center;padding-top:16px;font-size:.7rem;">
					<select name="current_price_select_type" id="current_price_select_type" class="input-select" onchange="cyp_change_vat_price(this);">
						<?php $this->cyp_get_bulk_options( 'current_price' ); ?>
					</select>
				</th>
				<th style="vertical-align: top;"><input type="number" name="new_price_select" id="new_price_select" class="input-txt" onmouseover="<?php echo esc_js( $this->functions->cyp_get_autofocus_value( '' ) ); ?>">
				<br/>
					<select name="new_price_select_type" id="new_price_select_type" class="input-select" onchange="<?php echo esc_attr( $this->cyp_get_js_calc_function( 'new_price' ) ); ?>">
						<?php $this->cyp_get_bulk_options( 'new_price' ); ?>
					</select>
				</th>

				<th style="vertical-align: top;"><input type="number" name="stock_select" id="stock_select" class="input-txt" onmouseover="<?php echo esc_js( $this->functions->cyp_get_autofocus_value( '' ) ); ?>">
				<br/>
					<select name="stock_select_type" id="stock_select_type" class="input-select" onchange="<?php echo esc_attr( $this->cyp_get_js_calc_function( 'stock' ) ); ?>">
						<?php $this->cyp_get_bulk_options( 'stock' ); ?>
					</select>
				</th>
				<th colspan="3" style="vertical-align: top;text-align:center;">
					
				</th>
			</tr>

		<?php
	}

	/**
	 * Get options for bulk functions
	 *
	 * @param var $type type of select.
	 */
	public function cyp_get_bulk_options( $type ) {

		$option_blank              = __( 'Select', 'woo-calculate-your-price' );
		$option_increase           = __( 'Increase', 'woo-calculate-your-price' );
		$option_increase_percent   = __( 'Increase in %', 'woo-calculate-your-price' );
		$option_increase_number    = __( 'Increase fixed amount', 'woo-calculate-your-price' );
		$option_decrease           = __( 'Decrease', 'woo-calculate-your-price' );
		$option_decrease_percent   = __( 'Decrease in %', 'woo-calculate-your-price' );
		$option_decrease_number    = __( 'Decrease fixed amount', 'woo-calculate-your-price' );
		$option_set_fixed          = __( 'Set fixed', 'woo-calculate-your-price' );
		$option_set_fixed_markup   = __( 'Set fixed %', 'woo-calculate-your-price' );
		$option_set_fixed_number   = __( 'Set fixed amount', 'woo-calculate-your-price' );
		$option_round_up           = __( 'Round upwards', 'woo-calculate-your-price' );
		$option_round_down         = __( 'Round downwards', 'woo-calculate-your-price' );
		$option_round_two          = __( 'Round two decimals', 'woo-calculate-your-price' );
		$option_round_fixed        = __( 'Round to fixed decimals (e.g. 0.99)', 'woo-calculate-your-price' );
		$option_copy_price         = __( 'Copy current regular price', 'woo-calculate-your-price' );
		$option_copy_purchase      = __( 'Copy purchase price', 'woo-calculate-your-price' );
		$purchase_increase_percent = __( 'Plus % of purchase price', 'woo-calculate-your-price' );
		$purchase_plus_percent     = __( 'Purchase price plus %', 'woo-calculate-your-price' );
		$option_inc_vat            = __( 'Regular price inc.VAT (dynamic)', 'woo-calculate-your-price' );
		$option_ex_vat             = __( 'Regular price ex.VAT (default)', 'woo-calculate-your-price' );
		$option_cur_inc_vat        = __( 'Current price inc.VAT', 'woo-calculate-your-price' );
		$option_cur_ex_vat         = __( 'Current price ex.VAT', 'woo-calculate-your-price' );


		if ( 'stock' === $type ) {
			?>
				<option value="0" selected="selected"><?php echo esc_html( $option_blank ); ?></option>
				<option value="up_n"><?php echo esc_html( $option_increase ); ?></option>
				<option value="down_n"><?php echo esc_html( $option_decrease ); ?></option>
				<option value="fix_n"><?php echo esc_html( $option_set_fixed ); ?></option>
			<?php
		} elseif ( 'markup' === $type ) {
			?>
				<option value="0" selected="selected"><?php echo esc_html( $option_blank ); ?></option>
				<option value="fix_n"><?php echo esc_html( $option_set_fixed_markup ); ?></option>
				<option value="up_n"><?php echo esc_html( $option_increase ); ?> %</option>
				<option value="down_n"><?php echo esc_html( $option_decrease ); ?> %</option>
			<?php
		} elseif ( 'new_price' === $type ) {
			?>
				<option value="0" selected="selected"><?php echo esc_html( $option_blank ); ?></option>
				<option value="up_p"><?php echo esc_html( $option_increase_percent ); ?></option>
				<option value="up_n"><?php echo esc_html( $option_increase_number ); ?></option>
				<option value="down_p"><?php echo esc_html( $option_decrease_percent ); ?></option>
				<option value="down_n"><?php echo esc_html( $option_decrease_number ); ?></option>
				<option value="fix_n"><?php echo esc_html( $option_set_fixed_number ); ?></option>
				<option value="round_up"><?php echo esc_html( $option_round_up ); ?></option>
				<option value="round_down"><?php echo esc_html( $option_round_down ); ?></option>
				<option value="round_two"><?php echo esc_html( $option_round_two ); ?></option>
				<option value="round_set"><?php echo esc_html( $option_round_fixed ); ?></option>
				<option value="copy_price"><?php echo esc_html( $option_copy_price ); ?></option>
			<?php
		} elseif ( 'cost_of_goods' === $type ) {
			?>
				<option value="0" selected="selected"><?php echo esc_html( $option_blank ); ?></option>
				<option value="up_p"><?php echo esc_html( $option_increase_percent ); ?></option>
				<option value="purchase_plus_percent"><?php echo esc_html( $purchase_plus_percent ); ?></option>
				<option value="up_n"><?php echo esc_html( $option_increase_number ); ?></option>
				<option value="down_p"><?php echo esc_html( $option_decrease_percent ); ?></option>
				<option value="down_n"><?php echo esc_html( $option_decrease_number ); ?></option>
				<option value="fix_n"><?php echo esc_html( $option_set_fixed_number ); ?></option>
				<option value="round_up"><?php echo esc_html( $option_round_up ); ?></option>
				<option value="round_down"><?php echo esc_html( $option_round_down ); ?></option>
				<option value="round_two"><?php echo esc_html( $option_round_two ); ?></option>
				<option value="round_set"><?php echo esc_html( $option_round_fixed ); ?></option>
				<option value="copy_purchase"><?php echo esc_html( $option_copy_purchase ); ?></option>
			<?php
		} elseif ( 'other_cost' === $type || 'shipping' === $type ) {
			?>
				<option value="0" selected="selected"><?php echo esc_html( $option_blank ); ?></option>
				<option value="up_p"><?php echo esc_html( $option_increase_percent ); ?></option>
				<option value="percent_purchase"><?php echo esc_html( $purchase_increase_percent ); ?></option>
				<option value="up_n"><?php echo esc_html( $option_increase_number ); ?></option>
				<option value="down_p"><?php echo esc_html( $option_decrease_percent ); ?></option>
				<option value="down_n"><?php echo esc_html( $option_decrease_number ); ?></option>
				<option value="fix_n"><?php echo esc_html( $option_set_fixed_number ); ?></option>
				<option value="round_up"><?php echo esc_html( $option_round_up ); ?></option>
				<option value="round_down"><?php echo esc_html( $option_round_down ); ?></option>
				<option value="round_two"><?php echo esc_html( $option_round_two ); ?></option>	
				<option value="round_set"><?php echo esc_html( $option_round_fixed ); ?></option>
			<?php
		} elseif ( 'current_price' === $type ) {
			?>
				<option value="0" selected="selected"><?php echo esc_html( $option_blank ); ?></option>
				<option value="inc_vat"><?php echo esc_html( $option_inc_vat ); ?></option>
				<option value="ex_vat"><?php echo esc_html( $option_ex_vat ); ?></option>
				<option value="cur_inc_vat"><?php echo esc_html( $option_cur_inc_vat ); ?></option>
				<option value="cur_ex_vat"><?php echo esc_html( $option_cur_ex_vat ); ?></option>
			<?php
		} else {
			?>
				<option value="0" selected="selected"><?php echo esc_html( $option_blank ); ?></option>
				<option value="up_p"><?php echo esc_html( $option_increase_percent ); ?></option>
				<option value="up_n"><?php echo esc_html( $option_increase_number ); ?></option>
				<option value="down_p"><?php echo esc_html( $option_decrease_percent ); ?></option>
				<option value="down_n"><?php echo esc_html( $option_decrease_number ); ?></option>
				<option value="fix_n"><?php echo esc_html( $option_set_fixed_number ); ?></option>
				<option value="round_up"><?php echo esc_html( $option_round_up ); ?></option>
				<option value="round_down"><?php echo esc_html( $option_round_down ); ?></option>
				<option value="round_two"><?php echo esc_html( $option_round_two ); ?></option>
				<option value="round_set"><?php echo esc_html( $option_round_fixed ); ?></option>
			<?php
		}

	}

	/**
	 * Get table row
	 *
	 * @param var $product object.
	 * @param var $date_format string.
	 * @param var $has_attributes bool.
	 * @param var $parent_id int.
	 */
	public function cyp_get_table_row( $product, $has_attributes, $parent_id ) {
		
		$product_name   = $product->get_title();
		$product_id     = $product->get_id();
		$cyp_options    = get_option( 'cyp_options' );
		$cost_of_goods  = ( $cyp_options['cyp_cost_of_goods_field'] ) ? $cyp_options['cyp_cost_of_goods_field'] : '_cost_of_goods';
		$purchase_price = ( $cyp_options['cyp_purchase_price_field'] ) ? $cyp_options['cyp_purchase_price_field'] : '_purchase_price';
		$hide_id_sku    = ( isset( $cyp_options['cyp_table_hide_id_sku'] ) && 'yes' === $cyp_options['cyp_table_hide_id_sku'] ) ? true : false;
		$tag_ids        = $product->get_tag_ids();

		if ( $has_attributes ) {

			$product_name = $this->functions->cyp_get_product_name( $product );
			$product_id   = $product->get_parent_id();
			
			if ( 0 === $product_id ) {
				$product_id = $product->get_id();
			} else {
				$tag_ids = wc_get_product( $product_id )->get_tag_ids();
			}

		}

		$tax_rate  = '';
		$tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
		$tags      = $this->functions->cyp_get_product_tag_names( $tag_ids );

		foreach ( $tax_rates as $value ) {
			$tax_rate = $value['rate'];
		}

		//COGS value.
		$cost_of_goods_value = $this->functions->cyp_string_to_float( get_post_meta( $product->get_id(), $cost_of_goods, true ) );
		if ( '_cogs_value' === $cost_of_goods ) {
			$cost_of_goods_value = $product->get_cogs_value();
		}

		?>
		<tr class="lozad" data-exclude="" style="vertical-align: top;">
			<td id="<?php echo esc_attr( $product->get_id() ); ?>" style="padding-top:10px;">
				<form id="frm_<?php echo esc_attr( $product->get_id() ); ?>_<?php echo esc_attr( wp_rand( 1, 50000 ) ); ?>" method="post">
				<input name="id" value="<?php echo esc_attr( $product->get_id() ); ?>" type="checkbox" class="input-chk">
				<input type="hidden" name="tags" value="<?php echo esc_attr( $tags ); ?>">
				<input type="hidden" name="stock_status" value="<?php echo esc_attr( $product->get_stock_status() ); ?>">
			</td>
			<td style="display:flex;width:98%">
				<?php
				if ( ! $has_attributes ) {
					?>
					<div style="flex:1;width:100%;">
					<input type="text" class="input-name" name="product_name" value="<?php echo esc_html( $product_name ); ?>">
					<input type="hidden" name="name_update" value="true">
					<?php
				} else {
					?>
					<div style="flex:1;width:100%;padding-top:5px;">
					<input type="hidden" name="product_name" value="<?php echo esc_html( $product_name ); ?>">
					<input type="hidden" name="name_update" value="false">
					<?php echo esc_html( $product_name ); ?>
					<?php
				}
				?>
					<ul class="cyp-ul-info<?php echo esc_attr( ( $hide_id_sku ) ? '-hide' : '' ); ?>" id="cyp_ul_<?php echo esc_attr( $product->get_id() ); ?>">
						<li><?php esc_html_e( 'ID:', 'woo-calculate-your-price' ); ?> <?php echo esc_attr( $product->get_id() ); ?></li>
						<li><?php esc_html_e( 'SKU:', 'woo-calculate-your-price' ); ?> <?php echo esc_attr( $product->get_sku() ); ?></li>
					</ul>
				</div>
				<div style="flex: 0;text-align:center;">
					<a href="<?php echo esc_url( get_admin_url() . 'post.php?post=' . $product_id . '&action=edit' ); ?>" target="_blank" title="<?php esc_html_e( 'Edit product', 'woo-calculate-your-price' ); ?>">
					<i class="fas fa-edit"></i></a>
				</div>
				<?php
				if ( $product->get_image_id() ) {
					if ( ! $has_attributes ) {
						echo wp_kses( get_the_post_thumbnail( $product_id, array( '28', '28' ), array( 'class' => 'cyp-thumb-main' ) ), $this->functions->wbs_get_allowed_html() );
					} else {
						$variation = new WC_Product_Variation( $product->get_id() );
						echo wp_kses( $variation->get_image( array( '28', '28' ), array( 'class' => 'cyp-thumb-main' ) ), $this->functions->wbs_get_allowed_html() );
					}
				} else {
					?>
					<img height="28" src="<?php echo esc_attr( wc_placeholder_img_src() ); ?>" class="cyp-thumb-main wp-post-image">
					<?php
				}
				?>
			</td>
			<td>
				<input type="number" name="purchase_price" onchange="doCalc(this);" class="input-txt" value="<?php echo esc_attr( $this->functions->cyp_string_to_float( get_post_meta( $product->get_id(), $purchase_price, true ) ) ); ?>">
			</td>
			<td>
				<input type="number" name="other_cost" onchange="doCalc(this);" class="input-txt" value="<?php echo esc_attr( get_post_meta( $product->get_id(), '_other_cost', true ) ); ?>">
			</td>
			<?php
			if ( $this->functions->cyp_show_shipping_cost() ) {
				?>
				<td>
					<input type="number" name="shipping_cost" onchange="doCalc(this);" class="input-txt" value="<?php echo esc_attr( get_post_meta( $product->get_id(), '_cyp_shipping_cost', true ) ); ?>">
				</td>
				<?php
			}
			?>
			<td>
				<input type="number" name="cost_of_goods" onchange="doCalc(this);" class="input-txt" value="<?php echo esc_attr( $cost_of_goods_value ); ?>">
			</td>
			<td>
				<input type="number" name="markup" onchange="doCalc(this);" class="input-txt" value="<?php echo esc_attr( get_post_meta( $product->get_id(), '_markup_percent', true ) ); ?>">
			</td>
			<td>
				<input type="hidden" name="price_inc_vat" value="<?php echo esc_attr( wc_get_price_including_tax( $product, array( 'qty' => 1, 'price' => $product->get_regular_price() ) ) ); ?>">
				<input type="hidden" name="price_ex_vat" value="<?php echo esc_attr( wc_get_price_excluding_tax( $product, array( 'qty' => 1, 'price' => $product->get_regular_price() ) ) ); ?>">
				<input type="hidden" name="cur_price_ex_vat" value="<?php echo esc_attr( wc_get_price_excluding_tax( $product, array( 'qty' => 1, 'price' => $product->get_price() ) ) ); ?>">
				<input type="hidden" name="cur_price_inc_vat" value="<?php echo esc_attr( wc_get_price_including_tax( $product, array( 'qty' => 1, 'price' => $product->get_price() ) ) ); ?>">
				<input type="hidden" name="tax_rate" value="<?php echo esc_attr( $tax_rate ); ?>">
				<input type="number" step="any" name="price" readonly="readonly" class="input-txt <?php echo esc_attr( ( $product->get_sale_price() > 0 ) ? 'cyp-onsale' : '' ); ?>" value="<?php echo esc_attr( $product->get_regular_price() ); ?>"> 
			</td> 
			<td>
				<input type="number" step="any" name="price_new" onchange="doCalc(this);" class="input-txt" value=""> 
			</td> 
			<td>
				<input type="number" name="stock" onchange="doCalc(this);" class="input-txt" value="<?php echo esc_attr( $product->get_stock_quantity() ); ?>"> 
			</td>   
			<td>
				<input type="number" name="stock-value-cost" class="input-txt" value="" readonly="readonly" style="direction:rtl;">
			</td>
			<td>
				<input type="number" name="sales-value" class="input-txt" value="" readonly="readonly" style="direction:rtl;">
			</td>
			<td>
				<input type="number" name="margin" class="input-txt" value="" readonly="readonly" style="direction:rtl;"> 
				</form>
			</td>
		</tr>
		<?php
	}

	/**
	 * Get table footer
	 */
	public function cyp_get_table_footer() {
		
		$colspan_val   = 10;
		$shipping_cost = false;
		
		if ( $this->functions->cyp_show_shipping_cost() ) {
			$colspan_val   = 11;
			$shipping_cost = true;
		}

		?>
		<tfoot>
			<tr>
				<td><?php wp_nonce_field( 'footer_id' ); ?></td>
				<td><input type="hidden" id="msg-confirm-delete" value="<?php esc_attr_e( 'Move checked items to trash?', 'woo-calculate-your-price' ); ?>"></td>
				<td></td>
				<td></td>
				<?php 
				if ( $shipping_cost ) {
					?>
					<td></td>
					<?php
				}
				?>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td><span id="tbl-total-s" class="span-txt-center"></span></td>
				<td><span id="tbl-total-sc" class="span-txt"></span></td>
				<td><span id="tbl-total-sv" class="span-txt"></span></td>
				<td><span id="tbl-total-m" class="span-txt"></span></td>
			</tr>		
		</tfoot>
		<?php
	}

}


