<template>
  <div class="pay-success-info">
    <div class="paySuccess_content">
      <div
        class="paySuccess_detail"
        :style="{ 'padding-top': paddingTop, 'padding-bottom': paddingBottom }"
      >
        <div class="iconPay" :style="{'width':imgSize,'height':imgSize}" v-if="iconShow">
          <image mode="aspectFit" :src="icon" alt></image>
        </div>
        <!-- 企业要货开启了待审核 -->
        <template v-if="whetherOpenEnterprisesWantGoods == 1 && whetherOpenApproval == 1">
          <p class="success" :style="{ fontSize: payTitleSize, color: payTitleColor }">提交成功,等待审核</p>
        </template>
        <template v-else>
          <p class="success" :style="{ fontSize: payTitleSize, color: payTitleColor }">{{ payTitle }}</p>
          <p v-if="orderDetail.orderType == 5" class="tips">
            您可在【我的】-【我的礼包】中进行送礼
          </p>
          <p v-if="orderDetail.orderType == 5" class="tips">
            亦可以直接点下面按钮【立即送出】
          </p>
          <div v-if="orderDetail.orderType == 5"></div>
          <div v-else class="orderPrice" :style="subTitleStyle" v-html="subTitle">
            订单金额:
            <span v-if="routeQuery.multiMerchantsFlag || routeQuery.accountBalanceFlag"
              >¥{{ routeQuery.totalMoney }}</span
            >
            <span v-else>¥{{ totalMoney }}</span>
          </div>
        </template>
        <!-- 余额充值 -->
        <template v-if="routeQuery.accountBalanceFlag">
          <div class="balance">
            <p>获得{{ balanceRewardsPoints }}积分</p>
            <p class="balance-line">|</p>
            <p>获得{{ balanceRewardsPrice }}赠金</p>
          </div>
        </template>
        <div class="bottom flex">
          <template v-if="orderDetail.orderType == 5"> </template>
          <template v-else>
            <template v-if="routeQuery.isPayReplace != 1">
              <div
                v-if="routeQuery.multiMerchantsFlag"
                @click="toOrderList"
              >
                <van-button size="normal" :style="btnStyle">查看详情</van-button>
              </div>
              <div v-else @click="toOrderDetail" :style="btnStyle">
                <van-button size="normal" :style="btnStyle">查看详情</van-button>
              </div>
            </template>
            <div @click="toIndex" :style="btnStyle">
              <van-button size="normal" :style="btnStyle">返回首页</van-button>
            </div>
          </template>
        </div>
        <!-- 订阅消息 -->
        <div class="subscribe" v-if="miniIdlist.length>0">
          <p class="info">订阅消息及时获得物流信息及状态通知</p>
          <van-switch :checked="checkedSubscribe" @change="changeCheckedSubscribe" active-color="var(--minor-color)" size="20" />
        </div>
      </div>
    </div>
    <div class="line" v-if="orderDetail.orderType == 5"></div>
    <div class="steps_wrap" v-if="orderDetail.orderType == 5">
      <div class="steps_box">
        <div class="steps_item paystyle">
          <image
            src="http://mayi-newshop.oss-cn-shanghai.aliyuncs.com/public/png/ad3f8f16-c461-4844-a384-bf77a5e80263.png"
            alt
          ></image>
          付款
        </div>
        <div class="steps_dot">........</div>
      </div>
      <div class="steps_box">
        <div class="steps_item two">
          <image
            src="http://mayi-newshop.oss-cn-shanghai.aliyuncs.com/public/png/c925ae0f-1c56-43a2-8ca0-1ccdcf82c357.png"
            alt
          ></image>
          微信赠送
        </div>
        <div class="steps_dot">........</div>
      </div>
      <div class="steps_box">
        <div class="steps_item three">
          <image
            src="http://mayi-newshop.oss-cn-shanghai.aliyuncs.com/public/png/928f1247-ba6c-4760-a92a-c090200ae4f1.png"
            alt
          ></image>
          好友领取
        </div>
        <div class="steps_dot">........</div>
      </div>
      <div class="steps_box">
        <div class="steps_item four">
          <image
            src="http://mayi-newshop.oss-cn-shanghai.aliyuncs.com/public/png/4c004310-e19d-4475-834e-6391398b2646.png"
            alt
          ></image>
          送货上门
        </div>
      </div>
    </div>
    <div class="line" v-if="orderDetail.orderType == 5"></div>
    <div class="bottom_box" v-if="orderDetail.orderType == 5">
      <div class="bottom_item" @click="toShare">现在就分享</div>
      <div class="bottom_item bottom_last" @click="backTo">返回首页</div>
    </div>
  </div>
</template>

<script>
import shop from "@/api/shop";
const app = getApp();
const { log } = app;
export default {
  name: "pay-success-info",
  props: {
    render: {
      type: Boolean,
      default: false,
    },
    datas: {
      type: Object,
      default: () => {
        return {};
      },
    },
    orderDetail: {
      type: Object,
      default: () => {
        return {};
      },
    },
    routeQuery: {
      type: Object,
      default: () => {
        return {};
      },
    },
    totalMoney: {
      type: Number,
      default: 0,
    },
    giftsKey: {
      default: 0,
    },
    whetherOpenEnterprisesWantGoods: {
      default: 0,
    },
    whetherOpenApproval: {
      default: 0,
    },
    balanceRewardsPrice: {
      default: 0,
    },
    balanceRewardsPoints: {
      default: 0,
    },
  },
  data() {
    return {
      checkedSubscribe: false, // 订阅消息开关
      miniMsgList: [],
      miniIdlist: [],
    }
  },
  computed: {
    icon() {
      return this.datas.componentData["icon"]||'http://mayi-newshop.oss-cn-shanghai.aliyuncs.com/public/png/83a4a0bc-37fc-411e-bc5c-6dd9c1b4a921.png';
    },
    iconShow() {
      return this.datas.componentData["iconShow"] === undefined ? true : this.datas.componentData["iconShow"];
    },
    payTitle() {
      return this.datas.componentData["title"];
    },
    payTitleSize() {
      return (this.datas.componentData["titleSize"] || 16) + 'px';
    },
    payTitleColor() {
      return this.datas.componentData["titleColor"] || '#333';
    },
    subTitle() {
      let totalMoney = 0;
      if (this.routeQuery.multiMerchantsFlag) {
        totalMoney = this.routeQuery.totalMoney;
      } else if (this.routeQuery.accountBalanceFlag) {
        totalMoney = this.routeQuery.totalMoney;
      } else {
        totalMoney = this.totalMoney;
      }
      console.log(totalMoney,'--totalMoney')
      let subTitle = this.datas.componentData["subTitle"];
      let replaceList = [
        {
          key: "#金额#",
          val: totalMoney,
        },
        { key: "#订单号#", val: this.orderSn },
      ];
      replaceList.forEach((item, index) => {
        if (subTitle.includes(item.key)) {
          // <span style="font-size:18px;color:var(--main-color);padding:0 4px;">${item.val}</span>
          subTitle = subTitle.replace(new RegExp(item.key, "g"), item.val);
        }
      });
      return subTitle;
    },
    subTitleStyle() {
      return {
        color: this.datas.componentData["subTitleColor"] || '#333',
        fontSize: (this.datas.componentData['subTitleSize'] || 40) + 'px'
      }
    },
    paddingTop() {
      return this.datas.componentData["paddingTop"] + "px";
    },
    paddingBottom() {
      return this.datas.componentData["paddingBottom"] + "px";
    },
    imgSize() {
      return this.datas.componentData["imgSize"] + 'px';
    },
    orderSn() {
      return this.render?this.routeQuery.orderSn:'----------'
    },
    orderSns() {
      return this.render?this.routeQuery.orderSns:'----------'
    },
    btnStyle() {
      return {
        color: this.datas.componentData["btnColor"] || 'rgba(0, 0, 0, 0.4)',
        backgroundColor: this.datas.componentData["btnBackgroundColor"] || 'rgba(0, 0, 0, 0)',
        'border-radius': (this.datas.componentData["btnBorderRadius"] || 25) + 'px'
      }
    }
  },
  onLoad() {
    console.log("-----------------------info",this.$store.state.spokemanInfo)
    this.checkedSubscribe = false;
    this.getWxMiniSubscribeMessageConfig();
  },
  methods: {
    getWxMiniSubscribeMessageConfig() {
      shop.getWxMiniSubscribeMessageConfig().then(res => {
        if (res.data.code == 200) {
          const data = res.data.data;
          log.info("wx mini subscribe message data: ", data);
          console.log(data.miniMessageConfigResponseDTOList,'-------------data')            
          if(data.miniMessageConfigResponseDTOList && data.miniMessageConfigResponseDTOList.length>0) {
            this.miniMsgList = data.miniMessageConfigResponseDTOList.filter(item=>item.isOpen == 1);
            console.log(this.miniMsgList,'---this.miniMsgList')
            log.info(this.miniMsgList,'--this.miniMsgList')
            this.miniIdlist = this.miniMsgList.map(item=>item.priTmplId);
          }
        }
      });
    },
    toOrderDetail() {
      if (!this.render) return;
      console.log("查看详情");
      let h5Url = this.routeQuery.multiMerchantsFlag == 1 ? encodeURIComponent(`/order/orderList`) : encodeURIComponent(`/order/orderDetail?orderSn=${this.orderSn}&isReplace=true`);
      if (this.routeQuery.accountBalanceFlag) {
        h5Url = encodeURIComponent(`/accountBalance/list`);
      }
      let url = `/pages/index/main?from=home&backpath=${h5Url}`
      wx.redirectTo({
        url,
      });
    },
    changeCheckedSubscribe(val) {
      console.log(val,'--------------')
      this.checkedSubscribe = val.mp.detail;
      if(this.checkedSubscribe) {
        this.toDingyue();
      }
    },
    toDingyue() {
      let _this = this;
      if(!(this.miniIdlist && this.miniIdlist.length>0)) {
        return;
      }
      wx.requestSubscribeMessage({
        tmplIds: _this.miniIdlist,
        success(res) {
          log.info("用户点击订阅",res)
          let showToast = false;
          console.log(res, "-------------res");
          _this.miniIdlist.forEach(item=>{
            if(res[item] == "accept"){
              showToast = true;
            }
          })
          if(showToast) {
            wx.showToast({title:"订阅成功",icon:"none"});
          }
          _this.miniMsgList.forEach(item=>{
            let query = {
              openId: wx.getStorageSync("openid"),
              eventName: item.title,
              eventType: item.definitionType,
              sendTemplateId: item.priTmplId,
              extraParams: _this.routeQuery.orderSn,
            }
            shop.addWxMiniSubscribeMessageRecord(query).then()
          })
        },
      });
    },
    toOrderList() {
      let url = `/pages/index/main?from=home&backpath=${encodeURIComponent(`/order/orderList`)}`
      wx.redirectTo({
        url,
      });
    },
    toIndex() {
      if (!this.render) return;
      wx.reLaunch({
        url: "/pages/home/main",
      });
    },
    backTo() {
      if (!this.render) return;
      wx.reLaunch({
        url: "/pages/home/main",
      });
    },

    //云送礼去分享
    toShare() {
      if (!this.render) return;
      console.log(this.giftsKey, "this.giftsKey");

      if (this.giftsKey == 1) {
        let spokesmanRelId;
        let spokesmanGroupId;
        let spokesmanShopId;
        let spokemanInfo = this.$store.state.spokemanInfo;
        let userInfo = this.$store.state.userInfo;

        console.log(spokemanInfo,'-------spokemanInfo');
        console.log(this.$store.state.userInfo);
        if (spokemanInfo) {
          if (spokemanInfo.hasInvitationStatus == 1) {
            spokesmanRelId = spokemanInfo.spokesmanId;
            spokesmanGroupId = spokemanInfo.groupId;
            spokesmanShopId = spokemanInfo.shopId;
          }
        }

        let nickname = "";
        let headerImg =
          "http://mayi-newshop.oss-cn-shanghai.aliyuncs.com/public/jpg/02c0c235-daae-412d-bced-5fb315ad570e.jpg";
        if (userInfo) {
          if (userInfo.nickname) {
            nickname = userInfo.nickname;
          } else if (userInfo.wxNickname) {
            nickname = wxNickname;
          }

          if (userInfo.headPortraitUrl) {
            headerImg = userInfo.headPortraitUrl;
          }
        }

        console.log("nickname", nickname);
        console.log("headerImg", headerImg);
        app.$themeToLink({
          type: 1,
          link: `/goods/posters?type=cloudGift&href=${process.env.BASE_URL}/activty/receivingGift?mixid=${this.$store.state.mixid}&orderId=${this.orderDetail.orderId}&spokesmanGroupId=${spokesmanGroupId}&hasInvitationStatus=${spokemanInfo.hasInvitationStatus}&spokesmanShopId=${spokesmanShopId}&spokesmanRelId=${spokesmanRelId}&nickname=${nickname}&headerImg=${headerImg}&ordernumber=${this.routeQuery.orderSn}&orgName=${this.orgName}&goodsImg=${this.orderDetail.noShipmentOrderItems[0].specificationPictureUrl}`,
        })
        // this.$router.push({
        //   path: "/goods/posters",
        //   query: {
        //     type: "cloudGift",
        //     href: `${window.location.protocol}//${window.location.host}/activty/receivingGift?mixid=${this.routeQuery.mixid}&orderId=${this.orderId}`,
        //     spokesmanGroupId: spokesmanGroupId,
        //     hasInvitationStatus: spokemanInfo.hasInvitationStatus,
        //     spokesmanShopId: spokesmanShopId,
        //     spokesmanRelId: spokesmanRelId,
        //     nickname: nickname,
        //     headerImg: headerImg,
        //     ordernumber: this.routeQuery.orderSn,
        //     orderId: this.orderId,
        //     orgName: this.orgName,
        //     goodsImg:
        //       this.orderDetail.noShipmentOrderItems[0].specificationPictureUrl,
        //   },
        // });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.paySuccess_content {
  width: 100%;
  margin-top: 1px;
  overflow: hidden;
  .paySuccess_detail {
    padding: 0 30px;
    .iconPay {
      margin: 0 auto;
      border-radius: 50%;
      text-align: center;
      .iconfont {
        color: #fff;
        font-size: 50px;
        line-height: 80px;
      }
      image {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
    }
    .success {
      margin: 10px 0;
      text-align: center;
      line-height: 20px;
      font-size: 18px;
      font-family: MicrosoftYaHei;
      font-weight: 600;
      color: #191919;
    }
    .tips {
      text-align: center;
      color: #191919;
      font-size: 13px;
    }
    .orderPrice {
      text-align: center;
      margin: 10px;
      font-weight: 500;
    }
    .price {
      width: 116px;
      height: 14px;
      font-size: 13px;
      font-family: MicrosoftYaHei;
      font-weight: 400;
      color: rgba(25, 25, 25, 1);
      margin-bottom: 12px;
      margin: 17px auto;
      .dcolor {
        font-size: 15px;
        color: var(--minor-color);
      }
    }
    .balance{
      display: flex;
      align-items: center;
      justify-content: center;
      color: #666;
      font-size: 14px;
      &-line{
        padding: 0 10px;
      }
    }
    .bottom {
      margin-top: 10px;
      justify-content: center;
      gap: 20px;
      /deep/.van-button {
        height: auto;
        line-height: normal;
        padding: 6px 24px;
        border: 1px solid;
      }
      .toShare {
        background: var(--main-color);
        border-color: var(--main-color);
        color: #fff;
      }
    }
    .subscribe{
      padding: 8px 18px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      background: var(--main-color);
      margin-top: 20px;
      border-radius: 6px;
      p.info{
        color: #fff;
        font-size: 13px;
      }
    }
  }
  
}
.steps_wrap {
  display: flex;
  background: #fff;
  margin-top: 10px;
  justify-content: space-evenly;
  padding-top: 16px;
  padding-bottom: 10px;
  .steps_box {
    display: flex;
    .steps_item {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: flex-end;
      image {
        margin-bottom: 5px;
      }
    }
    .paystyle {
      image {
        width: 27.5px;
        height: 23.5px;
      }
      color: #e94f4f;
    }
    .two {
      image {
        width: 32px;
        height: 26px;
      }
    }
    .three {
      image {
        width: 26px;
        height: 26px;
      }
    }
    .four {
      image {
        width: 35px;
        height: 21.5px;
      }
    }
    .steps_dot {
      color: #999999;
      position: relative;
      top: 3px;
      left: 15px;
    }
  }
}
.line {
  height: 10px;
  background: #ededed;
}
.bottom_box {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 30px;
  .bottom_item {
    height: 40px;
    background: #e94f4f;
    width: 70%;
    text-align: center;
    line-height: 40px;
    border-radius: 20px;
    font-size: 16px;
    color: #fff;
  }
  .bottom_last {
    margin-top: 10px;
    background: #e3e3e3;
    color: #333333;
  }
}
</style>