<?php
	declare(strict_types=1);
	/**
	 * SignNow Api
	 * Sandbox PHP SDK script
	 */
	
	require dirname(__DIR__) . '/vendor/autoload.php';
	
	/*const HOST = 'https://api-eval.signnow.com';
	const BASIC_TOKEN = 'ZDFhMDI1MGM2MzM0YzAxMmQzODVkOTY2ODdhZWEzMmQ6ZmYxZTBkMTA0YjQ2NmZiNWQyNzZiMGZhNWZkNTJkMTA=';*/
	
	const HOST = 'https://api.signnow.com';
	const BASIC_TOKEN = 'MzI3MWQ2Y2ZhYTkwY2M3ODAxMjQ1ZTFmYzJhN2Y1YmI6NzA0MDFhYjc5Nzg2NmI0NzcyNDYzZWJkODU0OWY3MGQ=';
	const USER = 'info@yplmedia.com';
	const PASSWORD = 'SignNow2192';
	
	use SignNow\Api\Action\OAuth as SignNowOAuth;
	use SignNow\Api\Entity\Document\Document;
	use SignNow\Api\Entity\Document\Field\SignatureField;
	use SignNow\Api\Entity\Document\Field\TextField;
	use SignNow\Api\Entity\Document\Upload as DocumentUpload;
	use SignNow\Api\Entity\Invite\Invite;
	use SignNow\Api\Entity\Invite\Recipient;
	use SignNow\Rest\Http\Request;

// use \ReflectionException;
// use JMS\Serializer\SerializerBuilder;
// use SignNow\Api\Entity\FreeForm\Invite;
// use SignNow\Api\Entity\FreeForm\InviteResponse;

// freedom invite
// use JMS\Serializer\SerializerBuilder;
// use ReflectionException;
// use SignNow\Api\Entity\FreeForm\Invite;
// use SignNow\Api\Entity\FreeForm\InviteResponse;

// Pure invite
	
	$auth = new SignNowOAuth(HOST);
	
	$entityManager = $auth->bearerByPassword(BASIC_TOKEN, USER, PASSWORD);
	$entityManager->setUpdateHttpMethod(Request::METHOD_PUT);
	$uploadFile = (new DocumentUpload(new \SplFileInfo('BUSINESS INFORMATION.pdf')));
	$documentEntity = $entityManager->create($uploadFile);
	/*echo 'Uploaded the document: ', $documentEntity->getId(), PHP_EOL, PHP_EOL;*/
	usleep(500);
	$document = (new Document())
		->setId($documentEntity->getId());
	sleep(2);
	$order = isset($_GET['order']) ? $_GET['order'] : 1;
	$roleId = isset($_GET['roleId']) ? $_GET['roleId'] : "";
	$role = isset($_GET['role']) ? $_GET['role'] : "signer";
	$email = isset($_GET['email']) ? $_GET['email'] : "";
	$fax = isset($_GET['fax']) ? $_GET['fax'] : "";
	$BusinessName = isset($_GET['BusinessName']) ? $_GET['BusinessName'] : "";
	$Address = isset($_GET['Address']) ? $_GET['Address'] : "";
	$city = isset($_GET['city']) ? $_GET['city'] : "";
	$state = isset($_GET['state']) ? $_GET['state'] : "";
	$zip = isset($_GET['zip']) ? $_GET['zip'] : "";
	$phone = isset($_GET['phone']) ? $_GET['phone'] : "";
	$website = isset($_GET['website']) ? $_GET['website'] : "";
	$transaction = isset($_GET['transaction']) ? $_GET['transaction'] : "";
	$delete_en = isset($_GET['delete']) ? $_GET['delete'] : false;
	$cur_date = getdate();
	$date_string = $cur_date['mon'] . "/" . $cur_date['mday'] . "/" . $cur_date['year'];
	/****************Prefill *****************/
	$signature = (new SignatureField())
		->setName('Please sign here')
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(true)
		->setHeight(15)
		->setWidth(150)
		->setX(325)
		->setY(438);
	$text_transaction = (new TextField())
		->setName('text_transaction')
		->setLabel('Cannot edit')
		->setPrefilledText($transaction)
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(11)
		->setWidth(100)
		->setX(280)
		->setY(92);
	$text_return_business = (new TextField())
		->setName('text_return_business')
		->setLabel('Business')
		->setPrefilledText($BusinessName)
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(15)
		->setWidth(170)
		->setX(15)
		->setY(132);
	$text_return_address = (new TextField())
		->setName('text_return_address')
		->setLabel('address')
		->setPrefilledText($Address)
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(23)
		->setWidth(170)
		->setX(15)
		->setY(155);
	$text_return_city = (new TextField())
		->setName('text_return_city')
		->setLabel('city')
		->setPrefilledText($city . " " . $state . " " . $zip)
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(15)
		->setWidth(170)
		->setX(15)
		->setY(179);
	$text_verify_BusinessName = (new TextField())
		->setName('text_verify_BusinessName')
		->setLabel('business')
		->setPrefilledText($BusinessName)
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(15)
		->setWidth(170)
		->setX(15)
		->setY(415);
	$text_verify_Address = (new TextField())
		->setName('text_verify_Address')
		->setLabel('address')
		->setPrefilledText($Address)
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(23)
		->setWidth(170)
		->setX(15)
		->setY(439);
	$text_verify_city = (new TextField())
		->setName('text_verify_city')
		->setLabel('city')
		->setPrefilledText($city . " " . $state . " " . $zip)
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(15)
		->setWidth(170)
		->setX(15)
		->setY(462);
	$text_phone = (new TextField())
		->setName('text_phone')
		->setLabel('phone')
		->setPrefilledText('')
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(15)
		->setWidth(170)
		->setX(15)
		->setY(526);
	
	$text_print_name = (new TextField())
		->setName('text_print_name')
		->setLabel('Full Name')
		->setPrefilledText('')
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(true)
		->setHeight(15)
		->setWidth(140)
		->setX(360)
		->setY(470);
	$text_date = (new TextField())
		->setName('text_date')
		->setLabel('date')
		->setPrefilledText($date_string)
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(15)
		->setWidth(93)
		->setX(331)
		->setY(488);
	$text_business = (new TextField())
		->setName('text_business')
		->setLabel('Type Business Category')
		->setPrefilledText('')
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(true)
		->setHeight(11)
		->setWidth(175)
		->setX(105)
		->setY(566);
	$text_website = (new TextField())
		->setName('text_website')
		->setLabel('website')
		->setPrefilledText($website)
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(11)
		->setWidth(222)
		->setX(60)
		->setY(585);
	
	$text_email = (new TextField())
		->setName('text_email')
		->setLabel('email')
		->setPrefilledText($email)
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(11)
		->setWidth(225)
		->setX(52)
		->setY(606);
	$text_tollfree = (new TextField())
		->setName('text_tollfree')
		->setLabel('Toll-free Number')
		->setPrefilledText('')
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(11)
		->setWidth(220)
		->setX(63)
		->setY(626);
	$text_spec_1 = (new TextField())
		->setName('text_spec_1')
		->setLabel(' ')
		->setPrefilledText('')
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(15)
		->setWidth(272)
		->setX(306)
		->setY(542);
	$text_spec_2 = (new TextField())
		->setName('text_spec_2')
		->setLabel(' ')
		->setPrefilledText('')
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(15)
		->setWidth(272)
		->setX(306)
		->setY(565);
	$text_spec_3 = (new TextField())
		->setName('text_spec_3')
		->setLabel(' ')
		->setPrefilledText('')
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(15)
		->setWidth(272)
		->setX(306)
		->setY(588);
	$text_spec_4 = (new TextField())
		->setName('text_spec_4')
		->setLabel(' ')
		->setPrefilledText('')
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(false)
		->setHeight(15)
		->setWidth(272)
		->setX(306)
		->setY(611);
	$title = ( new TextField())
		->setName('client_title')
		->setPageNumber(0)
		->setRole('signer')
		->setRequired(true)
		->setHeight(15)
		->setWidth(91)
		->setX(488)
		->setY(440);
	$document->setFields([
		$signature,
		$text_transaction,
		$text_return_business,
		$text_return_address,
		$text_return_city,
		$text_verify_Address,
		$text_verify_BusinessName,
		$text_verify_city,
		$text_business,
		$text_website,
		$text_email,
		$text_tollfree,
		$text_spec_1,
		$text_spec_2,
		$text_spec_3,
		$text_spec_4,
		$text_print_name,
		$text_date,
		$title,
	]);
	sleep(2);
	$documentId = $document->getId();
	
	//update document with prefilled data
	sleep(2);
	$entityManager->update($document);
	sleep(2);
	$to[] = new Recipient($email, 'signer', $roleId, $order);
	
	$cc = [];
	sleep(2);
	$fieldInvite = new Invite("info@yplmedia.com", $to, $cc);
	sleep(2);
	$result = $entityManager->create($fieldInvite, ['documentId' => $documentId]);
	sleep(2);
	if ($delete_en)
		$entityManager->delete($document, ['id' => $documentId]);
	if ($result->getStatus() == "success")
		echo "The Revision Form has been Sent";
	else
		echo "The Revision Form has been not Sent";

?>