<?php

// To keep the code as clean as possible, separation of concerns and all that,
//  put the main logic for this application in a file separate from the HTML

	$value = $_GET['form'];
	$show_form = filter_var($value, FILTER_VALIDATE_BOOLEAN);
	$client_id = $_GET['cid'];
	$user_id = $_GET['uid'];
	if ($show_form)
	{
		require_once 'lib/save-signature.php';
	} 
	else
	{
		require_once 'lib/get-signature.php';
	}
	
?>
<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <title>Signature of Client</title>
  <link rel="stylesheet" href="signature-pad/build/jquery.signaturepad.css">
  <!--[if lt IE 9]><script src="signature-pad/build/flashcanvas.js"></script><![endif]-->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
  <?php
    // A small trigger to flip between showing the signature form and the regeneration screen
    if ($show_form) {
      require_once 'views/accept.php';
    } else {
      require_once 'views/regenerate.php';
    }
  ?>
  <script src="signature-pad/build/jquery.signaturepad.min.js"></script>
  <script type='text/javascript' src="https://github.com/niklasvh/html2canvas/releases/download/0.4.1/html2canvas.js"></script>
  <?php
    // Another trigger to write the appropriate Javascript to the page:
    //  If we are showing the form, write the initialization code
    //  If we are showing the final signature, write the regeneration code
    if ($show_form) :
  ?>
  <script>
    $(document).ready(function () {
      $('.sigPad').signaturePad({drawOnly : true});
    });
  </script>
  <?php else : ?>
  <script>
    $(document).ready(function () {
	  // Write out the complete signature from the database to Javascript
      var sig = <?php echo $output; ?>;
      $('.sigPad').signaturePad({displayOnly : true}).regenerate(sig);  
      var mycanvas = document.getElementById("sspad"); //get your canvas
      var image = mycanvas.toDataURL("image/png"); //Convert
      window.parent.jQuery('#dialogShowInvoice').data('param', image).dialog('close');
    });
  </script>
  <?php endif; ?>
  <script src="signature-pad/build/json2.min.js"></script>
</body>
