<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// ob_start();
$imap_account_data = eh_crm_get_settingsmeta( '0', 'imap_account_data' );

if ( ! in_array( 'imap', get_loaded_extensions() ) ) {
	?>
	<div style="text-align: center;">
		<h1><span class="glyphicon glyphicon-screenshot" style="font-size: 2em;color: lightgrey;"></span></h1>
		<br>
		<p><?php esc_html_e( 'IMAP is not enabled on your server. Please contact your service provider.', 'wsdesk' ); ?>
		<br>
		<span class="crm-divider"></span>
		<b><?php esc_html_e( 'Have a Google account?', 'wsdesk' ); ?></b>
		<?php esc_html_e( 'Use WSDesk Google OAuth setup.', 'wsdesk' ); ?>
		<br>
		<?php esc_html_e( '[ WSDesk -> E-Mail -> Google OAuth Setup ]', 'wsdesk' ); ?>
		<br>
		</p>
	</div>

	<?php
} else {
	?>
<div class="crm-form-element">
	<div class="col-md-12">
		<div style="vertical-align: middle" class="imap_email_">
			<label for="ticket_triggers" style="padding-right:1em !important;"><?php esc_html_e( 'IMAP E-Mail Setup', 'wsdesk' ); ?></label>

		</div>
		<div class="panel panel-default crm-panel" style="margin-top: 15px !important;margin-bottom: 0px !important;">
			<div class="panel-body" style="padding: 5px !important;">
				<div class="col-sm-12" style="padding-right: 5px !important;padding-left: 5px !important;">

					<ul class="list-group">
					<?php
					$us = 0;
					if ( $imap_account_data ) {
						foreach ( $imap_account_data as $key => $value ) {
							$us++;
							echo '<li class="list-group-item list-group-item-success" data-target="#' . esc_attr( $us ) . 's" aria-expanded="false" data-toggle="collapse" style="padding: 10px 10px !important;" id="trigger_QH38">' . esc_html( $value['imap_server_email'] ) . '</li>';
							?>
							<div id="<?php echo( esc_attr( $us ) . 's' ); ?>" class="panel-collapse collapse" aria-expanded="false">
								<div class="<?php echo esc_html( $value['imap_server_email'] ); ?>">
									<div class="crm-form-element">
										<div class="col-md-12">
											<span class="help-block"><?php esc_html_e( 'IMAP Server SSL URL', 'wsdesk' ); ?> <span class="glyphicon glyphicon-info-sign" style="color:lightgray;font-size:x-small;vertical-align:baseline;" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'Copy Paste the IMAP URL provided by your respective hosting service provider. ( viz. imap.gmail.com ).', 'wsdesk' ); ?>" data-container="body"></span></span>
											<input type="text" id="edit_server_url" placeholder="imap.gmail.com" value="<?php echo esc_html( $value['imap_server_url'] ); ?>" class="form-control crm-form-element-input">
										</div>
									</div>
									<div class="crm-form-element">
										<div class="col-md-12">
											<span class="help-block"><?php esc_html_e( 'IMAP Server SSL Port', 'wsdesk' ); ?> <span class="glyphicon glyphicon-info-sign" style="color:lightgray;font-size:x-small;vertical-align:baseline;" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( ' Provide port number of your hosting service provider (993 for Gmail).', 'wsdesk' ); ?>" data-container="body"></span></span>
											<input type="text" id="edit_server_port" placeholder="993" value="<?php echo esc_html( $value['imap_server_port'] ); ?>" class="form-control crm-form-element-input">
										</div>
									</div>
									<div class="crm-form-element">
										<div class="col-md-12">
											<span class="help-block"><?php esc_html_e( 'Email-ID', 'wsdesk' ); ?> <span class="glyphicon glyphicon-info-sign" style="color:lightgray;font-size:x-small;vertical-align:baseline;" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'Enter email address from where emails are to be extracted.', 'wsdesk' ); ?>" data-container="body"></span></span>
											<input type="text" id="edit_server_email" autocomplete="off" placeholder="example@gmail.com" value="<?php echo esc_html( $value['imap_server_email'] ); ?>" class="form-control crm-form-element-input">
										</div>
									</div>
									<div class="crm-form-element">
										<div class="col-md-12">
											<span class="help-block"><?php esc_html_e( 'Password', 'wsdesk' ); ?> <span class="glyphicon glyphicon-info-sign" style="color:lightgray;font-size:x-small;vertical-align:baseline;" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'Enter password for your email address.', 'wsdesk' ); ?>" data-container="body"></span></span>
											<input type="password" id="edit_server_email_pwd" autocomplete="off" placeholder="Password" value="<?php echo esc_html( $value['imap_server_email_pwd'] ); ?>" class="form-control crm-form-element-input">
										</div>
									</div>
									<div class="crm-form-element">
										<div class="col-md-12">
											<span class="help-block"><?php esc_html_e( 'Delete imported e-mail from the server', 'wsdesk' ); ?> <span class="glyphicon glyphicon-info-sign" style="color:lightgray;font-size:x-small;vertical-align:baseline;" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'Enabling this will delete the e-mail after importing it as ticket.', 'wsdesk' ); ?>" data-container="body"></span></span>
											<input type="checkbox" id="edit_delete_email" autocomplete="off" value="yes" <?php echo ( 'yes' == $value['delete_email'] || 'on' == $value['delete_email'] ) ? 'checked' : ''; ?> class="form-control"><label style="font-weight: normal;">Enable</label>
										</div>
									</div>
									<div class="crm-form-element">
										<div class="col-md-12">
											<span class="help-block"><?php echo esc_html_e( 'Username', 'wsdesk' ); ?> <span class="glyphicon glyphicon-info-sign" style="color:lightgray;font-size:x-small;vertical-align:baseline;" data-toggle="wsdesk_tooltip" title="<?php esc_html__( 'Enter your username if required', 'wsdesk' ); ?>" data-container="body"></span></span>
											<input type="text" value="<?php echo esc_html( $value['username'] ); ?>" class="form-control">
										</div>
									</div>
									<div class="crm-form-element">
										<div class="col-md-12">
											<span class="help-block"><?php esc_html_e( 'Charset', 'wsdesk' ); ?> <span class="glyphicon glyphicon-info-sign" style="color:lightgray;font-size:x-small;vertical-align:baseline;" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'The charset of your email server', 'wsdesk' ); ?>" data-container="body"></span></span>
											<input type="text" value="<?php echo esc_html( $value['charset'] ); ?>" class="form-control">
										</div>
									</div>
									<div class="crm-form-element">
										<div class="col-md-12">
							<?php
							if ( 'activated' == $value['imap_activation'] ) {
								?>
													<button type="button" class="btn btn-danger deactivate_imap" id="<?php echo( esc_attr( $us ) ); ?>"><span class="glyphicon glyphicon-remove"></span> <?php esc_html_e( 'Delete Account', 'wsdesk' ); ?></button>
											<?php
							} else {
								?>
													<button type="button" id="activate_imap" class="btn btn-primary"><span class="glyphicon glyphicon-ok"></span> <?php esc_html_e( 'Activate IMAP', 'wsdesk' ); ?></button>
										 <?php
							}
							?>
											<a href="https://elextensions.com/knowledge-base/configure-imap-wsdesk/" target="_blank" class="btn btn-info pull-right" ><span class="glyphicon glyphicon-link"></span> <?php esc_html_e( 'How to setup IMAP', 'wsdesk' ); ?></a>
										</div>
									</div>
								</div>
							</div>
							<?php

						}
					} else {
						echo "<li class='list-group-item list-group-item-success' style='padding: 10px 10px !important;' id='trigger_QH38'>No IMAP accounts!</li>";
					}
					?>
					</ul>
				</div>
			</div>
		</div>
	</div>
</div>

<li class="list-group-item" data-target="#imap_email_new" aria-expanded="false" data-toggle="collapse" style="padding: 10px 10px !important;border: none" id="data_dispaly">
	<button type="button" id="imap_email_trigger_add_button" class="btn btn-primary btn-xs pull-right"> <span class="glyphicon glyphicon-plus"></span> <?php esc_html_e( 'Add New Account', 'wsdesk' ); ?></button>
</li>

<div class="imap_email_new panel-collapse collapse" id="imap_email_new" aria-expanded="false">
	<div class="email_trigger_add_button">
		<label for="ticket_triggers" style="margin: 15px;"><?php esc_html_e( 'Add New IMAP Account', 'wsdesk' ); ?></label>
		<div class="crm-form-element">
			<div class="col-md-12">
				<span class="help-block"><?php esc_html_e( 'IMAP Server SSL URL', 'wsdesk' ); ?> <span class="glyphicon glyphicon-info-sign" style="color:lightgray;font-size:x-small;vertical-align:baseline;" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'Copy Paste the IMAP URL provided by your respective hosting service provider. ( viz. imap.gmail.com ).', 'wsdesk' ); ?>" data-container="body"></span></span>
				<input type="text" id="server_url" placeholder="imap.gmail.com" class="form-control crm-form-element-input">
			</div>
		</div>
		<div class="crm-form-element">
			<div class="col-md-12">
				<span class="help-block"><?php esc_html_e( 'IMAP Server SSL Port', 'wsdesk' ); ?> <span class="glyphicon glyphicon-info-sign" style="color:lightgray;font-size:x-small;vertical-align:baseline;" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( ' Provide port number of your hosting service provider (993 for Gmail).', 'wsdesk' ); ?>" data-container="body"></span></span>
				<input type="text" id="server_port" placeholder="993"  class="form-control crm-form-element-input">
			</div>
		</div>
		<div class="crm-form-element">
			<div class="col-md-12">
				<span class="help-block"><?php esc_html_e( 'Email-ID', 'wsdesk' ); ?> <span class="glyphicon glyphicon-info-sign" style="color:lightgray;font-size:x-small;vertical-align:baseline;" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'Enter email address from where emails are to be extracted.', 'wsdesk' ); ?>" data-container="body"></span></span>
				<input type="text" id="server_email" autocomplete="off" placeholder="example@gmail.com" class="form-control crm-form-element-input">
			</div>
		</div>
		<div class="crm-form-element">
			<div class="col-md-12">
				<span class="help-block"><?php esc_html_e( 'Password', 'wsdesk' ); ?> <span class="glyphicon glyphicon-info-sign" style="color:lightgray;font-size:x-small;vertical-align:baseline;" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'Enter password for your email address.', 'wsdesk' ); ?>" data-container="body"></span></span>
				<input type="password" id="server_email_pwd" autocomplete="off" placeholder="Password"  class="form-control crm-form-element-input">
			</div>
		</div>
		<div class="crm-form-element">
			<div class="col-md-12">
				<span class="help-block"><?php esc_html_e( 'Username', 'wsdesk' ); ?> <span class="glyphicon glyphicon-info-sign" style="color:lightgray;font-size:x-small;vertical-align:baseline;" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'Enter your username if required', 'wsdesk' ); ?>" data-container="body"></span></span>
				<input type="text" id="imap_username" value="" class="form-control">
			</div>
		</div>
		<div class="crm-form-element">
			<div class="col-md-12">
				<span class="help-block"><?php esc_html_e( 'Charset', 'wsdesk' ); ?> <span class="glyphicon glyphicon-info-sign" style="color:lightgray;font-size:x-small;vertical-align:baseline;" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'The charset of your email server', 'wsdesk' ); ?>" data-container="body"></span></span>
				<input type="text" id="charset" value="UTF-8" class="form-control">
			</div>
		</div>
		<div class="crm-form-element">
			<div class="col-md-12">
				<span class="help-block"><?php esc_html_e( 'Delete imported e-mail from the server', 'wsdesk' ); ?> <span class="glyphicon glyphicon-info-sign" style="color:lightgray;font-size:x-small;vertical-align:baseline;" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'Enabling this will delete the e-mail after importing it as ticket.', 'wsdesk' ); ?>" data-container="body"></span></span>
				<input type="checkbox" id="delete_email" autocomplete="off"  class="form-control"><label style="font-weight: normal;">Enable</label>
			</div>
		</div>
		<div class="crm-form-element">
			<div class="col-md-12">
				<button type="button" id="activate_imap" class="btn btn-primary"><span class="glyphicon glyphicon-ok"></span> <?php esc_html_e( 'Activate IMAP', 'wsdesk' ); ?></button>
				<a href="https://elextensions.com/knowledge-base/configure-imap-wsdesk/" target="_blank" class="btn btn-info pull-right" ><span class="glyphicon glyphicon-link"></span> <?php esc_html_e( 'How to setup IMAP', 'wsdesk' ); ?></a>
			</div>
		</div>
	</div>
</div>
	<?php
}
