<?php
/**
 * Created by mr2app.
 * User: hani
 * Date: 12/18/17
 * Time: 9:44 AM
 */
if (!defined( 'ABSPATH' )) exit;
$current_url="//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if(isset($_POST['submit'])) {
	$array = array(
		'post_title'    => ($_POST['title']),
		'post_content' => ($_POST['name']),
		'post_type'     => 'woo2app_order',
		'post_status'   => 'draft',
		'menu_order' => 100,
	);
	global $wpdb;
	$table_name = $wpdb->prefix . "posts";
	$res = $wpdb->get_results("select *FROM $table_name WHERE post_content = '_order_map' ");
	if($res && $_POST['type'] == 'map'){
		?>
        <div class="notice notice-danger is-dismissible">
            <p>  <?php _e('Sorry ! You are not allowed to add multiple map fields.', 'woo2app'); ?> </p>
        </div>
		<?php
	}
    elseif($_POST['type'] != 'map' && $_POST['name'] == '_order_map'){
		?>
        <div class="notice notice-danger is-dismissible">
            <p> <?php _e('Sorry! Consider a different field for the map field.', 'woo2app'); ?> </p>
        </div>
		<?php
	}
	else{
		if($post = wp_insert_post( $array )){
			add_post_meta($post,'default', ($_POST['default']));
			add_post_meta($post,'required',(($_POST['required'] == 'on')) ? 1 : 0);
			add_post_meta($post,'active',(($_POST['active']) == 'on') ? 1 : 0);
			add_post_meta($post,'display',(($_POST['display']) == 'on') ? 1 : 0);
			add_post_meta($post,'values',($_POST['values']));
			add_post_meta($post,'relation',($_POST['relation']));
			add_post_meta($post,'type',($_POST['type']));
			add_post_meta($post,'validate',($_POST['validate']));
			wp_redirect("admin.php?page=woo2app/order_form/order.php&action=edit&id=".$post);
		}
	}
}
if(isset($_REQUEST["submit_edit"])){
	global $wpdb;
	$table_name = $wpdb->prefix . "posts";
	$res = $wpdb->get_results("select * FROM $table_name WHERE post_content = '_order_map' ");
	$edit_post = get_post(($_GET['id']));
	if($res && $edit_post->post_content != '_order_map' && $_POST['type'] == 'map' ){
		?>
        <div class="notice notice-danger is-dismissible">
            <p> <?php _e('Sorry ! You are not allowed to add multiple map fields.', 'woo2app'); ?> </p>
        </div>
		<?php
	}
    elseif($_POST['type'] != 'map' && $_POST['name'] == '_order_map'){
		?>
        <div class="notice notice-danger is-dismissible">
            <p> <?php _e('Sorry! Consider a different field for the map field.', 'woo2app'); ?> </p>
        </div>
		<?php
	}
	else{
		$array = array(
			'ID' => ($_GET['id']),
			'post_title'    => ($_POST['title']),
			'post_content' => ($_POST['name']),
			'post_type'     => 'woo2app_order',
			'post_status'   => 'draft',
		);
		if($post = wp_update_post( $array )){
			update_post_meta($post,'default',($_POST['default']));
			update_post_meta($post,'required',(($_POST['required']) == 'on') ? 1 : 0);
			update_post_meta($post,'active',(($_POST['active'] == 'on')) ? 1 : 0);
			update_post_meta($post,'display',(($_POST['display'] == 'on')) ? 1 : 0);
			update_post_meta($post,'values', ($_POST['values']));
			update_post_meta($post,'relation', ($_POST['relation']));
			update_post_meta($post,'type', ($_POST['type']));
			update_post_meta($post,'validate', ($_POST['validate']));
			wp_redirect("admin.php?page=woo2app/order_form/order.php&action=edit&id=". ($_GET['id']));
		}
	}
}
if($_GET['action'] == 'edit'){
	$id = ($_GET['id']);
	$post = get_post($id);
	$default_fields  = array( 'first_name' , 'last_name' , 'address_1' , 'address_2','city' ,  'state' , 'postcode' , 'country' , 'email' , 'phone' , 'customer_note');
	?>
    <h1>
		<?php _e('Edit the field', 'woo2app'); ?>
    </h1>
    <div id="col-container" class="">
        <div class="col-wrap" style="width: 50%;">
            <div class="form-wrap">
                <form id="addtag" method="post" action="" class="validate">
					<?php wp_nonce_field('woo2app-admin', 'woo2app-admin'); ?>
                    <div class="form-field ">
                        <label> <?php _e('Title', 'woo2app'); ?> </label>
                        <input required type="text" value="<?= $post->post_title; ?>"
                               name="title"/>
                    </div>
                    <div class="form-field ">
                        <label> Name <span style="color: red">   <?= (in_array($post->post_content , $default_fields)) ? 'Not editable.' : '' ;?> </span> </label>
                        <input required type="text" dir="ltr" id="txt_name" <?= (in_array($post->post_content , $default_fields)) ? 'readonly' : '' ;?> value="<?= $post->post_content;?>" name="name"  />
                        <p>
							<?php _e('The name is post meta', 'woo2app'); ?>
                        </p>
                    </div>
                    <div class="form-field ">
						<?php
						$type = get_post_meta($post->ID,'type')[0];
						?>
                        <label> <?php _e('Type', 'woo2app'); ?> </label>
                        <select name="type" class="regular-text"  onchange="change_type_list(this)">
                            <option <?= ($type == 'text') ? 'selected' : ''; ?>
                                    value="text"> <?php _e('Text box', 'woo2app'); ?> </option>
                            <option <?= ($type == 'list') ? 'selected' : ''; ?>
                                    value="list"> <?php _e('Drop down list', 'woo2app'); ?> </option>
                            <option <?= ($type == 'paragraph') ? 'selected' : ''; ?>
                                    value="paragraph"> <?php _e('Paragraph', 'woo2app'); ?> </option>
                            <option <?= ($type == 'radio_button') ? 'selected' : ''; ?>
                                    value="radio_button"> <?php _e('Radio button', 'woo2app'); ?> </option>
							<?php
							if(!(in_array($post->post_content , $default_fields))){
								?>
                                <option <?= ($type == 'map') ? 'selected' : ''; ?>
                                        value="map"> <?php _e('Map', 'woo2app'); ?> </option>
								<?php
							}
							?>
                            <option <?= ($type == 'date')? 'selected' : '' ;?> value="date"> <?php _e('Date', 'woo2app'); ?> </option>

                            <option <?= ($type == 'time')? 'selected' : '' ;?> value="time"> <?php _e('Time', 'woo2app'); ?> </option>
                        </select>
                    </div>
                    <div class="form-field ">
						<?php
						$validate = get_post_meta($post->ID,'validate' , true);
						?>
                        <label>  <?php _e('Validation', 'woo2app'); ?> </label>
                        <select name="validate"  class="regular-text">
                            <option <?= ($validate == 'general') ? 'selected' : ''; ?>
                                    value="general"> <?php _e('Public', 'woo2app'); ?> </option>
                            <option <?= ($validate == 'email') ? 'selected' : ''; ?>
                                    value="email"> <?php _e('E-Mail', 'woo2app'); ?> </option>
                            <option <?= ($validate == 'phone') ? 'selected' : ''; ?>
                                    value="phone"> <?php _e('Phone', 'woo2app'); ?> </option>
                            <option <?= ($validate == 'number') ? 'selected' : ''; ?>
                                    value="number"> <?php _e('Number', 'woo2app'); ?> </option>
                            <option <?= ($validate == 'password') ? 'selected' : ''; ?>
                                    value="password"> <?php _e('Password', 'woo2app'); ?> </option>
                        </select>
                    </div>
                    <div class="form-field ">
                        <label> <?php _e('Default', 'woo2app'); ?> </label>
                        <input type="text" class="regular-text"  value="<?= get_post_meta($id , 'default')[0]?>" name="default"  />
                        <p>
							<?php _e('The hypothesis that is intended for the field.', 'woo2app'); ?>
                        </p>
                    </div>
                    <div class="form-field ">
                        <p style="">
                            <input   type="checkbox" <?= get_post_meta($id , 'required')[0] ? 'checked' : '';?>  name="required"  />
                            <label > <?php _e('Required ?', 'woo2app'); ?> </label>
                        </p>
                        <div class="clear"></div>
                        <p style="">
                            <input  type="checkbox"  <?= get_post_meta($id , 'active')[0] ? 'checked' : '';?> name="active"  />
                            <label > <?php _e('Active', 'woo2app'); ?> </label>
                        </p>
                        <div class="clear"></div>
                        <p style="">
                            <input type="checkbox" <?= get_post_meta($id , 'display')[0] ? 'checked' : '';?> name="display"  />
                            <label> <?php _e('Display ', 'woo2app'); ?> </label>
                        </p>
                    </div>
                    <br>
                    <?php
$display_style = 'display:none';
if ($post->post_content != 'state' && $post->post_content != 'city') {
    if (get_post_meta($id, 'type')[0] == 'list' || get_post_meta($id, 'type')[0] == 'radio_button') {
        $display_style = '';
    }
}
?>
<div class="form-field values_area" style="<?= $display_style ?>">
    <label><?php _e('Values', 'woo2app'); ?></label>
    <textarea class="regular-text" name="values"><?= get_post_meta($id, 'values')[0]; ?></textarea>
    <p><?php _e('values are separated by ","', 'woo2app'); ?></p>
</div>

                    <div class="form-field ">
                        <label>  <?php _e('Relation With User', 'woo2app'); ?> </label>
                        <input type="text" class="regular-text" value="<?= get_post_meta($id , 'relation')[0];?>" name="relation"  />
                        <p>
	                        <?php _e('This field related with meta user', 'woo2app'); ?>
                        </p>
                    </div>
                    <p class="submit">
                        <input type="submit" name="submit_edit" class="button button-primary"
                               value="<?php _e('Edit', 'woo2app'); ?>"/>
                    </p>
                </form>
            </div>
        </div>
    </div>
    </div>
	<?php
}
else{
?>
<div class="wrap" >
    <h1>
		<?php _e('New field', 'woo2app'); ?>
    </h1>
    <div id="col-container" class="">
        <div class="col-wrap" style="width: 50%;">
            <div class="form-wrap">
                <form id="addtag" method="post" action="" class="validate">
					<?php wp_nonce_field('woo2app-admin', 'woo2app-admin'); ?>
                    <div class="form-field ">
                        <label> <?php _e('Title', 'woo2app'); ?> </label>
                        <input type="text" class="regular-text" required name="title"/>
                    </div>
                    <div class="form-field ">
                        <label> <?php _e('Name', 'woo2app') ?> </label>
                        <input type="text" class="regular-text" required dir="ltr" id="txt_name" name="name"  />
                        <p>
							<?php _e('The name is post meta.', 'woo2app'); ?>
                        </p>
                    </div>
                    <div class="form-field ">
                        <label> <?php _e('Type', 'woo2app'); ?> </label>
                        <select name="type" class="regular-text" onchange="change_type_list(this)">
                            <option value="text"> <?php _e('TextBox', 'woo2app'); ?> </option>
                            <option value="list"> <?php _e('Drop Down List', 'woo2app'); ?> </option>
                            <option value="paragraph"> <?php _e('Paragraph', 'woo2app'); ?> </option>
                            <option value="radio_button"> <?php _e('Radio Button', 'woo2app'); ?> </option>
                            <option value="map"> <?php _e('Map', 'woo2app'); ?> </option>
                            <option value="date"> <?php _e('Date', 'woo2app'); ?> </option>
                            <option value="time"> <?php _e('Time', 'woo2app'); ?> </option>
                        </select>
                    </div>
                    <div class="form-field ">
                        <label> <?php _e('Default', 'woo2app'); ?> </label>
                        <input type="text"  name="default"  />
                        <p>
							<?php _e('The default is for the field.', 'woo2app'); ?>
                        </p>
                    </div>
                    <div class="form-field ">
                        <p style="">
                            <label style=""> <?php _e('Required ?', 'woo2app'); ?> </label>
                            <input  type="checkbox"  name="required"  />
                        </p>
                        <p >
                            <label style="margin-right: 5px;"> <?php _e('Active', 'woo2app'); ?> </label>
                            <input type="checkbox"  name="active"  />
                        </p>
                        <p>
                            <label style="margin-right: 5px;"> <?php _e('Display', 'woo2app'); ?>  </label>
                            <input type="checkbox" name="display"  />
                        </p>
                    </div>
                    <br>
                    <div class="form-field values_area display-none">
                        <label> <?php _e('Values', 'woo2app'); ?> </label>
                        <textarea name="values"></textarea>
                        <p class="description">
		                    <?php _e('values are separated by ","', 'woo2app'); ?>
                        </p>
                    </div>
                    <div class="form-field ">
                        <label>  <?php _e('Relation with user', 'woo2app'); ?> </label>
                        <input type="text"  name="relation"  />
                        <p>
							<?php _e('This field related with meta user', 'woo2app'); ?>
                        </p>
                    </div>
                    <p class="submit">
                        <input type="submit" name="submit" class="button button-primary"
                               value="<?php _e('Save', 'woo2app'); ?>"/>
                    </p>
                </form>
            </div>
        </div>
    </div>
	<?php
	}
	?>
    <script>
        function change_type_list(e) {
            if(e.value == 'list' || e.value == 'radio_button'){
                jQuery(".values_area").attr('style','display:block');
            }
            else{
                jQuery(".values_area").attr('style','display:none');
            }
			<?php
			if(!(in_array($post->post_content , $default_fields))){
			?>
            if(e.value == 'map'){
                jQuery("#txt_name").val('_order_map');
                jQuery("#txt_name").attr('readonly', 'true');
            }
            else{
                jQuery("#txt_name").val('');
                jQuery("#txt_name").attr('readonly', false);
            }
			<?php
			}
			?>
        }
    </script>