<template>
  <div class="InteractIssue">
    <div class="txt1">
      <div class="txtBs">{{ txt1.length }}/20</div>
      <van-field
        v-model.trim="txt1"
        maxlength="20"
        placeholder="在此处填写标题"
      />
    </div>
    <div class="txt2">
      <div class="txtBs">{{ txt2.length }}/500</div>
      <van-field
        v-model="txt2"
        type="textarea"
        maxlength="500"
        placeholder="在此处填写正文"
      />
    </div>
    <div class="imgTit">
      <img src="../../../assets/img/interact/topic.png" alt="" />
      话题：
    </div>
    <!-- 话题标签 -->
    <div class="topicBox">
      <div
        class="topic"
        v-for="item in topicData"
        :key="item.id"
        :class="{ topicAc: topic === item.id }"
        @click="topic = item.id"
      >
        #{{ item.name }}
      </div>
    </div>

    <div class="imgTit" @click="LocationShow = true">
      <div class="rightInco">
        <van-icon name="arrow" />
      </div>
      <img src="../../../assets/img/interact/add.png" alt="" />
      位置：
    </div>
    <!-- 定位标签 -->
    <div class="locationBox" v-show="locationInfo.id">
      <div class="location locationAc">{{ locationInfo.name }}</div>
    </div>

    <!-- 上传图片 -->
    <div class="upFile">
      <div>
        <input
          type="file"
          accept=".png,.jpg,.gif"
          ref="myInput"
          @input="handeUpPhoto"
          v-show="0"
        />
        <div
          class="upImg"
          @click="$refs.myInput.click()"
          v-show="imgList.length < 5"
        ></div>
      </div>
      <Draggable
        class="moveDraggable"
        :class="{ moveDraggableAll: imgList.length === 5 }"
        v-model="imgList"
        group="itxst"
        @start="start"
        animation="300"
      >
        <transition-group>
          <div class="imgRow" v-for="(item, index) in imgList" :key="item.id">
            <img
              :src="baseURL + item.filePath"
              alt=""
              @click="lookImg(baseURL + item.filePath)"
            />
            <!-- 删除 -->
            <div class="delImg" @click="delImg(item.id)"></div>
            <!-- 封面 -->
            <div class="imgCover" v-show="index === 0">封面</div>
          </div>
        </transition-group>
      </Draggable>
    </div>
    <div class="imgUpTit">
      可拖动图片调整顺序，首张图将作为封面。<br />最大支持5M，最多可上传5张图片。
    </div>
    <!-- 发布按钮 -->
    <div class="issueBtn" @click="issueDone">发 布</div>

    <!-- 点击定位之后出来的页面 -->
    <Location
      v-show="LocationShow"
      @closeLoc="LocationShow = false"
      @setLocationInfo="setLocationInfo"
    />
  </div>
</template>

<script>
import { baseURL } from "@/utils/request";
import {
  uploadImgAPI,
  shareSaveAPI,
  delImgAPI,
  getDictAPI,
} from "@/api/interact.js";
import Location from "./interactLocation.vue";
import { Toast } from "vant";
//导入draggable组件
import Draggable from "vuedraggable";
import { Dialog, ImagePreview } from "vant";
export default {
  name: "InteractIssue",
  components: { Draggable, Location },
  data() {
    return {
      baseURL,
      txt1: "",
      txt2: "",
      topicData: [
        // { id: 1, name: "景点" },
        // { id: 2, name: "美食" },
        // { id: 3, name: "游玩" },
        // { id: 4, name: "酒店" },
      ],
      topic: 1,
      locationInfo: {},
      LocationShow: false,
      // 上传相关
      imgList: [
        // {
        //   id: 65,
        //   fileName: "22222.jpg",
        //   filePath: "/comment/img/20221125_1113069452.jpg",
        // },
      ],
    };
  },
  computed: {},
  watch: {},
  methods: {
    // -------------关于上传
    async handeUpPhoto(e) {
      if (e.target.files) {
        // 拿到files信息
        const filesInfo = e.target.files[0];
        // 校验格式
        const type = ["image/jpeg", "image/png", "image/gif"];
        if (!type.includes(filesInfo.type)) {
          e.target.value = "";
          return Toast.fail("只支持jpg、png、gif格式!");
        }
        // 校验大小
        if (filesInfo.size > 5 * 1024 * 1024) {
          e.target.value = "";
          return Toast.fail("最大支持5M!");
        }
        // 创建FormData对象
        const fd = new FormData();
        // 把files添加进FormData对象（‘photo’为后端需要的字段）
        fd.append("file", filesInfo);
        fd.append("type", "img");
        const res = await uploadImgAPI(fd);
        if (res.code === 0) {
          Toast.success("上传成功!");
        } else Toast.fail(res.msg);
        this.imgList.push(res.data);
      }
    },

    // 设置位置
    setLocationInfo(item) {
      this.locationInfo = item;
      this.LocationShow = false;
    },
    // 点击发布
    async issueDone() {
      if (this.txt1 === "") return Toast.fail("标题不能为空!");
      if (this.txt2.replaceAll("\n", "").replaceAll(" ", "") === "")
        return Toast.fail("正文不能为空!");
      if (!this.locationInfo.id) return Toast.fail("请选择位置!");
      if (this.imgList.length === 0) return Toast.fail("至少上传一张图片!");
      // 图片id数组
      const imgArr = this.imgList.map((v) => {
        return v.id;
      });
      const obj = {
        description: this.txt2
          .replaceAll("\n", "<br/>")
          .replaceAll(" ", "&ensp;"),
        positionId: this.locationInfo.id,
        dictTopicId: this.topic,
        fileIds: imgArr.join(","),
        name: this.txt1,
        thumb: this.imgList[0].filePath,
      };
      const res = await shareSaveAPI(obj);
      if (res.code === 0) {
        Toast.success("发布成功!");
        this.$router.push("/layout/interact");
      } else Toast.fail(res.msg);
    },
    // 拖拽图片
    start(e) {},
    // 查看大图
    lookImg(url) {
      ImagePreview({
        images: [url],
        showIndex: false,
      });
    },
    // 点击删除图片
    delImg(id) {
      Dialog.confirm({
        title: "确定删除吗?",
        beforeClose: async (action, done) => {
          if (action === "confirm") {
            await delImgAPI(id);
            // 点击了确定
            this.imgList = this.imgList.filter((v) => v.id !== id);
            Toast.success("删除成功!");
          }
          done();
        },
      });
    },
  },
  async created() {
    // 获取话题列表
    const res = await getDictAPI("topic");
    this.topicData = res.data;
  },
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='less' scoped>
.InteractIssue {
  width: 100%;
  height: 100%;
  padding: 20px 15px;
  overflow-y: auto;

  /deep/.van-cell {
    width: 100%;
    height: 36px;
    line-height: 36px;
    padding: 0 40px 0 5px;
    background-color: #f4f4f4;
  }

  .txt1 {
    width: 100%;
    height: 36px;
    position: relative;

    .txtBs {
      z-index: 3;
      color: #9e9a9a;
      font-size: 14px;
      position: absolute;
      right: 3px;
      top: 50%;
      transform: translateY(-50%);
    }
  }

  .txt2 {
    margin: 20px 0;
    position: relative;
    .txtBs {
      z-index: 3;
      color: #9e9a9a;
      font-size: 14px;
      position: absolute;
      right: 3px;
      bottom: 5px;
    }
    /deep/.van-field__body {
      height: calc(100% - 30px);
    }
    /deep/textarea {
      height: 100%;
    }
    /deep/.van-cell {
      line-height: 21px;
      width: 100%;
      height: 200px;
      padding: 0 5px;
      background-color: #f4f4f4;
    }
  }

  .imgTit {
    margin: 20px 0;
    font-size: 18px;
    font-weight: 700;
    display: flex;
    align-items: center;
    position: relative;
    .rightInco {
      position: absolute;
      right: 0;
      top: 50%;
      transform: translateY(-50%);
      color: #c2c2c2;
      font-size: 22px;
    }
    & > img {
      width: 20px;
      margin-right: 8px;
    }
  }

  .topicBox {
    display: flex;
    flex-wrap: wrap;
    .topic {
      font-size: 14px;
      border-radius: 17px;
      border: 1px solid #393939;
      min-width: 60px;
      padding: 0 10px;
      height: 34px;
      line-height: 32px;
      margin-right: 16px;
      margin-bottom: 12px;
    }
    .topicAc {
      border: none;
      line-height: 34px;
      background-color: #ff7e89;
      color: #fff;
    }
  }

  .locationBox {
    display: flex;
    flex-wrap: wrap;

    .location {
      font-size: 14px;
      border-radius: 17px;
      border: 1px solid #393939;
      min-width: 60px;
      padding: 0 10px;
      height: 34px;
      line-height: 32px;
      margin-right: 16px;
      margin-bottom: 12px;
      text-align: center;
    }
    .locationAc {
      border: none;
      line-height: 34px;
      background-color: #b5d4ff;
      color: #fff;
    }
  }

  .upFile {
    margin: 20px 0 0px;
    display: flex;
    flex-wrap: wrap;

    .upImg {
      width: 80px;
      height: 80px;
      background-image: url("../../../assets/img/interact/upload.png");
      background-size: 100% 100%;
      margin-right: 10px;
      margin-bottom: 10px;
    }
    .moveDraggable {
      width: calc(100% - 90px);
    }
    .moveDraggableAll {
      width: 100%;
    }
    .moveDraggable span {
      display: flex;
      flex-wrap: wrap;
    }
    .imgRow {
      position: relative;
      margin-right: 10px;
      margin-bottom: 10px;
      width: 80px;
      height: 80px;
      border-radius: 6px;
      & > img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
      .delImg {
        position: absolute;
        width: 16px;
        height: 16px;
        top: -6px;
        right: -6px;
        background-image: url("../../../assets/img/interact/del.png");
        background-size: 100% 100%;
      }
      .imgCover {
        pointer-events: none;
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 40px;
        line-height: 50px;
        text-align: center;
        font-size: 14px;
        color: #fff;
        background-image: url("../../../assets/img/interact/bg.png");
        background-size: 100% 100%;
      }
    }
  }
  .imgUpTit {
    font-size: 12px;
    color: #a6a6a6;
    margin: 10px 0 40px;
  }
  .issueBtn {
    width: 80%;
    height: 50px;
    max-width: 300px;
    margin: 0 auto 30px;
    line-height: 50px;
    background-color: #ff7e89;
    border-radius: 25px;
    text-align: center;
    color: #fff;
    font-size: 18px;
  }
}
</style>