<?php
session_start();
if( $_SESSION["user"] == "" || $_SESSION["passwd"] == "" || $_SESSION["admin"] == false ) {
   mysqli_close($conn);
   header("Location: index.php");
}

$Title  = $_SESSION["module"] = "Importar Comprobantes";
$width = "80%";

$user_id = $_SESSION["user_id"];
$admin = $_SESSION["admin"];

//$filename = isset($_POST['filename']) ? $_POST["filename"]  : ""; 

include_once('db_config.php');
ini_set('max_execution_time', 120);

$admin = false;
include 'contableHeader.php'; 
?>
<style>
#modal_wait {
    background: transparent; 
    width: 0px;
    height: 0px;
    position: absolute;
    border-radius:10px;
    z-index:1000;
    top:0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
}

.dropdown-content {
  overflow-x: hidden;
}

#therow {
   background-color: white; 
}

#claim:hover {
   background-color: #cfd8dc; 
  background-color: #16a2d6;
  color: white;
}

.data {
   margin-top: 0; 
   margin-bottom: 0;                       
   padding-left: 5%;
}


.margin {
   margin-top: 0; 
   margin-bottom: 0; 
}

.margin-label {
   margin-top: 5px; 
}

input[type=select]:disabled + label,
input[type=select]:disabled,
input[type=textarea]:disabled,
input[type=textarea] + label,
textarea.materialize-textarea:disabled + label, 
input[type=text] + label,
input[type=text]:disabled + label {
   color: #009688;
   color: grey;
}

textarea.materialize-textarea:disabled,
input[type=text]:disabled {
   color: #303030;
}

input#filename.file-path.validate {
   height: 30px;
   margin-top:5px;
}

#fileToUpload {
   opacity: 0;
   position: absolute;
   z-index: -1;
   margin-top:5px;
}

.file-field input.file-path {
width:76%  !important; 
}

.file-field input[type=file] {
    width:76% !important;
}

.btn {
    padding-top:0px;
}
.btn:hover, .btn-large:hover, .btn-small:hover {
    background-color: #0F7095;
    padding-top:0px;
}
.select-dropdown.dropdown-content li.selected:hover {
    background-color: rgba(0, 0, 0, 0.03);
    color: black;
}

.select-dropdown.dropdown-content li:hover {
    background-color: rgba(0, 0, 0, 0.08);
    background-color: #16a2d6;
    color: white;
}
.dropdown-content li > a, .dropdown-content li > span {
    color: inherit;
}

#contextMenu li:hover, #contextMenu li.active {
    background-color: #16a2d6 !important;
    color: white;
    font-size: 14px !important;
}
.dropdown-content li > a, .dropdown-content li > span {
//   width:200px !important;
   font-size: 14px !important;
}
.activetr {
   background-color: rgb( 0, 0, 0, 0.2 ) !important;
}
</style>

<style>
input,
select,
textarea {
  color: blue;
  font: inherit;
}

.dropdown-content {
 overflow-x: hidden;
}

#claim:hover {
   background-color: #cfd8dc; 
}

.data {
   margin-top: 10px; 
   margin-bottom: 0;                       
   padding-left: 5%;
}


.margin {
   margin-top: 0; 
   margin-bottom: 0; 
}

.margin-label {
   margin-top: 5px; 
}

.row {
   padding: 0;
   margin: 0;
}

.sticky {
/*
  position: fixed;
  top: 0;
  width: 100%
  overflow: hidden;
  margin-left:10%;
  */
}
input[type=select]:disabled + label,
input[type=select]:disabled,
input[type=textarea]:disabled,
input[type=textarea] + label,
textarea.materialize-textarea:disabled + label, 
input[type=text] + label,
input[type=text]:disabled + label {
   color: grey;
}



textarea.materialize-textarea:disabled,
input[type=text]:disabled {
   color: #303030;
   color: #8c0000;
}

html {
  font-family: Calibri, "Roboto", sans-serif;
  font-weight: normal;
} 

a:hover, a:active {
    color: #23C5A0;
  color: blue;
}

a {
    color: rgb(40, 168, 127);
}

:focus {
    outline: none;
}

/*
nav {
  color: #fff;
  background-color: #00695c;
  width: 100%;
  border-color: green;
}

*/

.dashboard {
   background-color: #f4f4f4;
}

.nav-item,
.dropdown-trigger {
// text-transform: uppercase;
}

//#cover {position: fixed; height: 100%; width: 100%; top:0; left: 0; background: #000; z-index:9999;}
.shadow {
-webkit-box-shadow: 6px 7px 19px -8px rgba(0,0,0,0.63);
-moz-box-shadow: 6px 7px 19px -8px rgba(0,0,0,0.63);
box-shadow: 6px 7px 19px -8px rgba(0,0,0,0.63);
}
</style>

<?php
   echo "<link href='http://fonts.googleapis.com/icon?family=Material+Icons' rel='stylesheet'>";
   echo "<div id='content'>"; 
   echo "<div class='container' style='width:' . $width . ';' >"; 
   echo "<div style='height: 15px;'></div>"; 
   echo "<div class='col l9 m12 s12'>"; 
      echo "<div class='page-content'>"; 
            echo "<div class='row'>"; 
               echo "<div class='input-field col s3'>";
                  echo "<select id='tipo'>";
                     echo "<option value='' disabled>Tipo de Comprobante</option>";        
                     echo "<option value='1'>Ventas</option>";
                     echo "<option value='2'>Compras</option>";
                     echo "<option value='5'>Retenciones</option>";
                  echo "</select>";
                  echo "<label>Tipo Comprobante</label>";
               echo "</div>";
            echo "</div>";
            echo "<div class='row'>"; 
               echo "<div id='seccion_upload' class=''>";
                  echo "<div style='height:5px;'></div>";
                  echo "<div class='row'>";
                     echo "<div class='file-field input-field' style='padding-left:15px;'>";
                        echo "<div class='btn'>";
                           echo "<span>Archivo</span>";
                           echo "<input id='filesToUpload' type='file' accept='.csv,.txt'>";
                        echo "</div>";

                        echo "<div class='file-path-wrapper' style='display:inline;'>";
                           echo "<input id='filename' class='file-path validate' type='text' style='width:73% !important; padding-top:5px; display:inline;' onchange='verify()'>";
                        echo "</div>";

                        echo "<div class='btn right' id='upload' onClick='onSubmit(this);' style='margin-left:10px; display:inline;' disabled>";
                             echo "<span>Actualizar</span>"; 
                        echo "</div>";
                     echo "</div>";
                  echo "</div>"; 
                  echo "<div style='height:15px;'></div>";
               echo "</div>"; 
          echo "</div>";
     echo "</div>";   
   echo "</div>"; 

   echo "<div id='modal_wait' style='display:none;'>";
      echo "<div class='modal-content' style='background-color:transparent; border-radius:10px;'>";
         echo "<div class='preloader-wrapper big active'>";
            echo "<div class='spinner-layer spinner-blue-only'>"; 
               echo "<div class='circle-clipper left'>"; 
                  echo "<div class='circle'></div>"; 
                  echo "</div><div class='gap-patch'>"; 
                     echo "<div class='circle'></div>"; 
                     echo "</div><div class='circle-clipper right'>"; 
                     echo "<div class='circle'></div>"; 
                  echo "</div>"; 
               echo "</div>"; 
            echo "</div>"; 
         echo "</div>"; 
      echo "</div>"; 
   echo "</div>"; 



   echo "</div>"; 
   echo "</div>"; 
   mysqli_close($conn);
?>     
</style>
  <script type="text/javascript" src="js/jquery.min.js"></script>
  <script type="text/javascript" src="js/jquery.form.js"></script> 
  <script type="text/javascript" src="materialize/1.0.0/js/materialize.min.js"></script>

<script>
  $(document).ready(function() {

      $('.sidenav').sidenav();
      $('.dropdown-trigger').dropdown({
         "hover": true,
         "constrainWidth": false,
         "coverTrigger": false
      });
      $('.modal-trigger').modal({
          dismissible: false,
          preventScrolling: true,
          opacity: .7, // Opacity of modal background
          in_duration: 300, // Transition in duration
          out_duration: 200, // Transition out duration
          starting_top: '40%'
       });         
      $('select').formSelect();

      $('#uploadBtn').click(function(){
          $("#fileToUpload").click();
      });

      $('#fileToUpload').change(function() {
          console.log( $('#fileToUpload')[0].files[0].name );
          var filename = $('#fileToUpload')[0].files[0].name;
          $('#filename').val( filename );
          $('#upload').removeAttr('disabled');
//          $("#upload").attr("disabled",false );
          console.log( "DISABLE" );
      });

      /*
      $('#filesToUpload').change(function() {
          console.log( $('#filesToUpload')[0].files[0].name );
          var filename = $('#filesToUpload')[0].files[0].name;
          $('#filename').val( filename );
          $('#upload').removeAttr('disabled');
      });
      */
      $("#cover").fadeOut(200);
      $("#upload").attr("disabled",true );
  });  

   // Post to the provided URL with the specified parameters.
   function post(path, parameters) {
      var form = $('<form></form>');

       form.attr("method", "post");
       form.attr("action", path);

       $.each(parameters, function(key, value) {
           var field = $('<input></input>');
           field.attr("type", "hidden");
           field.attr("name", key);
           field.attr("value", value);
           form.append(field);
       });

       // The form needs to be a part of the document in
       // order for us to be able to submit it.
       $(document.body).append(form);
      form.submit();
   };

/*
   function Upload(e) {

      var name = $("#filename").val();
      if( name == "" ) {
         Materialize.toast('No se indico ningun archivo', 3000, 'rounded') 
         return;
      }

      $.ajax({
          type: "POST",
          url: "upload.php",
          data: { 'fileToUpload': name  },
              success: function(data){
                           var msg = JSON.parse(data);
                           if( msg.error == 0 ) {
                              Materialize.toast('El archivo fue cargado correctamente', 3000, 'rounded') 
                           }
                           else {
                             Materialize.toast(msg.errortext, 3000, 'rounded') 
                           } 
                       }
      });
   }
  */

   function verify(el) {
      var file_data = $('#filesToUpload').prop('files')[0];   
      if( file_data != "" ) 
         $("#upload").attr("disabled",false );
      else
         $("#upload").attr("disabled",true );

   }

   function onSubmit(el) {
      var file_data = $('#filesToUpload').prop('files')[0];   
      var tc = $("#tipo option:selected").val(); 

      if( tc == 1 || tc == 2 || tc == 5) {
         $( "#modal_wait" ).css( 'display', 'block' );
        var form_data = new FormData();                  
        form_data.append('fileToUpload', file_data );
        form_data.append('dbName', name );
        form_data.append('type', tc  );
  
        console.dir( file_data );
        console.dir( name );

        $.ajax({
            url: 'contableUploadCompras.php', // point to server-side PHP script 
            dataType: 'text',  // what to expect back from the PHP script, if anything
            cache: false,
            contentType: false,
            processData: false,
            data: form_data,                         
            type: 'post',
               success: function(php_script_response){
                 console.log( php_script_response );
                 $("#upload").attr("disabled",true );
                 M.toast({html: 'Importacion Exitosa',displayLength:4000, classes: 'rounded'}) 
     
  //               $('#filesToUpload').prop('files')[0] = "";   
                 $('#filename').val( "" );
                 $('#nombre').val( "" );
  
              },
              complete: function(){
                 $( "#modal_wait" ).css( 'display', 'none' );
              }
  
        });
      }
      return false;
  };

</script>
</body>
</html>

