<?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_register',
		'post_status'   => 'draft',
		'menu_order' => 100,
	);
	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);
		update_post_meta($post,'display_edit',(($_POST['display_edit']) == 'on') ? 1 : 0);
		update_post_meta($post,'display_register',(($_POST['display_register']) == 'on') ? 1 : 0);
		add_post_meta($post,'values',($_POST['values']));
		add_post_meta($post,'type', ($_POST['type']));
		if($_POST['type'] == 'text'){
			add_post_meta($post,'validate', ($_POST['validate']));
		}
		else{
			add_post_meta($post,'validate','');
		}
		wp_redirect("admin.php?page=woo2app/register/register.php&action=edit&id=".$post);
	}
}
if(isset($_REQUEST["submit_edit"])){
	$array = array(
		'ID' => ($_GET['id']),
		'post_title'    => ($_POST['title']),
		'post_content' => ($_POST['name']),
		'post_type'     => 'woo2app_register',
		'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_edit',(($_POST['display_edit']) == 'on') ? 1 : 0);
		update_post_meta($post,'display_register',(($_POST['display_register']) == 'on') ? 1 : 0);
		update_post_meta($post,'values', ($_POST['values']));
		update_post_meta($post,'type', ($_POST['type']));
		if($_POST['type'] == 'text'){
			update_post_meta($post,'validate', ($_POST['validate']));
		}
		else{
			update_post_meta($post,'validate','');
		}
		wp_redirect("admin.php?page=woo2app/register/register.php&action=edit&id=".($_GET['id']));
	}
}
if($_GET['action'] == 'edit'){
	$id = ($_GET['id']);
	$post = get_post($id);
	$default_fields  = array( 'user_login'  , 'user_email' , 'user_pass','user_url' ,  'display_name' ,
		'first_name' , 'last_name' , 'description' , 'billing_first_name' , 'billing_last_name','billing_company','billing_address_1',
		'billing_address_2','billing_city','billing_state','billing_postcode','billing_country','billing_email','billing_phone');
	?>
    <div class="wrap" >
        <h1>
			<?php echo _e('Edit', 'woo2app') . ' - ' .$post->post_content; ?>
        </h1>
        <hr>
        <div class="col-wrap">
            <div class="form-wrap">
                <form id="addtag" method="post" action="" class="validate">
					<?php wp_nonce_field('woo2app-admin', 'woo2app-admin'); ?>
                    <table class="form-table">
                        <tr class="">
                            <th> <?php _e('Title', 'woo2app'); ?> </th>
                            <td>
                                <input required type="text"  class="regular-text" value="<?= $post->post_title;?>" name="title"  />
                            </td>
                        </tr>
                        <tr class="form-field ">
                            <th> Name
                                <span style="color: red">   <?= (in_array($post->post_content , $default_fields)) ? 'Not editable.' : '' ;?> </span>
                            </th>
                            <td>
                                <input required  class="regular-text" type="text" dir="ltr" id="txt_name" <?= (in_array($post->post_content , $default_fields)) ? 'readonly' : '' ;?> value="<?= $post->post_content;?>" name="name"  />
                                <p class="description">
	                                <?php _e('Name is meta user and must unique.', 'woo2app'); ?>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <th> <?php _e('Type', 'woo2app'); ?> </th>
                            <td>
								<?php
								$type = get_post_meta($post->ID,'type')[0];
								?>
                                <select name="type" class="regular-text" <?= ($post->post_content  == 'state' || $post->post_content == 'city')? 'readonly' : ''?> 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>
                                    <option <?= ($type == 'map') ? 'selected' : ''; ?>
                                            value="map"> <?php _e('Map', 'woo2app'); ?> </option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <th>
								<?php _e('Validation', 'woo2app'); ?>
                            </th>
                            <td>
								<?php
								$validate = get_post_meta($post->ID,'validate' , true);
								?>
                                <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>
                            </td>
                        </tr>
                        <tr class="form-field">
                            <th> <?php _e('Default', 'woo2app'); ?> </th>
                            <td>
                                <input type="text"  class="regular-text" <?= ($post->post_content == 'user_login' || $post->post_content == 'user_email' || $post->post_content == 'user_pass')? 'readonly' : ''?> value="<?= get_post_meta($id , 'default')[0]?>" name="default"  />
                            </td>
                        </tr>
                        <tr class="form-field ">
                            <td colspan="2">
                                <p>
                                    <input style="float:left;"  <?= ($post->post_content == 'user_login' || $post->post_content == 'user_email' || $post->post_content == 'user_pass')? 'readonly' : ''?>   type="checkbox" <?= get_post_meta($id , 'required')[0] ? 'checked' : '';?>  name="required"  />
                                    <label style="margin-left: 5px"> <?php _e('Required', 'woo2app'); ?> </label>
                                </p>
                                <div class="clear"></div>
                                <p style="">
                                    <input style="float:left;" <?= ($post->post_content == 'user_login' || $post->post_content == 'user_email' || $post->post_content == 'user_pass')? 'readonly' : ''?>  type="checkbox"  <?= get_post_meta($id , 'active')[0] ? 'checked' : '';?> name="active"  />
                                    <label style="margin-right: 5px;"> <?php _e('Enable', 'woo2app'); ?> </label>
                                </p>
                                <div class="clear"></div>
                                <p style="">
                                    <input style="float:left;"   <?= ($post->post_content == 'user_login' || $post->post_content == 'user_pass')? 'readonly' : ''?>   type="checkbox" <?= get_post_meta($id , 'display_register')[0] ? 'checked' : '';?> name="display_register"  />
                                    <label style="margin-right: 5px;">
			                            <?php _e('Showing in  register page', 'woo2app'); ?>
                                    </label>
		                            <?php
		                            if($post->post_content == 'user_email'){
			                            echo '<div class="clear"></div>';
			                            _e('(If this field is disable, save email such as Example: username@site.com)', 'woo2app');
		                            }
		                            ?>
                                </p>
                                <div class="clear"></div>
                                <p style="">
                                    <input   type="checkbox" class="regular-text" <?= get_post_meta($id , 'display_edit')[0] ? 'checked' : '';?> name="display_edit"  />
                                    <label style="margin-right: 5px;">
			                            <?php _e('Showing in user edit page', 'woo2app'); ?>
                                    </label>
                                </p>
                            </td>
                        </tr>
                        <?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 = '';
    }
}
?>
<tr class="form-field values_area" style="<?= $display_style ?>">

                        <tr class="form-field " >
                            <td colspan="2" class="submit">
                                <input type="submit" name="submit_edit" class="button button-primary"
                                       value="<?php _e('Edit', 'woo2app'); ?>"/>
                            </td>
                        </tr>
                    </table>
                </form>
            </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" required dir="ltr" id="txt_name" name="name"  />
                            <p class="description">
								<?php _e('Name is meta user and must unique.', '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('Text box', '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>
                            </select>
                        </div>
                        <div class="form-field validate_form">
                            <label>  <?php _e('Validation', 'woo2app'); ?> </label>
                            <select name="validate" class="regular-text">
                                <option value="general"> <?php _e('Public', 'woo2app'); ?> </option>
                                <option value="email"> <?php _e('E-Mail', 'woo2app'); ?> </option>
                                <option value="phone"> <?php _e('Phone', 'woo2app'); ?> </option>
                                <option value="number"> <?php _e('Number', 'woo2app'); ?> </option>
                                <option 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" name="default"  />
                        </div>
                        <div class="form-field ">
                            <p style="">
                                <input  style="float:left;"   type="checkbox"   name="required"  />
                               <?php _e('Required', 'woo2app'); ?>
                            </p>
                            <div class="clear"></div>
                            <p style="">
                                <input  style="float:left;"  type="checkbox"   name="active"  />
                                 <?php _e('Enable', 'woo2app'); ?>
                            </p>
                            <div class="clear"></div>
                            <p style="">
                                <input  style="float:left;"   type="checkbox"  name="display_register"  />
									<?php _e('Showing in register page', 'woo2app'); ?>
                            </p>
                            <div class="clear"></div>
                            <p style="">
                                <input  style="float:left;"   type="checkbox"  name="display_edit"  />
	                                <?php _e('Showing in user edit page', 'woo2app'); ?>
                            </p>
                        </div>
                        <br>
                        <div class="form-field values_area display-none">
                            <label> <?php _e('Values', 'woo2app'); ?> </label>
                            <textarea name="values" class="regular-text"></textarea>
                            <p>
								<?php _e('If the field has selectable values ​​stored in this field, values ​​are separated by.', 'woo2app'); ?>
                            </p>
                        </div>
                        <div class="form-field " >
                            <p class="submit">
                                <input type="submit" name="submit" class="button button-primary"
                                       value="<?php _e('Save', 'woo2app'); ?>"/>
                            </p>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
	<?php
}
?>
<script>
    function change_type_list(e) {
        if(e.value == 'list' || e.value == 'radio_button'){
            jQuery(".values_area").attr('style','display:content');
        }
        else{
            jQuery(".values_area").attr('style','display:none');
        }
        if(e.value != 'text'){
            jQuery(".validate_form").attr('style','display:none');
        }
        else{
            jQuery(".validate_form").attr('style','display:content');
        }
    }
</script>