<style>
    td { padding:7px !important; }
</style>
<div class="form form-profile" style='width:100%'>

   <form action="" method="post">

	   <table class="admintable" id="projects_table" style="width: 95%">
			<tr>
				<th style='width:35px;'>&nbsp;</th>
				<th>OrderID</th>
				<th>OrderID</th>
				<th>Pmt Amt</th>
				<th>Status</th>
				<th>Fulfilled?</th>
				<th>Date
				<!--<FORM>
					<INPUT TYPE="button" onClick="history.go(0)" VALUE="Ref">
				</FORM>-->
				</th>
				<th>Email</th>
				<th>&nbsp;</th>
				<th style="display:none;">OrderID</th>
			</tr>

			<?php
			foreach ($orders as $order) {
				if ($order['payment_status'] & FLAG_FULFILLED) {
					$fulfilled = 'checked="checked"';
					$oscolor = '#38BC00';
				} else {
					$fulfilled = '';
					$oscolor = '#ff0000';
				}	
				if($order['payment_status'] == 4){
						$status = 'Fulfilled';
				} else {
						$status = 'Unfulfilled';
				}
                if($order['paid'] == 'paid'){
                    $pdstyle = 'background: #00f900;'; 
                } else {
                    $pdstyle = 'background:none;';
                }
				//$total 		= "$".$order['payment_amount'];		
                $total = '$' . $order['total'];
                //$total       = "$".$order['ordertotal']; 	
				$orderdate	= date( 'm-d-Y', strtotime($order['createdtime']));
				if($order['email'] !=''){
					$custemail = $order['email'];
				} else {
					$custemail = $order['guestemail'];	
				}
				echo
				"<tr style='border-top:5px solid #eee;'>
                    <td colspan='9' style='font-weight:300;'><span style='color:#1f8020'><i class='far fa-money-bill-alt'></i></span> NEW ORDER!</td>
                 </tr>				 
				 <tr>
					<td style='text-align:center; max-width:150px; overflow:hidden; line-height:22px; text-wrap:none'>&nbsp;</td>
					<td style='text-align:left; color:#aaa'><strong>Order ID:</strong> {$order['orderid']}</td>
					<td style='text-align:center; font-weight:bold; {$pdstyle}'>{$order['paid']}</td>
					<td style='text-align:center; font-weight:800; color:#1f8020; background:#d6f1cd; text-shadow:0px 1px 0px rgba(255,255,255,.4)'>{$total}</td>
					<td style='text-align:center; width:90px; font-weight:300; color:{$oscolor}' id='ostatus-{$order['orderid']}'>{$status}</td>
					<td style='text-align:center'><input flag='" . FLAG_FULFILLED . "' type='checkbox' name='fulfilled' class='fulfilled' $fulfilled id='{$order['orderid']}' /></td>                
					<td style='text-align:center'>{$orderdate}</td>
					<td style='text-align:center'>
					{$custemail}
					</td>
					<td style='text-align:center;'>
					   &nbsp;
					</td>
					</td>
					<td style='text-align:center; display:none;'><a href='/orders/{$order['id']}/edit' class='confirm'>edit</a> | <a href='?delete={$order['id']}' class='confirm'>delete</a></td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td colspan='8' style='background:#f8f8f8; text-shadow:0px 1px 0px #fff;'>
					     <span style='font-size:10px; font-weight:300;'>SHIPPING INFO:</span><br />
						<i class='fas fa-truck'></i>&nbsp; <strong>CUSTOMER:</strong> {$order['fullname']} <strong>&nbsp;&bull;&nbsp;</strong>
						&nbsp;<strong>ADDRESS:</strong> {$order['address1']} <strong>&nbsp;&bull;&nbsp;</strong> <span style='color:#38BC00'>{$order['address2']}</span> <strong>&nbsp;&bull;&nbsp;</strong> {$order['city']}, {$order['state']} {$order['zip']} <strong>&nbsp;&bull;&nbsp;</strong> <strong>DELIVERY TYPE:</strong> {$order['delivtype']} 
					</td>
				</tr>";
				
				echo "<tr><td>&nbsp;</td>
    				<td colspan='8' style='color:#1c6078; font-size:11px; border-bottom:1px solid #aaa; font-weight:300;'>
    				    <i class='fas fa-shopping-basket'></i>&nbsp; P R O D U C T S  &nbsp; O R D E R E D :
    				</td>
				</tr>";
				// Start of Order Items
				foreach ($order['items'] as $item) {
					$itemamt = '$' . $item['payment_amount'];
					// Statements
					echo "<tr>
						<td>&nbsp;</td>
						<td>{$item['itemid']}</td>
						<td style='text-align:center'>{$item['orderid']}</td>
						<td style='text-align:center'>{$itemamt} x {$item['creditAmount']}</td>
						<td colspan='5' style='color:#000;'>
						  <span style='color:#ccc;'>Options:</span> {$item['options']} &bull; 
						  <span style='color:#ccc;'>Add-Ons:</span> {$item['addons']} &bull; 
						  <span style='color:#ccc;'>Comments:</span> {$item['comments']}
						 </td>
					</tr>";
				}
				echo "<tr style='border-top:1px solid #000;'>
                                <td colspan='9'>&nbsp;</td>
                    </tr>";
			}
			?>

		</table>
		<br /><br /><br />
		<!--<input type="submit" value="Submit?" /> &nbsp;-->
	</form>

</div>
<div class="clear;"></div>
								
<script>
$('.fulfilled').change(function () {	
	var ischecked = $(this).is(':checked');
	var oid = $(this).attr('id');
    
	if(!ischecked){
		$.ajax({
			url: '?setFulfilled',
			type: 'post',
			data: {
				id: oid,
				flag: ($(this).attr('flag')),
				fulfilled: ($(this).attr('checked') == '')
			},
			success: function (d) {
				$('#ostatus-' + oid).html('<span style="color:#ff0000;">Unfulfilled</span>');
				if (d != 'ok') {
					alert("Unable to update fulfillment status");
				} else {
					//alert(d + ' if and id is:' + oid + 'and ostatus is #ostatus-' + oid);
				}
			}
		})			
	} else {
		$.ajax({
			url: '?setFulfilled',
			type: 'post',
			data: {
				//id: $(this).attr('id'),
				id: oid,
				flag: ($(this).attr('flag')),
				fulfilled: ($(this).attr('checked') == 'checked')
			},
			success: function (d) {
				$('#ostatus-' + oid).html('<span style="color:#38BC00;">Fulfilled</span>');
				if (d != 'ok') {
					alert("Unable to update fulfillment status");
				} else {
					//alert(d + ' else and id is:' + oid + 'and ostatus is #ostatus-' + oid);
				}
			}
		})		
	}
});
</script>								