<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
$users_data = get_users( array( 'role__in' => array( 'administrator', 'WSDesk_Agents', 'WSDesk_Supervisor' ) ) );
$users      = array();
$select     = array();
for ( $i = 0;$i < count( $users_data );$i++ ) {
	$current = $users_data[ $i ];
	$temp    = array();
	$roles   = $current->roles;
	foreach ( $roles as $value ) {
		$current_role = $value;
		array_push( $temp, ucfirst( str_replace( '_', ' ', $current_role ) ) );
	}
	$users[ implode( ' & ', $temp ) ][ $current->ID ] = $current->data->display_name;
}
$args                    = array( 'type' => 'label' );
$fields                  = array( 'slug', 'title', 'settings_id' );
$avail_labels            = eh_crm_get_settings( $args, $fields );
$ticket_rows             = eh_crm_get_settingsmeta( '0', 'ticket_rows' );
$satisfaction_hyper_link = eh_crm_get_settingsmeta( '0', 'satisfaction_hyper_link' );
if ( '' == $satisfaction_hyper_link ) {
	$satisfaction_hyper_link = esc_html__( 'We’d love to hear your feedback!', 'wsdesk' );
}
if ( '' == $ticket_rows ) {
	$ticket_rows = 25;
}
$avail_fields   = eh_crm_get_settings( array( 'type' => 'field' ), array( 'slug', 'title', 'settings_id' ) );
$all_field_slug = array();
for ( $i = 0;$i < count( $avail_fields );$i++ ) {
	array_push( $all_field_slug, $avail_fields[ $i ]['slug'] );
}
?>
<script>
	function custom_attachment()
	{
		if(document.getElementById('custom-attachment-folder-enable').checked)
			document.getElementById('custom-attachment-path').style.display="block";
		else
			document.getElementById('custom-attachment-path').style.display="none";
	}
	function enable_scheduled_triggers_enable(){
		if(document.getElementById('scheduled_triggers_enable').checked)
			document.getElementById('scheduled_triggers_time_option').style.display="block";
		else
			document.getElementById('scheduled_triggers_time_option').style.display="none";
	}
	function enable_api_click()
	{
		if(document.getElementById('enable_api').checked)
			document.getElementById('api_key').style.display="block";
		else
			document.getElementById('api_key').style.display="none";
	};
</script>
<div class="crm-form-element">
	<div class="col-md-3">
		<label for="default_assignee" style="padding-right:1em !important;"><?php esc_html_e( 'Default Assignee', 'wsdesk' ); ?></label>
		<label for="default_assignee" class="premium_green_set"><a href="https://elextensions.com/plugin/wsdesk-wordpress-support-desk-plugin/" target="_blank"><sup class="text-success">[Premium!]</sup></a></label>
	</div>
	<div class="col-md-9">
		<span class="help-block"><?php esc_html_e( 'Choose default assignee for new tickets', 'wsdesk' ); ?></span>
		<select id="default_assignee" style="width: 100% !important;display: inline !important" class="form-control" aria-describedby="helpBlock">
			<?php
				$assignee        = eh_crm_get_settingsmeta( '0', 'default_assignee' );
				$tag_selected    = '';
				$no_assignee     = '';
				$vendor_selected = '';
			switch ( $assignee ) {
				case 'no_assignee':
					$tag_selected    = '';
					$no_assignee     = 'selected';
					$vendor_selected = '';
					break;
			}
			if ( EH_CRM_WOO_STATUS ) {
				$vendor_roles = eh_crm_get_settingsmeta( 0, 'woo_vendor_roles' );
				if ( ! $vendor_roles ) {
					$vendor_roles = array();
				}
				if ( ! in_array( 'woo_vendors', $all_field_slug ) && ! empty( $vendor_roles ) ) {
					echo '<option disabled="true" value="ticket_vendors" ' . esc_html( $vendor_selected ) . '>' . esc_html__( 'Depends on Vendors', 'wsdesk' ) . '</option>';
				}
			}
				echo '<option disabled="true" value               = ticket_tags ' . esc_html( $tag_selected ) . ' > ' . esc_html__( 'Depends on Ticket Tags', 'wsdesk' ) . '</option >
				<option value               = no_assignee ' . esc_html( $no_assignee ) . ' > ' . esc_html__( 'No Assignee', 'wsdesk' ) . ' </option>';
			foreach ( $users as $key => $value ) {
				echo '<optgroup label="' . esc_html( $key ) . '">';
				foreach ( $value as $uid => $name ) {
					$selected = '';
					if ( $assignee == $uid ) {
						$selected = 'selected';
					}
					echo '<option disabled="true" value="' . esc_html( $uid ) . '" ' . esc_html( $selected ) . '>' . esc_html( $name ) . '</option>';
				}
				echo '</optgroup>';
			}
			?>
		</select>
	</div>
</div>
<span class="crm-divider"></span>
<div class="crm-form-element">
	<div class="col-md-3">
		<label for="default_label" style="padding-right:1em !important;"><?php esc_html_e( 'Default Status', 'wsdesk' ); ?></label>
	</div>
	<div class="col-md-9">
		<span class="help-block"><?php esc_html_e( 'Choose default status for new tickets() / raiser replies', 'wsdesk' ); ?></span>
		<select id="default_label" style="width: 100% !important;display: inline !important" class="form-control" aria-describedby="helpBlock">
			<?php
				$label = eh_crm_get_settingsmeta( '0', 'default_label' );
			for ( $i = 0;$i < count( $avail_labels );$i++ ) {
				$selected = '';
				if ( $label === $avail_labels[ $i ]['slug'] ) {
					$selected = 'selected';
				}
				echo '<option value        = "' . esc_attr( $avail_labels[ $i ]['slug'] ) . '" ' . esc_attr( $selected ) . ' > ' . esc_html( $avail_labels[ $i ]['title'] ) . '</option> ';
			}
			?>
		</select>
	</div>
</div>

<span class="crm-divider"></span>
<div class="crm-form-element">
	<div class="col-md-3">
		<label for="default_raiser_label" style="padding-right:1em !important;" ><?php esc_attr_e( 'Default Raiser Reply Status', 'wsdesk' ); ?></label>
		<label><a href="https://elextensions.com/plugin/wsdesk-wordpress-support-desk-plugin/" target="_blank"><sup class="text-success">[Premium!]</sup></a></label>  
	</div>
	<div class="col-md-9">
		<span class="help-block"><?php esc_attr_e( 'Choose default status for raiser replies', 'wsdesk' ); ?></span>
		<select id="default_raiser_label" style="width: 100% !important; display: inline !important" class="form-control" aria-describedby="helpBlock">
			<?php
			$selected = '';
			$label    = eh_crm_get_settingsmeta( '0', 'default_raiser_label' );
			
			if ( ! $label ) {
				$selected = 'selected';
			}
			
			echo '<option value="" ' . esc_attr( $selected ) . ' disabled>' . esc_html__( 'No Change', 'wsdesk' ) . '</option>';
			
			for ( $i = 0; $i < count( $avail_labels ); $i++ ) {
				$selected = '';
				if ( $label === $avail_labels[ $i ]['slug'] ) {
					$selected = 'selected';
				}
				$disabled = 'disabled'; // Disable all options
				echo esc_html( '<option value="' . esc_attr( $avail_labels[ $i ]['slug'] ) . '" ' . esc_attr( $selected ) . ' ' . $disabled . '>' . esc_attr( $avail_labels[ $i ]['title'] ) . '</option>' );
			}
			?>
		</select>
	</div>
</div>

<span class="crm-divider"></span>
<div class="crm-form-element">
	<div class="col-md-3">
		<label for="ticket_raiser" style="padding-right:1em !important;"><?php esc_html_e( 'Tickets Raisers', 'wsdesk' ); ?></label>
		<label for="ticket_raiser" class="premium_green_set"><a href="https://elextensions.com/plugin/wsdesk-wordpress-support-desk-plugin/" target="_blank"><sup class="text-success">[Premium!]</sup></a></label>
	</div>
	<div class="col-md-9">
		<span class="help-block"><?php esc_html_e( 'Who can raise the tickets ? ', 'wsdesk' ); ?></span>
		<span style="vertical-align: middle;">
			<?php
				$ticket_raiser = eh_crm_get_settingsmeta( '0', 'ticket_raiser' );
				$all           = '';
				$registered    = '';
				$guest         = '';
			switch ( $ticket_raiser ) {
				case 'all':
					$all        = 'checked';
					$registered = '';
					$guest      = '';
					break;
				case 'registered':
					$all        = '';
					$registered = 'checked';
					$guest      = '';
					break;
				case 'guest':
					$all        = '';
					$registered = '';
					$guest      = 'checked';
					break;
			}
			?>
			<input type="radio" disabled="true" style="margin-top: 0;" id="ticket_raiser" class="form-control" name="ticket_raiser" <?php echo esc_html( $all ); ?> value="all"> <?php esc_html_e( 'All', 'wsdesk' ); ?><br>
			<input type="radio" disabled="true" style="margin-top: 0;" id="ticket_raiser" class="form-control" name="ticket_raiser" <?php echo esc_html( $registered ); ?> value="registered"> <?php esc_html_e( 'Registered Users', 'wsdesk' ); ?><br>
			<input type="radio" disabled="true" style="margin-top: 0;" id="ticket_raiser" class="form-control" name="ticket_raiser" <?php echo esc_html( $guest ); ?> value="guest"> <?php esc_html_e( 'Guest Users', 'wsdesk' ); ?>
		</span>
	</div>
</div>
<span class="crm-divider"></span>
<div class="crm-form-element">
	<div class="col-md-3">
		<label for="agent_ticket_section" style="padding-right:1em !important;"><?php esc_html_e( 'Agents Ticket Section', 'wsdesk' ); ?></label>
	</div>
	<div class="col-md-9">
		<span class="help-block"><?php esc_html_e( 'Allow an agent to see other agents tickets in the Tickets dashboard', 'wsdesk' ); ?></span>
		<span style="vertical-align: middle;">
			<?php
				$allow_agent_tickets = eh_crm_get_settingsmeta( '0', 'allow_agent_tickets' );
				$enable              = '';
			switch ( $allow_agent_tickets ) {
				case 'enable':
					$enable = 'checked';
					break;
				default:
					$enable = '';
					break;
			}
			?>
			<input type="checkbox" <?php echo esc_html( $enable ); ?> style="margin-top: 0;" id="allow_agent_tickets" class="form-control" name="allow_agent_tickets" value="enable"> <?php esc_html_e( 'Enable', 'wsdesk' ); ?><br>
		</span>
	</div>
</div>
<span class="crm-divider hide"></span>
<div class="crm-form-element hide">
	<div class="col-md-3">
		<label for="ticket_display_row" style="padding-right:1em !important;"><?php esc_html_e( 'Tickets Row', 'wsdesk' ); ?></label>
	</div>
	<div class="col-md-9">
		<span class="help-block"><?php esc_html_e( 'Number of tickets per page', 'wsdesk' ); ?></span>
		<input type="text" id="ticket_display_row" placeholder="25" value="<?php echo esc_html( $ticket_rows ); ?>" class="form-control crm-form-element-input"  oninput="validity.valid||(value='');" pattern="[0-9]+">
	</div>
</div>


<?php if ( wsdesk_is_premium() ) { ?>
<span class="crm-divider"></span>
<div class="crm-form-element">
	<div class="col-md-3">
		<label for="ticket_display_hyperlink" style="padding-right:1em !important;"><?php esc_html_e( 'Satisfaction Survey', 'wsdesk' ); ?></label>
	</div>
	<div class="col-md-9">
		<span class="help-block"><?php esc_html_e( 'Satisfaction survey hyperlink text', 'wsdesk' ); ?></span>
		<input type="text" id="ticket_display_hyperlink" placeholder="25" value="<?php echo esc_html( $satisfaction_hyper_link ); ?>" class="form-control crm-form-element-input">
	</div>
</div>
<?php } ?>


<span class="crm-divider"></span>
<div class="crm-form-element">
	<div class="col-md-3">
		<label for="auto_assign" style="padding-right:1em !important;"><?php esc_html_e( 'Auto Assign Tickets', 'wsdesk' ); ?></label>
	</div>
	<div class="col-md-9">
		<span class="help-block"><?php esc_html_e( 'Want to auto assign tickets to the replier if the ticket is unassigned ? ', 'wsdesk' ); ?></span>
		<span style="vertical-align: middle;">
			<?php
				$auto_assign = eh_crm_get_settingsmeta( '0', 'auto_assign' );
				$enable      = '';
				$disable     = '';
			switch ( $auto_assign ) {
				case 'enable':
					$enable  = 'checked';
					$disable = '';
					break;
				default:
					$enable  = '';
					$disable = 'checked';
					break;
			}
			?>
			<input type="radio" <?php echo esc_html( $enable ); ?> style="margin-top: 0;" id="auto_assign" class="form-control" name="auto_assign" value="enable"> <?php esc_html_e( 'Enable', 'wsdesk' ); ?><br>
			<input type="radio" <?php echo esc_html( $disable ); ?> style="margin-top: 0;" id="auto_assign" class="form-control" name="auto_assign" value="disable"> <?php esc_html_e( 'Disable', 'wsdesk' ); ?><br>
		</span>
	</div>
</div>
<span class="crm-divider"></span>
<div class="crm-form-element">
	<div class="col-md-3">
		<label for="auto_suggestion" style="padding-right:1em !important;"><?php esc_html_e( 'Auto Suggestion', 'wsdesk' ); ?></label>
		<label for="auto_suggestion" class="premium_green_set"><a href="https://elextensions.com/plugin/wsdesk-wordpress-support-desk-plugin/" target="_blank"><sup class="text-success">[Premium!]</sup></a></label>
	</div>
	<div class="col-md-9">
		<span class="help-block"><?php esc_html_e( 'Want to enable auto suggestion for agent and ticket raisers ? ', 'wsdesk' ); ?></span>
		<span style="vertical-align: middle;">
			<?php
				$auto_suggestion = eh_crm_get_settingsmeta( '0', 'auto_suggestion' );
				$enable          = '';
				$disable         = '';
			switch ( $auto_suggestion ) {
				case 'enable':
					$enable  = 'checked';
					$disable = '';
					break;
				case 'disable':
					$enable  = '';
					$disable = 'checked';
					break;
			}
			?>
			<input type="radio" disabled="true" <?php echo esc_html( $enable ); ?> style="margin-top: 0;" id="auto_suggestion" class="form-control" name="auto_suggestion" value="enable"> <?php esc_html_e( 'Enable', 'wsdesk' ); ?><br>
			<input type="radio" disabled="true" <?php echo esc_html( $disable ); ?> style="margin-top: 0;" id="auto_suggestion" class="form-control" name="auto_suggestion" value="disable"> <?php esc_html_e( 'Disable', 'wsdesk' ); ?><br>
		</span>
	</div>
</div>
<span class="crm-divider"></span>
<div class="crm-form-element">
	<div class="col-md-3">
		<label for="custom-attachment-folder" style="padding-right:1em !important;"><?php esc_html_e( 'Custom Attachment Folder', 'wsdesk' ); ?></label>
		<label for="custom-attachment-folder" class="premium_green_set"><a href="https://elextensions.com/plugin/wsdesk-wordpress-support-desk-plugin/" target="_blank"><sup class="text-success">[Premium!]</sup></a></label>
	</div>
	<div class="col-md-9">
		<span class="help-block"><?php esc_html_e( 'Want to enable custom folder for attachments ? ', 'wsdesk' ); ?></span>
		<span style="vertical-align: middle;">
			<?php
				$custom_attachment_enable = eh_crm_get_settingsmeta( '0', 'custom_attachment_folder_enable' );
				$custom_attachment_path   = eh_crm_get_settingsmeta( '0', 'custom_attachment_folder_path' );
				$enable                   = '';
				$disable                  = '';
			switch ( $custom_attachment_enable ) {
				case 'yes':
					$enable  = 'checked';
					$disable = '';
					$upath   = 'block';
					break;
				default:
					$enable  = '';
					$disable = 'checked';
					$upath   = 'none';
			}
			?>
			<input type="radio" disabled="true" <?php echo esc_html( $enable ); ?> style="margin-top: 0;" onclick="custom_attachment();" id="custom-attachment-folder-enable" class="form-control" name="custom_attachment" value="yes"> <?php esc_html_e( 'Enable', 'wsdesk' ); ?><br>
			<input type="radio" disabled="true" <?php echo esc_html( $disable ); ?> style="margin-top: 0;" onclick="custom_attachment();" id="custom-attachment-folder-disable" class="form-control" name="custom_attachment" value="no"> <?php esc_html_e( 'Disable', 'wsdesk' ); ?><br>
			<div id="custom-attachment-path" style="display: <?php echo esc_html( $upath ); ?>">
				<span class="help-block"><?php esc_html_e( 'Path : ', 'wsdesk' ); ?></span>
				<?php echo esc_url( ABSPATH ); ?> <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( 'Please make sure the location exists . ', 'wsdesk' ); ?>" data-container="body"></span><input type="text" id="custom_attachment_path" placeholder="wp-content/uploads" value="<?php echo esc_html( $custom_attachment_path ); ?>" class="form-control crm-form-element-input">
			</div>

		</span>
	</div>
</div>
<span class="crm-divider"></span>
<div class="crm-form-element">
	<?php
	$max_file_size = eh_crm_get_settingsmeta( '0', 'max_file_size' );
	?>
	<div class="col-md-3">
		<label for="custom-attachment-folder" style="padding-right:1em !important;"><?php esc_html_e( 'Maximum file size of attachments( MB )', 'wsdesk' ); ?></label>
	</div>
	<div class="col-md-9">
		<span class="help-block"><?php esc_html_e( 'You can set maximum size of the attachments', '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( 'default value: 1MB . Decimal values are allowed . ', 'wsdesk' ); ?>" data-container="body"></span></span>
		<input type="number" oninput="this.value = !!this.value && Math.abs(this.value) >= 1 ? Math.abs(this.value) : null" id="max_file_size" placeholder="1" min='1' value="<?php echo esc_html( $max_file_size ); ?>" class="form-control crm-form-element-input">
	</div>
</div>
<span class="crm-divider"></span>
<div class="crm-form-element">
	<div class="col-md-3">
		<label for="custom-attachment-folder" style="padding-right:1em !important;"><?php esc_html_e( 'Valid Attachment Extensions', 'wsdesk' ); ?></label>
	</div>
	<div class="col-md-9">
		<span class="help-block"><?php esc_html_e( 'Select File Extensions', '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( 'Select nothing to make all extensions valid . ', 'wsdesk' ); ?>" data-container="body"></span></span>
		<script>
			jQuery("#file-extension-select").select2({
				width: '100 % ',
				minimumResultsForSearch: -1
			});
		</script>
		 <span style="vertical-align: middle;">
			<select class="file-extension" id='file-extension-select' name="ext[]" multiple="multiple">
				<?php
					$valid_exts = eh_crm_get_settingsmeta( '0', 'valid_file_extension' );
					$valid_exts = explode( ',', $valid_exts );
					$exts       = array(
						'jpg'  => 'JPG',
						'jpeg' => 'JPEG',
						'png'  => 'PNG',
						'gif'  => 'GIF',
						'pdf'  => 'PDF',
						'docx' => 'DOCX',
						'txt'  => 'TXT',
						'zip'  => 'ZIP',
						'xml'  => 'XML',
						'csv'  => 'CSV',
						'mp3'  => 'MP3',
						'mp4'  => 'MP4',
						'xlsx' => 'XLSX',
						'3gp'  => '3GP',
						'avi'  => 'AVI',
						'wmv'  => 'WMV',
						'mpg'  => 'MPG',
						'mov'  => 'MOV',
						'flv'  => 'FLV',
						'syx'  => 'SYX',
						'rtf'  => 'RTF',
						'cdr'  => 'CDR',
						'bmp'  => 'BMP',
						'ppt'  => 'PPT',
						'pptx' => 'PPTX',
					);

					foreach ( $exts as $key => $ext ) {

						if ( in_array( $key, $valid_exts ) ) {
							?>
							<option value="<?php echo esc_attr( $key ); ?>" selected><?php echo esc_html( $ext ); ?></option>
							<?php
						} else {
							?>
							<option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $ext ); ?></option>
							<?php
						}
					}
					?>
			</select>
		 </span>
	</div>
</div>
<span class="crm-divider"></span>
<div class="crm-form-element">
	<div class="col-md-3">
		<label for="auto_create_user" style="padding-right:1em !important;"><?php esc_html_e( 'Create WordPress User', 'wsdesk' ); ?></label>
	</div>
	<div class="col-md-9">
		<span class="help-block"><?php esc_html_e( 'Create WordPress user while guest user submitting the tickets through the form', 'wsdesk' ); ?></span>
		<span style="vertical-align: middle;">
			<?php
				$auto_create_user = eh_crm_get_settingsmeta( '0', 'auto_create_user' );
				$enable           = '';
			switch ( $auto_create_user ) {
				case 'enable':
					$enable = 'checked';
					break;
				default:
					$enable = '';
					break;
			}
			?>
			<input type="checkbox" <?php echo esc_html( $enable ); ?> style="margin-top: 0;" id="auto_create_user" class="form-control" name="auto_create_user" value="enable"> <?php esc_html_e( 'Enable', 'wsdesk' ); ?><br>
		</span>
	</div>
</div>
<span class="crm-divider"></span>
<div class="crm-form-element">
	<div class="col-md-3">
		<label for="close_tickets" style="padding-right:1em !important;"><?php esc_html_e( 'Close Tickets', 'wsdesk' ); ?></label>
	</div>
	<div class="col-md-9">
		<span class="help-block"><?php esc_html_e( 'Allow users to close the tickets they submitted', 'wsdesk' ); ?></span>
		<span style="vertical-align: middle;">
			<?php
				$close_tickets = eh_crm_get_settingsmeta( '0', 'close_tickets' );
			switch ( $close_tickets ) {
				case 'disable':
					$enable = '';
					break;
				default:
					$enable = 'checked';
					break;
			}
			?>
			<input type="checkbox" <?php echo esc_html( $enable ); ?> style="margin-top: 0;" id="close_tickets" class="form-control" name="close_tickets" value="enable"> <?php esc_html_e( 'Enable', 'wsdesk' ); ?><br>
		</span>
	</div>
</div>
<span class="crm-divider"></span>
<div class="crm-form-element">
	<div class="col-md-12">
		<button type="button" id="save_general" class="btn btn-primary btn-sm"> <span class="glyphicon glyphicon-ok"></span> <?php esc_html_e( 'Save Changes', 'wsdesk' ); ?></button>
	</div>
</div>
<?php
// return ob_get_clean();
