<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="form-label">转让数量</div>
          <input
            v-model="form.number"
            placeholder="请填写转赠数量，正整数"
            type="number"
            class="input-text"
          />
        </div>
        <div class="form-item flex-default">
          <div class="form-label">转让方式</div>
          <div class="radio-list flex-default">
            <div
              class="radio-item flex-default"
              v-for="(item, index) of transferTypeList"
              :key="`radio-item-${index}`"
              @click="form.type = item.val"
            >
              <div
                class="radio-icon"
                :class="{ active: form.type == item.val }"
              />
              <span class="label">{{ item.label }}</span>
            </div>
          </div>
        </div>
        <div class="form-item">
          <div class="flex-default">
            <div class="form-label">转让价格</div>
            <input
              v-model="form.price"
              placeholder="请输入出售价格，保留两位小数"
              type="number"
              class="input-text"
            />
          </div>
          <div class="price-con flex-default">
            <div class="caution">!</div>
            <span class="price">成本价：{{ skuData.originalPrice }}元</span>
            <span class="price">现价：{{ skuData.originalPrice }}元</span>
          </div>
        </div>
        <div class="form-item flex-default">
          <div class="form-label">备注</div>
          <input
            v-model="form.note"
            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: {},
      transferTypeList: [
        { label: "单价", val: 1 },
        { label: "打包价", val: 2 },
      ],
      form: {
        coin: "",
        goodId: "",
        skuId: "",
        price: "",
        number: "",
        type: "",
        note: "",
      },
    };
  },
  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.number) {
        return "请填写转赠数量！";
      } else if (!this.form.type) {
        return "请选择转让方式！";
      } else if (!this.form.price) {
        return "请填写转让价格！";
      } else if (this.form.number > this.coinData.amount) {
        return "可转赠数量不足！";
      } else if (!this.form.note) {
        return "请填写备注！";
      }
      return null;
    },
    handleSubmit() {
      let errMsg = this.checkForm();
      if (errMsg) {
        return this.$toast(errMsg);
      }
      this.$showLoading();
      this.$api({
        apiName: "assetsTransfer",
        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;
        .form-label {
          width: 60px;
          font-size: 14px;
          margin-right: 18px;
          font-weight: bold;
        }
        .input-text {
          flex: 1;
          font-size: 14px;
          &::placeholder {
            color: #aeaeae;
          }
        }
        .QRcode {
          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=);
        }
        .radio-list {
          flex: 1;
          .radio-item {
            margin-right: 30px;
            &:last-of-type {
              margin-right: 0;
            }
          }
          .radio-item > .label {
            color: #aeaeae;
            font-size: 14px;
          }
          .radio-item > .radio-icon {
            height: 18px;
            width: 18px;
            border: 1px solid #aeaeae;
            border-radius: 50%;
            margin-right: 10px;
            &.active {
              border: 4px solid @mall-primary-color;
            }
          }
        }
        .price-con {
          margin-top: 8px;
          background: #fef1f4;
          border-radius: 2px;
          border: 1px solid #dd2e41;
          height: 25px;
          color: @mall-primary-color;
          font-size: 12px;
          padding-left: 12px;
          .caution {
            width: 12px;
            height: 12px;
            line-height: 12px;
            background: @mall-primary-color;
            color: #fef1f4;
            border-radius: 50%;
            text-align: center;
            margin-right: 8px;
          }
          .price {
            margin-right: 15px;
          }
        }

        &: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>
