<template>
  <div v-if="info" class="p-benefits bg-gray">
    <h3 class="ptc-title">Member benefits</h3>
    <div class="ptc-block">
      <div class="ptc-inner">
        <div class="l1">
          <i class="icon-lite"></i
          ><strong class="primary">{{ info.product_name }}</strong
          >{{ info.start_time }} to {{ info.end_time }}
        </div>
        <p class="l2">{{ info.phone_info }}</p>
        <p class="l3">{{ info.phone_imei }}</p>
        <p class="l4">
          <button class="ptc-button ptc-button--stroke">Renewal</button>
        </p>
        <p class="l5">
          <router-link :to="`/renewal?id=${$route.params.id}`">
            Renewal management >
          </router-link>
        </p>
      </div>
    </div>
    <div class="ptc-block">
      <div class="ptc-inner">
        <div
          v-for="(item, index) of info.rights"
          :key="index"
          class="benefit"
          :style="{ backgroundImage: `url(${item.icon})` }"
        >
          <p class="benefit-name">{{ item.title }}</p>
          <p class="benefit-desc">
            {{ item.type == 1 ? `Available ${item.left_times}` : item.remark }}
          </p>
          <p v-if="item.type == 1" class="benefit-sup">
            <template v-if="item.available">
              Member price：<span class="highlight">${{ item.price }}</span>
            </template>
            <template v-else
              >Available after {{ item.next_available_at }}</template
            >
          </p>
          <button
            v-if="item.available && getButtonText(item)"
            class="ptc-button"
            @click="onClickButton(item)"
          >
            {{ getButtonText(item) }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { defineComponent } from 'vue'
import { state, getCurrentOrder } from '@/store'

export default defineComponent({
  name: 'OrderDetail',
  async beforeRouteEnter(to) {
    return await getCurrentOrder(to.params.id)
  },
  async beforeRouteUpdate(to) {
    return await getCurrentOrder(to.params.id)
  },
  computed: {
    info() {
      return state.currentOrder
    },
  },
  methods: {
    getButtonText(item) {
      switch (+item.type) {
        case 1:
          return +item.need_apply ? 'Apply' : 'Nearby shops'
        case 2:
          return item.button_type === 'contact_us'
            ? 'Contact us'
            : item.button_type && item.button_name
        case 4:
          return +item.receive_method === 2 ? 'Nearby shops' : 'Send to me'
        default:
          return ''
      }
    },
    onClickButton(item) {
      switch (+item.type) {
        case 1:
          return +item.need_apply
            ? this.$router.push(`/repaire/appointment?right_id=${item.id}`)
            : 'Nearby shops'
        case 2:
          return item.button_type === 'contact_us'
            ? 'Contact us'
            : item.button_type && (location.href = item.button_link)
        case 4:
          return +item.receive_method === 2
            ? 'Nearby shops'
            : this.$router.push({
                path: '/mailing',
                query: {
                  right_id: item.id,
                  right_name: item.title,
                },
              })
        default:
          return ''
      }
    },
  },
})
</script>

<style lang="scss">
.p-benefits {
  .l1 {
    display: flex;
    font-size: 28px;
    color: #90a0c0;

    strong {
      margin: 0 20px 0 10px;
    }
  }
  .l2 {
    margin-top: 48px;
    line-height: 56px;
    font-size: 40px;
    font-weight: bold;
    color: #333;
    text-align: center;
  }
  .l3 {
    margin-top: 8px;
    line-height: 40px;
    font-size: 28px;
    color: #999;
    text-align: center;
  }
  .l4 {
    margin: 64px 0 24px;
    font-size: 0;
    text-align: center;
    button {
      margin: auto;
      width: 412px;
      height: 68px;
      font-size: 32px;
    }
  }
  .l5 {
    line-height: 44px;
    font-weight: bold;
    font-size: 32px;
    color: $primary-color;
    text-align: center;
  }

  .benefit {
    padding-left: 100px;
    padding-bottom: 46px;
    background-repeat: no-repeat;
    background-position: 0 0;
    background-size: 64px 64px;
    // &-a {
    //   background-image: url(@img/benefit0.png);
    // }
    // &-b {
    //   background-image: url(@img/benefit1.png);
    // }
    // &-c {
    //   background-image: url(@img/benefit2.png);
    // }
    // &-d {
    //   background-image: url(@img/benefit3.png);
    // }
    // &-e {
    //   background-image: url(@img/benefit4.png);
    // }

    + .benefit {
      padding-top: 48px;
      background-position: 0 48px;
      @include thin-border(top);
    }

    &-name {
      line-height: 56px;
      font-size: 40px;
      font-weight: bold;
      color: #1a1a1a;
    }
    &-desc {
      margin-top: 24px;
      line-height: 44px;
      font-size: 32px;
      color: #1a1a1a;
    }
    &-sup {
      margin-top: 8px;
      line-height: 44px;
      font-size: 32px;
      color: #999;
    }
    .ptc-button {
      margin-top: 24px;
      padding: 0 24px;
      width: auto;
      min-width: 248px;
      height: 68px;
      font-size: 32px;
    }
  }
}
</style>
