<?php 

include('config/function.php');

if(!isset($_SESSION['user_name']) || empty($_SESSION['user_name']))
{
header('location:login.php');
exit();
}

/* set variable */
$pagename = "Team";
$pagetaskname = " Add ";

/* set var blank */
$id = "";
$msg = "";	
$sess_msg = "";
$team_name = "";
$cid = "";
$line1 = "";
$line2 = "";
$team_img = "";
$content = "";
$status = "";

/* get id */
if(isset($_GET['id']) && $_GET['id']!="")
{
	$id = $_GET['id'];
}

if(isset($_POST['submit']) && $_POST['submit']=="add")
{
	
	$team_name = addslashes(trim($_POST['team_name']));
	$line1 = addslashes(trim($_POST['line1']));
	$content = addslashes($_POST['content']);
	
	if(!empty($_POST['status'])) { $status=1; } else{ $status=0; }
	
	/* check team_name in database */
	$checkDuplicate ="";
	if($id!="")
	{
		$checkDuplicate = "AND id!='$id'"; 
	}
	$query_duplicate="SELECT * FROM tbl_team WHERE team_name='".$team_name."' $checkDuplicate";
	if($sql_duplicate=$conn->query($query_duplicate))
	{
		if($sql_duplicate->num_rows>0)
		{
			$msg = "This team_name is already exist, please try another.";
		}
		else
		{
			if($id!="")
			{
				$query_update="UPDATE tbl_team SET team_name='".$team_name."',status='".$status."',line1='".$line1."',content='".$content."' WHERE id='".$id."'";
				if($sql_update=$conn->prepare($query_update))
				{
					$sql_update->execute();
					$sess_msg = $pagename." Update Successfully.";	
				}
			}
			else
			{
				$query_insert="INSERT INTO tbl_team SET team_name='".$team_name."',status='".$status."',line1='".$line1."',content='".$content."'";
				if($sql_insert=$conn->prepare($query_insert))
				{
					$sql_insert->execute();
					$id = mysqli_insert_id($conn);
					$sess_msg = $pagename." Added Successfully.";
				}
			}
			if(isset($_FILES['team_img']) && $_FILES['team_img']['error']==0)
			{

				$array = explode('.', $_FILES['team_img']['name']);
				$bannerteam_img = $array[0];
				$bannerteam_img1 = $array[1];
				$time =time();
				$bannerteam_img = $time.$bannerteam_img;
				$bannerteam_img = str_replace("---","-",preg_replace("/[^-a-zA-Z0-9s]/", "-", strtolower(trim($bannerteam_img)))); 
				$bannerteam_img = $bannerteam_img.".".$bannerteam_img1;  

			    $bannerteam_imgname = "";
					if($id!="")
					{

					$queryinfo = "SELECT team_img FROM `tbl_team` WHERE id = '".$id."'";
					if($sql_select = $conn->query($queryinfo))
					{
					if($sql_select->num_rows>0)
					{
					$resultinfo = $sql_select->fetch_array(MYSQLI_ASSOC);

					$bannerteam_imgname =  trim($resultinfo['team_img']);
					}
					}
					}
						if($bannerteam_imgname!="")
						{
						$unlkheaderfile = "uploads/team/".$bannerteam_imgname;
						if (file_exists($unlkheaderfile)) { unlink($unlkheaderfile); }
						}
					$bannerfilename = "uploads/team/". $bannerteam_img;
					$mv =move_uploaded_file($_FILES['team_img']['tmp_name'],$bannerfilename);
					$query_team_imgup="UPDATE tbl_team SET team_img='".$bannerteam_img."' WHERE id='".$id."'";
					if($sql_team_imgup=$conn->prepare($query_team_imgup))
					$sql_team_imgup->execute();
			} 

			echo "<script>document.location.href='view-team.php?msg=".$sess_msg."';</script>";
			exit;
		}
	}
}

/* Listing  */
if($id!="")
{
    $query_select="SELECT * FROM tbl_team WHERE id='".$id."'";
	if($sql_select=$conn->query($query_select))
	{ 
		if($sql_select->num_rows>0)
		{
			$result=$sql_select->fetch_array(MYSQLI_ASSOC);
			
			$team_name = stripslashes($result['team_name']);
			$line1 = stripslashes($result['line1']);
			$content = stripslashes($result['content']);
			$status = $result['status'];
			$team_img = $result['team_img'];
			$pagetaskname = " Update ";
		}
		else
		{
			echo "<script>document.location.href='view-team.php';</script>";
			exit;
		}
    }
}

?>
<!DOCTYPE html>
<html>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"> 
  <team_name>Dashboard</team_name>
 
  <link rel="stylesheet" href="bower_components/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="bower_components/Ionicons/css/ionicons.min.css">
  <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
  <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
		<script src="dist/js/editor.js"></script>
		<script>
			$(document).ready(function() {
				$("#txtEditor").Editor();
			});
		</script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
		<link href="dist/css/editor.css" type="text/css" rel="stylesheet"/>
    
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <?php include('header.php') ?>
  <?php include('left-menu.php') ?>
  <div class="content-wrapper">
    <section class="content">
      <div class="row">
       <div class="col-md-12">
	   <?php if($msg!="") { ?><h5 class="h5-headn" style="color:red;"><?php echo $msg; ?></h5><?php } else { ?>
          <div class="main-team_name">
           <h3>Add Team</h3>
           </div>
		   <?php } ?>
          </div>
          <div class="col-md-12">
          <div class="field-section">
              <form method="post" enctype="multipart/form-data">
              <div class="row">
               <div class="col-md-4">
                    <label class="input-text">Team Name</label>
                  <div class="form-group">
                      <input type="text" name="team_name" value="<?php echo $team_name; ?>" class="from-control custom-type">
                   </div>
                  </div>
                  <div class="col-md-4">
                    <label class="input-text">team_img</label>
                    <div class="form-group">
					<?php if($team_img!="") { ?>
					<img src="uploads/team/<?php echo $result['team_img']?>" height="100" width="120" team_name="<?php echo $team_name; ?> team_img">
					<?php } ?>
                      <input type="file" name="team_img" value="<?php echo $result['team_img']?>" class="from-control custom-type">
                   </div>
                  </div>
                   <div class="col-md-4">
                    <label class="input-text">Designation</label>
                  <div class="form-group">
                      <input type="text" name="line1" value="<?php echo $line1;?>" class="from-control custom-type">
                   </div>
                  </div>
                  </div>
                  <div class="row">
                 
                  <div class="col-md-4">
                   <label class="input-text">Status</label>
                <br>
                 <label class="switch">
				 <input type="checkbox" class="form-control" name="status[]" id="status" value="1" <?php if($status==  1) { echo "checked"; } ?>/>
				<div id="status_status"></div>
                  
                   </label>
                  </div>
                  <div class="col-md-12">
                    <label class="input-text">About</lable>
                      <div class="form-group">
                          <textarea name="content" class="from-control custom-type" rows="5" cols="100%"><?php echo $content;?></textarea>
                   
                  </div>                          
                 <button type="submit" name="submit" value="add" class="btn btn-submit">Add</button>  
                  </div>
              </div>
              </form>
              </div>
          </div>
        
      </div>
    </section>
  </div>
  <!-- /.content-wrapper -->
 <?php include 'footer.php';?>
</div>
	

<script src="bower_components/jquery-ui/jquery-ui.min.js"></script>
<script>
  $.widget.bridge('uibutton', $.ui.button);
</script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="dist/js/adminlte.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="lib/ToggleSwitch.css"/>
<script src="lib/ToggleSwitch.js"></script>
<script>
$(function(){
$("#status").toggleSwitch();
$("#myonoffswitch2").toggleSwitch();
});
</script>


</body>
</html>
