import "./index.css";
/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
import { componentMixin, createComponentName } from '../../utils/base';
import Modal from '../modal';
import Button from '../button';
import Upload from '../upload';
import tips from '../tips';
import request from '../../service/request';
const __vue_script__ = {
  name: createComponentName('feedback-modal'),
  components: {
    Modal,
    Button,
    Upload
  },
  mixins: [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: {
    async submit() {
      try {
        const content = this.content.trim();
        const contact = this.contact.trim();

        if (this.customCheck) {
          await this.customCheck({
            content,
            contact
          });
        } else {
          if (!content) {
            throw new Error('反馈内容不能为空！');
          } else if (content.length < 8) {
            throw new Error('反馈内容不能少于8个字哦～');
          } else if (content.length > 1000) {
            throw new Error('反馈内容请控制在1000字以内哦～');
          }

          if (!contact) {
            throw new Error('联系方式不能为空！');
          }
        }

        if (this.beforeSubmit) await this.beforeSubmit({
          content,
          contact
        });
        tips.loading('提交中，请稍后...');
        this.feedbackPending = true;
        await this.$refs.upload.uploadFiles();
        const fileList = this.$refs.upload.fileList;
        await request.post('/api/feedbacks', {
          content,
          contact: contact,
          images: fileList.map(file => ({
            width: file.width,
            height: file.height,
            url: file.src
          })),
          origin_url: document.URL,
          referrer: document.referrer
        });
        tips('您的反馈我们已收到~');
        this.content = '';
        this.contact = '';
        this.$emit('update:visible');
      } catch (error) {
        tips(error.message);
      }
    },

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

    onExceed() {
      tips('最多允许上传4张图片');
    }

  }
};
/* template */

var __vue_render__ = function () {
  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 (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 ($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 ($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 */

const __vue_inject_styles__ = undefined;
/* scoped */

const __vue_scope_id__ = undefined;
/* module identifier */

const __vue_module_identifier__ = undefined;
/* functional template */

const __vue_is_functional_template__ = false;
/* component normalizer */

import __vue_normalize__ from '../../utils/vue-normalize-component.js';
/* style inject */

/* style inject SSR */

export default __vue_normalize__({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);