<?php
    if (empty($_GET['id'])) {
       header("Location: " . PT_Link('admin-cp'));
       exit();
    }
    $table            = T_FIELDS;
    $id               = (!empty($_GET['id'])) ? $_GET['id'] : false;
    $fl_profile_field = $db->where('id',$id)->getOne($table);
    if (empty($fl_profile_field)) {
       header("Location: " . PT_Link('admin-cp'));
       exit();
    }
    $field  = $fl_profile_field; 

?>
<div class="container-fluid">
    <div class="block-header">
        <h2>Users > Edit Custom Field </h2>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-8 col-md-8">
            <div class="card">
                <div class="header">
                    <h2>Edit Custom Field #<?php echo $_GET['id'] ?></h2>
                </div>
                <div class="body">
                    <div class="alert alert-success add-settings-alert"></div>
                    <form class="add-settings" method="POST">
                        <div class="form-group form-float">
                            <div class="form-line">
                                <input type="text" id="name" name="name" class="form-control" value="<?php echo $field->name?>">
                                <label class="form-label">Field Name</label>
                            </div>
                        </div>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <input type="text" id="length" name="length" class="form-control" value="<?php echo $field->length?>">
                                <label class="form-label">Field Length: <small>Default value is 32, and max value is 1000</small></label>
                            </div>
                        </div>
                         <div class="form-group form-float">
                            <div class="form-line">
                                <textarea name="description" id="description" class="form-control" cols="30" rows="3"><?php echo $field->description?></textarea>
                                <label class="form-label">Field Description: <small>The description will show under the field</small></label>
                            </div>
                        </div>
                        <label for="placement">Field placement</label>
                        <select class="form-control show-tick " id="placement" name="placement">
                         	<option value="general" <?php echo ($field->placement == 'general') ? 'selected': '';?>>General settings</option>
		                 	<option value="profile" <?php echo ($field->placement == 'profile') ? 'selected': '';?>>Profile settings</option>
		                 	<option value="social" <?php echo ($field->placement == 'social') ? 'selected': '';?>>Social links</option>
		                 	<option value="none" <?php echo ($field->placement == 'none') ? 'selected': '';?>>Don't show the field in settings page</option>
                        </select>
                        <div class="clearfix"></div>
                        <br>
                        <label for="registration_page">Show On The Registration Page</label>
                        <select class="form-control show-tick" id="registration_page" name="registration_page">
                         		<option value="0" <?php echo ($field->registration_page == '0') ? 'selected': '';?>>No</option>
                 				<option value="1" <?php echo ($field->registration_page == '1') ? 'selected': '';?>>Yes</option>
                        </select>
                        <div class="clearfix"></div>
                        <br>
                        <label for="profile_page">Show On User Profile Page</label>
                        <select class="form-control show-tick" id="profile_page" name="profile_page">
                         		<option value="0" <?php echo ($field->profile_page == '0') ? 'selected': '';?>>No</option>
                  				<option value="1" <?php echo ($field->profile_page == '1') ? 'selected': '';?>>Yes</option>
                        </select>
                        <div class="clearfix"></div>
                        <br>
                        <label for="active">Active</label>
                        <select class="form-control show-tick" id="active" name="active">
                         		<option value="0" <?php echo ($field->active == '0') ? 'selected': '';?>>No</option>
                 				 <option value="1" <?php echo ($field->active == '1') ? 'selected': '';?>>Yes</option>
                        </select>
                        <div class="clearfix"></div>
                        <br><br>
                        <?php if ($field->type == 'select') { ?>
                            <div class="form-group form-float options">
                                <div class="form-line">
                                    <textarea name="options" id="options" class="form-control" cols="30" rows="3"><?php echo str_replace(',', "\n", $field->options)?></textarea>
                                    <label class="form-label">Select Field Options: <small>One option per line.</small></label>
                                </div>
                            </div>
                        <?php } ?>
                        <input type="hidden" name="id" value="<?php echo $field->id;?>">
                        <button type="submit" class="btn btn-primary m-t-15 waves-effect">Save</button>
                    </form>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->
<script>
$(function() {
	$('.chosen-select').on('change', function(evt, params) {
        var selectedValue = $(this).val();
        if (selectedValue == 'select') {
       	  $('.options').fadeIn(200);
        } 

        else {
       	  $('.options').fadeOut(200);
        }
    });
});

$(function() {
    var form_add_settings = $('form.add-settings');
    form_add_settings.ajaxForm({ 
        url: '{{LINK aj/ap/update-field}}',
        beforeSend: function() {
            form_add_settings.find('.waves-effect').text('Please wait..');
        },
        success: function(data) {
            if (data.status == 200) {
                $("html, body").animate({ scrollTop: 0 }, "slow");
                $('.add-settings-alert').html('<i class="fa fa-check"></i> Field saved successfully');
                setTimeout(function () {
		            window.location.href = '<?php echo PT_LoadAdminLinkSettings('manage-profile-fields'); ?>';
		        }, 1000);
            } 
            else if (data.status == 400) {
	          alert(data.message);
	        }
	        form_add_settings.find('.waves-effect').text('Save');
        }
    });
});
</script>