<template>
  <div class="feedback-info">
    <div class="cell">
      <div class="title">选择反馈类型</div>
      <div class="type-list">
        <div :class="params.type === v.type ? 'type active': 'type'" v-for="v in typeList" :key="v.type" @click="params.type = v.type">{{v.name}}</div>
      </div>
    </div>
    <div class="cell">
      <div class="title">问题描述</div>
      <el-input class="descripe" clearable type="textarea" maxlength="200" show-word-limit placeholder="请详细描述您的问题，我们会尽快解决" v-model="params.content">
      </el-input>
    </div>
    <div class="cell">
      <div class="title">提交截图（最多3张）</div>
      <el-upload action="" list-type="picture-card" :on-preview="handlePictureCardPreview" :on-remove="removeImg" :http-request="submitUpload">
        <i class="el-icon-plus"></i>
      </el-upload>
    </div>
    <el-button type="primary" :loading="loading" @click="submit">提交</el-button>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import { uploadFeedbackImg, saveFeedback } from "../../api/setting.js";
export default {
  name: "feedbackInfo",
  data() {
    return {
      typeList: [
        {
          name: "账号问题",
          type: 1,
        },
        {
          name: "功能问题",
          type: 2,
        },
        {
          name: "优化建议",
          type: 3,
        },
        {
          name: "其他问题",
          type: 4,
        },
      ],
      params: {
        type: 1,
        content: "",
        pictures: [],
      },
      pictures: [],
      baseUrl: process.env.VUE_APP_BASE_URL,
      dialogImageUrl: "",
      dialogVisible: false,
      fileList: [],
      loading: false,
    };
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    submit() {
      if (!this.params.content.trim()) {
        return this.$message.warning("请描述你的问题");
      }
      this.params.pictures = this.pictures.map((v) => v.url);
      console.log(this.params);
      this.loading = true;
      saveFeedback(this.params).then((res) => {
        this.loading = false;
        if (res.code === 200) {
          this.$message.success("反馈成功");
          this.params = {
            type: 1,
            content: "",
            pictures: [],
          };
          this.pictures = [];
          this.$bus.$emit('goFeedbackList',1)
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    removeImg(file) {
      console.log(file, fileList);
      this.pictures = this.pictures.filter((v) => {
        return v.fileName !== file.name;
      });
    },
    submitUpload(params) {
      let file = new FormData();
      file.append("file", params.file);
      uploadFeedbackImg(file).then((res) => {
        if (res.code === 200) {
          this.pictures.push(res.data);
        } else {
          this.$message.error(res.msg);
        }
        console.log(res);
      });
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  // 生命周期 - 创建之前
  beforeCreate() {},
  // 生命周期 - 挂载之前
  beforeMount() {},
  // 生命周期 - 更新之前
  beforeUpdate() {},
  // 生命周期 - 更新之后
  updated() {},
  // 生命周期 - 销毁之前
  beforeDestroy() {},
  // 生命周期 - 销毁完成
  destroyed() {},
  // 如果页面有keep-alive缓存功能，这个函数会触发
  activated() {},
};
</script>

<style lang="scss" scoped  type="text/css">
.cell {
  margin: 40px 0;
  .title {
    position: relative;
    font-size: 16px;
    font-weight: bold;
    line-height: 22px;
    color: #333333;
    padding-left: 10px;
    margin: 24px 0 13px;
  }
  .title::before {
    width: 3px;
    height: 16px;
    background: rgba(252, 114, 0, 0.39);
    border-radius: 2px;
    content: "";
    position: absolute;
    top: 2px;
    left: 0;
  }
  .type-list {
    display: flex;
    .type {
      width: 90px;
      height: 36px;
      line-height: 36px;
      text-align: center;
      border-radius: 2px 2px 2px 2px;
      border: 1px solid #e6e6e6;
      font-size: 14px;
      font-weight: 500;
      color: #333333;
      margin-right: 24px;
    }
    .active {
      border-color: #fc7200;
      color: #fc7200;
    }
  }
  .descripe {
    width: 800px;
    height: 200px;
    border-radius: 8px 8px 8px 8px;
    background: #f5f5f5;
    /deep/ .el-textarea__inner {
      border: none !important;
      background: #f5f5f5 !important;
      height: 200px !important;
    }
  }
}
</style>