<?php
  // NOTE: This form is based on uploadForm2_iframe.php but modified to be called and displayed through admin.php

  global $CURRENT_USER, $TABLE_PREFIX;

  // Plugin security check
  if (!@$CURRENT_USER)                       { die("You must be logged in to use this feature!"); }
  //if (!@$GLOBALS['CURRENT_USER']['isAdmin']) { die("You must be an administrator to use this feature!"); }
  if (!count(debug_backtrace()))  { die("This is a library file and not meant to be run directly!"); }  

  //
  $table           = '_csv_import_jobs'; // hardcoded as it's always the same, ignore: @$_REQUEST['table'];
  $field           = 'csv_upload';       // hardcoded as it's always the same, ignore: @$_REQUEST['field'];
  $recordNum       = intval( @$_REQUEST['num'] );
  $preSaveTempId   = @$_REQUEST['preSaveTempId'];
  $submittedForm   = @$_REQUEST['REQUEST_METHOD'] == 'POST' || @$_REQUEST['submitForm'];
  $errorsAndAlerts = '';

  if ($recordNum) {  // if a $recordNum was supplied, ensure that the user owns it before doing anything!
    if (!@$CURRENT_USER) { die("You must login to modify a record!"); }
    $record = mysql_query_fetch_row_assoc("SELECT * FROM {$TABLE_PREFIX}$table WHERE num = '$recordNum'");
    //if (!$record || $record['createdByUserNum'] != $CURRENT_USER['num']) { die("Invalid recordNum"); }
  }

  // error checking
  if (!$recordNum && !$preSaveTempId)        { die("No 'recordNum' or 'preSaveTempId' value was specified!"); }
  if ($submittedForm && !preg_match("/multipart\/form-data/", @$_SERVER['CONTENT_TYPE'])) {
    die("Upload Error: &lt;form&gt; tag must have enctype=\"multipart/form-data\"");
  }

  // save uploads
  foreach (getUploadInfoArrays() as $uploadInfo) { // add uploads
    $errorsAndAlerts .= saveUpload($table, $field, $recordNum, $preSaveTempId, $uploadInfo, $newUploadNums);
  }

  // remove uploads
  if (@$_REQUEST['removeUpload']) { // delete upload
    $uploadNum = @$_REQUEST['removeUpload'];
    removeUpload($uploadNum, $recordNum, $preSaveTempId);
  }

  // load uploads
  $uploads = getUploadRecords($table, $field, $recordNum, $preSaveTempId);

?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=8"><?php // Force IE to not use quirks-mode ?>
<title></title>
<style>
  body, td { font-family: arial; font-size: 12px; background-color: #fff !important; }
  .btn-file {
    position: relative;
    overflow: hidden;
  }
  .btn-file input[type=file] {
    position: absolute;
    top: 0;
    right: 0;
    min-width: 100%;
    min-height: 100%;
    font-size: 100px;
    text-align: right;
    filter: alpha(opacity=0);
    opacity: 0;
    background: red;
    cursor: inherit;
    display: block;
  }
</style>

<!-- CSS -->
<?php includeCDN('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.min.css" integrity="sha512-cp9JSDyi0CDCvBfFKYLWXevb3r8hRv5JxcxLkUq/LEtAmOg7X0yzR3p0x/g+S3aWcZw18mhxsCXyelKWmXgzzg==" crossorigin="anonymous" referrerpolicy="no-referrer" />'); ?>
<link rel="stylesheet" href="<?php echo noCacheUrlForCmsFile("3rdParty/clipone/css/".$GLOBALS['SETTINGS']['cssTheme']); ?>" id="skin_color">
<?php includeCDN('<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>'); ?>
<script>

  function submitUploadForm(message) {
    if (message == undefined) { message = "Loading, please wait..."; }
    document.getElementById('uploadField').style.display = 'none'; // hide upload field
    document.getElementById('uploadMessage').innerHTML = message;  // display message
    document.uploadForm.submit();
  }

  function eraseUploadNum(uploadNum) {
    document.uploadForm.removeUpload.value = uploadNum;
    submitUploadForm();
  }

  // resize iframe on load
  $(document).ready(function() {
    autosizeIframe();  // resize on page load
    if (parent.csvImport_updatePreview) { parent.csvImport_updatePreview(); }
  });
  //$(window).load(function()    { autosizeIframe(); }); // resize after all images loaded

  function autosizeIframe() {
    //return false; // disable resizing
    var padding         = 0;
    var contentHeight   = $(document.body).outerHeight(true) + padding;
    //var contentHeight   = $(document).height();
    $(window.frameElement).animate({ height: contentHeight + 'px' }, '100');
  }

</script>
</head>
<body style="margin: 0px; padding: 0px;">

<form method="post" name="uploadForm" action="?" enctype="multipart/form-data">
<input type="hidden" name="menu"          value="<?php echo htmlencode(@$_REQUEST['menu']) ?>">
<input type="hidden" name="submitForm"    value="1">
<input type="hidden" name="_pluginAction" value="<?php echo htmlencode(@$_REQUEST['_pluginAction']) ?>">
<input type="hidden" name="num"           value="<?php echo $recordNum ?>">
<input type="hidden" name="preSaveTempId" value="<?php echo $preSaveTempId ?>">
<input type="hidden" name="removeUpload" id="removeUpload" value="0">

<div>
    
    <span class="file-input btn btn-default btn-file">
        Upload File <input type="file" name="<?php echo $field ?>[]" name="<?php echo $field ?>[]" id="uploadField" onchange="submitUploadForm();">
    </span>
    <span id="uploadMessage" style="clear: both"></span>

    <?php foreach ($uploads as $upload): ?>
      <?php echo $upload['filename'] ?>
      <a class="text-danger" href="#" onclick="eraseUploadNum('<?php echo $upload['num'] ?>');"> remove</a>
    <?php endforeach ?>

    <?php if (@$errorsAndAlerts): ?>
      <p class="text-danger" style="padding: 10px 0"><?php echo $errorsAndAlerts; ?></p>
    <?php endif ?>

</div>

</form>
</body>
</html>
