<template>
  <div class="image_form">
    <div class="label">
      <img class="pre-tag" src="../../../assets/images/template/pre-tag.png" />
      <div class="label-text" :title="property.label">{{ property.label }}</div>
    </div>
    <div
      class="form_icon_require"
      :class="{ 'js-form_icon_require_empty': imageInfo.imgList.length === 0 }"
    >
      *
    </div>
    <div class="content_wrapper">
      <div class="list_wrapper">
        <div class="list-items">
          <div
            class="list-item"
            v-for="(item, index) in imageInfo.imgList"
            :key="index"
            @click="ReviewImages(item)"
          >
            <img class="list-item-img" :src="item.url || item.dataUrl" />
            <i
              class="iconfont iconshanchu3"
              @click="deleteimg(item, index)"
            ></i>
          </div>
          <div class="add_wrapper">
            <div class="content-upload_box" @click="showSelectFile">
              <i class="iconfont iconjiahao"></i>
            </div>
          </div>
        </div>
      </div>
    </div>
    <input
      ref="inputDom"
      type="file"
      accept="image/jpeg, image/png"
      @change="fileChange"
      hidden
    />
    <!-- 图片放大镜 -->
    <common-dialog
      v-if="Magnifier"
      @closePopup="Magnifier = false"
      showMask
      @ClickingBackground="Magnifier = false"
    >
      <div class="display-area">
        <img :src="pcUrl" />
      </div>
    </common-dialog>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue, Watch } from "vue-property-decorator";
import { ImageInfo } from "@/utils/app/AddProductTypes";
import { Property } from "@/plugins/types2";
import { GO_URLS } from "@/config/URLS";
import CommonDialog from "@/components/CommonDialog.vue";
@Component({
  components: {
    CommonDialog,
  },
})
export default class ImageForm extends Vue {
  @Prop({ type: Boolean, default: false }) disable!: boolean;
  @Prop() property!: Property;
  public imageInfo: any = {
    imgList: [],
  };
  public Magnifier: boolean = false;
  public pcUrl: string = "";
  mounted() {
    if (!Array.isArray(this.property.value)) return;
    this.property.value.forEach((url) => {
      this.PushList(url);
    });
  }
  @Watch(`imageInfo.imgList`)
  imageInfoChange() {
    this.property.value = this.imageInfo.imgList;
  }
  public async PushList(url: any) {
    if (!url) return false;
    this.imageInfo.imgList.push({
      url: await this.requestPictures(url.value),
      hash: url.value, //同时保存hash
    });
  }
  // 图片回显
  public ReviewImages(item: any) {
    if (item.url != "") {
      this.pcUrl = item.url;
    } else {
      this.pcUrl = item.dataUrl;
    }
    this.Magnifier = true;
  }
  // 图片替换
  public async requestPictures(string: string) {
    const res = await this.$ajax({
      type: "post",
      url: GO_URLS.getFiles,
      params: { hashes: [string] },
    });

    if (res) {
      return res.data.files[0].url;
    }
  }
  public showSelectFile() {
    if (this.disable) {
      return;
    }
    if (this.imageInfo.imgList.length > 10) {
      return;
    }
    (this.$refs.inputDom as any).click();
  }
  // 删除图片
  deleteimg(item: any, index: number) {
    this.imageInfo.imgList.splice(index, 1);
  }
  public fileChange(e: any) {
    const file: File = e.target.files[0];
    if (!file) {
      return;
    }
    const fr = new FileReader();
    fr.onload = (event: any) => {
      this.imageInfo.imgList.push({
        url: "",
        dataUrl: event.target.result,
        file,
        hash: "",
      });
    };
    fr.readAsDataURL(file);
    e.target.value = null;
  }
}
</script>
<style scoped lang="less">
.display-area {
  max-height: 80vh;
  overflow-y: scroll;
  img {
    width: 100%;
  }
}
.image_form {
  flex-shrink: 0;
  position: relative;
  padding-top: 10px;
  padding-left: 18px;
  padding-bottom: 10px;
  font-size: 0;
  display: flex;
  justify-content: flex-start;
  // align-items: center;
}
.label {
  display: inline-block;
  width: 150px; /* 104 - 18 */
  height: 44px;
  line-height: 44px;
  color: #797d84; /* todo ui图颜色不一致 */
  font-size: 14px;
  font-weight: 300;

  position: relative;
}
.label-text {
  text-overflow: ellipsis;
  flex-shrink: 0;
  white-space: nowrap; /*规定段落中的文本不进行换行 */
  overflow: hidden;
}

.content_wrapper {
  flex: 1;
  display: flex;
}
.list_wrapper {
  width: 452px;
}
.add_wrapper {
  margin-right: 10px;
  float: left;
}
.list-items {
  float: left;
}
.content-upload_box {
  width: 80px;
  height: 80px;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  text-align: center;
  line-height: 80px;
  .iconjiahao {
    font-size: 24px;
    color: #d7d7d7;
  }
}
// 图片
.list-item {
  float: left;
  margin-right: 10px;
  margin-bottom: 10px;
  width: 80px;
  height: 80px;
  cursor: pointer;
  background: rgba(216, 216, 216, 1);
  border-radius: 4px;
  text-align: center;
  position: relative;
  .list-item-img {
    width: 100%;
    height: 100%;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    overflow: hidden;
  }
  .iconshanchu3 {
    position: absolute;
    right: -10px;
    font-size: 18px;
    top: -10px;
    cursor: pointer;
  }
}

.form_icon_require {
  position: absolute;
  left: -10px;
  top: 0;
  color: #ff6565;
  font-size: 14px;
  line-height: 44px;
  display: none;
}

.pre-tag {
  position: absolute;
  left: -30px;
  top: 14px;
  display: none;
}
</style>
