<template>
  <div class="assets-transfer-gift-con bg-grey">
    <div style="height: 5px" />
    <div class="crad-con goods-con">
      <div class="title">转赠商品</div>
      <div class="goods-content flex-default">
        <img class="img-box" :src="skuData.thumb" />
        <div class="goods-message">
          <div class="desc">
            {{ skuData.name }}
          </div>
          <div class="desc">
            {{ skuData.coinName }}
          </div>
          <div class="classification flex-default">
            <div class="text">{{ skuLabel }}</div>
          </div>
          <div class="number">
            <div class="text">可赠数量：{{ coinData.amount }}</div>
          </div>
        </div>
      </div>
    </div>
    <div style="height: 10px" />
    <div class="crad-con form-con">
      <div class="form-list">
        <div class="form-item flex-default">
          <div class="label">转赠地址</div>
          <input
            v-model="form.toAddress"
            placeholder="请填写转赠地址"
            class="input-text"
          />
          <!-- <div class="m-icon QRcode" /> -->
        </div>
        <div class="form-item flex-default">
          <div class="label">转赠数量</div>
          <input
            v-model="form.num"
            placeholder="请填写转赠数量，正整数"
            type="number"
            class="input-text"
          />
        </div>
        <div class="form-item flex-default">
          <div class="label">备注</div>
          <input
            v-model="form.remark"
            placeholder="请填写转赠备注，50字以内"
            class="input-text"
          />
        </div>
      </div>
    </div>
    <div class="foorter-btn">
      <van-button type="primary" round block @click="handleSubmit">
        提 交
      </van-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      coin: "",
      coinData: {},
      form: {
        coin: "",
        goodsId: "",
        skuId: "",
        toAddress: "",
        num: "",
        remark: "",
      },
    };
  },
  computed: {
    skuData() {
      if (this.coinData && this.coinData.skuVo) {
        return this.coinData.skuVo;
      }
      return {};
    },
    skuLabel() {
      if (this.skuData.skuProps) {
        return this.skuData.skuProps
          .map((item) => `${item.propKey}: ${item.propVal}`)
          .join(" ");
      }
      return "";
    },
  },
  methods: {
    requestData(coin) {
      this.$showLoading();
      this.$api({
        apiName: "getOwnAsset",
        postData: { coin },
        success: (res) => {
          this.coinData = res;
          this.form.coin = coin;
          this.form.goodsId = this.skuData.goodsId;
          this.form.skuId = this.skuData.skuId;
        },
        complete: this.$hideLoading,
      });
    },
    checkForm() {
      if (!this.form.toAddress) {
        return "请填写转赠地址！";
      } else if (!this.form.num) {
        return "请填写转赠数量！";
      } else if (this.form.num > this.coinData.amount) {
        return "可转赠数量不足！";
      } else if (!this.form.remark) {
        return "请填写备注！";
      }
      return null;
    },
    handleSubmit() {
      let errMsg = this.checkForm();
      if (errMsg) {
        return this.$toast(errMsg);
      }
      this.$showLoading();
      this.$api({
        apiName: "assetsTransferGift",
        postData: this.form,
        success: () => {
          this.$toast("提交成功~");
          setTimeout(() => {
            this.goBack();
          }, 500);
        },
        complete: this.$showLoading(),
      });
    },
  },
  created() {
    this.coin = this.$route.query.coin;
    this.requestData(this.coin);
  },
};
</script>

<style lang="less">
.assets-transfer-gift-con {
  height: 100%;

  .crad-con {
    background-color: #ffffff;
    border-radius: 10px;
  }

  .goods-con {
    margin-top: 5px;
    padding: 16px;

    .title {
      font-size: 16px;
      line-height: 22px;
      margin-bottom: 16px;
      color: #140603;
      font-weight: bold;
      padding-left: 2px;
    }

    .goods-content {
      .img-box {
        width: 98px;
        height: 98px;
        background-color: #d8d8d8;
        border-radius: 8px;
        overflow: hidden;
        flex-shrink: 0;
      }

      .goods-message {
        flex-shrink: 1;
        height: 98px;
        margin-left: 10px;
        width: calc(~"100% - 108px");

        .desc {
          font-size: 14px;
          line-height: 20px;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
          max-height: 40px;
        }

        .text {
          font-size: 12px;
          color: #aeaeae;
          line-height: 17px;
        }

        .classification {
          // height: 17px;
          margin: 9px 0 2px;
          min-height: 17px;
          max-height: 34px;
        }
      }
    }
  }

  .form-con {
    padding: 0 16px;
    .form-list {
      .form-item {
        width: 100%;
        padding: 18px 0;
        border-bottom: 1px solid #f1f1f1;
        .label {
          width: 60px;
          font-size: 14px;
          margin-right: 18px;
          font-weight: bold;
        }
        .input-text {
          flex: 1;
          font-size: 14px;
          &::placeholder {
            color: #aeaeae;
          }
        }
        .QRcode {
          margin-left: 10px;
          width: 11px;
          height: 11px;
          background-size: 100%;
          background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAA/klEQVRIS9WVr04DQRDGv+98gywGg8MQbCUCcFx2XwEeAlEJSR8CHuFmc7gWkkoswfAACKgs9feRudSRivZuRUdOdn/z55udJQCY2QmACclzSQP3bWskV5LmAO5ijJ9cQ98AHGwL23B+CWDk4BrANYAfkmMAXzsGOJJ0D+AQwDNTSr9ePsnbEMLTjtD2WkrpRtKjt8UzljtJXoUQZh3Bl5KmLW8vwd8kh0VRnJVl+dGlFXVdnzZN8y5pwaqqjh0cY/SR62xmNmrBnUkbAC6eq3jhQvYUxKfsJRf4ta8s/xWbTzwzyzNue/ny8iyhnGuzz0X/AGDYLvpsX1Ouz/QP7eXR4sSrB1EAAAAASUVORK5CYII=);
        }

        &:last-child {
          border: none;
        }
      }
    }
  }
  .foorter-btn {
    height: 60px;
    background-color: #ffffff;
    z-index: 100;
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    button {
      width: 343px;
      height: 40px;
      border-radius: 20px;
      font-size: 16px;
      margin: 12px auto 0;
    }
  }
}
</style>
