import _extends from "@babel/runtime/helpers/extends";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _Message from "@alifd/next/es/message";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React, { Component } from 'react';
import { Form, Input, Radio, Checkbox, Button } from 'element-react';
import 'element-theme-default';
import Placeholder from "../FormPlaceholder";
import { formSubmit } from "../../../api/base";
import { $_GET, getCookieData, setCookieData } from "../../../utils/utils";
import Upload from "../../../components/XtForm/FormUpload";
import style from "./index.module.scss";
import "../../../module/assets/iconfont/xt-icon/xiatu-rich/iconfont.css";
var FormTplView = /*#__PURE__*/function (_Component) {
  _inheritsLoose(FormTplView, _Component);
  function FormTplView(props) {
    var _this;
    _this = _Component.call(this, props) || this;
    _this.getUserIsLogin = function () {
      var pathname = window.location.pathname;
      if (pathname === '/preview.html') {
        // eslint-disable-next-line react/prop-types
        _this.props.triggerWXLogin && _this.props.triggerWXLogin();
      } else {
        _Message.error('请先完成登录');
      }
    };
    _this.triggerSubmitSuccess = function () {
      var pathname = window.location.pathname;
      if (pathname === '/preview.html') {
        // eslint-disable-next-line react/prop-types
        _this.props.submitSuccess && _this.props.submitSuccess();
      }
    };
    _this.handleSubmit = function (e) {
      e.preventDefault();
      var _this$state = _this.state,
        form = _this$state.form,
        submited = _this$state.submited,
        isLogin = _this$state.isLogin;
      if (submited) return false;
      if (!isLogin) {
        _this.getUserIsLogin();
        return false;
      }

      // eslint-disable-next-line react/no-string-refs
      _this.refs.form.validate( /*#__PURE__*/function () {
        var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(valid) {
          var _Number, ideaId, submitData, _yield$formSubmit, code;
          return _regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  if (!valid) {
                    _context.next = 8;
                    break;
                  }
                  ideaId = (_Number = Number($_GET.activityId || $_GET.id)) !== null && _Number !== void 0 ? _Number : 0; // eslint-disable-next-line react/prop-types
                  submitData = _this.props.formData.map(function (item, index) {
                    // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
                    var value = form["prop" + (index + 1)];
                    return _extends({}, item, {
                      labelKey: item.key,
                      value: Array.isArray(value) ? value : [value]
                    });
                  });
                  _context.next = 5;
                  return formSubmit({
                    ideaId: ideaId,
                    submitData: submitData
                  });
                case 5:
                  _yield$formSubmit = _context.sent;
                  code = _yield$formSubmit.code;
                  if (code === 10200) {
                    _Message.success('恭喜：表单提交成功！');
                    _this.setState({
                      submited: !submited
                    });
                    _this.triggerSubmitSuccess();
                  }
                case 8:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }));
        return function (_x) {
          return _ref.apply(this, arguments);
        };
      }());
    };
    var _form =
    // eslint-disable-next-line react/prop-types
    props.formData.length ? Object.fromEntries(
    // eslint-disable-next-line react/prop-types
    props.formData.map(function (item, index) {
      return [// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
      "prop" + (index + 1), item.type === 'checkbox' ? [] : ''];
    })) : {};
    var rules =
    // eslint-disable-next-line react/prop-types
    props.formData.length ? Object.fromEntries(
    // eslint-disable-next-line react/prop-types
    props.formData.map(function (item, index) {
      var _item$label;
      var required = _extends({
        required: item.required,
        message: ((_item$label = item.label) !== null && _item$label !== void 0 ? _item$label : '暂无选项标题') + "\u4E0D\u80FD\u4E3A\u7A7A",
        trigger: ['blur', 'change']
      }, item.type === 'checkbox' && {
        type: 'array'
      });
      return [// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
      "prop" + (index + 1), item.required && item.pattern ? [_extends({}, required), {
        validator: function validator(_, value, callback) {
          var reg = new RegExp(item.pattern.replace(/^\/+|\/+$/g, ''));
          if (!reg.test(value)) callback(new Error("\u8BF7\u8F93\u5165\u6B63\u786E\u7684" + item.label));else callback();
        }
      }] : _extends({}, required)];
    })) : {};
    _this.state = {
      form: _form,
      // eslint-disable-next-line react/no-unused-state
      rules: rules,
      // eslint-disable-next-line react/prop-types
      submited: false,
      isLogin: false
    };
    return _this;
  }
  var _proto = FormTplView.prototype;
  _proto.componentDidMount = function componentDidMount() {
    var _getCookieData = getCookieData(),
      token = _getCookieData.token;
    // setCookieData({
    //   token: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uSWQiOiJXRUNIQVRfd3hmOTc4NGQ0YTgzNjg3NmVhX2luVExSZDdFVkZ1IiwicGxhdGZvcm1JZCI6IldFQ0hBVCIsImFwcElkIjoid3hmOTc4NGQ0YTgzNjg3NmVhIiwiZXhwIjoxNjcxNTA2NTg0fQ.JVVtzLqCAYYbc3KxETvnAgFp-QOwVIjaGTa7cMnjCtA`,
    // });
    // this.setState({ isLogin: true });
    if (token) {
      this.setState({
        isLogin: true
      });
    }
  };
  _proto.onChange = function onChange(key, value) {
    var _Object$assign;
    var isLogin = this.state.isLogin;
    if (!isLogin) this.getUserIsLogin();
    this.setState({
      // eslint-disable-next-line react/no-access-state-in-setstate
      form: Object.assign({}, this.state.form, (_Object$assign = {}, _Object$assign[key] = value, _Object$assign))
    });
  };
  _proto.render = function render() {
    var _this2 = this;
    var _this$state2 = this.state,
      form = _this$state2.form,
      rules = _this$state2.rules,
      submited = _this$state2.submited;
    // eslint-disable-next-line react/prop-types
    var _this$props = this.props,
      formData = _this$props.formData,
      _this$props$formBtnTe = _this$props.formBtnText,
      formBtnText = _this$props$formBtnTe === void 0 ? null : _this$props$formBtnTe,
      isEditting = _this$props.isEditting;
    var block = function block(item) {
      return /*#__PURE__*/React.createElement(React.Fragment, null, item.image ? /*#__PURE__*/React.createElement("img", {
        src: item.image
      }) : /*#__PURE__*/React.createElement("span", {
        className: style['content-image'] + " xiatu-rich xt-gongjulan-xinzeng-chatu"
      }), /*#__PURE__*/React.createElement("span", {
        className: "" + style['content-text']
      }, item.value));
    };
    var content = function content(item, index) {
      // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
      var key = "prop" + (index + 1);
      // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
      var value = _this2.state.form["prop" + (index + 1)];
      return {
        input:
        /*#__PURE__*/
        // eslint-disable-next-line react/jsx-no-bind
        React.createElement(Input, {
          value: value,
          onChange: _this2.onChange.bind(_this2, key)
        }),
        radio:
        /*#__PURE__*/
        // eslint-disable-next-line react/jsx-no-bind
        React.createElement(Radio.Group, {
          value: value,
          onChange: _this2.onChange.bind(_this2, key)
        }, item.options && item.options.length ? item.options.map(function (cItem, idx) {
          // eslint-disable-next-line @typescript-eslint/no-shadow
          var isImage = item.options.some(function (item) {
            return item.image;
          });
          return /*#__PURE__*/React.createElement(Radio, {
            value: cItem.value
            // eslint-disable-next-line react/no-array-index-key
            ,
            key: idx,
            className: isImage ? '' : style['form-radio']
          }, isImage ? block(cItem) : item.value);
        }) : null),
        checkbox:
        /*#__PURE__*/
        // eslint-disable-next-line react/jsx-no-bind
        React.createElement(Checkbox.Group, {
          value: value,
          onChange: _this2.onChange.bind(_this2, key)
        }, item.options && item.options.length ? item.options.map(function (cItem, idx) {
          // eslint-disable-next-line @typescript-eslint/no-shadow
          var isImage = item.options.some(function (item) {
            return item.image;
          });
          return /*#__PURE__*/React.createElement(Checkbox, {
            label: cItem.value
            // eslint-disable-next-line react/no-array-index-key
            ,
            key: idx,
            className: isImage ? '' : style['form-radio']
          }, isImage ? block(cItem) : item.value);
        }) : null),
        upload:
        /*#__PURE__*/
        // eslint-disable-next-line react/jsx-no-bind
        React.createElement(Upload, {
          onChange: _this2.onChange.bind(_this2, key),
          className: style['form-upload']
        }),
        address:
        /*#__PURE__*/
        // eslint-disable-next-line react/jsx-no-bind
        React.createElement(Input, {
          value: value,
          onChange: _this2.onChange.bind(_this2, key)
        })
      };
    };
    return /*#__PURE__*/React.createElement("div", {
      className: style.view
    }, /*#__PURE__*/React.createElement(Form
    // eslint-disable-next-line react/no-string-refs
    , {
      ref: "form",
      model: form,
      rules: rules,
      className: style['user-form']
    }, isEditting ? /*#__PURE__*/React.createElement("div", {
      className: style['view-mask']
    }) : null, formData.length ?
    // eslint-disable-next-line react/prop-types
    formData.map(function (item, idx) {
      var _content, _item$type;
      return /*#__PURE__*/React.createElement(Form.Item, {
        key: item.key
        // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
        ,
        label: idx + 1 + "." + (item.label || '暂无选项标题')
        // eslint-disable-next-line
        ,
        prop: "prop" + (idx + 1)
      }, (_content = content(item, idx)[(_item$type = item.type) !== null && _item$type !== void 0 ? _item$type : 'input']) !== null && _content !== void 0 ? _content : null);
    }) : null, formData.length ? /*#__PURE__*/React.createElement(Button, {
      type: "primary",
      className: style['submit-form'],
      disabled: submited
      // eslint-disable-next-line react/jsx-no-bind
      ,
      onClick: this.handleSubmit.bind(this)
    }, submited ? '已提交' : formBtnText || '提 交') : /*#__PURE__*/React.createElement(Placeholder, null)));
  };
  return FormTplView;
}(Component);
export { FormTplView as default };