
<?php 
	$wpsociline_username="";
	if (isset($_GET['id']))
	 {	

		$wpsociline_edit_id=sanitize_text_field($_GET['id']);		
		global $wpdb;
		$prefix='wpsociline_';
		$table_name=$prefix.'user_profile';
		// for update
		$idexist = $wpdb->prepare("select * from $table_name where id=$wpsociline_edit_id");
		$edit_results=$wpdb->get_results($idexist);
		foreach ($edit_results as $edit_value) 
		{
			$wpsociline_username=$edit_value->username;
		}
					
	 }

	 if (!isset($_GET['id']) && isset($_POST['add_profile']) && wp_verify_nonce($_POST['socilinecsrf'],'socilinecsrf'))
	 {
				global $wpdb;
			$prefix='wpsociline_';
			$table_name=$prefix.'user_profile';
			$username=sanitize_text_field($_POST['username']);

			$url="https://cdn.syndication.twimg.com/timeline/profile?screen_name=".$username."";
			$result =  wp_remote_get($url);
			$wpteetline_res = $result['response'];

			  if($wpteetline_res['code'] == '200')
			  {
			$created_at=date('Y-m-d H:i:s');
			$queryexist = $wpdb->prepare("SELECT username FROM $table_name WHERE username='".$username."'");
			$result = $wpdb->get_col($queryexist);
     
			if (count ($result) == "0") {		
			$sql = $wpdb->prepare( "insert into $table_name(username, created_at) values(%s,%s)",$username, $created_at );
			$insert = $wpdb->query($sql);
			$wpsociline_sucessmsg="Profile Name Saved Sucessfully";
			}
			else{

				$wpsociline_errormsg="Profile Name alreday exist";
		
				}
			  }
			
		else{
			$wpsociline_errormsg="Profile Does not exist on Twitter.";
		}
	}
	elseif (isset($_GET['id']) && isset($_POST['add_profile']) && wp_verify_nonce($_POST['socilinecsrf'],'socilinecsrf')) {
		//$twit_username="";
				global $wpdb;
				$prefix='wpsociline_';
				$table_name=$prefix.'user_profile';
								
									
				$wpsociline_username=sanitize_text_field($_POST['username']);
			
				$url="https://cdn.syndication.twimg.com/timeline/profile?screen_name=".$wpsociline_username."";
				$result =  wp_remote_get($url);
				$wpteetline_res = $result['response'];
	
				  if($wpteetline_res['code'] == '200')
				  {
				$wpsociline_update = $wpdb->query($wpdb->prepare("UPDATE $table_name SET username='$wpsociline_username' WHERE id=$wpsociline_edit_id"));

					if ($wpsociline_update>0) {
						//echo "<script>alert('Profile name update successfully..')</script>";
						$wpsociline_sucessmsg='Profile updated successfully..';
						
					}
					else
					{
						$wpsociline_errormsg="Update Failed";
						
					}

				}
				else{
					$wpsociline_errormsg="Profile Does not exist on Twitter";
				}
				
	}
	?>
	<div class="container-fluid">
		<div  id="wpsociline_dashboard">
			<div class="row">
				<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
					<ul id="wpsociline_dash_nav" >
						<li><a href="admin.php?page=wp_sociline_dashboard"><i class="fa fa-users"></i> User Profile</a></li>
						<li><a href="admin.php?page=wp_sociline_settings"> <i class="fa fa-cogs" style="margin-right: 8px;"></i>Template Settings</a></li>
						<li><a href="admin.php?page=wp_sociline_addprofile"><i class="fa fa-plus-circle"></i> Add New Profile</a></li>
						
					</ul>
				</div>

			</div>
			
			<div class="wpsociline_addgroup" style="background-color: white;">
				<div class="container-fluid">
					<div class="row">
						<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12" style="background-color:#4B75A0;height:40px;">
								<div class="wpsociline_table-profile">
									
								<p class="wpsociline_template_name">User Profile</p>
								</div>
								
						</div>
					</div>
					<div class="row" style="margin-top:20px;">
						<div class="col-xs-12 col-sm-12 col-md-12">
							<div id="wpsocilineuser_add_profile_form">
								<form method="post" action="" id="wpsocilineuser_form">				
									<div class="wrap">
										<label for="footer_scripts">Profile Name</label>
										<input type="text" name="username" class="form-control" value="<?php echo esc_attr($wpsociline_username); ?>" placeholder="Enter Profile Name" required>
<?php
if (isset($wpsociline_sucessmsg))
{?>
<p style="color:#00ff00;font-size:20px;text-align:center;">
<?php echo esc_html($wpsociline_sucessmsg);?></p>
<?php 
}
else{
	?>
	<p style="color:#FF0000;font-size:20px;text-align:center;"><?php echo esc_html($wpsociline_errormsg);?></p>
	<?php 
}
?> </div>
									 <input type="hidden" name="socilinecsrf" value="<?php echo wp_create_nonce('socilinecsrf'); ?>">
									<button type="submit" name="add_profile" class="btn btn-primary">Save</button><br>
								</form>
							</div>
						</div>
					</div>
				</div>
			</div>
			
	</div>
<span style="right:0px;bottom:40px;position:absolute;"><a href="https://teknikforce.com" target="_BLANK"><img src="<?php echo esc_url(plugins_url("assets/images/tekniklogo.png",__FILE__)); ?>" style="max-height:40px;max-width:150px"></a></span>