<template>
  <view class="submit-bar custom-class" :style="{ '--primary-color': primaryColor }">
    <slot name="top" />

    <view class="submit-bar__tip">
      <u-icon v-if="tipIcon" size="12" :name="tipIcon" custom-class="submit-bar__tip-icon" />
      <view v-if="tip" class="submit-bar__tip-text">
        {{ tip }}
      </view>
      <slot name="tip" />
    </view>

    <view class="bar-class submit-bar__bar">
      <view style="flex: 3">
        <slot />
      </view>
      <view style="flex: 3.5">
        <view v-if="price" class="submit-bar__text">
          <text>{{ label || '合计：' }}</text>
          <text class="submit-bar__price price-class">
            <text class="submit-bar__currency">{{ currency }} </text>
            <text class="submit-bar__price-integer">{{ integerStr }}</text
            ><text>{{ decimalStr }}</text>
          </text>
          <text class="submit-bar__suffix-label">{{ suffixLabel }}</text>
        </view>
      </view>
      <view style="flex: 3.5">
        <u-button
          round
          :type="buttonType"
          :loading="loading"
          :disabled="disabled"
          class="submit-bar__button"
          custom-class="button-class"
          :custom-style="{ width: '100%', backgroundColor: primaryColor, fontSize: '13px' }"
          @click="onSubmit"
        >
          {{ loading ? '' : buttonText }}
        </u-button>
      </view>
    </view>

    <view v-if="safeAreaInsetBottom" class="submit-bar__safe"></view>
  </view>
</template>

<script>
import { mapState } from 'vuex';

export default {
  name: 'SubmitBar',
  props: {
    tip: {
      type: String,
      default: null,
    },
    tipIcon: String,
    type: Number,
    price: {
      type: Number,
      default: 0,
    },
    label: String,
    loading: Boolean,
    disabled: Boolean,
    buttonText: String,
    currency: {
      type: String,
      default: '¥',
    },
    buttonType: {
      type: String,
      default: 'primary',
    },
    decimalLength: {
      type: Number,
      default: 2,
    },
    suffixLabel: String,
    safeAreaInsetBottom: {
      type: Boolean,
      default: true,
    },
  },
  computed: {
    ...mapState({
      primaryColor: state => (state.primaryColor ? state.primaryColor : '#ee0a24'),
    }),
  },
  data() {
    return {
      integerStr: null,
      decimalStr: null,
    };
  },
  watch: {
    price: {
      immediate: true,
      handler: function (v) {
        this.updatePrice(v);
      },
    },
  },
  methods: {
    updatePrice(price) {
      const { decimalLength } = this;
      const priceStrArr = typeof price === 'number' && price.toFixed(decimalLength).split('.');
      this.integerStr = priceStrArr && priceStrArr[0];
      this.decimalStr = decimalLength && priceStrArr ? `.${priceStrArr[1]}` : '';
    },
    onSubmit(event) {
      this.$emit('submit', event.detail);
    },
  },
};
</script>

<style scoped lang="scss">
$primary-color: var(--primary-color, #ee0a24);

.submit-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  -webkit-user-select: none;
  user-select: none;
  z-index: 100;
  background-color: #fff;
}
.submit-bar__tip {
  padding: 10px;
  color: #f56723;
  font-size: 12px;
  line-height: 1.5;
  background-color: #fff7cc;
}
.submit-bar__tip:empty {
  display: none;
}
.submit-bar__tip-icon {
  width: 12px;
  height: 12px;
  margin-right: 4px;
  vertical-align: middle;
  font-size: 12px;
  min-width: 18px;
}
.submit-bar__tip-text {
  display: inline;
  vertical-align: middle;
}
.submit-bar__bar {
  display: -webkit-flex;
  display: flex;
  flex-direction: row;
  -webkit-align-items: center;
  align-items: center;
  -webkit-justify-content: flex-end;
  justify-content: flex-end;
  padding: 0 16px;
  height: 50px;
  font-size: 14px;
  background-color: #fff;
}
.submit-bar__safe {
  height: constant(safe-area-inset-bottom);
  height: env(safe-area-inset-bottom);
}
.submit-bar__text {
  -webkit-flex: 1;
  flex: 1;
  text-align: right;
  color: #323233;
  padding-right: 12px;
}
.submit-bar__price,
.submit-bar__text {
  font-weight: 500;
}
.submit-bar__price {
  color: $primary-color;
  font-size: 12px;
}
.submit-bar__price-integer {
  font-size: 20px;
  font-family: Avenir-Heavy, PingFang SC, Helvetica Neue, Arial, sans-serif;
}
.submit-bar__currency {
  font-size: 12px;
}
.submit-bar__suffix-label {
  margin-left: 5px;
}
.submit-bar__button {
  width: 110px;
  font-weight: 500;
  --button-default-height: 40px !important;
  --button-line-height: 40px !important;
}
</style>
