"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs2/regenerator"));

require("regenerator-runtime/runtime");

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/asyncToGenerator"));

require("./index.css");

var _base = require("../../utils/base");

var _modal = _interopRequireDefault(require("../modal"));

var _button = _interopRequireDefault(require("../button"));

var _upload = _interopRequireDefault(require("../upload"));

var _tips = _interopRequireDefault(require("../tips"));

var _request = _interopRequireDefault(require("../../service/request"));

var _vueNormalizeComponent = _interopRequireDefault(require("../../utils/vue-normalize-component.js"));

/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
var __vue_script__ = {
  name: (0, _base.createComponentName)('feedback-modal'),
  components: {
    Modal: _modal.default,
    Button: _button.default,
    Upload: _upload.default
  },
  mixins: [_base.componentMixin],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    // 自定义校验
    customCheck: {
      type: Function,

      default() {}

    },
    title: {
      type: String,
      default: '反馈&建议'
    },
    // 表单提交前钩子
    beforeSubmit: {
      type: [Function, undefined]
    },
    contentModule: {
      type: Object,

      default() {
        return {
          label: '您的反馈&建议：',
          placeholder: '您在使用中遇到了什么问题？有什么建议，欢迎和我们说说。'
        };
      }

    },
    contactModule: {
      type: Object,

      default() {
        return {
          label: '联系方式：',
          placeholder: '填写联系方式我们才方便给你回答哦'
        };
      }

    }
  },

  data() {
    return {
      content: '',
      contact: ''
    };
  },

  methods: {
    submit() {
      var _this = this;

      return (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee() {
        var content, contact, fileList;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.prev = 0;
                content = _this.content.trim();
                contact = _this.contact.trim();

                if (!_this.customCheck) {
                  _context.next = 8;
                  break;
                }

                _context.next = 6;
                return _this.customCheck({
                  content,
                  contact
                });

              case 6:
                _context.next = 20;
                break;

              case 8:
                if (content) {
                  _context.next = 12;
                  break;
                }

                throw new Error('反馈内容不能为空！');

              case 12:
                if (!(content.length < 8)) {
                  _context.next = 16;
                  break;
                }

                throw new Error('反馈内容不能少于8个字哦～');

              case 16:
                if (!(content.length > 1000)) {
                  _context.next = 18;
                  break;
                }

                throw new Error('反馈内容请控制在1000字以内哦～');

              case 18:
                if (contact) {
                  _context.next = 20;
                  break;
                }

                throw new Error('联系方式不能为空！');

              case 20:
                if (!_this.beforeSubmit) {
                  _context.next = 23;
                  break;
                }

                _context.next = 23;
                return _this.beforeSubmit({
                  content,
                  contact
                });

              case 23:
                _tips.default.loading('提交中，请稍后...');

                _this.feedbackPending = true;
                _context.next = 27;
                return _this.$refs.upload.uploadFiles();

              case 27:
                fileList = _this.$refs.upload.fileList;
                _context.next = 30;
                return _request.default.post('/api/feedbacks', {
                  content,
                  contact: contact,
                  images: fileList.map(function (file) {
                    return {
                      width: file.width,
                      height: file.height,
                      url: file.src
                    };
                  }),
                  origin_url: document.URL,
                  referrer: document.referrer
                });

              case 30:
                (0, _tips.default)('您的反馈我们已收到~');
                _this.content = '';
                _this.contact = '';

                _this.$emit('update:visible');

                _context.next = 39;
                break;

              case 36:
                _context.prev = 36;
                _context.t0 = _context["catch"](0);
                (0, _tips.default)(_context.t0.message);

              case 39:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this, [[0, 36]]);
      }))();
    },

    onCancel() {
      this.content = '';
      this.contact = '';
    },

    onExceed() {
      (0, _tips.default)('最多允许上传4张图片');
    }

  }
};
/* template */

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c("modal", _vm._g(_vm._b({
    class: _vm.bem(),
    attrs: {
      visible: _vm.visible,
      width: "614px",
      title: _vm.title,
      onCancel: _vm.onCancel,
      maskClosable: ""
    },
    on: {
      "update:visible": function updateVisible(v) {
        return _vm.$emit("update:visible", v);
      }
    }
  }, "modal", _vm.$attrs, false), _vm.$listeners), [_c("div", {
    class: _vm.bem("field-wrapper")
  }, [_c("label", {
    attrs: {
      for: "feedback-content"
    }
  }, [_vm._v(_vm._s(_vm.contentModule.label))]), _vm._v(" "), _c("textarea", {
    directives: [{
      name: "model",
      rawName: "v-model",
      value: _vm.content,
      expression: "content"
    }],
    class: _vm.bem("field"),
    attrs: {
      cols: "30",
      rows: "7",
      maxlength: "1000",
      placeholder: _vm.contentModule.placeholder,
      autofocus: ""
    },
    domProps: {
      value: _vm.content
    },
    on: {
      input: function input($event) {
        if ($event.target.composing) {
          return;
        }

        _vm.content = $event.target.value;
      }
    }
  })]), _vm._v(" "), _c("div", {
    class: _vm.bem("field-wrapper")
  }, [_c("label", {
    attrs: {
      for: "feedback-contact"
    }
  }, [_vm._v(_vm._s(_vm.contactModule.label))]), _vm._v(" "), _c("input", {
    directives: [{
      name: "model",
      rawName: "v-model",
      value: _vm.contact,
      expression: "contact"
    }],
    class: _vm.bem("field"),
    attrs: {
      type: "text",
      maxlength: "100",
      placeholder: _vm.contactModule.placeholder
    },
    domProps: {
      value: _vm.contact
    },
    on: {
      input: function input($event) {
        if ($event.target.composing) {
          return;
        }

        _vm.contact = $event.target.value;
      }
    }
  })]), _vm._v(" "), _c("upload", {
    ref: "upload",
    attrs: {
      type: "card",
      autoUpload: false,
      multiple: "",
      limit: 4
    },
    on: {
      exceed: _vm.onExceed
    }
  }), _vm._v(" "), _c("div", {
    class: _vm.bem("btn-wrapper")
  }, [_c("Button", {
    class: _vm.bem("btn"),
    attrs: {
      type: "primary"
    },
    on: {
      click: _vm.submit
    }
  }, [_vm._v("提交反馈")])], 1)], 1);
};

var __vue_staticRenderFns__ = [];
__vue_render__._withStripped = true;
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* component normalizer */

/* style inject */

/* style inject SSR */
var _default = (0, _vueNormalizeComponent.default)({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);

exports.default = _default;