<?php get_header(); 


if($_REQUEST['print']){ ?>

<style>
    @media print {
  header, footer, .rec.button, .rec.nav, .rec input, .rec .orderid {
    display: none;
  }

  .leadt form {
    display: block;
  }
 .rec{max-width: 150px;flex-wrap: wrap;font-size: 12px;height: 150px;max-height:150px;float:left;border:0.5 !important;border-left:0 !important;padding-left:2px;}
}
</style>
<script>
     window.focus();
  window.print();
</script>
<?php } ?>

	<div id="primary" class="content-area">
		<div id="content" class="show-leads">
	<div class="leadt">
	    <div class="rec nav"><a href="?st=0">New Order</a> - <a href="?st=1">Pending</a> - <a href="?st=2">Production Done</a> - <a href="?st=3">Sent</a></div>
	    <form action="" method="post">
	    
	    <?php
	        global $wpdb;
	        $showrec = true;
	       
	        if($_REQUEST['st'] != '') $and = ' AND astatus = '.$_REQUEST['st'];
	        
	        $postd = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'ajox_product' AND post_status = 'publish' ".$and." ORDER BY astatus ASC");

            foreach ($postd as $data): $pi = 0; ?>
                <div class="rec">
                <span class="orderid"><input type="checkbox" name="orderid[]" value="<?php echo $data->ID ;?>" /></span>
                <span class="orderid"><?php echo $data->ID ;?></span>
           
           <span class="photog"><?php
            $aphotos = unserialize($data->aphotos);
            foreach($aphotos as $p){
                if(++$pi > 4) break;
                echo '<img class="thumb" src="/CRM/lead_files/'.$p.'" />';
                
            }
            ?></span>
            <span><?php echo $data->full_name ;?></span>
            <span><?php echo $data->phone ;?></span>
            <span><?php echo $data->postcode ;?></span>
            <span><?php echo $data->amount ;?> (<?php echo $data->payment_type ;?>)</span>
            <span><?php echo date('d M Y', strtotime($data->post_date)); ?></span>
            
            </div>
            
            <?php $showrec = false;
            endforeach;    
	    
	    if($showrec) echo '<div class="rec">No record found.</div>';
	    
	    ?>
	    
	    <div class="rec button"><button name="print" value="mp">Mark as done and print</button>  <button name="print" value="p">Print only</button></div>

        </form>
		</div>
	</div>

<?php 

    if($_REQUEST['print']){
        
        global $wpdb;
        foreach($_REQUEST['orderid'] as $id){
            $wpdb->query($wpdb->prepare("UPDATE $wpdb->posts SET astatus=2 WHERE ID=$id"));
        }
    
    }

?>
<?php get_footer(); ?>