<template>
  <div class="goods-bottom">
    <template v-if="status == 1">
      <cr-button
        class="mutiplie"
        plain
        type="primary"
        shape="circle"
        :disabled="disabled"
        @click="createGroup"
        >自己做团长</cr-button
      >
      <cr-button
        class="mutiplie"
        shape="circle"
        :disabled="disabled"
        type="primary"
        block
        @click="joinGroup"
      >
        立即参团
      </cr-button>
    </template>
    <template v-if="status == 2">
      <cr-button shape="circle" :disabled="disabled" type="primary" block @click="createGroup">
        立即开团
      </cr-button>
    </template>
  </div>
</template>
<script>
import { isApp, isWxMp } from '@/service/validation.service';
import Bridge from '@qg/js-bridge';
import MpBridge from '@/service/mp';
import localStorage from '@/service/localStorage.service';
export default {
  name: 'BottomNav',
  props: {
    type: {
      type: String,
      default: 'shopCar' // shoppingCar 购物车；settlement 结算;submitOrder 确认订单
    },
    shopBtnName: {
      type: String,
      default: '立即购买' // shoppingCar 购物车；settlement 结算;submitOrder 确认订单
    },
    info: {
      type: Object,
      default() {
        return {};
      }
    },
    disabled: {
      type: Boolean,
      default: false
    },
    goodsId: {
      type: String,
      default: ''
    },
    status: {
      type: Number,
      default: 2
    }
  },
  data() {
    return {
      nativeBridge: null
    };
  },
  methods: {
    getToken() {
      const that = this;
      window.xyqbNativeEvent = function(res) {
        const json = typeof res === 'string' ? JSON.parse(res) : res;
        if (json.event === 'getTokenSuccess') {
          const appData = json.data || {};
          if (appData && appData.token) {
            localStorage.get('vccToken', appData.token);
            that.$nextTick(() => {
              window.location.reload();
            });
          }
        }
      };
      this.nativeBridge.getToken();
    },
    checkLogin() {
      const vccToken = localStorage.get('vccToken');
      const needLogin = vccToken ? false : true;
      if (needLogin) {
        if (isApp) this.nativeBridge = new Bridge();
        else if (isWxMp) this.nativeBridge = new MpBridge();
        this.$dialog({
          message: '参与拼团活动需要您先进行登录哦~',
          title: '',
          showCancelButton: false,
          confirmButtonText: '登录',
          onConfirm: () => {
            if (isWxMp) {
              this.nativeBridge.openNewUrl({
                newUrl: `/pages/user/login`
              });
            } else if (isApp) {
              // native调用getToken
              this.getToken();
            } else {
              this.$dialog({
                message: '请在App或小程序中参与活动~',
                title: '',
                showCancelButton: false,
                confirmButtonText: '我知道了'
              });
            }
          }
        });
      }
      return needLogin;
    },
    joinGroup() {
      // 参团
      if (this.checkLogin()) {
        // todo
        return;
      }
      this.$emit('buy');
    },
    createGroup() {
      // 开团
      if (this.checkLogin()) {
        // todo
        return;
      }
      this.$emit('buy');
    }
  }
};
</script>
<style lang="less" scoped>
.goods-bottom {
  position: fixed;
  left: 0;
  bottom: 0;
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  width: 100%;
  background: @white;
  border-top: 2px solid #f7f7f7;
  padding: 10px 12px;
  box-sizing: border-box;
  z-index: 2;
  button {
    flex: 1;
    &.mutiplie {
      margin: 0 @padding-xs;
    }
  }
}
</style>
