<template>
  <el-main>
    <x2-progress-track v-if="(trackerConfig.term == 'x2_cash' || trackerConfig.term == 'x2_cheque' || trackerConfig.term == 'x2_ba')" v-model="trackerConfig" />
    <progress-track v-else v-model="trackerConfig" />
    <h1>Bankin Slip Approved</h1>
    <h2>Transfer In Progress</h2>
    <div class="goBackButton">
      <el-button type="primary" @click="$router.go(-1)">Go Back</el-button>
    </div>
    <!-- order details -->
    <div class="card">
    <div class="card-header">Order Details</div>
    <div class="card-body">
        <div class="row">
          <div class="col">
            <table class="table table-striped">
              <tbody>
                <tr>
                  <td>Ref No:</td>
                  <td>{{ booking_details.id }}</td>
                </tr>
                <tr>
                  <td>Date :</td>
                  <td>{{ booking_details.created_at }}</td> 
                </tr>
                <tr>
                  <td>Customer Marking :</td>
                  <td>{{ booking_details.marking }}</td> 
                </tr>
                <tr>
                  <td>Term: </td>
                  <td>{{ booking_details.term }}</td>
                </tr>
                <tr>
                  <td>Beneficiary Account : </td>
                  <td>{{ booking_details.account_name }}</td>
                </tr>
              </tbody>
            </table>
          </div>
          <div class="col">
            <table class="table table-striped">
              <tbody>
                <tr>
                  <td>CNY/RMB :</td>
                  <td>CNY {{ booking_details.amount }}</td>
                </tr>
                <tr>
                  <td>+ Service Charge :</td>
                  <td>CNY {{ booking_details.service_charge }}</td> 
                </tr>
                <tr>
                  <td>RATE :</td>
                  <td>{{ booking_details.rate }}</td> 
                </tr>
                <tr>
                  <td></td>
                  <td><b>MYR {{ booking_details.bia }}</b></td>
                </tr>
                <tr>
                  <td>Beneficiary Account Number : </td>
                  <td>{{ booking_details.account_num }}</td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
    <div class="card">
      <div class="card-header">Order Bankin Slip</div>
      <div class="row">
        <div class="card-body">
          <a target="_blank" :href="booking_details.user_bankslip_path">
            <a target="_blank" :href="getURL(booking_details.user_bankslip_path)">
              <img v-if="this.bankslip_is_image" v-bind:src="booking_details.user_bankslip_path" class="image">
              <p v-else>Download</p>
            </a>
          </a>    
        </div>
      </div>
    </div>
  </el-main>
</template>
<style>
  h1 {
    margin-top:5%;
    font-size:3vw;
    text-align:center;
  }
  h2 {
    font-size:2vw;
    text-align:center;
    margin-bottom:3%;
  }
  .goBackButton {
    text-align:center;
    margin-bottom:3%;
  }
  .uploadedImage {
      border: 1px solid #ddd;
      border-radius: 4px;
      margin-left: auto;
      margin-right: auto;
      width: 150px;
      display: block;
  }
  .uploadedImage:hover {
    box-shadow: 0 0 2px 1px rgba(0, 140, 186, 0.5);
  }
  .card {
    margin-bottom: 3%;
  }
  @media screen and (max-width: 767px) {
		.table {
			font-size: 3.5vmin;
		}
		h1 {
      font-size: 1em;
    }
    h2 {
      font-size: 0.8em;
    }
	}
</style>
<script>
import ProgressTrack from '~/components/ProgressTrack'
import X2ProgressTrack from '~/components/X2ProgressTrack'
import axios from 'axios'

export default {
  components: {
    'progress-track': ProgressTrack,
    'x2-progress-track': X2ProgressTrack
  },
  data () {
    return {
      trackerConfig :{
          status: 4,
          term: null
      },
      loading_btn: false,
      bankslip_is_image: false,
      booking_details:{
          id: null,
          amount: null,
          bia: null,
          user_bankslip_path: null,
        }
    }
  },
  beforeCreate () {
    const loading = this.$loading({
      lock: true,
      text: 'Please wait..',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)'
    })

    axios
      .get('/api/booking/' + this.$route.params.id)
      .then((response) => {
        this.booking_details = response.data
        this.trackerConfig.term = response.data.term
        
        // check if path is image
        var ext = this.booking_details.user_bankslip_path.split('.').pop(); 
        if (ext === 'jpg' || ext === 'jpeg' || ext === 'bmp' || ext === 'png'){
          this.bankslip_is_image = true
        }

        loading.close()

      }).catch((error) => {
        console.log(error)
        loading.close()
        this.$router.push({
          name: 'notfound'
        })
      })
    


  },
  methods: {
    getURL(path){
      var getUrl = window.location;
      var baseUrl = getUrl.protocol + "//" + getUrl.host;
      return baseUrl + path;
    }
  }
}
</script>
