<?php
$this_file = str_replace( '\\', '/', __FILE__ );
$this_file = explode( 'wp-content', $this_file );
$this_file = $this_file[ 0 ];

// required for making tables easier
require_once( $this_file . 'wp-config.php' );

// alternative
//require_once( '../../../wp-config.php' );
?>
<?php get_header(); ?>
<?php 
$aOptions = SofaShoppr::initOptions( false );
$entrycurrency = $aOptions[ 'entrycurrency' ];
$taxpercentage = $aOptions[ 'taxpercentage' ];
if( $taxpercentage < 0 ) $taxpercentage = 0;
$shipinlandcost = $aOptions[ 'shipinlandcost' ];
if( $shipinlandcost < 0 ) $shipinlandcost = 0;

$entrycurrency = $aOptions[ 'entrycurrency' ];
global $current_user;
get_currentuserinfo();
?>

<?php if( $current_user->ID == 1 ) { ?>

<div id="content" class="fullwidth">
    
    <div class="article">

        <h1><?php _e( "Stock Inventory", "sofa_shoppr" ); ?></h1>
        
        <?php
		$global_counter = 1;
		
		$temp_store_query = $wp_query;
		$wp_query = NULL;
		$quer_y = 'post_type=post';
		$wp_query = new WP_Query( $quer_y );
		
		if( $wp_query->have_posts() ) : 
		
		echo '<p style="padding-bottom: 10px; overflow: hidden; ">';
        echo '<span style="display: block; float: left; font-weight: bold; width: 80px; padding-top: 8px;">' . __( "SEARCH", "sofa_shoppr" ) . '</span>';
		echo '<input type="text" id="txtSearch" name="txtSearch" maxlength="50" style="display: block; float: left;" />&nbsp;';
        echo '<span id="imgSearch" style="display: inline; margin-left: 10px; font-size: 120%; font-weight: bold; color: red; width: 20px; cursor: default;">X</span>';
		echo '</p>';

		echo '<table id="overvew">';
		
		echo '<tr>';
		echo '<th style="width: 20%;">' . __( "Product ID", "sofa_shoppr" ) . '</th>';
		echo '<th style="width: 75%;">' . __( "Product Name", "sofa_shoppr" ) . '</th>';
		echo '<th style="width: 5%;"><a href="#" id="expander">+++</a></th>';
		echo '</tr>';
		
		while( $wp_query->have_posts() ) : $wp_query->the_post();
		
		$custom_fields = get_post_custom( intval( $name ) );
		$item_size  = ( $custom_fields[ 'size' ][ 0 ] ) ? $custom_fields[ 'size' ][ 0 ] : 0;
		$item_stock = ( $custom_fields[ 'stock' ][ 0 ] ) ? $custom_fields[ 'stock' ][ 0 ] : 0;
		$item_price = ( $custom_fields[ 'price' ][ 0 ] ) ? $custom_fields[ 'price' ][ 0 ] : 0;
				
		if( $global_counter % 2 == 0 ) echo '<tr class="bggray">';
		else echo '<tr class="bgsemiwhite">';
				
		$prod_perma = get_permalink( $post->ID );
		$prod_title = stripslashes( strip_tags( $post->post_title ) );
		echo '<td style="width: 20%;"><a href="' . $prod_perma . '">#' . $post->ID . '</a></td>';
		echo '<td colspan="2" style="width: 80%; font-size: 150%;"><a href="#" id="showbasket_' . $global_counter . '">' . $prod_title . '</a></td>';

		echo '</tr>';
				
		// meta size and info
		echo '<tr class="bgwhite meta" id="metabasket_' . $global_counter . '">';
		
		echo '<td colspan="2">';
		
			echo '<table style="width: 100%; font-size: 75%;">';
			echo '<tr id="notme_' . $post->ID . '">';
			echo '<th style="width: 60%;">' . __( "Product Sizes", "sofa_shoppr" ) . ' [ <span style="text-transform: none;">' . $prod_title . '</span> ]</th>';
			echo '<th style="width: 20%; text-align: center;">' . __( "In Stock", "sofa_shoppr" ) . '</th>';
			echo '<th style="width: 20%; text-align: right;">' . __( "Manage Stock", "sofa_shoppr" ) . '</th>';
			echo '</tr>';
			
			// get all sizes and stock
			$item_sizes_array = explode( "\n", $item_size );
			$item_stock_array = explode( "\n", $item_stock );
			$item_price_array = explode( "\n", $item_price );
			$zzz = 0;
			$a_l = count( $item_sizes_array );
			while( $zzz < $a_l ) {
				echo '<tr id="notme_' . $zzz . '">';
				echo '<td style="width: 60%;">' . $item_sizes_array[ $zzz ] . '</td>';
				if( $item_stock_array[ $zzz ] <= 0 ) echo '<td style="width: 20%; background-color: red; color: white; text-align: center;">' . $item_stock_array[ $zzz ] . '</td>';
				else echo '<td style="width: 20%; text-align: center;">' . $item_stock_array[ $zzz ] . '</td>';
				$edit_link = get_bloginfo( 'template_directory' ) . '/stock_item_screen.php?itemid=' . $post->ID . '&price=' . urlencode( $item_price_array[ $zzz ] ) . '&size=' . urlencode( $item_sizes_array[ $zzz ] ) . '&stock=' . $item_stock_array[ $zzz ] . '&arrpos=' . $zzz;
				echo '<td style="width: 20%; text-align: right;"><a href="' . $edit_link . '" class="nyroModal">' . __( "MANAGE", "sofa_shoppr" ) . '</a></td>';
				echo '</tr>';
				$zzz ++;
			}
			
			// end coding
			
			echo '</table>';
		
		echo '</td>';
		
		echo '</tr>';
		
		$global_counter ++;
		
		endwhile;
		
		echo '</table>';
		
		?>
        
		<script language="javascript" type="text/javascript">
		
        var all_up = 1;
		
		jQuery.expr[":"].containsNoCase = function(el, i, m) {
            var search = m[3];
            if (!search) return false;
            return eval("/" + search + "/i").test(jQuery(el).text());
        };
		
		jQuery( document ).ready( function() {
			
			jQuery('#overvew tr').show(); // expand all by default
			
			jQuery( "#overvew td a" ).click( function( event ) {
				var objclicked = event.target.id;
				var prefix_array = objclicked.split( "_" );
				var trgt = "";
				
				if( prefix_array[ 0 ] == "showbasket" ) {
					trgt = "#metabasket_" + prefix_array[ 1 ];
					jQuery( trgt ).toggle();
				}
				
				return false;
			} );
			
			jQuery( "#expander" ).click( function( event ) {
				if( !all_up ) {
					jQuery('#overvew tr').show();
					all_up = 1;
				} else {
					jQuery('#overvew tr[id^=metabasket_]').hide();
					all_up = 0;
				}
				return false;
			} );
			
			// table search
            jQuery('#imgSearch').hide();

            jQuery('#imgSearch').click(function() {
                resetSearch();
            });

            jQuery('#txtSearch').keyup(function(event) {
                if (event.keyCode == 27) {
                    resetSearch();
                }
            });

            // execute the search
            jQuery('#txtSearch').keyup(function() {
                // only search when there are 3 or more characters in the textbox
                if (jQuery('#txtSearch').val().length > 2) {
                    jQuery('#overvew tr').not('tr[id^=notme_]').hide();
                    jQuery('#overvew tr:first').show();
                    jQuery('#overvew tr td:containsNoCase(\'' + jQuery('#txtSearch').val() + '\')').parent().show();
                    jQuery('#imgSearch').show();
                }
                else if (jQuery('#txtSearch').val().length == 0) {
                    resetSearch();
                }

                // if there were no matching rows, tell the user
                if (jQuery('#overvew tr:visible').length == 1) {
                    jQuery('.norecords').remove();
                    jQuery('#overvew').append("<tr class='norecords'><td colspan='2'>No records were found.</td></tr>");
                }
            });
			
		} );

        function resetSearch() {
            jQuery('#txtSearch').val('');
            jQuery('#overvew tr').show();
			jQuery('#overvew tr[id^=metabasket_]').hide();
            jQuery('.norecords').remove();
            jQuery('#imgSearch').hide();
            jQuery('#txtSearch').focus();
        }

        </script>

		<?php
		
		else :
		
		echo '<p>' . __( "No inventory found!", "sofa_shoppr" ) . '</p>';
		
		endif;
		
		$wp_query = NULL; 
		$wp_query = $temp_store_query;
		
		?>

    </div>

</div>

<?php } else { ?>
<div id="content" class="fullwidth">
    
    <div class="article">

        <h1><?php _e( "Ups!", "sofa_shoppr" ); ?></h1>
        <p><?php _e( "You don't have a permission to view this page!", "sofa_shoppr" ); ?></p>
        
    </div>

</div>
<?php } ?>

<?php get_footer(); ?>