<?php
    session_start();
    include "../../db.php";
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('M j, Y g:i a');

    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) ) {
        header("Location: logout.php");
        exit();
    }
    if ( $_SESSION['utype'] != 'master_admin' && $_SESSION['uref'] != 'fssdf4543frts' ) {
        header("Location: logout.php");
        exit();
    }

    $info_for = $_GET['info_for'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
 <?php include "includes/meta.php"; ?>             
    <?php include "includes/css.php"; ?>
    <link href="css/all.min.css" rel="stylesheet" type="text/css">
    <link href="css/alltable.css" rel="stylesheet"> 
       
 
    
</head>

<body id="page-top" class="mt-55">

  <!--==========================
  Header
  ============================-->
  
  <?php include "includes/header2.php"; ?>

  <div class="container">
            <div class="row mx-0 just ify-content-center">
                <div class="col-12 col-sm-12 col-md-12">
                        <div class="card-body">

                        
                            <h1 class="h3 mb-2 text-gray-800 text-center" style="margin-top: 90px">Extra Data For User</h1>
                            <hr>
                      <?php
                          $edit_run = mysqli_query($con, "SELECT * FROM user2 WHERE u_id = '$info_for'");
                              
                          if ($edit_row = mysqli_fetch_assoc($edit_run))
                            {  
                      ?>
                         
                            <form method='post' action='' enctype='multipart/form-data'>
                                 <font color="black">WebSite Short Name</font>
                                <input type="text" class="form-control mb-2"  value="<?php echo $edit_row['u_shortname']; ?>" name="short_name">
                                <font color="black">Upload Website Logo
                                  <?php
                                  if (!empty($edit_row['u_flogo']))
                                    {
                                    echo " <a href='../upload/logo/$edit_row[u_flogo]' title='Click to open' target='_blank'> (<b>Already Uploaded - </b>$edit_row[u_flogo]</a> )
                                    ";
                                    }
                                 ?>
                                </font>
                                <input type='file' name='f_file' class="form-control"><br>
                                <font color="black">Upload Title Icon
                                  <?php
                                  if (!empty($edit_row['u_slogo']))
                                    {
                                    echo " <a href='../upload/logo/$edit_row[u_slogo]' title='Click to open' target='_blank'> (<b>Already Uploaded - </b>$edit_row[u_slogo]</a> )
                                    ";
                                    }
                                 ?>
                                </font>
                                <input type='file' name='s_file' class="form-control"><br>
                                <font color="black">Logo Url</font>
                                <input type="text" class="form-control mb-2"  value="<?php echo $edit_row['u_logoUrl']; ?>" name="logo_url">
                               
                               <br>

                                <input type='submit' value='Update' class="btn btn-primary" name='u_update'>

                                <a href="admin_lvl2.php?see_id=<?php echo $info_for; ?>" class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i> Back </a>    

                            </form>
                        <?php 
                        }
                        else
                        {  ?>
                          
                            <form method='post' action='' enctype='multipart/form-data'>
                                <font color="black">WebSite Short Name</font>
                                <input type="text" class="form-control mb-2"  placeholder="WebSite Short Name" name="short_name">
                                <font color="black">Upload Website Logo</font>
                                <input type='file' name='f_file' class="form-control"><br>
                                <font color="black">Upload Title Icon</font>
                                <input type='file' name='s_file' class="form-control"><br>
                                <font color="black">Logo Url</font>
                                <input type="text" class="form-control mb-2"  placeholder="Logo Url" name="logo_url">
                               
                                
                                <br>

                                <input type='submit' value='Submit' class="btn btn-primary" name='u_submit'> 
                                <a href="admin_lvl2.php?see_id=<?php echo $info_for; ?>" class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i> Back </a>   

                            </form>


                        <?php 
                        }   ?> 


            </div>
        </div>

    </div>

</div>
<br><br><br><br>
<?php include "includes/ft.php"; ?>



  



     <script>
        function checkFileDetails() {
            var fi = document.getElementById('file');
            if (fi.files.length > 0) {
                for (var i = 0; i <= fi.files.length - 1; i++) {
                    var fileName, fileExtension, fileSize, fileType, dateModified;
                    fileName = fi.files.item(i).name;
                    fileExtension = fileName.replace(/^.*\./, '');
                    if (fileExtension == 'png' || fileExtension == 'jpg' || fileExtension == 'jpeg') {
                        readImageFile(fi.files.item(i));
                    }
                }
                function readImageFile(file) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        var img = new Image();
                        img.src = e.target.result;
                        img.onload = function () {
                            var imgWidth = this.width;
                            var imgHeight = this.height;

                            if ( imgWidth == 1200 && imgHeight == 512 || imgWidth == 1500 && imgHeight == 640 ) {
                                var imgCheck = "<i class='fas fa-check-circle text-success'></i>";
                            } else {
                                var imgCheck = "<i class='fas fa-times-circle text-danger'></i>";
                            }
                            document.getElementById('fileInfo').innerHTML =
                                document.getElementById('fileInfo').innerHTML + '<br> ' +
                                'Name: <b>' + file.name + '</b> <br>' +
                                'Size: <b>' + Math.round((file.size / 1024)) + '</b> KB <br>' +
                                'Dimension: <b>' + imgWidth + '</b> x <b>' + imgHeight + '</b> ' + imgCheck + ' <br>';
                        }
                    };
                    reader.readAsDataURL(file);
                }
            }
        }
    </script>




    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.easing.min.js"></script>
    <script src="js/sb-admin-2.min.js"></script>
    <script src="js/sweetalert2.min.js"></script>
    <script src="js/jquery.dataTables.min.js"></script>
    <script src="js/dataTables.bootstrap4.min.js"></script>
    <script src="js/datatables.mark.min.js"></script>
    <script src="js/jquery-ui-custom.min.js"></script>
    <script src="js/custom.js"></script>

     <script src="js/mobile-nav/mobile-nav.js"></script>


</body>
</html>



<?php 

      if(isset($_POST['u_submit']))
       {
          
            if (!empty($_FILES["f_file"]["name"]) AND empty($_FILES["s_file"]["name"]))
            {
                         #file name with a random number so that similar dont get replaced
                             $fname = rand(1000,10000)."-".$_FILES["f_file"]["name"];
                 
                        #temporary file name to store file
                             $tname = $_FILES["f_file"]["tmp_name"];
                           
                        #upload directory path
                            $uploads_dir = '../upload/logo';
                        #TO move the uploaded file to specific location
                            move_uploaded_file($tname, $uploads_dir.'/'.$fname);


                    $sql = "INSERT INTO user2 (u_logoUrl, u_shortname, u_id, u_flogo) VALUES ('$_POST[logo_url]', '$_POST[short_name]', '$info_for', '$fname') ";

               
                   

            }
            elseif(empty($_FILES["f_file"]["name"]) AND !empty($_FILES["s_file"]["name"])){

                #file name with a random number so that similar dont get replaced
                             $sname = rand(1000,10000)."-".$_FILES["s_file"]["name"];
                 
                        #temporary file name to store file
                             $tsname = $_FILES["s_file"]["tmp_name"];
                           
                        #upload directory path
                            $uploads_dir = '../upload/logo';
                        #TO move the uploaded file to specific location
                            move_uploaded_file($tsname, $uploads_dir.'/'.$sname);


                    $sql = "INSERT INTO user2 (u_logoUrl, u_shortname, u_id, u_slogo) VALUES ('$_POST[logo_url]', '$_POST[short_name]', '$info_for', '$sname') ";


            }
            elseif(!empty($_FILES["f_file"]["name"]) AND !empty($_FILES["s_file"]["name"])){

                  #file name with a random number so that similar dont get replaced
                             $fname = rand(1000,10000)."-".$_FILES["file"]["name"];
                 
                        #temporary file name to store file
                             $tname = $_FILES["file"]["tmp_name"];
                           
                        #upload directory path
                            $uploads_dir = '../upload/logo';
                        #TO move the uploaded file to specific location
                            move_uploaded_file($tname, $uploads_dir.'/'.$fname);




                            #file name with a random number so that similar dont get replaced
                             $sname = rand(1000,10000)."-".$_FILES["s_file"]["name"];
                 
                        #temporary file name to store file
                             $tsname = $_FILES["s_file"]["tmp_name"];
                           
                        #upload directory path
                            $uploads_dir = '../upload/logo';
                        #TO move the uploaded file to specific location
                            move_uploaded_file($tsname, $uploads_dir.'/'.$sname);


                    $sql = "INSERT INTO user2 (u_logoUrl, u_shortname, u_id, u_slogo, u_flogo) VALUES ('$_POST[logo_url]', '$_POST[short_name]', '$info_for', '$sname', '$fname') ";

            }
            else
            {

                     $sql = "INSERT INTO user2 (u_logoUrl, u_shortname, u_id) VALUES ('$_POST[logo_url]', '$_POST[short_name]', '$info_for') ";
            }
               




               
    

                if(mysqli_query($con, $sql))
                {
                    ?>
                    <script>
                        alert("Record updated successfully");
                        window.location = 'extra_info.php?info_for=<?php echo $info_for; ?>';
                    </script>
                    <?php
                }
                else
                {
                    ?>
                    <script>
                        alert('OOPS !!!!');
                        /*setTimeout(() => {
                            window.location = 'extra_info.php';
                        }, 500); */
                    </script>
                    <?php
                }
    }
 ?>



<?php 

      if(isset($_POST['u_update']))
       {
          
           if (!empty($_FILES["f_file"]["name"]) AND empty($_FILES["s_file"]["name"]))
            {
                         #file name with a random number so that similar dont get replaced
                             $fname = rand(1000,10000)."-".$_FILES["f_file"]["name"];
                 
                        #temporary file name to store file
                             $tname = $_FILES["f_file"]["tmp_name"];
                           
                        #upload directory path
                            $uploads_dir = '../upload/logo';
                        #TO move the uploaded file to specific location
                            move_uploaded_file($tname, $uploads_dir.'/'.$fname);


                    $edit_sql = "UPDATE user2 SET u_logoUrl = '$_POST[logo_url]', u_shortname ='$_POST[short_name]', u_flogo ='$fname' WHERE u_id = '$info_for'";

               
                   

            }
            elseif(empty($_FILES["f_file"]["name"]) AND !empty($_FILES["s_file"]["name"])){

                #file name with a random number so that similar dont get replaced
                             $sname = rand(1000,10000)."-".$_FILES["s_file"]["name"];
                 
                        #temporary file name to store file
                             $tsname = $_FILES["s_file"]["tmp_name"];
                           
                        #upload directory path
                            $uploads_dir = '../upload/logo';
                        #TO move the uploaded file to specific location
                            move_uploaded_file($tsname, $uploads_dir.'/'.$sname);


                    $edit_sql = "UPDATE user2 SET u_logoUrl = '$_POST[logo_url]', u_shortname ='$_POST[short_name]', u_slogo ='$sname' WHERE u_id = '$info_for'";


            }
            elseif(!empty($_FILES["f_file"]["name"]) AND !empty($_FILES["s_file"]["name"])){

                  #file name with a random number so that similar dont get replaced
                             $fname = rand(1000,10000)."-".$_FILES["file"]["name"];
                 
                        #temporary file name to store file
                             $tname = $_FILES["file"]["tmp_name"];
                           
                        #upload directory path
                            $uploads_dir = '../upload/logo';
                        #TO move the uploaded file to specific location
                            move_uploaded_file($tname, $uploads_dir.'/'.$fname);




                            #file name with a random number so that similar dont get replaced
                             $sname = rand(1000,10000)."-".$_FILES["s_file"]["name"];
                 
                        #temporary file name to store file
                             $tsname = $_FILES["s_file"]["tmp_name"];
                           
                        #upload directory path
                            $uploads_dir = '../upload/logo';
                        #TO move the uploaded file to specific location
                            move_uploaded_file($tsname, $uploads_dir.'/'.$sname);

                    $edit_sql = "UPDATE user2 SET u_logoUrl = '$_POST[logo_url]', u_shortname ='$_POST[short_name]', u_slogo ='$sname', u_flogo ='$fname' WHERE u_id = '$info_for'";

            }
            else
            {
                


                     echo $edit_sql = "UPDATE user2 SET u_logoUrl = '$_POST[logo_url]', u_shortname ='$_POST[short_name]' WHERE u_id = '$info_for'";
            }
               
 
    

                if(mysqli_query($con, $edit_sql))
                {
                    ?>
                    <script>
                        alert("Record updated successfully");
                        window.location = 'extra_info.php?info_for=<?php echo $info_for; ?>';
                    </script>
                    <?php
                }
                else
                {
                    ?>
                    <script>
                        alert('OOPS !!!!');
                        /*setTimeout(() => {
                            window.location = 'extra_info.php';
                        }, 500); */
                    </script>
                    <?php
                }
    }
 ?>