<?php include_once('includes/header.php'); ?>

<?php 

    if (isset($_GET['id'])) {
        $ID = clean($_GET['id']);
    } else {
        $ID = clean('');
    }

    $qry    = "SELECT * FROM tbl_social WHERE id ='$ID'";
    $result = mysqli_query($connect, $qry);
    $row    = mysqli_fetch_assoc($result);

    if (isset($_POST['submit'])) {

		if ($_FILES['icon']['name'] != '') {
			unlink('upload/social/'.$_POST['old_image']);
			$icon = time().'_'.$_FILES['icon']['name'];
			$pic		 = $_FILES['icon']['tmp_name'];
   			$tpath		 = 'upload/social/'.$icon;
			copy($pic, $tpath);
		} else {
			$icon = $_POST['old_image'];
		}

        $data = array(
            'name'  => $_POST['name'],
            'icon'  => $icon,
            'url'   => $_POST['url']
            );

        $result = Update('tbl_social', $data, "WHERE id = '$ID'");

        if ($result > 0) {
	        $_SESSION['msg'] = "Changes Saved...";
	        header("Location:social-edit.php?id=$ID");
	        exit;
	    }

    }

?>

<section class="content">

	<ol class="breadcrumb">
		<li><a href="dashboard.php">Dashboard</a></li>
		<li><a href="social.php">Manage Social</a></li>
		<li class="active">Edit Social</a></li>
	</ol>

	<div class="container-fluid">

		<div class="row clearfix">
			<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">

				<form id="form_validation" method="post" enctype="multipart/form-data">
					<div class="card corner-radius">
						<div class="header">
							<h2>EDIT SOCIAL</h2>
						</div>
						<div class="body">

							<?php if(isset($_SESSION['msg'])) { ?>
							<div class='alert alert-info alert-dismissible corner-radius' role='alert'>
								<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span>&nbsp;&nbsp;</button>
								<?php echo $_SESSION['msg']; ?>
							</div>
							<?php unset($_SESSION['msg']); } ?>                            

							<div class="row clearfix">

								<div class="form-group form-float col-sm-12">
									<div class="form-line">
										<div class="font-12">Name</div>
										<input type="text" class="form-control" name="name" id="name" value="<?php echo $row['name']; ?>" placeholder="Name" required>
									</div>
								</div>

								<div class="col-sm-6" style="margin-bottom: 0px;">
									<div class="form-group">
										<div class="font-12 ex1">Image ( JPG, JPEG, PNG or GIF )</div>
										<input type="file" name="icon" id="icon" class="dropify-image" data-max-file-size="3M" data-allowed-file-extensions="jpg jpeg png gif" data-default-file="upload/social/<?php echo $row['icon']; ?>" data-show-remove="false"/>
									</div>
								</div>

								<div class="form-group form-float col-sm-12">
                                    <div class="font-12">Url *</div>
                                    <div class="form-line">
                                        <input type="text" class="form-control" name="url" id="url" value="<?php echo $row['url']; ?>" placeholder="Url" required>
                                    </div>
                                </div>

                                <input type="hidden" name="old_image" value="<?php echo $row['icon'];?>">
                                <input type="hidden" name="id" value="<?php echo $row['id'];?>">

								<div class="col-sm-12">
									<button class="button button-rounded waves-effect waves-float pull-right" type="submit" name="<?php if (validateResourceAccess($inputToken)) { echo 'submit'; } else { echo 'validate'; } ?>">SUBMIT</button>
								</div>

							</div>
						</div>
					</div>
				</form>

			</div>
		</div>

	</div>

</section>

<?php include_once('includes/footer.php'); ?>