<?php
session_start();
$companyname=$_SESSION['companyname'];
$companytype=$_SESSION['companytype'];
$companymailid=$_SESSION['companymailid'];
$adminemailid=$_SESSION['emailid'];
$responsible=$_SESSION['responsible_employee'];
$createby=$_SESSION['emailid'];
$createtype=$_SESSION['employeetype'];

if(isset($_POST['insertuser'])){
    
    $firstname=$_POST['firstname'];
    $lastname=$_POST['lastname'];
    $employeename=trim($firstname)." ".trim($lastname);
    $empid=$_POST['empid'];
    $empmailid=$_POST['empmailid'];
    $country1=$_POST['country'];
    $country_array=explode("#@", $country1);
    $country=$country_array[0];
    $phonenumber=$country_array[1]." ".$_POST['phonenumber'];
    $file_name = $_FILES['files']['name'];
    $mapseperate = explode('.',$file_name);
	$s2= $mapseperate[count($mapseperate)-1];

    $name1=$empmailid.".".$s2;
    $target_dir = "../../signupicon/";
    $target_file = $target_dir . $name1;
    move_uploaded_file($_FILES['files']['tmp_name'], $target_file);
    $logopath=$target_file;
    include('../Model/configuration.php');
    include('../../View/mcrypt.php');
    $mcrypt = new MCrypt();
    include ('../../View/emialverified.php');
    $sendmail=new emialverified1();
    $password1="";
    for ($i = 0; $i<8; $i++) 
    {
        $password1 .= mt_rand(0,9);
    }
    $password2= $mcrypt->encrypt($password1);
    $check1=$sendmail->fdemail($empmailid,$password1);
    date_default_timezone_set("Asia/Kolkata");
    $DateandTime=date("Y-m-d H:i:s");
    
    $sql="INSERT INTO `signupdetails`(`companyname`, `adminemailid`, `employeename`, `emp_id`, `emailid`, `mobilenumber`, `companytype`, `designation`, `password`, `first_name`, `last_name`, `nationality`, `dateandtime`, `imagefilepath`, `empcompanytype`)values('".$companyname."','".$companymailid."','".$employeename."','".$empid."','".$empmailid."','".$phonenumber."','".$companytype."','Employee','".$password2."','".$firstname."','".$lastname."','".$country."','".$DateandTime."','".$name1."','Sales')";
    //echo $sql;
    $pdo->query($sql);
}

if(isset($_POST['updateuser'])){
    
    echo $firstname=$_POST['firstname'];
    $lastname=$_POST['lastname'];
    $employeename=trim($firstname)." ".trim($lastname);
    $empid=$_POST['empid'];
    $empmailid=$_POST['empmailid'];
    $country1=$_POST['country'];
    $fileisthere=$_POST['fileisthere'];
    $country_array=explode("#@", $country1);
    $country=$country_array[0];
    $phonenumber=$country_array[1]." ".$_POST['phonenumber'];
    include('../Model/configuration.php');
    include('../../View/mcrypt.php');
    $mcrypt = new MCrypt();
    include ('../../View/emialverified.php');
    $sendmail=new emialverified1();
    $newpassword=$_POST['newpassword'];
    $oldempmailid=$_POST['oldempmailid'];
    $oldempid=$_POST['oldempid'];
    $oldid=$_POST['oldid'];
    $password2= $mcrypt->encrypt($newpassword);
    if($oldempmailid==$empmailid){
        $check1=$sendmail->fdemail($empmailid,$newpassword);
    }
    date_default_timezone_set("Asia/Kolkata");
    $DateandTime=date("Y-m-d H:i:s");
    $sql="update `signupdetails` set `employeename`='".$employeename."', `emp_id`='".$empid."', `emailid`='".$empmailid."', `mobilenumber`='".$phonenumber."', `password`='".$password2."', `first_name`='".$firstname."', `last_name`='".$lastname."', `nationality`='".$country."' where Id='".$oldid."'";
    
    echo $sql;
    $pdo->query($sql);
}

if(isset($_POST['updateuserphoto'])){
    include('../Model/configuration.php');
    $empmailid=$_POST['empmailid'];
    $oldid=$_POST['oldid'];
    $file_name = $_FILES['files']['name'];
    $mapseperate = explode('.',$file_name);
	$s2= $mapseperate[count($mapseperate)-1];

    $name1=$empmailid.".".$s2;
    $target_dir = "../../signupicon/";
    $target_file = $target_dir . $name1;
    move_uploaded_file($_FILES['files']['tmp_name'], $target_file);
    $logopath=$target_file;
    $sql="update `signupdetails` set  `imagefilepath`='".$name1."' where  Id='".$oldid."'";
    $pdo->query($sql);
    
}
