"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _dialog = _interopRequireDefault(require("@alifd/next/lib/dialog"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var React = _interopRequireWildcard(require("react"));
require("./index.scss");
var _react2 = require("@lucky-canvas/react");
var _LuckDrawSucc = _interopRequireDefault(require("../../common/LuckDrawSucc"));
var _utils = require("../../../utils/utils");
var _base = require("../../../api/base");
var _index2 = _interopRequireDefault(require("../../common/shippingAddress/index"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
var wheel01 = function wheel01(wheel01Props) {
  var width = wheel01Props.width;
  // eslint-disable-next-line react-hooks/rules-of-hooks
  var _useState = (0, React.useState)({
      isOpenPrize: false,
      countPrized: 5,
      prizeTextList: {
        icon: 'prize',
        msg: '恭喜您中奖啦!',
        prizeLv: ['特等奖', '冬奥冰墩墩钥匙扣 × 1']
      }
    }),
    state = _useState[0],
    setState = _useState[1];
  var btnState = function btnState(prize) {
    setState({
      countPrized: state.countPrized,
      isOpenPrize: !state.isOpenPrize,
      prizeTextList: {
        icon: prize.imgs && prize.imgs[0].src,
        msg: '恭喜您中奖啦!',
        prizeLv: [prize.fonts && prize.fonts[0].text, '']
      }
    });
  };
  var isOpenPrize = state.isOpenPrize,
    prizeTextList = state.prizeTextList;
  var container = {
    style: {
      width: width + "%"
    }
  };
  return /*#__PURE__*/React.createElement("div", container, /*#__PURE__*/React.createElement(_LuckDrawSucc["default"], {
    btnState: btnState,
    prizeTextList: prizeTextList,
    isOpenPrize: isOpenPrize
  }), /*#__PURE__*/React.createElement(WheelChild, {
    wheel01Props: wheel01Props,
    btnState: btnState
  }));
};
var WheelChild = /*#__PURE__*/function (_Component) {
  (0, _inheritsLoose2["default"])(WheelChild, _Component);
  function WheelChild(props) {
    var _this;
    _this = _Component.call(this, props) || this;
    _this.prizeMessageFun = /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
      var _PrizeArray$data, _PrizeArray$data$a;
      var _this$props$wheel01Pr, prizeFontTop, prizeTextColor, prizeFontSize, prizeFontFamily, prizeImgWidth, prizeImgTop, activityId, obj, PrizeArray, prizeRenderList, prizeNone, prizeLen;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _this$props$wheel01Pr = _this.props.wheel01Props, prizeFontTop = _this$props$wheel01Pr.prizeFontTop, prizeTextColor = _this$props$wheel01Pr.prizeTextColor, prizeFontSize = _this$props$wheel01Pr.prizeFontSize, prizeFontFamily = _this$props$wheel01Pr.prizeFontFamily, prizeImgWidth = _this$props$wheel01Pr.prizeImgWidth, prizeImgTop = _this$props$wheel01Pr.prizeImgTop; // 获取当前页面activitdId
            activityId = _utils.$_GET.activityId;
            if (activityId) {
              _context.next = 4;
              break;
            }
            return _context.abrupt("return");
          case 4:
            setTimeout(function () {
              _this.setState({
                activityId: activityId
              });
            }, 0);
            obj = {
              b: Number(activityId)
            };
            _context.next = 8;
            return (0, _base.prizeMessage)(obj);
          case 8:
            PrizeArray = _context.sent;
            prizeRenderList = []; // console.log("这个是奖品信息", PrizeArray.data.a.xa)
            PrizeArray === null || PrizeArray === void 0 ? void 0 : (_PrizeArray$data = PrizeArray.data) === null || _PrizeArray$data === void 0 ? void 0 : (_PrizeArray$data$a = _PrizeArray$data.a) === null || _PrizeArray$data$a === void 0 ? void 0 : _PrizeArray$data$a.xa.map(function (item, index) {
              var _item$xc$;
              prizeRenderList.push({
                // 概率
                range: item.f,
                prizeId: item.d,
                fonts: [{
                  text: item.e,
                  top: prizeFontTop + "%",
                  fontColor: prizeTextColor,
                  fontSize: prizeFontSize + "vw",
                  fontStyle: prizeFontFamily
                }],
                imgs: [{
                  src: ((_item$xc$ = item.xc[0]) === null || _item$xc$ === void 0 ? void 0 : _item$xc$.c) || "//img.maimairen.com/low-code/components/bigWheel2/kouhong01.png",
                  width: prizeImgWidth + "%",
                  top: prizeImgTop + "%"
                }]
              });
              // return prizeRenderList
            });
            prizeNone = {
              // range: 111,
              // prizeId: 0,
              // stepIndex: prizeRenderList.length,
              prizeId: '',
              fonts: [{
                text: '谢谢参与',
                top: prizeFontTop + "%",
                fontColor: prizeTextColor,
                fontSize: prizeFontSize + "vw",
                fontStyle: prizeFontFamily
              }],
              imgs: [{
                src: "//img.maimairen.com/low-code/components/bigWheel2/empty.png",
                width: prizeImgWidth + "%",
                top: prizeImgTop + "%"
              }]
            }; // 插入默认的未中奖数据
            prizeRenderList.unshift(Object.assign({}, prizeNone));
            prizeLen = prizeRenderList.length;
            if (prizeLen % 2 === 1) prizeRenderList.splice((prizeLen - 0 + 1) / 2, 0, Object.assign({}, prizeNone));
            // 斑马纹
            prizeRenderList.map(function (item, idx) {
              item.background = idx % 2 === 0 ? '#FCDFA5' : '#FFD8C9';
              item.stepIndex = idx;
              return item;
            });
            _this.setState({
              prizes: prizeRenderList
            });
          case 17:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    _this.onStart = /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
      var code;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            code = _utils.$_GET.code;
            if (code) {
              _context2.next = 6;
              break;
            }
            _context2.next = 4;
            return _this.loginWechat();
          case 4:
            _context2.next = 11;
            break;
          case 6:
            _this.setState({
              openAddress: false
            });
            _context2.next = 9;
            return _this.drawUtilsFun();
          case 9:
            _this.myLucky.current.play();
            setTimeout(function () {
              var index = _this.state.Stepindex;
              _this.myLucky.current.stop(index);
            }, 3000);
          case 11:
          case "end":
            return _context2.stop();
        }
      }, _callee2);
    }));
    _this.drawUtilsFun = /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
      var code;
      return _regenerator["default"].wrap(function _callee3$(_context3) {
        while (1) switch (_context3.prev = _context3.next) {
          case 0:
            code = _utils.$_GET.code;
            if (code) {
              _context3.next = 6;
              break;
            }
            _context3.next = 4;
            return _this.loginWechat();
          case 4:
            _context3.next = 8;
            break;
          case 6:
            _context3.next = 8;
            return _this.drawFun();
          case 8:
          case "end":
            return _context3.stop();
        }
      }, _callee3);
    }));
    _this.loginWechat = /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4() {
      return _regenerator["default"].wrap(function _callee4$(_context4) {
        while (1) switch (_context4.prev = _context4.next) {
          case 0:
            // window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe677e4aad49ba784&redirect_uri=${encodeURIComponent(window.location.href)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
            window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + _utils.getWxAppid + "&redirect_uri=" + encodeURIComponent(window.location.href) + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

            // let weChatToken = localStorage.getItem("weChatToken")
            // const { code, auth } = $_GET
            // if (weChatToken || auth === '1'){
            //
            //     const { b: weChatToken, ...a } = await getWechatUserInfo({ code })
            //     console.log(a, weChatToken)
            //     // localStorage.setItem("weChatToken", weChatToken)
            // } else {
            //     window.location.href=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe677e4aad49ba784&redirect_uri=${baseUrl}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
            // }
            //
            // // let result = getSearchParams(String(baseUrl))
            // // 获取授权code(从localStorage中)
            // if (code) return
            //
            // // window.open(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe677e4aad49ba784&redirect_uri=${baseUrl}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`)
            // window.location.href=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe677e4aad49ba784&redirect_uri=${baseUrl}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
            //
            // // const newBaseUrl = decodeURIComponent(window.location.href)
            // // result = getSearchParams(String(newBaseUrl))
            // const obj = {
            //     code
            // }
            // // 调用微信回来的接口信息
            // const userInfo = await getWechatUserInfo(obj)
            // debugger
            // // 获取header
            // const xiatuTokenHeader = userInfo.b
            // // 插入header 到 localStorage
            // localStorage.setItem("DrawXiatu_tp_token", xiatuTokenHeader)
          case 1:
          case "end":
            return _context4.stop();
        }
      }, _callee4);
    }));
    _this.drawFun = /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5() {
      var _this$state, _this$state$prizes$fi, _this$state2, _this$state3;
      var obj, drawMessage, Stepindex;
      return _regenerator["default"].wrap(function _callee5$(_context5) {
        while (1) switch (_context5.prev = _context5.next) {
          case 0:
            obj = {
              b: _this.state.activityId
              // b:71
            };
            _context5.next = 3;
            return (0, _base.lottery)(obj);
          case 3:
            drawMessage = _context5.sent;
            // 查找对应的索引
            // this.state.prizes.forEach((item) => {
            //     if (item.prizeId === drawMessage.data.f) {
            //         this.setState({
            //             Stepindex: item.stepIndex
            //         })
            //     }
            // })
            // const {stepIndex: Stepindex = 0} = this.state.prizes.find(res => res?.prizeId === drawMessage.data.f)
            Stepindex = ((_this$state = _this.state) === null || _this$state === void 0 ? void 0 : (_this$state$prizes$fi = _this$state.prizes.find(function (res) {
              return (res === null || res === void 0 ? void 0 : res.prizeId) === drawMessage.data.f;
            })) === null || _this$state$prizes$fi === void 0 ? void 0 : _this$state$prizes$fi.stepIndex) || (((_this$state2 = _this.state) === null || _this$state2 === void 0 ? void 0 : _this$state2.prizes.filter(function (item) {
              return item.prizeId === '';
            }).length) % 2 === 1 ? 0 : Math.random() > 0.5 ? 0 : ((_this$state3 = _this.state) === null || _this$state3 === void 0 ? void 0 : _this$state3.prizes.length) / 2);
            _this.setState({
              Stepindex: Stepindex,
              drawMessage: drawMessage.data,
              singerID: drawMessage.data.a
            });
            // console.log("JSON.stringify(this.state.drawMessage)",JSON.stringify(this.state.drawMessage))
            // debugger
          case 6:
          case "end":
            return _context5.stop();
        }
      }, _callee5);
    }));
    _this.onEnd = function (prize) {
      var fonts = prize.fonts,
        imgs = prize.imgs;
      _dialog["default"].confirm({
        v2: true,
        title: '提示',
        content: prize.prizeId !== '' ? "\u606D\u559C\u60A8\u4E2D\u5956\u5566-\u5956\u54C1: " + fonts[0].text : '很遗憾: 谢谢您的参与',
        messageProps: {
          type: prize.prizeId !== '' ? 'success' : 'warning'
        },
        onOk: function onOk() {
          return prize.prizeId !== '' ? _this.setState({
            openAddress: true
          }) : _this.setState({
            openAddress: false
          });
        }
        // onCancel: () => console.log("cancel")
      });

      // if (prize.prizeId !== '') {
      //     //fonts: Array(1)
      //     // 0: Object
      //     // fontColor: "#D16C3B"
      //     // fontSize: "2.6vw"
      //     // fontStyle: "MiSans-Normal"
      //     // text: "福袋"
      //     // top: （…）
      //     // __luckyOb__: t {dep: t}
      //     // get fontColor: ƒ ()
      //     // set fontColor: ƒ (e)
      //     // get fontSize: ƒ ()
      //     // set fontSize: ƒ (e)
      //     // get fontStyle: ƒ ()
      //     // set fontStyle: ƒ (e)
      //     // get text: ƒ ()
      //     // set text: ƒ (e)
      //     // get top: ƒ ()
      //     // set top: ƒ (e)
      //     // [[Prototype]]: Object
      //     // length: 1
      //     // __luckyOb__: t {dep: t}
      //     // get 0: ƒ ()
      //     // set 0: ƒ (e)
      //     // [[Prototype]]: Array
      //     // imgs: Array(1)
      //     // 0: Object
      //     // src: "https://img.maimairen.com/upload/dev/goods/dcac19b63e9b19c4052fa7f2780b9fe0.png"
      //     // top: （…）
      //     // this.setState({
      //     //   isOpenPrize: !this.state.isOpenPrize,
      //     //   prizeTextList: {
      //     //     // icon: prize.imgs && prize.imgs[0].src,
      //     //     icon: "prize",
      //     //     msg: '恭喜您中奖啦!',
      //     //     prizeLv: [prize.fonts && prize.fonts[0].text, ''],
      //     //   },
      //     // })
      //     // setTimeout(() => {
      //     //
      //     // }, 2000);
      // }

      // this.props.btnState(prize);
      // alert(`恭喜你抽到 ${prize.fonts[0].text} 号奖品`);
    };
    _this.myLucky = /*#__PURE__*/React.createRef();
    _this.state = {
      activityId: null,
      prizes: [],
      // drawMessage: "",
      openAddress: false,
      Stepindex: 0

      // 打开中奖弹框
      // isOpenPrize: false
    };
    return _this;
  }
  var _proto = WheelChild.prototype;
  _proto.componentDidMount = function componentDidMount() {
    this.prizeMessageFun();
  }

  // 获取奖品选项接口
  ;
  _proto.render = function render() {
    var _this2 = this;
    var _this$props$wheel01Pr2 = this.props.wheel01Props,
      bgImg = _this$props$wheel01Pr2.bgImg,
      pointImg = _this$props$wheel01Pr2.pointImg,
      padding = _this$props$wheel01Pr2.padding,
      setGutter = _this$props$wheel01Pr2.setGutter,
      setStopRange = _this$props$wheel01Pr2.setStopRange,
      setOffsetDegree = _this$props$wheel01Pr2.setOffsetDegree;
    // console.log(prizeTextColor);
    // 转盘配置详情 https://100px.net/docs/wheel.html 或 https://blog.csdn.net/crazy_jialin/article/details/115613060
    // 背景
    var blocks = [
      // {
      //
      // },
    ];
    // debugger
    // const prizes = [
    //   {
    //     range:0.001,
    //     background: '#FCDEA0',
    //     fonts: [
    //       {
    //         text: '随机红包',
    //         top: `${prizeFontTop}%`,
    //         fontColor: prizeTextColor,
    //         fontSize: `${prizeFontSize}vw`,
    //         fontStyle: prizeFontFamily,
    //       },
    //     ],
    //     imgs: [
    //       {
    //         src: '//img.maimairen.com/low-code/components/bigWheel2/hongbao01.png',
    //         width: `${prizeImgWidth}%`,
    //         top: `${prizeImgTop}%`,
    //       },
    //     ],
    //   },
    //   {
    //     background: '#FFD8C9',
    //     fonts: [
    //       {
    //         text: '50元优惠券',
    //         top: `${prizeFontTop}%`,
    //         fontColor: prizeTextColor,
    //         fontStyle: prizeFontFamily,
    //         fontSize: `${prizeFontSize}vw`,
    //       },
    //     ],
    //     imgs: [
    //       {
    //         src: '//img.maimairen.com/low-code/components/bigWheel2/youhuiquan01.png',
    //         width: `${prizeImgWidth}%`,
    //         top: `${prizeImgTop}%`,
    //       },
    //     ],
    //   },
    //   {
    //     background: '#FCDEA0',
    //     fonts: [
    //       {
    //         text: 'DIOR39号',
    //         top: `${prizeFontTop}%`,
    //         fontColor: prizeTextColor,
    //         fontStyle: prizeFontFamily,
    //         fontSize: `${prizeFontSize}vw`,
    //       },
    //     ],
    //     imgs: [
    //       {
    //         src: '//img.maimairen.com/low-code/components/bigWheel2/kouhong01.png',
    //         width: `${prizeImgWidth}%`,
    //         top: `${prizeImgTop}%`,
    //       },
    //     ],
    //   },
    //   {
    //     background: '#FFD8C9',
    //     fonts: [
    //       {
    //         text: '随机红包',
    //         top: `${prizeFontTop}%`,
    //         fontColor: prizeTextColor,
    //         fontStyle: prizeFontFamily,
    //         fontSize: `${prizeFontSize}vw`,
    //       },
    //     ],
    //     imgs: [
    //       {
    //         src: '//img.maimairen.com/low-code/components/bigWheel2/hongbao01.png',
    //         width: `${prizeImgWidth}%`,
    //         top: `${prizeImgTop}%`,
    //       },
    //     ],
    //   },
    //   {
    //     background: '#FCDEA0',
    //     fonts: [
    //       {
    //         text: '50元优惠券',
    //         top: `${prizeFontTop}%`,
    //         fontColor: prizeTextColor,
    //         fontStyle: prizeFontFamily,
    //         fontSize: `${prizeFontSize}vw`,
    //       },
    //     ],
    //     imgs: [
    //       {
    //         src: '//img.maimairen.com/low-code/components/bigWheel2/youhuiquan01.png',
    //         width: `${prizeImgWidth}%`,
    //         top: `${prizeImgTop}%`,
    //       },
    //     ],
    //   },
    //   {
    //     background: '#FFD8C9',
    //     fonts: [
    //       {
    //         text: 'DIOR39号',
    //         top: `${prizeFontTop}%`,
    //         fontColor: prizeTextColor,
    //         fontStyle: prizeFontFamily,
    //         fontSize: `${prizeFontSize}vw`,
    //       },
    //     ],
    //     imgs: [
    //       {
    //         src: '//img.maimairen.com/low-code/components/bigWheel2/kouhong01.png',
    //         width: `${prizeImgWidth}%`,
    //         top: `${prizeImgTop}%`,
    //       },
    //     ],
    //   },
    // ];
    var buttons = [{
      radius: '50%',
      imgs: [{
        src: pointImg,
        height: '100%',
        top: '-100%'
      }]
    }];
    var defaultConfig = {
      gutter: setGutter + "vw",
      offsetDegree: setOffsetDegree ? setOffsetDegree.toString() : '',
      stopRange: setStopRange
    };
    var wheelstyle = {
      style: {
        display: 'block',
        backgroundImage: "url('" + bgImg + "')",
        backgroundSize: '100% 100%'
      }
    };
    // const theStartBtn = {
    //   style: {
    //     position:`absolute`,
    //     width: `30%!important`,
    //     height: `30%!important`,
    //     top: `50%`,
    //     left: `50%`,
    //     transform: `translate(-50%, -50%)`
    //   }
    // }
    return /*#__PURE__*/React.createElement("div", (0, _extends2["default"])({
      className: "wheelStyle"
    }, wheelstyle), /*#__PURE__*/React.createElement("div", {
      className: "wheelContent",
      style: {
        position: 'absolute',
        width: '100%',
        height: '100%',
        padding: padding + "%"
      }
    }, /*#__PURE__*/React.createElement(_react2.LuckyWheel, {
      ref: this.myLucky,
      blocks: blocks,
      prizes: this.state.prizes,
      buttons: buttons,
      defaultConfig: defaultConfig
      // onStart={this.onStart}
      ,
      onEnd: function onEnd(prize) {
        _this2.onEnd(prize);
      }
      // onEnd={(prize) => {
      //   this.myLucky.current.$lucky.step = 0;
      //   // this.props.btnState(prize);
      // }}
    }), /*#__PURE__*/React.createElement("div", {
      onClick: this.onStart,
      className: "theWheelStartBtn"
    }), /*#__PURE__*/React.createElement(_index2["default"], {
      touchState: this.state.openAddress,
      drawMessage: JSON.stringify(this.state.drawMessage),
      activityId: this.state.activityId,
      singerID: this.state.singerID
    })));
  };
  return WheelChild;
}(React.Component);
WheelChild.displayName = 'WheelChild';
var _default = wheel01;
exports["default"] = _default;