"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.postLogin = exports.getUserByNameOrRut = exports.getUser = exports.getBiByUserId = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _connection = require("../database/connection");
var getUser = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(req, res) {
    var _req$params$rut$split, _req$params$rut$split2, rut, idRol, filtroRol, idUsuario, pool, result;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _req$params$rut$split = req.params.rut.split('+'), _req$params$rut$split2 = (0, _slicedToArray2["default"])(_req$params$rut$split, 4), rut = _req$params$rut$split2[0], idRol = _req$params$rut$split2[1], filtroRol = _req$params$rut$split2[2], idUsuario = _req$params$rut$split2[3];
            _context.prev = 1;
            _context.next = 4;
            return (0, _connection.getConnection)();
          case 4:
            pool = _context.sent;
            _context.next = 7;
            return pool.request().query("exec [dbo].[LectorQRV2] '".concat(rut, "',").concat(idRol, ",'").concat(filtroRol, "',").concat(idUsuario));
          case 7:
            result = _context.sent;
            res.json(result.recordset);
            _context.next = 14;
            break;
          case 11:
            _context.prev = 11;
            _context.t0 = _context["catch"](1);
            console.log(_context.t0);
          case 14:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[1, 11]]);
  }));
  return function getUser(_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();
exports.getUser = getUser;
var getBiByUserId = /*#__PURE__*/function () {
  var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(req, res) {
    var _req$params$id$split, _req$params$id$split2, id, idRol, filtroRol, idUsuario, pool, result;
    return _regenerator["default"].wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _req$params$id$split = req.params.id.split('+'), _req$params$id$split2 = (0, _slicedToArray2["default"])(_req$params$id$split, 4), id = _req$params$id$split2[0], idRol = _req$params$id$split2[1], filtroRol = _req$params$id$split2[2], idUsuario = _req$params$id$split2[3];
            _context2.prev = 1;
            _context2.next = 4;
            return (0, _connection.getConnection)();
          case 4:
            pool = _context2.sent;
            _context2.next = 7;
            return pool.request().query("exec [dbo].[Paneles_UsuariosV2] ".concat(id, ",").concat(idRol, ",'").concat(filtroRol, "'"));
          case 7:
            result = _context2.sent;
            res.json(result.recordset);
            _context2.next = 14;
            break;
          case 11:
            _context2.prev = 11;
            _context2.t0 = _context2["catch"](1);
            console.log(_context2.t0);
          case 14:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2, null, [[1, 11]]);
  }));
  return function getBiByUserId(_x3, _x4) {
    return _ref2.apply(this, arguments);
  };
}();
exports.getBiByUserId = getBiByUserId;
var getUserByNameOrRut = /*#__PURE__*/function () {
  var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(req, res) {
    var _req$params$reg$split, _req$params$reg$split2, reg, idRol, filtroRol, idUsuario, pool, result;
    return _regenerator["default"].wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            _req$params$reg$split = req.params.reg.split('+'), _req$params$reg$split2 = (0, _slicedToArray2["default"])(_req$params$reg$split, 4), reg = _req$params$reg$split2[0], idRol = _req$params$reg$split2[1], filtroRol = _req$params$reg$split2[2], idUsuario = _req$params$reg$split2[3];
            _context3.prev = 1;
            _context3.next = 4;
            return (0, _connection.getConnection)();
          case 4:
            pool = _context3.sent;
            _context3.next = 7;
            return pool.request().query("exec [dbo].[BusquedaRutNombreV2]  '".concat(reg, "',").concat(idRol, ",'").concat(filtroRol, "',").concat(idUsuario, ";\n"));
          case 7:
            result = _context3.sent;
            res.json(result.recordset);
            _context3.next = 14;
            break;
          case 11:
            _context3.prev = 11;
            _context3.t0 = _context3["catch"](1);
            console.log(_context3.t0);
          case 14:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3, null, [[1, 11]]);
  }));
  return function getUserByNameOrRut(_x5, _x6) {
    return _ref3.apply(this, arguments);
  };
}();

// export const postLogin = async (req, res) => {
//     const {id, password} = req.body;
//     try {
//         const pool = await getConnection();
//         const result = await pool.request().query(`exec [dbo].[InicioSesion] '${id}','${password}'`);
//         if (result.recordset[0]['Respuesta'] === 'Usuario No Válido') {
//             res.status(203).json('No autorizado');
//         } else {
//             res.status(200)
//             res.json(result.recordset)
//         }
//     } catch (e) {
//         console.log(e)
//     }
// }
exports.getUserByNameOrRut = getUserByNameOrRut;
var postLogin = /*#__PURE__*/function () {
  var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(req, res) {
    var _req$body, id, password, pool, result;
    return _regenerator["default"].wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            _req$body = req.body, id = _req$body.id, password = _req$body.password;
            _context4.prev = 1;
            _context4.next = 4;
            return (0, _connection.getConnection2)();
          case 4:
            pool = _context4.sent;
            _context4.next = 7;
            return pool.request().query("select * from dbmas.dbo.VW_USER_SACC WHERE rut = '" + id + "' and clave = '" + password + "'");
          case 7:
            result = _context4.sent;
            if (result.recordset.length === 0) {
              res.status(401).json('No autorizado');
            } else {
              res.status(200);
              res.json(result.recordset);
            }
            _context4.next = 14;
            break;
          case 11:
            _context4.prev = 11;
            _context4.t0 = _context4["catch"](1);
            console.log(_context4.t0);
          case 14:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4, null, [[1, 11]]);
  }));
  return function postLogin(_x7, _x8) {
    return _ref4.apply(this, arguments);
  };
}();
exports.postLogin = postLogin;