import _extends from "@babel/runtime/helpers/extends";
import _Message from "@alifd/next/es/message";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import * as React from 'react';
import "./index.scss";
import cosUpload from "../../module/axios/cosUpload";
// import { event } from '@alilc/lowcode-engine';
import { parseqrcode } from "../../api/base";
import global from "../../utils/global";
// 文件上传 输入框
var inputFileSetter = /*#__PURE__*/function (_React$PureComponent) {
  _inheritsLoose(inputFileSetter, _React$PureComponent);
  function inputFileSetter(props) {
    var _this;
    _this = _React$PureComponent.call(this, props) || this;
    _this.state = {
      img: "",
      img2: "",
      // 原生inputFile
      inputFileMessage: ""
    };
    return _this;
  }
  // componentDidMount() {
  //   // 这里由于面板上会有多个 setter，使用 field.id 来标记 setter 名
  //   this.emitEventName = `${'SelectImgSetter'}-${this.props.SelectImgSetter}`;
  //   event.on(`common:${this.emitEventName}.bindEvent`, this.bindEvent)
  // }

  // bindEvent = (eventName) => {
  //   // do someting
  // }
  //  sendMessage = (x) => {
  //   // 这里由于面板上会有多个 setter，使用 field.id 来标记 setter 名
  //   this.emitEventName = `${'SelectImgSetter'}-${this.props.SelectImgSetter}`;
  //    event.on(`common:${this.emitEventName}.bindEvent`, this.bindEvent)
  // }
  // bindEvent = (x) => {
  //   console.log("1234")
  //   // return x
  // }

  // componentWillUnmount() {
  //   // setter 是以实例为单位的，每个 setter 注销的时候需要把事件也注销掉，避免事件池过多
  //   event.off(`common:${this.emitEventName}.bindEvent`, this.sendMessage)
  // }
  // bindFunction = (x) => {
  //   const { field, value } = this.props;
  //   console.log("field",field)
  //   // 这里展示的和插件进行通信, 事件规则是插件名 + 方法
  //   // event.emit('qrcodeValue', field.name, (() => { this.emitEventName(x)}) );
  //   event.emit('qrcodeValue', (() => { this.emitEventName(x)}) );
  // }
  // emitEventName = (x) => {
  //   console.log("触发", x)
  //   // return x
  //   return {
  //     a:123
  //   }
  // }
  var _proto = inputFileSetter.prototype;
  _proto.render = function render() {
    var _this2 = this;
    //  initvalue  1是二维码上传   2是logo上传
    var initvalue = this.props.initvalue;
    var customUpload = /*#__PURE__*/function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(ev) {
        var file, succ;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                file = ev === null || ev === void 0 ? void 0 : ev.target.files[0];
                _this2.setState({
                  inputFileMessage: file
                });
                succ = cosUpload(file, {
                  pathDir: "qrcodelogo",
                  isDateDir: true,
                  reName: true
                  // onProgress({ total, loaded }) {
                  //   setUploadStatus({ total, loaded })
                  // }
                });

                succ.then(function (_ref2) {
                  var code = _ref2.code,
                    data = _ref2.data,
                    msg = _ref2.msg;
                  if (code === '0000') {
                    if (initvalue === 1) {
                      _this2.setState({
                        img: data.ossPath
                      });
                      renderQRcode();
                    }
                    if (initvalue === 2) {
                      _this2.setState({
                        img2: data.ossPath
                      });
                      cover();
                    }
                    _Message.success("上传成功");
                  } else {
                    _Message.error(msg);
                  }
                });
              case 4:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));
      return function customUpload(_x) {
        return _ref.apply(this, arguments);
      };
    }();
    // const clickSure = async () => {
    //   // const info = this.state.infoMessage
    //   // const obj = { ...info }
    //   // obj.valueId = initvalue
    //   if (this.state.img && this.state.infoMessage === "QR") {
    //     await renderQRcode()
    //     // const { field, value } = this.props;
    //     // const propsField = field.parent;
    //     // // 获取同级其他属性showJump的值
    //     // const otherValue = propsField.getPropValue('QRImg');
    //     // console.log("otherValue", otherValue)

    //     // set同级其他属性showJump的值
    //     // propsField.setPropValue('showJump', false);
    //   } else {
    //     cover()
    //   }
    // }
    // 覆盖图片
    var cover = /*#__PURE__*/function () {
      var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
        return _regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                global.liveCode.logoImg = "";
                global.liveCode.logoImg = _this2.state.img2;
                // global.liveCode.logoImg.length = 0
                // global.liveCode.logoImg.push(this.state.img2)

                // global.liveCode.logoImg.length = 0
                // global.liveCode.logoImg.push(this.state.img2)
              case 2:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }));
      return function cover() {
        return _ref3.apply(this, arguments);
      };
    }();
    var renderQRcode = /*#__PURE__*/function () {
      var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
        var a, _yield$parseqrcode, data, code;
        return _regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                a = {
                  file: _this2.state.inputFileMessage
                };
                _context3.next = 3;
                return parseqrcode(a);
              case 3:
                _yield$parseqrcode = _context3.sent;
                data = _yield$parseqrcode.data;
                code = _yield$parseqrcode.code;
                if (code === 200) {
                  global.liveCode.qrCode.code = data.a;
                }
              case 7:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3);
      }));
      return function renderQRcode() {
        return _ref4.apply(this, arguments);
      };
    }();

    // const InputStypeContentStyle = {
    //   style: {
    //     display: `flex`,
    //     // justifyContent:`space-around`,
    //     alignItems: `center`,
    //   }
    // }
    var imgStyle = {
      style: {
        display: "" + (this.state.img || this.state.img2 ? 'block' : 'none'),
        width: "80px",
        height: "80px"
      }
    };
    var img = {
      style: {
        width: "100%",
        height: "100%",
        objectFit: "cover"
      }
    };
    return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("input", {
      type: "file",
      name: "file",
      id: "file",
      onChange: function onChange(val) {
        return customUpload(val);
      }
    }), /*#__PURE__*/React.createElement("div", imgStyle, this.props.initvalue === 1 ? /*#__PURE__*/React.createElement("img", _extends({
      src: this.state.img
    }, img)) : /*#__PURE__*/React.createElement("img", _extends({
      src: this.state.img2
    }, img))));
  };
  return inputFileSetter;
}(React.PureComponent);
inputFileSetter.displayName = 'inputFileSetter';
export { inputFileSetter as default };