<?php $this->include('rs/utils.php')?>
<?= $this->extend('rs/template-user'); ?>
<?= $this->section('content'); ?>
<style>
.done{color:#000;padding:2px 7px;margin:5px;background:#4CFF00;border:1px solid #000;display:inline-block;border-radius:13px;}
.todo{color:#000;padding:2px 7px;margin:5px;background:#7FC9FF;border:1px solid #000;display:inline-block;border-radius:13px;}
.notdone{color:#000;padding:2px 7px;margin:5px;background:#ccc;border:1px solid #000;display:inline-block;border-radius:13px;}

</style>
  
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>

	  <div class="card shadow mb-4">
		<div class="card-body small font-weight-bold" style="min-height:100px;background:#E5EEFF;color:#000;">
			<h6 class="card-title">Transaction <?=$transaction['trans_code']?></h6>
			Start date: <?=$transaction['created_at']?><br>
			Satus curent: <?=$transaction['step_name']?><br>
			Car:<a class='ls-modal' href='/cars/cardetails/<?=$transaction['car_code']?>'><?=$transaction['car_code']?></a><br>
			Seller:<a  class='ls-modal' href='/user/viewuser/<?=$transaction['seller_id']?>'><?=$transaction['sell_name']?></a><br>
			Buyer:<a class='ls-modal' href='/user/viewuser/<?=$transaction['buyer_id']?>'><?=$transaction['buy_name']?></a>
			
		</div>
		
		<table style="width:1100px;color:#000"><tr><td style="width:600px">				
		<div class="card-body small p-1"  style="margin-top:10px">
			<?php //var_dump($transaction);?>
			
			<?php 
			$ctls=false;
			foreach ($transHist as $histItem)
			{
				if($histItem['created_at'] && $histItem['completed'])
				{	
					echo '<span class="done">'.$histItem['step_ord'].'</span><a href="javascript:showForm(\'frm_'.$histItem['history_id'].'\')"><b> '.$histItem['step_name'].'</b></a> [started'.' '.$histItem['created_at'].($histItem['completed']==1?', completed '.$histItem['completed_at']:'').']<br>';
					echo '<div class="card-body small d-none" id="frm_'.$histItem['history_id'].'">';
					$contentForm0= '<h6>'. $histItem['step_desc'].'</h6>';
					$contentForm0.= '<form method="post" enctype="multipart/form-data" action="/transactions/savestep"><input type="hidden" name="history_id" value="'.$histItem['history_id'].'"/><table cellpadding="2">';
					$ctls=false;
					if($histItem['chk1_label'])
					{
						$ctls=true;
						$contentForm0.= '<tr><td><label for="chk1">'.$histItem['chk1_label'].'</label></td><td><input type="checkbox" name="chk1" checked="'.($histItem['chk1']==1).'"/></td></tr>';
						}
					if($histItem['chk2_label'])
					{
						$ctls=true;
						$contentForm0.= '<tr><td><label for="chk2">'.$histItem['chk2_label'].'</label></td><td><input type="checkbox" name="chk2" checked="'.($histItem['chk2']==1).'"/></td></tr>';
						
					}
					
					if($histItem['chk3_label'])
					{
						$ctls=true;
						$contentForm0.= '<tr><td><label for="chk3">'.$histItem['chk3_label'].'</label></td><td><input type="checkbox" name="chk3" checked="'.($histItem['chk3']==1).'"/></td></tr>';
						
					}
					
					if($histItem['attach1_label'])
					{
						$ctls=true;
						$contentForm0.= '<tr><td><label for="attach1">'.$histItem['attach1_label'].'</label></td><td><a href="'.TRAN_VPATH.$histItem['attach_1'].'" target="_blank">'.$histItem['attach_1'].'</a></td></tr>';
						
					}
					
					if($histItem['attach2_label'])
					{
						$ctls=true;
						$contentForm0.= '<tr><td><label for="attach2">'.$histItem['attach2_label'].'</label></td><td><input type="file" name="attach2"></td></tr>';
						
					}
					
					if($histItem['attach3_label'])
					{
						$ctls=true;
						$contentForm0.= '<tr><td><label for="attach3">'.$histItem['attach3_label'].'</label></td><td><input type="file" name="attach3"></td></tr>';
						
					}
					$contentForm0.= '<tr><td><label>Notes</label></td><td><textarea  name="chk_notes">'.$histItem['chk_notes'].'</textarea></td></tr>';
							
					$contentForm0.= '</table>';
					if($ctls)
						$contentForm0.='<input type="submit" class="btn btn-primary" value="Save data">';
					
					$contentForm0.= '</form></div>';
					echo $contentForm0;
				
				}
				else
				{
					if($histItem['created_at'] && !$histItem['completed'])
					{
						echo '<span class="todo">'.$histItem['step_ord'].'</span><a class="bg-primary text-white" href="javascript:showForm(\'frm_'.$histItem['history_id'].'\')"><b> '.$histItem['step_name'].'</b></a> [started'.' '.$histItem['created_at'].($histItem['completed']==1?', completed '.$histItem['completed_at']:'').']<br>';
						echo '<div class="card-body small d-none" id="frm_'.$histItem['history_id'].'">';
						$contentForm= '<h6>'. $histItem['step_desc'].'</h6><br>';
						$contentForm.= '<form method="post" enctype="multipart/form-data" action="/transactions/savestep"><input type="hidden" name="history_id" value="'.$histItem['history_id'].'"/><table cellpadding="2">';
						$ctls=false;
						if($histItem['chk1_label'])
						{
							$ctls=true;
							$contentForm.= '<tr><td><label for="chk1">'.$histItem['chk1_label'].'</label></td><td><input required="true" type="checkbox" name="chk1"></td></tr>';
							}
						if($histItem['chk2_label'])
						{
							$ctls=true;
							$contentForm.= '<tr><td><label for="chk2">'.$histItem['chk2_label'].'</label></td><td><input required="true" type="checkbox" name="chk2"></td></tr>';
							
						}
						
						if($histItem['chk3_label'])
						{
							$ctls=true;
							$contentForm.= '<tr><td><label for="chk3">'.$histItem['chk3_label'].'</label></td><td><input required="true" type="checkbox" name="chk3"></td></tr>';
							
						}
						
						if($histItem['attach1_label'])
						{
							$ctls=true;
							$contentForm.= '<tr><td><label for="attach1">'.$histItem['attach1_label'].'</label></td><td><input required="true" type="file" name="attach1"></td></tr>';
							
						}
						
						if($histItem['attach2_label'])
						{
							$ctls=true;
							$contentForm.= '<tr><td><label for="attach2">'.$histItem['attach2_label'].'</label></td><td><input type="file" required="true" name="attach2"></td></tr>';
							
						}
						
						if($histItem['attach3_label'])
						{
							$ctls=true;
							$contentForm.= '<tr><td><label for="attach3">'.$histItem['attach3_label'].'</label></td><td><input type="file" required="true" name="attach3"></td></tr>';
							
						}
						
						$contentForm.= '<tr><td><label>Notes</label></td><td><textarea  name="chk_notes">'.$histItem['chk_notes'].'</textarea></td></tr>';
						
						$contentForm.= '</table>';
						if($ctls)
							$contentForm.='<br><input type="submit" class="btn btn-primary" value="Save data">';
						$contentForm.= '</form></div>';
						
						echo $contentForm;
					}
					else
						echo '<span class="notdone">'.$histItem['step_ord'].'</span><b style="color:#999">'.$histItem['step_name'].'</b><br>';
				}
			}
			
			//var_dump($transHist);
			
			?>
		</div>
		</td>
		<td>
			<div class="card-body small border border-dark h-100 w-100 pt-4 px-3" style="margin-top:-80px" id="form_content">
				<?php echo $contentForm; ?>
			</div>
		</td>
		</tr>
		</table>
		
	</div>
	
	<div id="myModal" class="modal fade">
		<div class="modal-dialog">
			<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					</div>
					<div class="modal-body">
						<p>Loading...</p>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
					</div>
			</div>
		</div>
	</div>

	
	<script>
		function showForm(formid)
		{
			document.getElementById("form_content").innerHTML=document.getElementById(formid).innerHTML;
		}

		$('.ls-modal').on('click', function(e){
		  e.preventDefault();
		  $('#myModal').modal('show').find('.modal-body').load($(this).attr('href'));
		  $('#myModal').on('hide.bs.modal', function () { 
			console.log('Fired at start of hide event!')
			$('.modal-body').html('');
		});  
		});
	</script>
			
		<!-- /.container-fluid -->

		
		<!-- End of Main Content -->

		<!-- Footer -->
		

<?= $this->endsection(); ?>