function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

import React, { useState, useEffect, useRef } from "react";
import PropTypes from "prop-types";
import _ from "lodash";
import { styled } from "@mui/material/styles";
import AttachFileIcon from "@mui/icons-material/AttachFile";
import IconButton from "@mui/material/IconButton";
import Stack from "@mui/material/Stack";
import { IdUnico } from "@intersoft/utiles";
import { Depura } from "@intersoft/depura"; //import Notif from "./../notif/Notif";

import { Notif } from "@intersoft/notif";
import IconButtonBack from "./../icon/IconButtonBack";
import PhotoCamera from '@mui/icons-material/PhotoCamera'; //------------------------------------------------------------------------------------

/*
dos botones, 
uno para mostrar pdf, 
otro para seleccionarlo y guardarlo en google drive

empresa
urlFileUp
urlPdf
onNewId
*/
//------------------------------------------------------------------------------------

var TAG = "SelArchPdf>"; //------------------------------------------------------------------------------------

var dep = new Depura(TAG, true);
dep.version(); //------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------

var V = "1.0.0"; //------------------------------------------------------------------------------------

var URL_DEV = "https://localhost:3003/fup";
var URL_DEV2 = "https://localhost:3003/fup2"; // no sube a drive...

var URL_PROD = "https://preofertas.com.ar:3003/fup"; //----------------------------------------------------------

var Input = styled("input")({
  display: "none"
}); //----------------------------------------------------------

var SelArchPdf = function SelArchPdf(props) {
  //--------------------------------------------------------
  var empresa = props.empresa,
      urlFileUp = props.urlFileUp,
      urlPdf = props.urlPdf,
      row = props.row,
      showMostrar = props.showMostrar,
      showSubir = props.showSubir;
  var uploadInputRef = useRef(null);
  var id1 = IdUnico.getCorto("-btn-pdf-");
  var id2 = IdUnico.getCorto("-btn-up-pdf-");
  dep.log("id1: " + id1);
  dep.log("id2: " + id2); //--------------------------------------------------------

  var getFormData = function getFormData(file) {
    dep.logTit("getFormData");
    dep.logC("file");
    dep.log(file);
    var formData = new FormData();
    formData.append("sampleFile", file);
    formData.append("empresa", empresa);
    console.log(formData);
    return formData;
  }; //--------------------------------------------------------


  var enviar = /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(formData) {
      var promise;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              dep.logTit("enviar");
              _context2.next = 3;
              return new Promise( /*#__PURE__*/function () {
                var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(resolve, reject) {
                  var response, data;
                  return regeneratorRuntime.wrap(function _callee$(_context) {
                    while (1) {
                      switch (_context.prev = _context.next) {
                        case 0:
                          _context.prev = 0;
                          _context.next = 3;
                          return fetch(urlFileUp, {
                            method: "POST",
                            body: formData
                          });

                        case 3:
                          response = _context.sent;

                          if (!response.ok) {
                            _context.next = 13;
                            break;
                          }

                          dep.logG("Todo bien");
                          _context.next = 8;
                          return response.json();

                        case 8:
                          data = _context.sent;
                          //debugger;
                          dep.obj(data);
                          resolve(data);
                          _context.next = 15;
                          break;

                        case 13:
                          dep.log("Respuesta de red OK pero respuesta de HTTP no OK");
                          reject(null);

                        case 15:
                          _context.next = 21;
                          break;

                        case 17:
                          _context.prev = 17;
                          _context.t0 = _context["catch"](0);
                          dep.err("Hubo un problema con la petición Fetch:" + _context.t0.message);
                          reject(null);

                        case 21:
                        case "end":
                          return _context.stop();
                      }
                    }
                  }, _callee, null, [[0, 17]]);
                }));

                return function (_x2, _x3) {
                  return _ref2.apply(this, arguments);
                };
              }());

            case 3:
              promise = _context2.sent;
              return _context2.abrupt("return", promise);

            case 5:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }));

    return function enviar(_x) {
      return _ref.apply(this, arguments);
    };
  }(); //--------------------------------------------------------


  var guardarArchivo = /*#__PURE__*/function () {
    var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(file) {
      var formData, rta;
      return regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              dep.logTit("guardar"); // 1- cremos el formData

              formData = getFormData(file);
              dep.obj(formData); // 2- enviamos el formData

              _context3.next = 5;
              return enviar(formData);

            case 5:
              rta = _context3.sent;

              if (rta) {
                dep.logC(rta); // enviar rta como evento, para que grabe link/id en BD
                // y el id del articulo en la tabla de articulos

                if (_.isFunction(props.onNewId)) {
                  props.onNewId(row.id_articulo, rta);
                }
              }

            case 7:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    }));

    return function guardarArchivo(_x4) {
      return _ref3.apply(this, arguments);
    };
  }(); //--------------------------------------------------------


  var doOnnChange = function doOnnChange(e) {
    //debugger;
    dep.logTit("->doOnnChange");
    var file = e.target.files[0];
    dep.log(file);

    if (file) {
      if (_.isFunction(props.onNewId)) {
        var reader = new FileReader();
        reader.onloadend = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
          var b64, guardar;
          return regeneratorRuntime.wrap(function _callee4$(_context4) {
            while (1) {
              switch (_context4.prev = _context4.next) {
                case 0:
                  //data:application/pdf;base64,JVBERi0xLjMKJcT
                  b64 = reader.result.split(","); //const strTob64 = b64[1];

                  _context4.next = 3;
                  return Notif.showSavePdf("Guardar archivo PDF", b64);

                case 3:
                  guardar = _context4.sent;
                  //debugger;
                  dep.log(guardar);

                  if (guardar) {
                    dep.log("Guardar");
                    guardarArchivo(file);
                  }

                case 6:
                case "end":
                  return _context4.stop();
              }
            }
          }, _callee4);
        }));
        reader.readAsDataURL(file);
      }
    }
  }; //----------------------------------------------------------


  return /*#__PURE__*/React.createElement(Stack, {
    direction: "column",
    alignItems: "center",
    spacing: 2
  }, /*#__PURE__*/React.createElement("label", {
    htmlFor: "icon-button-file"
  }, /*#__PURE__*/React.createElement(Input, {
    ref: uploadInputRef,
    accept: ".pdf" //accept="application/pdf"
    ,
    inputProps: {
      accept: ".pdf, application/pdf"
    } //id={"icon-button-file" + IdUnico.getCorto("-up-pdf-")}
    ,
    type: "file",
    onChange: doOnnChange
  }), /*#__PURE__*/React.createElement(IconButton, {
    color: "primary",
    "aria-label": "upload picture",
    component: "span"
  }, /*#__PURE__*/React.createElement(PhotoCamera, {
    visible: showMostrar,
    id: id1,
    onClick: function onClick() {
      console.log(row);
      console.log(row.id_articulo);
      console.log(row.pdf);
      console.log(row.titulo);

      if (row.pdf) {
        Notif.infoPdf(row.titulo, row.pdf);
        return true;
      } else {
        Notif.infoTmp("No hay información, o aún no se ha cargado....");
      }
    }
  })), showSubir && /*#__PURE__*/React.createElement(IconButton, {
    color: "primary",
    "aria-label": "upload picture",
    component: "span"
  }, /*#__PURE__*/React.createElement(AttachFileIcon, {
    onClick: function onClick() {
      return uploadInputRef.current && uploadInputRef.current.click();
    }
  }))));
}; //----------------------------------------------------------

/*

*/
//----------------------------------------------------------


SelArchPdf.propTypes = {
  showMostrar: PropTypes.bool,
  showSubir: PropTypes.bool,
  empresa: PropTypes.number,
  urlFileUp: PropTypes.string,
  urlPdf: PropTypes.string,
  row: PropTypes.object,
  onNewId: PropTypes.func.isRequired
}; //----------------------------------------------------------

SelArchPdf.defaultProps = {
  showMostrar: true,
  showSubir: false,
  empresa: -1,
  // Rollos...
  urlFileUp: URL_DEV,
  urlPdf: "",
  row: {}
}; //----------------------------------------------------------

export default SelArchPdf;