<template>
  <div class="hub hub-detail" id="detailToPrint" style="color: #000000!important; background-color: #FFFFFF!important;">
    <nav>
      <ul class="top-links">
        <li class="list-item">
          <a @click.prevent="$emit('backTolist')" class="top-link backToListeButton button primary" style="cursor: pointer;" :style="[btnBgColor, btnFgColor]">
            <span class="padding10">{{ $t('front_extern.detail_zurueck') }}</span>
          </a>
        </li>
        <li v-if="false" class="list-item leftMarg10"><a href="" class="top-link advancedSearchButton button primary" style="cursor: pointer;" :style="[btnBgColor, btnFgColor]"><span class="padding10"> Suche anpassen</span></a></li>
        <li v-if="getNext" @click.prevent="$emit('gotoNext', getNext)" class="list-item next">
          <a href="" class="top-link nextInserateButton button primary" data-id="2693330" style="cursor: pointer;" :style="[btnBgColor, btnFgColor]">
            <span class="padding10">{{ $t('front_extern.detail_naechstes') }} &gt;</span>
          </a>
        </li>
        <li v-if="getPrev" @click.prevent="$emit('gotoPrev', getPrev)" class="list-item prev">
          <a href="" class="top-link prevInserateButton button primary" data-id="" style="cursor: pointer;" :style="[btnBgColor, btnFgColor]">
            <span class="padding10">&lt; {{ $t('front_extern.detail_vorher') }}</span>
          </a>
        </li>
      </ul>
    </nav>
    <header>
      <div class="heading-container">
        <p v-if="false" class="printHeader"><b>{{ details.standort.firma_name }}</b> {{ details.standort.kunde_strasse }} {{ details.standort.kunde_plz }} {{ details.standort.kunde_ort }} Telefon: {{ details.standort.kunde_telefon }} Fax: {{ details.standort.kunde_fax }} </p>
        <hr style="margin-top: -10px;">
        <p></p>
        <h1 v-html="fahrzeug + ' - ' + details.rel_category['bezeichnung_' + lang]" class="title-main" :style="titleColor"></h1>
        <div v-if="false" class="button-container printPdfContainer exportShowNormal">
          <a href="javascript:;" class="printDetail" style="color: #000000!important;">
            <span class="caption" styl="">Drucken | </span>
          </a>
          <a href="javascript:;" class="downloadPdfDetail" style="color: #000000!important;">
            <span class="caption">Download PDF</span>
          </a>
        </div>
      </div>
    </header>
    <div class="columns" id="divToPrint">
      <div class="left">
        <div data-gallery-closetext="Schliessen" class="gallery">
          <div ref="defaultDContainer" class="image-large-container" style="text-align: center;">
            <a href="#" @click.prevent="picClicked(0)" rel="group1">
              <detail-image 
                :details="details"
                :imgOffset="0"
                :container="'defaultDContainer'"
              />
            </a>
          </div>
          <div class="object-list-container">
            <ul v-if="details.pictures.length > 1" class="object-list thumb-list closed">
              <template v-for="(pic, ndx) in details.pictures">
              <li v-if="ndx>0" v-bind:key="ndx" ref="dContainer" style="text-align: center;" class="imageGroup object-list-item">
                <a href="#" @click.prevent="picClicked(ndx)" class="object-thumb" rel="group1">
                  <detail-image 
                    :details="details"
                    :imgOffset="ndx"
                    :container="'dContainer'"
                  />
                </a>
              </li>
              </template>
            </ul>
          </div>
        </div>
        <section>
          <div class="vehicle-data greyBgAndBorderDetail">
            <h2 class="title-secondary" :style="titleColor">{{ $t('front_extern.detail_titel') }}</h2>
            <table :style="fontColor">
              <tbody>
                <tr>
                  <th class="data-label">{{ $t('front_extern.detail_kategorie') }}</th>
                  <td class="data">{{ details.rel_category['bezeichnung_' + lang]}}</td>
                </tr>
                <tr v-if="details.fzg_1iv">
                  <th class="data-label">{{ $t('front_extern.detail_invsetz') }}</th>
                  <td class="data">{{formatDate(details.fzg_1iv)}}</td>
                </tr>
                <tr>
                  <th class="data-label">{{ $t('front_extern.detail_modell') }}</th>
                  <td class="data">{{ fahrzeug }}</td>
                </tr>
                <tr>
                  <th class="data-label">{{ $t('front_extern.detail_fzg') }}</th>
                  <td class="data">{{ details.rel_art['bezeichnung_' + lang] }}</td>
                </tr>
                  <tr v-if="details.fzg_farbe">
                  <th class="data-label">{{ $t('front_extern.detail_farbe') }}</th>
                  <td class="data">{{ details.rel_farbe['bezeichnung_' + lang] }}</td>
                </tr>
                <tr v-if="details.fzg_rubrik_vermietung == 0">
                  <th class="data-label">{{ $t('front_extern.detail_km') }}</th>
                  <td class="data">{{formatNum(details.fzg_km)}} km</td>
                </tr>
                <tr>
                  <th class="data-label">{{ $t('front_extern.detail_hubraum') }}</th>
                  <td class="data">{{ details.modell_ccm!=0 ? formatNum(details.modell_ccm) : '-'}} ccm</td>
                </tr>
                <tr>
                  <th class="data-label">{{ $t('front_extern.detail_zustand') }}</th>
                  <td class="data">{{ details.zustand }}</td>
                </tr>
                <tr>
                  <th class="data-label">{{ $t('front_extern.detail_leistung') }}</th>
                  <td class="data">
                    {{details.fzg_leistung ? formatNum(details.fzg_leistung, 1) : '-'}} kW /
                    {{details.fzg_leistung ? formatNum(details.fzg_leistung*1.35962) : '-'}} PS
                  </td>
                </tr>
                <tr>
                  <th class="data-label" v-if="details.garantie_typ == 3">
                    {{ $t('front_extern.detail_warranty_manufacturer') }}
                  </th>
                  <th class="data-label" v-else>
                    {{ $t('front_extern.detail_warranty') }}
                  </th>
                  <td v-html="getWarranty()" class="data"></td>
                </tr>
                <template v-if="details.fzg_rubrik_vermietung == 1">
                  <tr class="price beschNormal">
                    <th class="data-label">{{ $t('front_extern.detail_preis') }}</th>
                    <td class="data">{{ $t("front_extern.ab") }} {{formatPrice(details.mietkonditionen.miete_tag, 0)}}  / {{ $t("front_extern.day") }}</td>
                  </tr>
                </template>
                <template v-else>
                  <tr v-if="details.actualprice > 0" class="price beschNormal">
                    <th class="data-label">{{ $t('front_extern.detail_preis') }}</th>
                    <td class="data">{{formatPrice(details.actualprice, 0)}}</td>
                  </tr>
                </template>
                <template v-if="details.fzg_text && details.fzg_text.length > 0">
                  <tr class="beschNormal">
                    <th class="data-label "><b>{{ $t('front_extern.detail_bemerkung') }}</b></th>
                    <td class="data"></td>
                  </tr>
                  <tr class="beschNormal">
                    <td v-html="details.fzg_text" colspan="2"></td>
                  </tr>
                </template>
              </tbody>
            </table>
          </div>
        </section>
        <div class="beschPrint">
          <h2 class="title-secondary">{{ $t('front_extern.detail_bemerkung') }}</h2>
          <p v-html="details.fzg_text"></p>
        </div>
        <div class="beschPreis">{{ $t('front_extern.detail_preis') }} {{formatPrice(details.actualprice, 0)}} </div>
      </div>
      <div class="right">
        <div class="padding">
          <div class="chapters">
            <section>
              <div class="chapter dealer">
                <div class="chapter-content greyBgAndBorder">
                  <div style="width: 220px; float: left;">
                    <h2 class="title-secondary" :style="titleColor">{{ $t('front_extern.detail_anbieter') }}</h2>
                    <div class="vcard">
                      <span v-html="details.standort.firma_name" class="org fn"></span>
                      <span class="adr">
                        <span class="street-address">{{ details.standort.kunde_strasse }}</span>
                        <span class="postal-code">{{ details.standort.kunde_plz }}</span>&nbsp;
                        <span class="locality">{{ details.standort.kunde_ort }}</span>
                      </span>
                    </div>
                    <a :href="googleMapsLink" target="_blank" class="geo" style="color: #000000;!important">{{ $t('front_extern.detail_googlemap')}} </a>
                  </div>
                  <div v-if="standortLogo" style="float: right; padding: 0px; padding-top: 25px;"><img :src="standortLogo" :alt="details.standort.firma_name + ' ' + details.standort.kunde_ort" style="max-width: 200px;"></div>
                  <address class="phone-container">
                    <table style="margin-left: -2px;">
                      <tbody>
                        <tr v-if="details.standort.kunde_telefon || details.standort.kunde_mobile" class="primary">
                          <th class="header">{{ $t('front_extern.details_telefon') }}</th>
                          <td class="data">
                            <span class="phone">
                              <a id="lnkPhoneCall_1" :href="'tel:' + (details.standort.kunde_telefon ? details.standort.kunde_telefon.replace(/ /g, '') : details.standort.kunde_mobile.replace(/ /g, ''))" class="tel call-on-me" style="color: #000000!important;">{{ details.standort.kunde_telefon ? details.standort.kunde_telefon : details.standort.kunde_mobile }}</a>
                            </span>
                          </td>
                        </tr>
                        <tr v-if="details.standort.kunde_telefon">
                          <th class="header">{{ $t('front_extern.details_fax') }}</th>
                          <td class="data"><span class="phone"><span class="tel">{{  details.standort.kunde_telefon }}</span></span></td>
                        </tr>
                      </tbody>
                    </table>
                  </address>
                </div>
              </div>
            </section>
            <section>
              <div class="kontaktDiv">
                <div class="DetailContact">
                  <div id="kontaktArea">
                    <form v-if="!contactSent" id="kontaktdaten" name="kontaktdaten" method="post" onsubmit="return false;" novalidate="novalidate">
                      <div class="kontaktDiv1">
                        <span class="DCTitle" :style="titleColor">{{ $t('front_extern.form_kontakt') }}</span>
                        <label class="inpLabel">{{ $t('front_extern.form_anrede') }}</label>
                        <input id="anrede0" name="anrede" type="radio" value="1" v-model="form.anrede">
                        <label for="anrede0" class="labelFrau">{{ $t('front_extern.form_frau') }}</label>
                        <input id="anrede1" name="anrede" type="radio" value="2" v-model="form.anrede">
                        <label for="anrede1">{{ $t('front_extern.form_herr') }}</label>
                        <input id="anrede2" name="anrede" type="radio" value="3" v-model="form.anrede">
                        <label for="anrede2">{{ $t('front_extern.form_firma') }}</label>
                        <label ref="vorname" class="inpLabel">{{ $t('front_extern.form_vorname') }} * </label>
                        <input id="vorname" name="vorname" type="text" v-model="form.vorname" class="DetailInputText" :style="errorClass('vorname')" title="">
                        <label ref="name" class="inpLabel">{{ $t('front_extern.form_name') }} *</label>
                        <input id="name" name="name" type="text" v-model="form.name" class="DetailInputText" :style="errorClass('name')" title="">
                        <label ref="telefon" class="inpLabel">{{ $t('front_extern.form_telefon') }} *</label>
                        <input id="telefon" name="telefon" type="text" v-model="form.telefon" class="DetailInputText" :style="errorClass('telefon')" title="">
                        <label ref="email" class="inpLabel">{{ $t('front_extern.form_email') }} *</label>
                        <input id="email" name="email" type="text" v-model="form.email" class="DetailInputText" :style="errorClass('email')" title="">
                        <div class="contactOptions">
                          <input id="chkCopy" type="checkbox" value="1" v-model="form.sendCopy">
                          <label for="chkCopy">{{ $t('front_extern.form_kopie') }}</label>
                        </div>
                        <div class="dieMitInfo">{{ $t('front_extern.form_required') }}<br> </div>
                        <span class="DCTitle" style="color: #DE182E!important;"></span>
                        <label class="inpLabel" style="width:200px;">{{ $t('front_extern.form_interested_in') }}</label>
                        <div class="contactRightOptions">
                          <input id="chkBesichtigung" type="checkbox" v-model="form.chkBesichtigung">
                          <label for="chkBesichtigung">{{ $t('front_extern.form_interested_in_visit') }}</label>
                          <input id="chkProbefahrt" type="checkbox" v-model="form.chkProbefahrt">
                          <label for="chkProbefahrt">{{ $t('front_extern.form_interested_in_testdrive') }}</label>
                        </div>
                        <label class="inpLabel" style0="width:200px;">{{ $t('front_extern.form_bemerkung') }}</label>
                        <textarea id="bemerkungen" v-model="form.bemerkungen" name="bemerkungen" class="contactTextarea"></textarea>
                        <div class="smsSumbitPart">
                          <input type="submit" @click.prevent="sendContact" class="button primary submit advSearchBtn submitKontaktForm" :value="$t('front_extern.form_absenden')" style="cursor: pointer; height: 32px;" :style="[btnBgColor, btnFgColor]">
                        </div>
                      </div>
                    </form>
                    <div v-else>
                      <span v-html="$t('front_extern.contact_sent')" class="DCTitle" :style="titleColor"></span>
                    </div>
                  </div>
                </div>
              </div>
            </section>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import axios from 'axios'

import DetailImage from './DetailImage.vue'

import PathMixin from '../mixins/PathMixin.js'
import Numberformatting from '../mixins/Numberformatting.js'
import defaultImageMixin from '../mixins/defaultImageMixin.js'

export default {
  props: ['list', 'details', 'lang', 'settings', 'url'],
  mixins: [PathMixin, Numberformatting, defaultImageMixin],
  components: {
    'detail-image': DetailImage
  },
  data() {
    return {
      isLoading: false,
      contactSent: false,
      form: {
        rowID: 0,
        adID: 0,
        anrede: 1,
        vorname: '',
        name: '',
        telefon: '',
        email: '',
        sendCopy: false,
        chkBesichtigung: false,
        chkProbefahrt: false,
        bemerkungen: '',
      },
      formErrors: {
        name: false,
        vorname: false,
        email: false,
        teleforn: false,
      },
    }
  },
  methods: {
    picClicked: function() {
    },
    defaultPic: function() {
      if(!this.details.pictures) {
        return this.getDefaultImage();
        // return '/front/v30/images/J56420132201.jpg';
      }
      if(this.details.pictures.length == 0) {
        return this.getDefaultImage();
        // return '/front/v30/images/J56420132201.jpg';
      }
      return this.adCDNPath(this.details.ident_nr_kunde, this.details.ident_nr_standort) + this.details.pictures[0].datei;
    },
    getPic: function(pic) {
      return this.adCDNPath(this.details.ident_nr_kunde, this.details.ident_nr_standort) + pic;
    },
    getWarranty: function() {
      var ret = '';
      switch(this.details.garantie_typ){
        case 1:
          return '-';
          break;
        case 2:
          if(this.details.garantie_monate) {
            ret += this.details.garantie_monate + ' ' + this.$t('front_extern.detail_months');
          }
          if(this.details.garantie_km && this.details.garantie_km * 1 != 0 && !isNaN(this.details.garantie_km)) {
            if(ret != '') {
              ret += ' ' + this.$t('front_extern.detail_or') + '<br>';
            }
            ret += this.formatNum(this.details.garantie_km) + ' ' + this.$t('front_extern.detail_kilometer');
          }
          break;
        case 3:
          if(this.details.garantie_datum) {
            ret += this.$t('front_extern.detail_until') + ' ' + this.formatDate(this.details.garantie_datum);
          }
          if(this.details.garantie_km && this.details.garantie_km * 1 != 0 && !isNaN(this.details.garantie_km)) {
            if(ret != '') {
              ret += ' ' + this.$t('front_extern.detail_or') + '<br>';
            }
            ret += this.$t('front_extern.detail_until') + ' ' + this.formatNum(this.details.garantie_km) + ' ' + this.$t('front_extern.detail_kilometer');
          }
          break;
        case 4:
          if(this.details.garantie_datum) {
            ret += this.$t('front_extern.detail_until') + ' ' + this.formatDate(this.details.garantie_datum);
          }
          if(this.details.garantie_km && this.details.garantie_km * 1 != 0 && !isNaN(this.details.garantie_km)) {
            if(ret != '') {
              ret += ' ' + '<br>' + this.$t('front_extern.detail_or') + ' ';
            }
            ret += this.$t('front_extern.detail_until') + " 3'000 " + this.$t('front_extern.detail_kilometer');
          }
          if(this.details.garantie_vers_anbieter && this.details.garantie_vers_anbieter != 1) {
            if(ret != '') {
              ret += '<br/>';
            }
            ret += this.$t('front_extern.detail_provider') +  ': ' + this.details.garantieAnbieter;
          }
          if(this.details.garantie_vers_kosten && this.details.garantie_vers_kosten > 0) {
            if(ret != '') {
              ret += '<br>';
            }
            ret += this.$t('front_extern.detail_insurance_cost') + ': ' + this.formatPrice(this.details.garantie_vers_kosten, 0);
          }
          break;
      }
      return ret;
    },
    errorClass(name) {
      if(this.formErrors[name]) {
        return "border: 2px solid red;"
      }
      return '';
    },
    resetFormErrors() {
      this.formErrors = {
        name: false,
        vorname: false,
        email: false,
        teleforn: false,
      }
    },
    sendContact() {
      this.isLoading = true;
      this.form.rowID = this.settings.id;
      this.form.adID = this.details.id;
      axios
        .get(this.url + "/api/ext/sendContact", {
          params: this.form
        })
        .then(response => {
          this.resetFormErrors();
          this.contactSent = true;
        })
        .catch(error => {
          this.resetFormErrors();
          if(error.response.status == 422){
            var errList = error.response.data.errors;
            var listKeys = Object.keys(errList);
            if(listKeys.length > 0) {
              listKeys.forEach(element => {
                this.formErrors[element] = true;
              });
            }
          }else{
            console.log('Error: ', error.response.data)
          }
        })
        .finally(() => {
          this.isLoading = false;
        })
    },    
  },
  computed: {
    axiosParams() {
      const params = new URLSearchParams();
      params.append('param1', 'value1');
      params.append('param2', 'value2');
      return params;
    },    
    getPrev: function() {
      var id = this.details.id;
      var ndx = this.list.findIndex(i => i.id === id);
      if(ndx > 0) {
        return this.list[ndx-1].id;
      }
      return false;
    },
    getNext: function() {
      var id = this.details.id;
      var ndx = this.list.findIndex(i => i.id === id);
      if(ndx < this.list.length-1) {
        return this.list[ndx+1].id;
      }
      return false;
    },
    fahrzeug() {
      if(this.details.modell_zusatz && this.details.modell_zusatz.length > 0) {
        return this.details.rel_marke.Markenbezeichnung + ' '   + this.details.rel_modelle.Modellbezeichnung + ' ' + this.details.modell_zusatz;
      }else{
        return this.details.rel_marke.Markenbezeichnung + ' ' + this.details.rel_modelle.Modellbezeichnung;
      }
    },
    titleColor: function() {
      return this.settings.titleColor ? 'color: #' + this.settings.titleColor + ' !important;' : '';
    },
    bgColor: function() {
      return this.settings.bgColor ? 'background-color: #' + this.settings.bgColor + ';' : '';
    },
    fontColor: function() {
      return this.settings.fontColor ? 'color: #' + this.settings.fontColor + ' !important;' : '';
    },
    btnBgColor: function() {
      return this.settings.buttonColor ? 'background: #' + this.settings.buttonColor + ' !important' : '';
    },
    btnFgColor: function() {
      return this.settings.buttonTextColor ? 'color: #' + this.settings.buttonTextColor + ' !important;' : '';
    },
    googleMapsLink: function() {
      return '//maps.google.ch/maps?q=' + this.details.standort.kunde_strasse + ',' + this.details.standort.kunde_plz + '+' + this.details.standort.kunde_ort;
    },
    standortLogo: function() {
      var logo = this.details.standort.medien.find(b => b.medientyp_id == 1);
      if(logo) {
        return this.clientCDNPath(this.details.ident_nr_kunde, this.details.ident_nr_standort) + '/logos/' + logo.media;
      }
      return false;
    },
    
    
  },


}
</script>

      