<?php
echo DapMemberShowLoader();
$permalink1 = get_site_url();
$permalink2 = get_home_url();
if ($permalink1 == $permalink2) {
    $permalink = $permalink1;
} else {
    $permalink = $permalink2;
}
$find = array('http://', 'https://');
$replace = 'https://';
$siteurl = str_replace($find, $replace, $permalink);
$allMemberPages_obj = DAP_MemberPages::loadByAction('invoice');
$page_id_already_used = array();
if(count($allMemberPages_obj)){
	foreach($allMemberPages_obj as $allMemberPage_obj){
		$page_id_already_used[] = $allMemberPage_obj->getPageId();
	}
}

if(isset($_GET['id'])){ 
	$id = $_GET['id'];

	$data = DAP_MemberPages::loadById($id);

	if(isset($data)){
		$invoicetemplate = $template = $data->getTemplate();	
	}	

	$adminemail = DAP_MemberPagesValues::loadByActionIdAndName($id,'admin_email');
	if(isset($adminemail)){
		$adminemail =  $adminemail->getValue();	
	}
	
	$page_template = DAP_MemberPagesValues::loadByActionIdAndName($id,'page_template');
	if(isset($page_template)){
		$page_template =  $page_template->getValue();	
	}
	
	$showinvoice = DAP_MemberPagesValues::loadByActionIdAndName($id,'view_invoice_link');
	if(isset($showinvoice)){
		$showinvoice =  $showinvoice->getValue();	
	}
	
	$companyname = DAP_MemberPagesValues::loadByActionIdAndName($id,'company_name');
	if(isset($companyname)){
		$companyname =  $companyname->getValue();	
	}
	
	$companyurl = DAP_MemberPagesValues::loadByActionIdAndName($id,'company_url');
	if(isset($companyurl)){
		$companyurl =  $companyurl->getValue();	
	}  
	
	$no_transaction_msg = DAP_MemberPagesValues::loadByActionIdAndName($id,'no_transaction_msg');
	if(isset($no_transaction_msg)){
		$no_transaction_msg =  $no_transaction_msg->getValue();	
	} 
	  
	$logo = DAP_MemberPagesValues::loadByActionIdAndName($id,'logo');
	if(isset($logo)){
		$logo =  $logo->getValue();	
	}
	 
}	
 ?>


<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800&display=swap" rel="stylesheet">

<div id="dap_member_page_wrapper">
	<div class="dap_member_invoice_outer">
		<div class="master_heading d-flex align-items-center justify-content-between pb-3">
			<h4><i class="fa fa-indent mr-2"></i> Member Invoice Page</h4>
			<a href="<?= admin_url('admin.php?page=dap_member_pages'); ?>"class="btn btn-success theme-btn">Return to Member Pages</a>
		</div>
		
		<div class="inner_section" id="member_affiliate_outer">
					<input type="hidden" id="saved_id" value="<?= isset($_GET['id']) ? $_GET['id'] : '' ; ?>" >
					<input type="hidden" id="page_id" value="<?= isset($data) ? $data->getPageId() : '' ?>" >
					 <div class="form-group row form_group_inner">  		
					       <div class="col-sm-3 align-items-center">
							  <label> Create a New Invoice Page </label>
						   </div>
						   <div class="col-sm-5">
							  <div class="input-group">
								  <div class="input-group-prepend">
									<span class="input-group-text"><?php echo $siteurl; ?>/</span>
								  </div>
								<input type="text" name="create_invoice_page" id="create_invoice_page" class="form-control page_name" value="" placeholder="Enter Name" <?php if($data){ echo 'disabled=disbled';}?>>
							  </div>
							</div>
							<div class="col-sm-3">
								<button type="button"  id="invoice_page" data-id ="create_invoice_page" class="btn theme-btn btn-blue notransform check_availability_page_btn create_page_btn" <?php if($data){ echo 'disabled=disbled';}?>>Check Availability</button>									 
							</div>
							<div class="col-sm-12  mt-3">
								<div class="error-msgs text-center messages-div">
									<h5 class="alert alert-danger error_create_invoice_page error_msg" style="display:none">Sorry, This Name is already taken. Please Try with another one. </h5>
									<h5 class="alert alert-success success_create_invoice_page success_msg" style="display:none">Congrats! This Name is available</h5>
								</div>
							</div>
					   </div>
						
					<div class="form-group row" >  
						  <div class="col-sm-3"></div>
							<div class="col-sm-4">
								<div class="or">
								  <span>OR</span>
								</div>
							</div>
						</div>
					   <div class="form-group row form_group_inner">  
					  
						  <div class="col-sm-3 d-flex align-items-center">
							<label> Select an Existing Page for Invoice </label>
						  </div>
						  <div class="col-sm-4">
								<select class="form-control" id="invoice_page_url" name="invoice_page_url" <?php if($data){ echo 'disabled=disbled';}?>>
									<option value="">Select a URL</option>
									 <?php
										$page_ids = get_all_page_ids();
										foreach($page_ids as $page){
											$page_name = get_page( $page );
											$selected = '';
											if ($page_name->post_status == 'publish'){
												if(isset($data) && ($data->getPageId() == $page)){
													$selected = 'selected=selected';
												}else if(in_array($page,$page_id_already_used)){
													continue;
												}
												
												
											?>
												<option value='<?php echo $page; ?>' <?php echo $selected;?>>
												<?php echo get_permalink($page); ?>
												</option>
											<?php } 
								 
										}//close foreach loop ?>
							  </select>
						  </div>
					</div>
					<input type="hidden" id="invoice_page_id" name="invoice_page_id" class="page_id" value=""/>
				</div>
		
		<div class="form-group row">  
		  <div class="col-sm-3 d-flex align-items-center">
			<label> Select a Page Template</label>
		  </div>
		  <div class="col-sm-4">
			  <select class="form-control" id="ptemplate" name="ptemplate">
				  <option value="template1">Template 1</option>
			  </select>
		  </div>
		</div>
		
		<div class="form-group row">  
		  <div class="col-sm-3 d-flex align-items-center">
			<label> Select an Invoice Template</label>
		  </div>
		  <div class="col-sm-4">
			  <select class="form-control"  name="itemplate" id="itemplate">
				 <option value="template1" <?= isset($invoicetemplate) && $invoicetemplate == 'template1' ? 'selected' : '' ?>>Template1</option>
				 <?php //<option value="template2" <?= isset($invoicetemplate) && $invoicetemplate == 'template2' ? 'selected' : '' >Template2</option> ?>
			   </select>
		  </div>
		  <div class="col-sm-5">
			  <button type="button" class="btn btn-success theme-btn mr-3" onclick="dap_member_invoice_preview('1')">Preview Template 1</button>
			  <!--button type="button" class="btn btn-success theme-btn mr-3" onclick="dap_member_invoice_preview('2')">Preview Template 2</button-->
			</div>
		</div>
		
		<div class="form-group row">  
		  <div class="col-sm-3 d-flex align-items-center">
			<label> Show a "View Invoice" Link</label>
		  </div>
		  <div class="col-sm-4">
			  <select class="form-control" id="view_invoice_link" name="view_invoice_link">
				  <option value="N" <?= isset($showinvoice) && $showinvoice == 'N' ? 'selected' : '' ?>>No</option>
				  <option value="Y" <?= isset($showinvoice) && $showinvoice == 'Y' ? 'selected' : '' ?>>Yes</option>
			  </select>
		  </div>
		</div>
		
		<div class="form-group row">  
		  <div class="col-sm-3 d-flex align-items-center">
			<label> Enter Admin Email</label>
		  </div>
		  <div class="col-sm-4">
			  <input type="text" name="adminemail"  id="adminemail" class="form-control" value="<?= $adminemail; ?>">
		  </div>
		</div>
		
		<div class="form-group row">  
		  <div class="col-sm-3 d-flex align-items-center">
			<label> Enter Company Name</label>
		  </div>
		  <div class="col-sm-4">
			  <input type="text" class="form-control" name="companyname" id="companyname" value="<?= $companyname; ?>">
		  </div>
		</div>
		
		<div class="form-group row">  
		  <div class="col-sm-3 d-flex align-items-center">
			<label> Enter Company URL</label>
		  </div>
		  <div class="col-sm-4">
			  <input type="text" class="form-control" id="companyurl" name="companyurl" value="<?= $companyurl; ?>">
		  </div>
		</div>
		
		<div class="form-group row">  
		  <div class="col-sm-3">
			<label> Enter "No Transaction Found" message</label>
		  </div>
		  <div class="col-sm-4">
			  <textarea type="text" class="form-control" id="no_transaction_msg" name="no_transaction_msg" rows="5"><?= isset($no_transaction_msg) && $no_transaction_msg ? $no_transaction_msg : 'Sorry, no transactions found' ?></textarea>
		  </div>
		</div>
		
		<div class="form-group row">  
		  <div class="col-sm-3">
			<label> Enter full URL of your Logo image</label>
		  </div>
		  <div class="col-sm-4 photo_upload_outer">
			  <input type="button" name="photo" class="photo_upload" value="Upload Image">
			  <div class="or my-4">
				<span>OR</span>
			  </div>
			  <input type="text" class="form-control photo_url" value="<?= isset($logo) && $logo ? $logo : '' ?>">
		  </div>
		</div>
		
		<div class="form-group row shortcode_div" style='display:<?= isset($_GET['id']) ? 'block' : 'none' ?>'>

			<div class="col-sm-12 d-flex align-items-center">
				<div class="get_shortcode">
					<h3 class="section_heading">Here is your Invoice page URL: <span class="shortcode_page_link"><a href="<?= isset($data) ? get_permalink($data->getPageId()) : '' ?>" target="_blank"><?= isset($data) ? get_permalink($data->getPageId()) : '' ?></a></span></h3>
					<div class="cancellation_shortcode_outer ">
						<span class="check_heading">Here is your Invoice page Shortcode: </span>
						<span id="dynamic_copyable_text" class="d-inline" name="shortcode_display"><?= dapMemberGetInvoiceShortcodeNameById($_GET['id']) ?></span>
						<span data-id="dynamic_copyable_text" class="copy-btn theme-button" onclick="dap_member_copy_to_clipboard(this)"><i class="fa fa-files-o" aria-hidden="true"></i> Copy</span>
					</div>
				</div>
			</div>
		</div>
		
		
		<div class="form-group row" >
		   <div class="col-sm-12 tab_bottom_button">
			  <a href="<?= admin_url('admin.php?page=dap_member_pages'); ?>"class="btn btn-success theme-btn">Return to Member Pages</a>
			  <button type="button" class="btn btn-success theme-btn create_shortcode_btn">Save</button>
			</div>
		</div>
	</div>
</div>

<?php
/*
$file = dap_member_dir."includes/transactions/invoices/template1/invoicetemplate.html";
$file2 = dap_member_dir."includes/transactions/invoices/template2/invoicetemplate.html";
//$csspath = dap_member_path."includes/transactions/invoices/template1/css/style.css";

$file = file_get_contents($file);

$file = str_replace( '%%IMAGESPATH%%', $imgpath, $file);	
*/
?>
<!-- Modal -->
<div class="modal fade" id="previewTemplateModal" tabindex="-1" role="dialog" aria-labelledby="previewTemplateModalTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">Preview</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body template_preview_body">
		  <?php 
		  //echo $file;
		  ?>
		  
      </div>
    </div>
  </div>
</div>
