<template>
  <view class="draw-style1" :style="{ zoom: zoom }">
    <u-top-tips ref="uTips"></u-top-tips>
    <canvas
      canvas-id="firstCanvas"
      class="sty-box"
      @error="canvasIdErrorCallback"
    ></canvas>
  </view>
</template>

<script>
export default {
  props: {
    autoHeight: {
      type: String,
      default: "0",
    },
    images: {
      type: Array,
      default: [],
    },
    bgType: {
      type: Number,
      default: 0,
    },
    bgColor: {
      type: String,
      default: "",
    },
    info: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      zoom: 1,
      config: {
        images: [],
        bgType: 1,
        bgColor: "",
        info: {},
        loca_images: [],
      },
      ctx: null,
    };
  },
  watch: {
    images: {
      handler: function (val, oldval) {
        this.config.images = val;
        this.drawCtx();
      },
      deep: true,
    },
    bgType: {
      handler: function (val, oldval) {
        this.config.bgType = val;
        this.drawCtx();
      },
      deep: true,
    },
    bgColor: {
      handler: function (val, oldval) {
        this.config.bgColor = val;
        this.drawCtx();
      },
      deep: true,
    },
    info: {
      handler: function (val, oldval) {
        this.config.info = val;
        this.drawCtx();
      },
      deep: true,
    },
  },
  mounted() {
    console.log("in draw");

    this.zoom = (parseFloat(this.autoHeight) / 1344.0).toFixed(2);
    this.config.images = this.images;
    this.config.bgType = this.bgType;
    this.config.bgColor = this.bgColor;
    this.drawCtx();
  },
  onReady: function (e) {},
  methods: {
    colorRgb(color) {
      var sColor = color.toLowerCase();
      //十六进制颜色值的正则表达式
      var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
      // 如果是16进制颜色
      if (sColor && reg.test(sColor)) {
        if (sColor.length === 4) {
          var sColorNew = "#";
          for (var i = 1; i < 4; i += 1) {
            sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
          }
          sColor = sColorNew;
        }
        //处理六位的颜色值
        var sColorChange = [];
        for (var i = 1; i < 7; i += 2) {
          sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));
        }
        return sColorChange.join(",");
      }
      return sColor;
    },
    saveImage() {
      uni.showLoading({
        title: "正在生成海报",
      });
      let that = this;
      uni.canvasToTempFilePath(
        {
          canvasId: "firstCanvas",
          success: (res) => {
            uni.hideLoading();
            uni.showLoading({
              title: "正在保存海报",
            });
            uni.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success: function () {
                uni.showToast({
                  title: "保存成功",
                });
              },
              fail: function (e) {
                that.$refs.uTips.show({
                  title: "保存失败，请稍后重试",
                  type: "error",
                  duration: "2300",
                });
              },
              complete: function () {
                uni.hideLoading();
              },
            });
          },
          fail: function (e) {
            console.log(e);
            that.$refs.uTips.show({
              title: "生成失败，请稍后重试",
              type: "error",
              duration: "2300",
            });
          },
        },
        this
      );
    },
    drawCtx() {
      let ctx = uni.createCanvasContext("firstCanvas", this);
      let that = this;
      if (!this.info.avatar_loca) {
        uni.getImageInfo({
          src: this.info.avatar,
          success: function (image) {
            that.drawBg(ctx);
            that.drawGoodPicture(ctx);
            that.info.avatar_loca = image.path;
          },
        });
      } else {
        this.drawBg(ctx);
        this.drawGoodPicture(ctx);
      }
    },
    drawGoodPicture(ctx) {
      console.log(this.config.images);
      if (this.config.images.length == 1) {
        this.drawOnePic(ctx);
      } else if (this.config.images.length == 2) {
        this.drawTowPic(ctx);
      } else if (this.config.images.length == 3) {
        this.drawThreePic(ctx);
      } else if (this.config.images.length == 4) {
        this.drawFourPic(ctx);
      } else if (this.config.images.length == 5) {
        this.drawFivePic(ctx);
      }
    },
    drawAvatar(ctx) {
      ctx.save();
      ctx.beginPath();
      ctx.arc(99, 1150, 45, 0, Math.PI * 2);
      ctx.setFillStyle("#ffffff");
      ctx.fill();
      ctx.clip();
      ctx.drawImage(this.info.avatar_loca, 54, 1105, 90, 90);
      ctx.restore();
      ctx.fillStyle = "#000000";
      ctx.font = "20px 'microsoft yahei'";
      ctx.fillText(this.info.nickname, 54+90+20, 1160);
    },
    drawTitle(ctx) {
      let text = "长按识别小程序码进入";
      this.roundRect(ctx, 54, 1210, 280, 52, 52, "#f5f5f5");
      ctx.fillStyle = "#000000";
      ctx.font = "24px 'microsoft yahei'";
      ctx.fillText(text, 74, 1244);
    },
    drawBg(ctx) {
      if (this.config.bgType == 1) {
        ctx.fillStyle = this.config.bgColor;
        ctx.fillRect(0, 0, 750, 1344);
      } else {
        let g = ctx.createLinearGradient(0, 0, 0, 1344);
        g.addColorStop(0, this.config.bgColor);
        console.log(`rgba(${this.colorRgb(this.config.bgColor)},0.3)`);
        g.addColorStop(1, `rgba(${this.colorRgb(this.config.bgColor)},0.3)`);
        ctx.fillStyle = g;
        ctx.fillRect(0, 0, 750, 1344);
      }
    },
    drawQrCode(ctx) {
      uni.getImageInfo({
        src: this.info.qrcode_url,
        success: function (image) {
          ctx.drawImage(image.path, 458, 1063, 215, 215);
          ctx.draw();
        },
      });
    },
    drawOnePic(ctx) {
      let that = this;
      if (!this.config.loca_images[0]) {
        uni.getImageInfo({
          src: this.config.images[0],
          success: function (image) {
            that.config.loca_images[0] = image.path;
            ctx.drawImage(image.path, 0, 0, 750, 750);
            that.otherDraw(ctx);
          },
        });
      } else {
        ctx.drawImage(this.config.loca_images[0], 0, 0, 750, 750);
        that.otherDraw(ctx);
      }
    },
    drawTowPic(ctx) {
      if (this.config.loca_images.length < 2) {
        let that = this;
        uni.getImageInfo({
          src: this.config.images[this.config.loca_images.length],
          success: function (image) {
            that.config.loca_images.push(image);
            that.drawTowPic(ctx);
          },
        });
      } else {
        ctx.fillStyle = "#000";
        ctx.fillRect(0, 0, 750, 375);
        var imgRect = this.coverImg(
          750,
          375,
          this.config.loca_images[0].width,
          this.config.loca_images[0].height
        );
        ctx.drawImage(
          this.config.loca_images[0].path,
          imgRect.sx,
          imgRect.sy,
          imgRect.sWidth,
          imgRect.sHeight,
          0,
          0,
          750,
          375
        );

        ctx.fillStyle = "#000";
        ctx.fillRect(0, 375, 750, 375);
        var imgRect = this.coverImg(
          750,
          375,
          this.config.loca_images[1].width,
          this.config.loca_images[1].height
        );
        ctx.drawImage(
          this.config.loca_images[1].path,
          imgRect.sx,
          imgRect.sy,
          imgRect.sWidth,
          imgRect.sHeight,
          0,
          375,
          750,
          375
        );

        this.otherDraw(ctx);
      }
    },
    drawThreePic(ctx) {
      if (this.config.loca_images.length < 3) {
        let that = this;
        uni.getImageInfo({
          src: this.config.images[this.config.loca_images.length],
          success: function (image) {
            that.config.loca_images.push(image);
            that.drawThreePic(ctx);
          },
        });
      } else {
        ctx.fillStyle = "#000";
        ctx.fillRect(0, 0, 750, 375);
        var imgRect = this.coverImg(
          750,
          375,
          this.config.loca_images[0].width,
          this.config.loca_images[0].height
        );
        ctx.drawImage(
          this.config.loca_images[0].path,
          imgRect.sx,
          imgRect.sy,
          imgRect.sWidth,
          imgRect.sHeight,
          0,
          0,
          750,
          375
        );
        ctx.drawImage(this.config.loca_images[1].path, 0, 375, 375, 375);
        ctx.drawImage(this.config.loca_images[2].path, 375, 375, 375, 375);
        this.otherDraw(ctx);
      }
    },
    drawFourPic(ctx) {
      if (this.config.loca_images.length < 4) {
        let that = this;
        uni.getImageInfo({
          src: this.config.images[this.config.loca_images.length],
          success: function (image) {
            that.config.loca_images.push(image);
            that.drawFourPic(ctx);
          },
        });
      } else {
        ctx.drawImage(this.config.loca_images[0].path, 0, 0, 375, 375);
        ctx.drawImage(this.config.loca_images[1].path, 375, 0, 375, 375);
        ctx.drawImage(this.config.loca_images[2].path, 0, 375, 375, 375);
        ctx.drawImage(this.config.loca_images[3].path, 375, 375, 375, 375);
        this.otherDraw(ctx);
      }
    },
    drawFivePic(ctx) {
      if (this.config.loca_images.length < 5) {
        let that = this;
        uni.getImageInfo({
          src: this.config.images[this.config.loca_images.length],
          success: function (image) {
            that.config.loca_images.push(image);
            that.drawFivePic(ctx);
          },
        });
      } else {
        ctx.drawImage(this.config.loca_images[0].path, 0, 0, 375, 375);
        ctx.drawImage(this.config.loca_images[1].path, 0, 375, 375, 375);

        ctx.fillStyle = "#000";
        ctx.fillRect(375, 0, 375, 250);
        var imgRect = this.coverImg(
          375,
          250,
          this.config.loca_images[2].width,
          this.config.loca_images[2].height
        );
        ctx.drawImage(
          this.config.loca_images[2].path,
          imgRect.sx,
          imgRect.sy,
          imgRect.sWidth,
          imgRect.sHeight,
          375,
          0,
          375,
          250
        );
        ctx.fillStyle = "#000";
        ctx.fillRect(375, 250, 375, 250);
        imgRect = this.coverImg(
          375,
          250,
          this.config.loca_images[3].width,
          this.config.loca_images[3].height
        );
        ctx.drawImage(
          this.config.loca_images[3].path,
          imgRect.sx,
          imgRect.sy,
          imgRect.sWidth,
          imgRect.sHeight,
          375,
          250,
          375,
          250
        );
        ctx.fillStyle = "#000";
        ctx.fillRect(375, 500, 375, 250);
        imgRect = this.coverImg(
          375,
          250,
          this.config.loca_images[4].width,
          this.config.loca_images[4].height
        );
        ctx.drawImage(
          this.config.loca_images[4].path,
          imgRect.sx,
          imgRect.sy,
          imgRect.sWidth,
          imgRect.sHeight,
          375,
          500,
          375,
          250
        );
        this.otherDraw(ctx);
      }
    },
    coverImg(box_w, box_h, source_w, source_h) {
      var sx = 0,
        sy = 0,
        sWidth = source_w,
        sHeight = source_h;
      if (source_w > source_h || (source_w == source_h && box_w < box_h)) {
        sWidth = (box_w * sHeight) / box_h;
        sx = (source_w - sWidth) / 2;
      } else if (
        source_w < source_h ||
        (source_w == source_h && box_w > box_h)
      ) {
        sHeight = (box_h * sWidth) / box_w;
        sy = (source_h - sHeight) / 2;
      }
      return {
        sx,
        sy,
        sWidth,
        sHeight,
      };
    },

    otherDraw(ctx) {
      if (!this.config.twostyle) {
        let that = this;
        uni.getImageInfo({
          src:
            "https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Static/style-two-end.png",
          success: function (image) {
            that.config.twostyle = image.path;
            ctx.drawImage(that.config.twostyle, 24, 707, 702, 600);
            that.showGoodName(ctx);
            that.showGoodPrice(ctx);
            //that.showTips(ctx);
            that.drawQrCode(ctx);
            that.drawAvatar(ctx);
            that.drawTitle(ctx);
          },
        });
      } else {
        ctx.drawImage(this.config.twostyle, 24, 707, 702, 600);
        this.showGoodName(ctx);
        this.showGoodPrice(ctx);
        //this.showTips(ctx);
        this.drawQrCode(ctx);
        that.drawAvatar(ctx);
        that.drawTitle(ctx);
      }
    },
    showGoodPrice(ctx) {
      ctx.fillStyle = "#fc4a3b";
      ctx.font = "26px 'microsoft yahei'";
      let x = 54;
      let y = 900;
      ctx.fillText("￥", x, y);
      ctx.fillStyle = "#fc4a3b";
      ctx.font = "40px 'microsoft yahei'";
      ctx.fillText(this.info.min_price, x + 30, y);
    },
    showTips(ctx) {
      ctx.fillStyle = "gray";
      ctx.font = "24px 'microsoft yahei'";
      let x = 54;
      let y = 1194;
      ctx.fillText("长按识别小程序码进入", x, y);
    },
    showGoodName(ctx) {
      ctx.fillStyle = "#000000";
      ctx.font = "30px 'microsoft yahei'";
      let maxWidth = 646;
      let n = this.info.goods_name;
      let lines = 2;

      let x = 54;
      let y = 780;
      for (let i = 0; i < lines; i++) {
        let lineText = "";
        let currentWidth = 0;
        for (let x = 0; x < n.length; x++) {
          currentWidth += ctx.measureText(n[x]).width;
          if (currentWidth > maxWidth) {
            break;
          } else {
            lineText += n[x];
          }
        }
        ctx.fillText(lineText, x, y + i * 55);
        n = n.substring(lineText.length);
        if (n == "") {
          break;
        }
      }
      if (n != "") {
        ctx.fillText("...", x + maxWidth, y + 55);
      }
    },
    roundRect(ctx, x, y, w, h, r, c = "#fff") {
      if (w < 2 * r) {
        r = w / 2;
      }
      if (h < 2 * r) {
        r = h / 2;
      }

      ctx.beginPath();
      ctx.fillStyle = c;

      ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5);
      ctx.moveTo(x + r, y);
      ctx.lineTo(x + w - r, y);
      ctx.lineTo(x + w, y + r);

      ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2);
      ctx.lineTo(x + w, y + h - r);
      ctx.lineTo(x + w - r, y + h);

      ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5);
      ctx.lineTo(x + r, y + h);
      ctx.lineTo(x, y + h - r);

      ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI);
      ctx.lineTo(x, y + r);
      ctx.lineTo(x + r, y);

      ctx.fill();
      ctx.closePath();
    },
    canvasIdErrorCallback: function (e) {
      console.error(e.detail.errMsg);
    },
  },
};
</script>

<style>
.draw-style1 {
  width: 750px;
  height: 1344px;
  background: #fff;
}
.draw-style1 .sty-box {
  width: 100%;
  height: 100%;
}
.draw-style1 .sty-box .header {
  height: 90px;
  display: flex;
  align-items: center;
  margin-bottom: 40px;
}
.draw-style1 .sty-box .header .image {
  width: 90px;
  height: 90px;
  border-radius: 90px;
  margin-right: 20px;
}
.draw-style1 .sty-box .header .title {
  font-size: 20px;
  color: #000;
  line-height: 55px;
  height: 55px;
  border-radius: 55px;
  padding: 0 24px;
  background: #fff;
}
.draw-style1 .sty-box .content-img,
.draw-style1 .sty-box .content-img-one {
  width: 100%;
  height: 702px;
}
.draw-style1 .sty-box .info-box {
  padding: 28px 30px;
  display: flex;
  align-items: center;
  background: #fff;
}
.draw-style1 .sty-box .info-box .qrcode {
  width: 215px;
  height: 215px;
}
.draw-style1 .sty-box .info-box .left-info {
  width: 1px;
  flex: 1;
}
.draw-style1 .sty-box .info-box .left-info .good-name {
  font-size: 28px;
  color: #000;
  line-height: 1.5;
  text-overflow: -o-ellipsis-lastline;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
}
.draw-style1 .sty-box .info-box .left-info .good-price {
  color: #fc4a3b;
  font-size: 40px;
  margin-bottom: 75px;
}
.draw-style1 .sty-box .info-box .left-info .tips {
  font-size: 24px;
  color: gray;
}
</style>
