<template>
  <div class="gfcard" :class="{ 'gfcard-disabled': isDisabled }">
    <div v-if="cardItem" class="gfcard-header">
      <div class="gfcard-header-top gfcard-flex">
        <span class="gfcard-header-title">{{
          cardItem.createModeDesc || cardItem.cardName || ''
        }}</span>
        <!--         移除使用记录外显功能-->
        <!--        <template v-if="showRecord">-->
        <!--          <span-->
        <!--            v-if="cardItem.giftStatus != 0"-->
        <!--            class="gfcard-header-record"-->
        <!--            @click.stop="bindRecordClick"-->
        <!--            >使用记录</span-->
        <!--          >-->
        <!--          <span v-else>确认收货后即可激活</span>-->
        <!--        </template>-->
      </div>
      <div class="gfcard-header-bottom gfcard-flex">
        <span class="gfcard-header-amount">
          面值：{{ (+cardItem.denomination || 0).toFixed(2) }}元
        </span>
        <span class="gfcard-header-time">
          <template v-if="$config.qinghai">
            {{ cardItem.validTimeStart | format }} - {{ cardItem.validTimeEnd | format }}
          </template>
          <template v-else>
            <template v-if="cardItem.validDateStart && cardItem.validDateEnd">
              {{ cardItem.validDateStart }} - {{ cardItem.validDateEnd }}
            </template>
          </template>
        </span>
      </div>
    </div>
    <div class="gfcard-bottom">
      <div class="gfcard-bottom-money">
        <span class="gfcard-bottom-balance">余额</span>
        <span class="gfcard-bottom-num">
          ¥<b class="gfcard-bottom-bold">{{ formatPrice[0] }}</b>
          <b class="gfcard-bottom-zero">.{{ formatPrice[1] }}</b>
        </span>
      </div>
      <p class="gfcard-bottom-describe">{{ cardItem.cardDesc || '' }}</p>
      <div class="gfcard-checkbox-click" />
      <!-- 1-未到期2-使用中3-已失效4-已使用 -->
      <div v-if="cardItem.status !== 2" :class="['gfcard-bg', `bg-${cardItem.status}`]" />
      <cr-checkbox v-if="showCheckbox" :value="selectChecked" shape="round" class="gfcard-check" />
    </div>
  </div>
</template>

<script>
const BINDRECORD_CLICK = 'bind-record-click';
import { parseTime } from '@/service/utils.service';
export default {
  filters: {
    format(time) {
      return parseTime(time, '{y}.{m}.{d}');
    }
  },
  props: {
    cardItem: {
      type: Object,
      default: () => {}
    },
    checked: undefined,
    // 是否显示禁止使用
    beOverdue: {
      type: Boolean,
      default: false
    },
    // 是否显示使用记录文案
    showRecord: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      selectChecked: false
    };
  },
  computed: {
    isDisabled() {
      return this.beOverdue && this.cardItem.status !== 1;
    },
    formatPrice() {
      const balance = this.cardItem.balance;
      let price = !isNaN(balance) ? +balance : 0;
      return price.toFixed(2).split('.');
    },
    showCheckbox() {
      return this.checked !== undefined;
    }
  },
  watch: {
    checked: {
      handler(val) {
        this.selectChecked = val;
      },
      immediate: true
    }
  },
  methods: {
    bindRecordClick() {
      this.$emit(BINDRECORD_CLICK, this.cardItem);
    }
  }
};
</script>
<style scoped lang="less">
.gfcard {
  border-radius: 8px;
  margin-bottom: 8px;
  overflow: hidden;
  position: relative;
  margin-bottom: 8px;
  .text-size(12);
  &-check {
    position: absolute;
    right: 0;
  }
  &-checkbox-click {
    position: absolute;
    right: 0;
    height: 20px;
    width: 40px;
    z-index: 3;
  }

  &-flex {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  &-header {
    color: @white;
    height: 56px;
    padding: 8px 12px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    background: url(~@/assets/images/gift/gift-header.png) no-repeat;
    background-size: 101%;
    border-radius: 8px 8px 0 0;
    box-sizing: border-box;
    &-title {
      .text-size(14);
    }
    &-record {
      padding-left: 16px;
      background: url(~@/assets/images/gift/gift-header-record.png) no-repeat left center;
      background-size: 12px 14px;
    }
    &-time {
      opacity: 0.7;
    }
  }
  &-bottom {
    overflow: hidden;
    height: 90px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    text-align: center;
    background: url(~@/assets/images/gift/gift-bottom.png) no-repeat;
    background-size: 102%;
    border: 1px solid #ebebeb;
    border-top: 0;
    box-sizing: border-box;
    border-radius: 0 0 8px 8px;
    position: relative;
    &-money {
      .text-size(14);
      color: @red-light;
    }
    &-num {
      font-weight: bold;
      .text-size(16);
    }
    &-bold {
      font-size: 30px;
    }
    &-zero {
      font-size: 20px;
    }
    &-describe {
      .text-size(12);
      color: @gray-4;
      margin-top: 5px;
    }
  }
}
.gfcard-bg {
  position: absolute;
  width: 67px;
  height: 77px;
  right: 0;
  top: -20px;
  background-repeat: no-repeat;
  background-size: 100%;
  &.bg-1 {
    background-image: url(~@/assets/images/gift/gift-use-v2.png);
  }
  &.bg-3 {
    background-image: url(~@/assets/images/gift/gift-overdue-v2.png);
  }
  &.bg-4 {
    background-image: url(~@/assets/images/gift/gift-used-v2.png);
  }
}
.gfcard-disabled {
  .gfcard-header {
    background-image: url(~@/assets/images/gift/gift-header-dis.png);
  }
  .gfcard-bottom {
    background-image: url(~@/assets/images/gift/gift-bottom-dis.png);
    position: relative;
    overflow: hidden;
    &-money {
      color: @gray-5;
    }
  }
}
::v-deep .cr-icon {
  line-height: inherit !important;
}
</style>
