<?php
session_start();
$adminemail=$_SESSION['emailid'];
$companyname=$_SESSION['companyname'];
$companytype=$_SESSION['companytype'];
$designation=$_SESSION['designation'];
$companymailid=$_SESSION['companymailid'];
date_default_timezone_set("Asia/Kolkata");
include('../Model/configuration.php');
$sql="SELECT `emailid` FROM `signupdetails`";
$result = $pdo->query($sql);
$emp_id_array=[];
$emailid_array=[];
foreach($result as $row){
    $emp_emailid=$row['emailid'];
    array_push($emailid_array,$emp_emailid);
}
$sql="SELECT `emp_id` FROM `signupdetails` where companyname='".$companyname."'";
$result = $pdo->query($sql);
foreach($result as $row){
    $emp_id=$row['emp_id'];
    array_push($emp_id_array,$emp_id);
}
$sql1="SELECT `id`, `sortname`, `name`, `phonecode` FROM `countries`";
$result1 = $pdo->query($sql1);

?>
<html>
    
    <head>
        <link rel="icon" href="../../Images/cloud-icon-27.png">
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="../../Bootstrap/css/bootstrap.min.css">
        <script src="../../Bootstrap/jquery.min.js"></script>
        <script src="../../Bootstrap/js/bootstrap.min.js"></script>
        <script src="../../Bootbox/bootbox.min.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.min.css">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.min.js"></script>
        <script src="../../Js/bootstrapAlert.js"></script>
        <link href="../../font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet">
        <link href="deal_action_stream.css" rel="stylesheet">
        <script src="deal_action_stream.js"></script>
        
        <link href="../../Clockpicker/bootstrap-clockpicker.min.css" rel="stylesheet" type="text/css" />
        <link href="../../Clockpicker/github.min.css" rel="stylesheet" type="text/css" />
        <script src="../../Clockpicker/jquery-clockpicker.min.js"></script>
    
        <link href="../../Datepicker/bootstrap-datepicker.css" rel="stylesheet" type="text/css" />
        <script src="../../Datepicker/bootstrap-datepicker.js"></script>
    
        
        <link rel="stylesheet" href="../../Selectpicker/bootstrap-select.min.css">
        <link rel="stylesheet" href="../../Selectpicker/bootstrap-select.css.map">
        <script src="../../Selectpicker/bootstrap-select.js"></script>
        <script src="../../Selectpicker/bootstrap-select.js.map"></script>
        
        <style>
            .image-upload > input
            {
                visibility: hidden;
            }
        </style>
        <script>
            $(document).ready(function(){
               $("#selectpicker").selectpicker(); 
            });
        </script>  
        <script>
            function functionupload(input){
                
                if (input.files && input.files[0]) {
                    var reader = new FileReader();
    
                    reader.onload = function (e) {
                        $('#empphoto')
                            .attr('src', e.target.result)
                            .width(150)
                            .height(200);
                    };
    
                    reader.readAsDataURL(input.files[0]);
                }else{
                    
                    reader.onload = function (e) {
                        $('#empphoto')
                            .attr('src', "Image/profile-default-male.png")
                            .width(150)
                            .height(200);
                    };
                }
            }
        </script>
        <style>
            .bootstrap-select .dropdown-toggle .filter-option {
                position: inherit;
            }
            .bootstrap-select>.dropdown-toggle {
                height:35px;
            }
            .bootstrap-select>.btn {
            padding: 3px 12px;
            }
        </style>
    </head>
    
    <body>
        
        <div class="container-fluid">
            
            <form>
                <div class="row">
                    <h4><center>Add User</center></h4>
                </div>
                <div class="row">
                    <div class="col-xs-3">
                        <div class="image-upload">
                            <label for="file-input">
                                <img src="Image/profile-default-male.png" id="empphoto" width="100%" height="auto" style="z-index:1;cursor:pointer"/>
                            </label>
                            <input id="file-input" type="file" onchange="functionupload(this)"  style="z-index:2;margin-top:-35px;" accept="image/png, image/jpeg, image/jpg">
                        </div>
                    </div>
                    <div class="col-xs-9">
                        <div class="col-xs-6" style="padding-top:20px;">
                            <span>First Name</span>
                            <input type="text" id="firstname" class="form-control" name="firstname">
                        </div>
                        <div class="col-xs-6" style="padding-top:20px;">
                            <span>Last Name</span>
                            <input type="text" id="lastname" class="form-control" name="lastname">
                        </div>
                        <div class="col-xs-6" style="padding-top:20px;">
                            <span>Email</span>
                            <input type="text" id="empmailid" onchange="funempmailid()" class="form-control" name="empmailid">
                        </div>
                        <div class="col-xs-6" style="padding-top:20px;">
                            <span>Employee Id</span>
                            <input type="text" id="empid" onchange="funempid()" class="form-control" name="empid">
                        </div>
                        <div class="col-xs-6" style="padding-top:20px;">
                            <span>Country Code</span>
                            <select id="country" class="selectpicker form-control" name="country" data-live-search="true">
                                <?php
                                foreach($result1 as $row1){
                                    $id=$row1['id'];
                                    $name=$row1['name'];
                                    $phonecode=$row1['phonecode'];
                                    if(strtoupper($name)==strtoupper("India")){
                                    ?>
                                    <option value="<?php echo $name.'#@'.$phonecode;?>" selected><?php echo $name." (+".$phonecode.")";?></option>
                                    <?php
                                    }else{
                                    ?>
                                    <option value="<?php echo $name.'#@'.$phonecode;?>"><?php echo $name." (+".$phonecode.")";?></option>
                                    <?php  
                                    }
                                }
                                ?>
                            </select>
                        </div>
                        <div class="col-xs-6" style="padding-top:20px;">
                            <span>Phone Number</span>
                            <input type="text" id="phonenumber" class="form-control" name="phonenumber">
                        </div>
                    </div>
                        
                </div>
                <div style="padding-top:40px;">
                    <center><input type="button" class="btn btn-primary" name="save" id="save" value="Save" style="width:200px;"></center>
                </div>
                
            </form>
            
        </div>
        <script>
            function isValidEmail(email)
            {
                return /^[a-z0-9]+([-._][a-z0-9]+)*@([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,4}$/.test(email)
                    && /^(?=.{1,64}@.{4,64}$)(?=.{6,100}$).*/.test(email);
            }
            function isValidphone(phoneNumber){
                var filter = /^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/;
                if(phoneNumber!=""){
                    if (filter.test(phoneNumber)) {
                      if(phoneNumber.length>4){
                           return true;
                      } else {
                            BootstrapAlert.warning({
                              title: "Warning!",
                              message: "Please Fill the correct PhoneNumber"
                            });
                            return false;
                      }
                    }
                    else {
                        BootstrapAlert.warning({
                          title: "Warning!",
                          message: "Please Fill the correct PhoneNumber"
                        });
                      return false;
                    }
                }else {
                    BootstrapAlert.warning({
                      title: "Warning!",
                      message: "Please Fill the PhoneNumber"
                    });
                  return false;
                }
            }
            function funempmailid(){
                var empmailid=$("#empmailid").val();
                empmailid=empmailid.trim();
                var validateemail=isValidEmail(empmailid);
                if(validateemail==false){
                    BootstrapAlert.warning({
                      title: "Warning!",
                      message: "Please Fill the Email address"
                    });
                    $("#empmailid").val();
                    $("#empmailid").focus();
                    $("#empmailid").css("border-color","red");
                    return;
                }else{
                    $("#empmailid").css("border-color","green");
                }
                if(empmailid==""){
                    BootstrapAlert.warning({
                      title: "Warning!",
                      message: "Please Fill the Email address"
                    });
                    $("#empmailid").val();
                    $("#empmailid").focus();
                    return;
                }
                var emailid_array=<?php echo json_encode($emailid_array);?>;
                if(jQuery.inArray(empmailid, emailid_array)!='-1'){
                    //swal("error","Email Already is there","error");
                    BootstrapAlert.alert({
                      title: "Warning!",
                      message: "Emailid  Already is there"
                    });
                    $("#empmailid").val("");
                    $("#empmailid").focus();
                    return;
                }
            }
            function funempid(){
                var empid=$("#empid").val();
                var emp_id_array=<?php echo json_encode($emp_id_array);?>;
                empid=empid.trim();
                if(empid==""){
                    BootstrapAlert.warning({
                      title: "Warning!",
                      message: "Please Fill the Employee Id"
                    });
                    $("#empid").val();
                    $("#empid").focus();
                    return;
                }
                if(jQuery.inArray(empid, emp_id_array)!='-1'){
                    //swal("error","Email Already is there","error");
                    BootstrapAlert.alert({
                      title: "Warning!",
                      message: "Employee Id Already is there"
                    });
                    $("#empid").val("");
                    $("#empid").focus();
                    return;
                }
            }
        </script>
        <script>
            $(document).on("click", "#save", function(){
                var firstname=$("#firstname").val();
                var lastname=$("#lastname").val();
                var empid=$("#empid").val();
                var empmailid=$("#empmailid").val();
                var validateemail=isValidEmail(empmailid);
                if(validateemail==false){
                    BootstrapAlert.warning({
                      title: "Warning!",
                      message: "Please Fill the Email address"
                    });
                    $("#empmailid").val();
                    $("#empmailid").focus();
                    $("#empmailid").css("border-color","red");
                    return;
                }else{
                    $("#empmailid").css("border-color","green");
                }
               var country=$("#country").val();
               var phonenumber=$("#phonenumber").val();
                var validatephone=isValidphone(phonenumber);
                if(validatephone==false){
                    $("#phonenumber").val();
                    $("#phonenumber").focus();
                    $("#phonenumber").css("border-color","red");
                    return;
                }else{
                    $("#phonenumber").css("border-color","green");
                }
               firstname=firstname.trim();
               if(firstname==""){
                   BootstrapAlert.warning({
                      title: "Warning!",
                      message: "Please Fill the First Name"
                    });
                    $("#firstname").focus();
                    return;
               }
               lastname=lastname.trim();
               if(lastname==""){
                   BootstrapAlert.warning({
                      title: "Warning!",
                      message: "Please Fill the Last Name"
                    });
                    $("#lastname").focus();
                    return;
               }
               funempmailid();
               funempid();
                if(empid==""){
                    BootstrapAlert.warning({
                      title: "Warning!",
                      message: "Please Fill the Employee Id"
                    });
                    $("#empid").val();
                    $("#empid").focus();
                    return;
                }
                if(empmailid==""){
                    BootstrapAlert.warning({
                      title: "Warning!",
                      message: "Please Fill the Email address"
                    });
                    $("#empmailid").val();
                    $("#empmailid").focus();
                    return;
                }
               country=country.trim();
               if(country==""){
                   BootstrapAlert.warning({
                      title: "Warning!",
                      message: "Please Choose the Country"
                    });
                    $("#country").focus();
                    return;
               }
               phonenumber=phonenumber.trim();
               if(phonenumber==""){
                   BootstrapAlert.warning({
                      title: "Warning!",
                      message: "Please Fill the Phone Number"
                    });
                    $("#phonenumber").focus();
                    return;
               }
                var fileUpload = $("#file-input").get(0);
                var files = fileUpload.files;
                if (files.length == 0) {
                    swal("", "Please Choose the Contact Logo", "warning");
                    return;
                }
                var formData = new FormData();
                filesd = document.getElementById("file-input").files[0];
                formData.append("insertuser","filesd");
                formData.append("files",filesd);
                formData.append("firstname",firstname);
                formData.append("lastname",lastname);
                formData.append("empid",empid);
                formData.append("empmailid",empmailid);
                formData.append("country",country);
                formData.append("phonenumber",phonenumber);
                $.ajax({
                    url : "../Control/user.php",
                    type: 'POST',
                    data : formData,
                    cache       : false,
                    contentType : false,
                    processData : false,
                    success:function(data)
                    {
                        swal("","Photo uploaded Successfully", "success");
                        window.location.replace("all_employee.php");
                    },
                    error: function(xhr) {
                        swal("","Photo uploaded Unsuccessfully", "error");
                    }
                });
            });
        </script>
        
    </body>
    
</html>