<template>
    <div class="panel-container">
      <el-button class="panel-button" @click="showPanel = !showPanel" type="primary" circle>
        <i :class="showPanel ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i>
      </el-button>
      <el-card class="panel" :class="{ 'panel-open': showPanel }">
        <div slot="header" class="header-title">
        <p>Get in touch with a TrustyPay expert</p>
        <p>We're eager to help you succeed with <br>our range of solutions.</p>        
      </div>

        <el-form :model="form" :rules="rules" ref="contactForm" label-width="120px" label-position="top">
          <el-form-item label="Name" prop="name">
            <el-input v-model="form.name"  prop="name" ></el-input>
          </el-form-item>
          <el-form-item label="Business Name"  prop="businessName">
            <el-input v-model="form.businessName"></el-input>
          </el-form-item>
          <el-form-item label="Mobile(10 digits)" prop="mobile">
            <el-input v-model="form.mobile"></el-input>
          </el-form-item>
          <el-form-item label="Email" prop="email">
            <el-input v-model="form.email"></el-input>
          </el-form-item>
          <el-form-item label="Language Preference" prop="language">
            <el-select v-model="form.language" placeholder="Select Language">
              
              <el-option label="English" value="English"></el-option>
              <el-option label="中文" value="中文"></el-option>
              <el-option label="한국" value="한국"></el-option>
              <el-option label="हिंदी" value="हिंदी"></el-option>
              <el-option label="Ελληνικά" value="Ελληνικά"></el-option>              
              
            </el-select>
          </el-form-item>
          <el-form-item label="Your message" prop="message">
            <el-input type="text" v-model="form.message"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('contactForm')" class="sbuBtn" :loading="isLoading">Submit</el-button>
          </el-form-item>
        </el-form>

      </el-card>
    </div>
  </template>
  
  <script>

  import { elIsEmail, elIsValidatePhone } from "@/utils/el-validate";
  import { send as emailSend } from "@emailjs/browser";
  import { emailConfig } from "@/utils/config"
  import { SEND_COUNT_KEY, isSameDay } from "@/utils";
  import { Store } from "@/utils/store";

  export default {
    data() {
      return {
        showPanel: true,
        isLoading:false,
        form: {
          name: '',
          businessName: '',
          mobile: '',
          email: '',
          language: '',
          message: '',
        },
        rules: {
          name: [
            { 
              required: true, 
              message: 'Please enter your name', 
              trigger: 'blur' 
            }],
          businessName: [
            { 
              required: true, 
              message: 'Please enter your business name',
              trigger: 'blur' 
              }],
          mobile: [
            { 
              required: true, 
              trigger: ["blur"],
              validator: elIsValidatePhone
            }],
          email: [
            { 
              required: true, 
              trigger: ["blur"],
              validator: elIsEmail
             },
          ],

      },
      }
    },
    methods: {
      
      submitForm(formName) {
      const store = new Store();
      const localStorageData = store.get(SEND_COUNT_KEY, {
        time: Date.now(),
        count: 0,
      });

      // 判断是否同一天
      if (isSameDay(localStorageData.time, new Date().getTime())) {
        // 同一天 超过3次 则不允许提交
        if (localStorageData.count >= 3) {
          this.$alert(
            "You have reached the maximum number of submissions!",
            "",
            {
              callback: (action) => {},
            }
          );
          return;
        }
      }

        this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            this.isLoading = true;

            const data = await emailSend(
              emailConfig.SERVICE_ID,
              emailConfig.TEMPLATE_ID,
              {
                name: this.form.name,
                businessName: this.form.businessName,
                mobile: this.form.mobile,
                email: this.form.email,
                languagePreference: this.form.language,
                message: this.form.message,
              }
            );
              this.isLoading = false;
              
            if (data.status === 200) {
              this.$alert( "Thank you for contacting us. We’ll get in touch with you in the next 24 hours", '', 
              {
              callback: action => {
                this.$refs[formName].resetFields();
          }
         
        });
                    // 发送成功后，保存发送次数
                store.save(SEND_COUNT_KEY, {
                time: Date.now(),
                count:
                  localStorageData.count + 1 >= 4
                    ? 1
                    : localStorageData.count + 1,
              });1213             
             
            } else {
              window.alert("Form submitted failed!");
            }
          } catch (error) {
            console.log("Error submitting form!", error);
          }
        } else {
          this.isLoading=false;
          console.log("Error submitting form!");
          return false;
        }
      });
       
      }
    }
  }
  </script>
  
  <style scoped>
.el-card{
  background: #9bc943;
}
  .panel-container {
    position: fixed;
    bottom: 60px;
    right: 20px;
    z-index: 1000;
  }
  
  .panel-button {
    position: absolute;
    bottom: 0;
    right: 0;
    z-index: 999;
  }
  
  .panel {
    width: auto;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease;
  }
  
  .panel-open {
    
    max-height: 7090px;
  }
  .header-title{
    p{
      margin: 0;
      font-size: 16px;
      text-align: center;
    }
  }
  /* 去掉显示的底部 */
  .el-card{
    border: none;
  
  }
  /* 设置按钮的大小 */
  .el-button.is-circle{
    width: 70px;
    height: 70px;
    margin-bottom: -25px;
  }
  .el-form-item {
    margin-bottom: 0;
  }
  /deep/.el-form--label-top .el-form-item__label{
    padding-bottom: 0px;
  }
  .el-form-item__label{
    line-height: normal;
  }
  .el-select{
    display: initial;
  }
  .sbuBtn{
    margin: 15px 120px;
  }
  </style>