<?php 

function profile_cb() {
	global $app;
	global $post_meta;
	$user_meta = $app->user_meta;
	$session = $app->session;
	if(!current_user_can("edit_own_profile")) {
		do_action("display_profile_information", $user_meta);
		return;
	}
	if(isset($_GET['id']) && $_GET['id'] !== $user_meta['id']) {
		$id = !isset($_GET['id']) ? $user_meta['id'] : $_GET['id'];
		if(!current_user_can("edit_others_profile")) {
			do_action("display_profile_information", ['id' => $id]);
			return;
		}
	}
	if(isset($_GET['id'])) {
		$user = new user;
		$user_meta = $user->get_user_meta($_GET['id']);
	}
	$user_meta['country_code'] = isset($user_meta['country_code']) ? $user_meta['country_code'] : "+234";
	$code = str_replace("+", "", $user_meta['country_code']);
	$user_meta['phone'] = str_replace($code, "", $user_meta['phone']);
	$res = do_action("before_display_user_profile", $user_meta, "return");
	if(!$res) :
	?>
	<style type="text/css">
		.country-code .chosen-container-single .chosen-single {
			padding: 4.5px 0 4.5px 8px;
			height: 34px;
			border: 1px solid #aaa;
			border-radius: 5px 0 0 5px;
			/*margin-right: 10px;*/
			border-right: none;
		}
		.country-code .chosen-container-single .chosen-single div b {
			background: none;
		}
		.country-code .chosen-container-single .chosen-single span {
			margin-right: 0;
		}
		#phone {
			border-radius: 0 5px 5px 0;
			/*border-left: none;*/
		}
	</style>
		<div class="row">
			<div class="col-md-8">
				<div class="panel panel-custom">
				    <div class="panel-heading">
				        <!-- <h1>Edit Profile</h1> -->
				    </div>
				    <div class="panel-body">
				    	<form method="post" id="user-profile-form" action="app/includes/ajax.php">
					    	<div class="row">
						        <div class="col-md-8">
						        	<?php 
						        	input(['label' => '', 'id' => 'id', 'required' => 'required', 'value' => $user_meta['id'], 'type' => 'hidden']);

				                    input(['label' => 'Surname', 'id' => 'surname', 'required' => 'required', 'value' => $user_meta['surname']]);

				                    input(['label' => 'Other Names', 'id' => 'other_names', 'required' => 'required', 'value' => $user_meta['other_names']]);

				                    input(['label' => 'Email', 'id' => 'email', 'required' => 'required', 'type' => 'email', 'value' => $user_meta['email']]);
						        	 ?>
						        </div>
						        <div class="col-md-4 pt-5">
						        	<?php //do_action('get_image_uploader', ['width' => 200, 'height' => 200, 'boundaryheight' => 220, 'boundarywidth' => 220, 'url' => get_user_image($user_meta['id']), 'image-name' => 'profile_picture', 'append' => 'logo']) ?>
						        	<?php do_action('croppie_api', ['width' => 200, 'height' => 200, 'url' => get_user_image($user_meta['id']), 'image-name' => 'profile_picture']) ?>
						        </div>
					        </div>
					        <div class="row">
						        <div class="col-md-6">
						        	<div class="row">
										<div class="col-xs-4 pr-0">
											<label>...</label>
											<span class="country-code" data-value="<?php echo isset($user_meta['country_code']) ? $user_meta['country_code'] : ''; ?>"></span>
										</div>
										<div class="col-xs-8 pl-0 pr-0">
											<?php input(['label' => 'Phone', 'id' => 'phone', 'required' => 'required', 'value' => $user_meta['phone'], 'type' => 'phone']); ?>
										</div>
										<div class="col-xs-12 pr-0">
											<?php textarea(['label' => 'Address', 'id' => 'address', 'value' => $user_meta['address'] ?? ""]); ?>
										</div>
									</div>
						        </div>
						        <div class="col-md-6">
						        	<?php do_action("profile_state", $user_meta); 
						        	$exclude = ['software_engineer', 'unverified'];
						        	if(current_user_can("change_user_role") && !in_array($user_meta['role'], $exclude) && $user_meta['id'] !== $session->id) {
						        		$roles = get_system_roles($exclude);
										select(['label' => 'Role', 'id' => 'role', 'required' => 'required'], $roles, $user_meta['role']);
						        	}
						        	?>
						        </div>
						    </div>
					        <?php do_action("more_edit_profile_field", $user_meta); ?>
					        <div class="row">
						        <div class="col-xs-12">
						        	<?php alert(); submit_btn('edit_profile', 'Update', 'submit'); ?>
						        </div>
						    </div>
					    </form>
				    </div>
				</div>
			</div>
			<div class="col-md-4">
				<?php do_action("profile_right_column", $user_meta); ?>
			</div>
		</div>
		<?php
	endif;
	add_page_javascript('app/assets/js/page-scripts/submit.js');
	add_page_javascript('app/assets/js/page-scripts/js-functions.js');
	add_page_javascript('app/assets/js/page-scripts/country-code.js');
}	