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 _$_GET$address;
    var _this;
    _this = _Component.call(this, props) || this;
    _this.getUserIsLogin = function () {
      var _getCookieData = getCookieData(),
        token = _getCookieData.token;
      var pathname = window.location.pathname;
      if (token) {
        _this.setState({
          isLogin: true
        });
        return false;
      }
      if (!token && pathname === '/preview.html')
        // eslint-disable-next-line react/prop-types
        _this.props.triggerWXLogin && _this.props.triggerWXLogin();else _Message.error('请先完成登录！');
    };
    _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) 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) {
                    return _extends({}, item, {
                      labelKey: item.key,
                      // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
                      value: Array.isArray(form["prop" + (index + 1)]) ?
                      // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
                      form["prop" + (index + 1)] :
                      // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
                      [form["prop" + (index + 1)]]
                    });
                  });
                  _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
                    });
                  }
                case 8:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }));
        return function (_x) {
          return _ref.apply(this, arguments);
        };
      }());
    };
    console.log('初始化获取props数据--formData', props);
    var data = {
      checkbox: [],
      address: (_$_GET$address = $_GET.address) !== null && _$_GET$address !== void 0 ? _$_GET$address : ''
    };
    var _form = Object.fromEntries(
    // eslint-disable-next-line react/prop-types
    props.formData.map(function (item, index) {
      var _data$item$type;
      return [// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
      "prop" + (index + 1), (_data$item$type = data[item.type]) !== null && _data$item$type !== void 0 ? _data$item$type : ''];
    }));
    var rules = 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();
        }
      }] : required];
    }));
    _this.state = {
      form: _form,
      // eslint-disable-next-line react/no-unused-state
      rules: rules,
      submited: false,
      isLogin: false
    };
    return _this;
  }
  var _proto = FormTplView.prototype;
  _proto.componentDidMount = function componentDidMount() {
    // setCookieData({
    //   token:
    //     'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl93ZWJfa2V5IjoiNDNmODMwYWUtMTNlOC00NmNlLTk2ZDEtMzgyY2VjZTk5MGE5In0.pcWxVy6GA_cPm_gvLwbwAUr2uMfncTTiEUPLlREYnC48e-iKS-mBJjfPmwtG5uc2h3FsARXTUAi1G8NDEEVNjg',
    // });

    this.getUserIsLogin();
  };
  _proto.onChange = function onChange(key, value) {
    var _Object$assign;
    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;
    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 {
        // eslint-disable-next-line react/jsx-no-bind
        input: /*#__PURE__*/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']
        }),
        // eslint-disable-next-line react/jsx-no-bind
        address: /*#__PURE__*/React.createElement(Input, {
          value: value,
          onChange: _this2.onChange.bind(_this2, key)
        })
        // address: (
        //   <div className={style['address-content']} onClick={this.onChange.bind(this, key)}>
        //     <span className={`xiatu-rich xt-location2`} />
        //     <span>点击获取定位</span>
        //   </div>
        // ),
      };
    };

    return /*#__PURE__*/React.createElement("div", {
      className: style.view
    }, /*#__PURE__*/React.createElement(Form, {
      ref: "form",
      model: form,
      rules: rules,
      className: style['user-form']
    }, 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);
    }), 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 };