<template>
  <section id="intro">
    <div class="container">
      <div class="row">
        <div class="col-md-6">
          <div class="intro-content box text-center">
            <h2>Payment Option</h2>
            <select name="" id="" v-model="selectedOption" class="uniSelect form-control">
              <option value="CC">Credit/Debit Card</option>
              <option value="EFT">EFT</option>
            </select>
            <button class="btn-solid-blue mt-5" @click="pushToApi()">CONTINUE</button>
          </div>
        </div>
      </div>
    </div>

    <carousel
      :nav="false"
      :dots="false"
      :items="1"
      :autoplay="true"
      :loop="true"
      :autoHeight="true"
      id="intro-carousel"
      style="margin-top:-50px;"
      :responsive="{ 0: { items: 1, nav: false }, 600: { items: 1, nav: false } }"
    >
      <img class="item" src="/img/intro-carousel/home-1.jpg" alt="" />
      <img class="item" src="/img/intro-carousel/16.jpg" alt="" />
      <img class="item" src="/img/intro-carousel/comm-1.jpg" alt="" />
      <img class="item" src="/img/intro-carousel/comm-4.jpg" alt="" />
      <img class="item" src="/img/intro-carousel/3.jpg" alt="" />
      <img class="item" src="/img/intro-carousel/home-5.jpg" alt="" />
    </carousel>
  </section>
</template>

<script>
/* eslint-disable */
import { mapState, mapActions, mapGetters } from "vuex";
import carousel from "vue-owl-carousel";
import Log from "../../assets/Log";
import md5 from "js-md5";
export default {
  components: {
    carousel
  },
  props: {
    week: {},
    indiv: {}
  },
  data() {
    return {
      boolLoaded: false,
      selectedOption: "CC"
    };
  },
  computed: {
    ...mapGetters("fees", ["getListingFee"])
  },
  created() {
    console.log(this.week);
  },
  methods: {
    ...mapActions("payment", ["addPayment"]),
    ...mapActions("myWeeks", ["editSave"]),
    pushToApi() {
      if (this.selectedOption == "CC") {
        //this.paygateRedirect();
        this.generatePayment(this.week.id);
      } else {
        this.week.statusId = 1033; // change this value to link with status
        if (this.week.owner.telephone === "") {
          delete this.week.owner.telephone;
        }
        console.log(JSON.stringify(this.week));
        this.editSave(this.week).then(() => {
          this.$router.push({ name: "EFTPage", params: { week: this.week } });
        });
      }
    },
    generatePayment(id) {
      var myData = [];
      var date =
        "From: " +
        this.week.arrivalDate.substring(0, 10) +
        " To " +
        this.week.departureDate.substring(0, 10);
      // Merchant details
      myData["merchant_id"] = "10000100";
      myData["merchant_key"] = "46f0cd694581a";
      myData["return_url"] = "https://www.univateproperties.co.za/#/payments/success";
      myData["cancel_url"] = "https://www.univateproperties.co.za/#/";
      myData["notify_url"] = "http://www.yourdomain.co.za/notify_url";
      // Buyer details
      myData["name_first"] = this.indiv.name;
      myData["name_last"] = this.indiv.surname;
      myData["email_address"] = this.indiv.emailAddress;
      // Transaction details
      //myData["m_payment_id"] = "1234";
      myData["amount"] = this.getListingFee.amount.toString();
      myData["item_name"] = "Uni-Vate Week Sale. Reference: #" + id.toString();
      myData["item_description"] = this.week.resortName;
      myData["custom_str1"] = date;

      myData["signature"] = this.generateSignature(myData);
      //console.log(paymentObj);
      //axios.post("https://sandbox.payfast.co.za​/eng/process", paymentObj);
      //console.log(myData);
      var paymentObj = Object.assign({}, myData);
      console.log("Twas here");
      console.log(paymentObj);
      this.$router.push({
        name: "PayFast",
        params: {
          paymentObj: paymentObj
        }
      });
    },
    generateSignature(data, passPhrase = null) {
      let pfOutput = "";
      for (let key in data) {
        if (data.hasOwnProperty(key)) {
          if (data[key] !== "") {
            pfOutput += `${key}=${encodeURIComponent(data[key].trim()).replace(/%20/g, "+")}&`;
          }
        }
      }

      let getString = pfOutput.slice(0, -1);
      if (passPhrase !== null) {
        getString += `&passphrase=${encodeURIComponent(passPhrase.trim()).replace(/%20/g, "+")}`;
      }
      var hash = md5.create();
      hash.update(getString);
      console.log(hash.hex());
      console.log(getString);
      return hash.toString();
    }
    // paygateRedirect() {
    //   var amount = this.getListingFee.amount;
    //   var paymentObj = {
    //     timeshareWeekId: this.week.id, // this.sellItem.Id,
    //     propertyId: 0,
    //     creatydById: Log.getUser().id, //Log.getUser().id,
    //     amount: amount,
    //     paymentStatus: "",
    //     paymentToken: ""
    //   };

    //   this.addPayment(paymentObj).then(res => {
    //     this.$router.push({
    //       name: "PaymentGateway",
    //       params: {
    //         paymentReqId: res.PAY_REQUEST_ID,
    //         checksum: res.CHECKSUM
    //       }
    //     });
    //   });
    // }
  }
};
</script>

<style lang="scss" scoped></style>
