<?php
add_action( 'sendy_custom_field_tab_content', 'sendy_custom_field_tab_content' );
function sendy_custom_field_tab_content() { ?>
<div class="">
	<!-- <a class="add-new-h2 btn custom-field-modal-trigger" href="#">Add new Custom Field</a> -->
	<!-- Modal Structure -->
	<div id="custom-field-modal" class=" card  modal-fixed-footer">
		<form id="add-custom-field-form" method="post" action="edit.php?post_type=swp_forms&page=sendy-widget-pro-settings#sendy-tabs-5">
		<input type="hidden" name="swp_custom_field_form" value="1"  >
			<div class="modal-content">
				<h3>Add new Custom Field</h3>
				<div class="row">
					<div class="input-field col s12">
					<label for="custom_field_label">Field label</label>
						<input type="text" class="regular-text" name="custom_field_label" id="custom_field_label" placeholder="">
						<p><i>Label to show in frontend form e.g Favourite Dish</i></p>
					</div>
					<div class="input-field col s12">
					<label for="custom_field_name">Field Name</label>
						<input type="text" class="regular-text" name="custom_field_name" id="custom_field_name" placeholder="">
						<p><i>Name should be same as you have created in your Sendy App e.g Dish</i></p>
					</div>
					<div class="col s12">
						<label>Field Type</label>
						<select class="browser-default"  name="custom_field_type" id="custom_field_type">
							<option value="" disabled selected>Select field type</option>
							<option value="text">Text </option>
							<option value="dropdown">Dropdown</option>
							<option value="checkbox">Checkbox</option>
							<option value="hidden">Hidden</option>
						</select>
					</div>
					<div class="input-field col s12 hidden custom_field_options_cont">
					<label for="custom_field_options">Field Options</label>
						<input type="text" class="regular-text" name="custom_field_options" id="custom_field_options" placeholder="Breakfast,Lunch,Dinner">
						<p> <i>Separate each option with comma e.g Breakfast,Lunch,Dinner </i></p>
					</div>

					<div class="col s12 hidden_field_value_cont hidden">
						<label>Field Value</label>
						<select class="browser-default" name="hidden_field_value" id="hidden_field_value">
							<option value="" disabled selected>Select field value</option>
							<option value="ip_address">IP Address </option>
							<option value="current_page">Current Page Url</option>
							<option value="date">Today's Date</option>
							<option value="page_refer">Page Refer Url</option>
						</select>
					</div>
					</div> <!-- row ends -->
				</div>
				<div class="modal-footer">
					<button class="button-primary" type="submit" name="action">Add</button>
					<!-- <a href="#!" id="custom-field-modal-close" class="  button-secondary ">Cancel</a> -->
				</div>
			</form>
		</div>
		<table class="widefat" style="max-width:1200px;margin:35px 0px;">
			<thead>
				<tr>
					<th class="row-title">Sr No.</th>
					<th>Field Label</th>
					<th class="row-title">Field Name( same as in Sendy App )</th>
					<th>Field Type</th>
					<th>Action</th>
				</tr>
			</thead>
			<tbody>
			<?php
	$i =1;
	$custom_fields = get_option( 'swp_custom_fields' );
	if ( !empty( $custom_fields ) ) {
		foreach ( $custom_fields as $key => $custom_field ) { ?>
				<tr><td><?php  echo $i; ?></td><td> <?php echo $custom_field['label']; ?> </td><td> <?php echo $custom_field['name']; ?> </td><td><?php echo ( $custom_field['type']=='hidden' )? ucfirst( $custom_field['type'] ).'-'.$custom_field['hidden_field_value']:ucfirst( $custom_field['type'] ); ?></td><td><a href="edit.php?post_type=swp_forms&page=sendy-widget-pro-settings&sendy_action=delete_custom&id=<?php echo $key; ?>#sendy-tabs-5">Delete</a></td></tr>
			<?php
			$i++;
		}
	} ?>
			</tbody>
			<tfoot>
				<tr>
					<th class="row-title">Sr No.</th>
					<th>Field Label</th>
					<th class="row-title">Field Name( same as in Sendy App )</th>
					<th>Field Type</th>
					<th>Action</th>
				</tr>
			</tfoot>
		</table>
	</div>
	<?php
}

function save_sendy_custom_fields() {
	if ( empty( $_POST['custom_field_name'] ) || empty( $_POST['custom_field_type'] ) ) {
		return;
	}
	$id = rand( 100, 2000000 );
	$custom_fields = get_option( 'swp_custom_fields' );
	//echo '<pre>';var_dump($_POST);die;
	$fields_array = array();
	$fields_array['label'] = $_POST['custom_field_label'];
	$fields_array['name'] = $_POST['custom_field_name'];
	$fields_array['type'] = $_POST['custom_field_type'];
	$fields_array['options'] = $_POST['custom_field_options'];
	$fields_array['hidden_field_value'] = isset( $_POST['hidden_field_value'] )?$_POST['hidden_field_value']:'';
	// if id is already set for another field, generate again
	if ( isset( $custom_fields[$id] ) ) {
		$id = rand( 100, 2000000 );
	}
	$custom_fields[$id] = $fields_array;
	update_option( 'swp_custom_fields', $custom_fields );

}

function delete_sendy_custom_fields() {
	$custom_fields = get_option( 'swp_custom_fields' );
	if ( isset( $custom_fields[$_GET['id']] ) ) {
		unset( $custom_fields[$_GET['id']] );
	}
	update_option( 'swp_custom_fields', $custom_fields );
}
