<?php 

function edit_invoice_cb() {
	$invoice = new pm_invoice();
	$invoice->get_invoice_by_id($_GET['id']);
	$user_meta = get_user_meta($invoice->invoice->user_id);
	?>
	<div class="row">
		<div class="col-md-12">
			<div class="card">
			    <div class="card-header">
			        <strong class="card-title">Details of Invoice</strong>
			    </div>
			    <div class="card-body pt-4">
			      	<form id="create-invoice-form" method="post" action="contents/plugins/payment_module/ajax.php">
			      		<input type="hidden" name="id" value="<?php echo $invoice->invoice->id ?>">
			      			<div class="row">
			      				<div class="col-md-6">
			      					<address>
							            <strong><?php echo get_fullname($user_meta, true); ?></strong><br>
							            <?php echo str_replace(",", "</br>", $user_meta['address']); ?></br>
							            Phone: <?php echo $user_meta['phone'] ?><br>
							            Email: <?php echo $user_meta['email'] ?>
							          </address>
			      				</div>
			      				<div class="col-md-6"><?php input(['label' => 'Due Date', 'id' => 'due_date', 'type' => 'date', 'required' => 'required', 'value' => date("Y-m-d", $invoice->invoice->due_date)]); ?></div>
			      			</div>
			      			<div class="row">
			      				<div class="col-md-12">
			      					<div class="table-responsive">
					      				<table class="table" id="items-table">
						      				<thead>
						      					<tr>
						      						<th style="max-width: 20px;">QTY</th>
						      						<th>DESCRIPTION</th>
						      						<th>UNIT PRICE</th>
						      						<th>QTY PRICE</th>
						      						<th>REFERENCE</th>
						      						<th>SUCCESS METHOD</th>
						      						<th>ERROR METHOD</th>
						      						<th>ACTION</th>
						      					</tr>
						      				</thead>
						      				<tbody>
						      					<?php if (_count($invoice->items)): ?>
									              <?php foreach ($invoice->items as $item): ?>
									                <tr id="<?php echo $item['ref'] ?>">
									                	<td>
									                		<?php echo $item['quantity'] ?>
									                		<input name="items[qty][]" value="<?php echo $item['quantity'] ?>" style="display: none;">
									                	</td>
									                	<td>
									                		<?php echo $item['narration'] ?>
									                		<input name="items[description][]" value="<?php echo $item['narration'] ?>" style="display: none;">
									                	</td>
									                	<td>
									                		<?php echo toMoney($item['amount']) ?>
									                		<input name="items[unit_price][]" value="<?php echo $item['amount'] ?>" style="display: none;">
									                	</td>
									                	<td><?php echo toMoney($item['amount'] * $item['quantity']) ?></td>
									                	<td>
									                		<?php echo $item['ref'] ?>
									                		<input name="items[ref][]" value="<?php echo $item['ref'] ?>" style="display: none;">
									                	</td>
									                	<td>
									                		<?php echo $item['success_method'] ?>
									                		<input name="items[success_method][]" value="<?php echo $item['success_method'] ?>" style="display: none;">
									                	</td>
									                	<td>
									                		<?php echo $item['error_method'] ?>
									                		<input name="items[error_method][]" value="<?php echo $item['error_method'] ?>" style="display: none;">
									                	</td>
									                	<td><a href="javaScript:void(0)" data-id="<?php echo $item['ref'] ?>" class="edit-item btn btn-secondary btn-sm btn-block">Edit</a><br/><a href="javaScript:void(0)" data-id="<?php echo $item['ref'] ?>" class="remove-item btn btn-secondary btn-sm btn-block">Remove</a></td>
									                </tr>
									              <?php endforeach ?>
									            <?php endif ?>
						      				</tbody>
						      				<tfoot>
						      					<tr>
						      						<td><input id="new-qty" style="max-width: 130px; padding: 3px 3px;" type="text" name="new[qty]" class="form-control numbers-only"></td>
						      						<td><textarea id="new-description" name="new[description]" class="form-control"></textarea></td>
						      						<td><input id="new-unit_price" type="text" name="new[unit_price]" class="form-control numbers-only"></td>
						      						<td id="new-qty_price"></td>
						      						<td><input id="new-ref" type="text" name="new[ref]" class="form-control"></td>
						      						<td><input id="new-success_method" type="text" name="new[success_method]" class="form-control"></td>
						      						<td><input id="new-error_method" type="text" name="new[error_method]" class="form-control"></td>
						      						<td><button class="btn btn-primary" id="add-btn" disabled>Add</button></td>
						      					</tr>
						      				</tfoot>
						      			</table>
					      			</div>
					      			<?php
					      			alert(); submit_btn("edit_invoice", "Submit"); 
					      		?>
			      				</div>
			      			</div>
			      			
			      	</form>
			    </div>
			</div>
		</div>
	</div>
	<?php
	global $post_meta;
	add_page_javascript('app/elements/form.js');
	add_page_javascript('app/assets/js/page-scripts/js-functions.js');
	add_page_javascript('app/assets/js/page-scripts/submit.js');
	add_page_javascript('app/assets/js/page-scripts/koweb.js');
	add_page_javascript('contents/plugins/payment_module/js/edit-invoice.js');
}