// React, NextJS
import React from "react";
import Head from "next/head";

// Bootstrap components
import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';

// Formik
import { Formik } from "formik";
import axios from "axios";


import {
  TextInput,
  TextareaInput,
  SelectInput,
} from "../components/forms/formfields.jsx";

import { ContactSchema } from "../components/forms/schemas.jsx";

export default class Kontakt extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      res: null,
      status: null,
    };
  }
  render() {
    return (
      <>
        <Head>
          <meta
            name="description"
            content="Sie haben Fragen? Treten Sie einfach mit uns in Kontakt."
          />
          <meta name="keywords" content="Prothmann, Kontakt, Kontaktformular" />
          <title>Kontakt - Prothmann GmbH</title>
        </Head>

        <Container fluid className="products-container navbar-spacing">
          <Row className="align-items-center min-height-100">
            <Col xs={12} lg={6}
              className="align-self-stretchd-flex flex-column justify-content-center p-0 p-5"
            >
              <h1 className="mainheading">Kontakt</h1>
              <div className="description">
                <p>
                  Nutzen Sie einfach das Kontaktformular auf dieser Seite oder
                  eine der untern angegebenen Kontaktmöglichkeiten.
                  <br />
                  Telefonisch erreichen Sie uns Montag - Freitag von 9:00 Uhr
                  bis 16:00 Uhr.
                  <br />
                  <br />
                  E-Mail:{" "}
                  <a href="mailto:info@prothmann.com">info@prothmann.com</a>
                  <br />
                  Telefon: <a href="tel:+49814259951">+49 (0) 8142 59951</a>
                  <br />
                  Fax: <a href="fax:+49814259953">+49 (0) 8142 59953</a>
                  <br />
                  <br />
                  Anschrift:
                  <br />
                  Industriestraße 6<br />
                  82194 Gröbenzell (bei München)
                </p>
              </div>
            </Col>

            <Col xs={12} lg={6}
              className="pl-0 align-self-center p-5 mb-5 h-100"
            >
              <Formik
                validationSchema={ContactSchema}
                initialValues={{
                  firma: "",
                  anrede: "",
                  titel: "",
                  vorname: "",
                  nachname: "",
                  email: "",
                  telefon: "",
                  betreff: "",
                  nachricht: "",
                }}
                onSubmit={async (values) => {
                  const data = values;
                  try {
                    const res = await axios({
                      method: "post",
                      url: "/api/contactsend",
                      headers: {
                        "Content-Type": "application/json",
                      },
                      data,
                    });
                    this.setState({
                      status: "Ihre Nachricht wurde erfolgreich versendet",
                      res: res.status,
                    });
                  } catch (error) {
                    this.setState({
                      status:
                        "Beim versenden ist ein Fehler aufgetreten! Bitte versuchen Sie es in einigen Minuten noch einmal oder schicken Sie und direkt eine e-Mail.",
                      res: res.status,
                    });
                  }
                }}
              >
                {({
                  values,
                  errors,
                  touched,
                  handleChange,
                  handleBlur,
                  handleSubmit,
                  isSubmitting,
                }) => (
                  <form onSubmit={handleSubmit} className="description">
                    <Row className="g-2 mb-5">
                      <Col>
                        <TextInput
                          isSubmitting={isSubmitting}
                          rescode={this.state.res}
                          error={errors.firma}
                          touched={touched.firma}
                          placeholder={"Musterfirma"}
                          onChange={handleChange("firma")}
                          onBlur={handleBlur("firma")}
                          value={values.firma}
                          label="Firma (optional)"
                          type="text"
                        />
                      </Col>
                    </Row>

                    <Row className="g-2 mb-3">
                      <Col>
                        <SelectInput
                          isSubmitting={isSubmitting}
                          rescode={this.state.res}
                          error={errors.anrede}
                          touched={touched.anrede}
                          placeholder={"Anrede"}
                          onChange={handleChange("anrede")}
                          onBlur={handleBlur("anrede")}
                          value={values.anrede}
                          label="Anrede"
                          options={["Herr", "Frau", "Divers"]}
                        />
                      </Col>
                      <Col>
                        <SelectInput
                          isSubmitting={isSubmitting}
                          rescode={this.state.res}
                          error={errors.titel}
                          touched={touched.titel}
                          placeholder={"Titel"}
                          onChange={handleChange("titel")}
                          onBlur={handleBlur("titel")}
                          value={values.titel}
                          label="Titel"
                          options={["Dr.", "Prof.", "Dipl."]}
                        />
                      </Col>
                    </Row>

                    <Row className="g-2 mb-5">
                      <Col>
                        <TextInput
                          isSubmitting={isSubmitting}
                          rescode={this.state.res}
                          error={errors.vorname}
                          touched={touched.vorname}
                          placeholder={"Max"}
                          onChange={handleChange("vorname")}
                          onBlur={handleBlur("vorname")}
                          value={values.vorname}
                          label="Vorname (optional)"
                          type="text"
                        />
                      </Col>
                      <Col>
                        <TextInput
                          isSubmitting={isSubmitting}
                          rescode={this.state.res}
                          error={errors.nachname}
                          touched={touched.nachname}
                          placeholder={"Max"}
                          onChange={handleChange("nachname")}
                          onBlur={handleBlur("nachname")}
                          value={values.nachname}
                          label="Nachname"
                          type="text"
                        />
                      </Col>
                    </Row>

                    <Row className="g-2 mb-5">
                      <Col>
                        <TextInput
                          isSubmitting={isSubmitting}
                          rescode={this.state.res}
                          error={errors.email}
                          touched={touched.email}
                          placeholder={"max@mustermann.de"}
                          onChange={handleChange("email")}
                          onBlur={handleBlur("email")}
                          value={values.email}
                          label="eMail"
                          type="email"
                        />
                      </Col>
                      <Col>
                        <TextInput
                          isSubmitting={isSubmitting}
                          rescode={this.state.res}
                          error={errors.telefon}
                          touched={touched.telefon}
                          placeholder={"max@mustermann.de"}
                          onChange={handleChange("telefon")}
                          onBlur={handleBlur("telefon")}
                          value={values.telefon}
                          label="Telefon (optional)"
                          type="text"
                        />
                      </Col>
                    </Row>

                    <Row className="g-2 mb-3">
                      <Col xs={12}>
                        <TextInput
                          isSubmitting={isSubmitting}
                          rescode={this.state.res}
                          error={errors.betreff}
                          touched={touched.betreff}
                          placeholder={"Betreff"}
                          onChange={handleChange("betreff")}
                          onBlur={handleBlur("betreff")}
                          value={values.betreff}
                          label="Betreff"
                          type="text"
                        />
                      </Col>
                      <Col xs={12}>
                        <TextareaInput
                          isSubmitting={isSubmitting}
                          rescode={this.state.res}
                          error={errors.nachricht}
                          touched={touched.nachricht}
                          placeholder={"Nachricht"}
                          onChange={handleChange("nachricht")}
                          onBlur={handleBlur("nachricht")}
                          value={values.nachricht}
                          label="Nachricht"
                        />
                      </Col>
                    </Row>
                    <Row className="g-2 mb-3">
                      <Col>
                        <p>{this.state.status}</p>
                        {!isSubmitting && this.state.res != 200 && (
                          <button className="btn btn-dark" type="submit">
                            Abschicken
                          </button>
                        )}
                        {isSubmitting && (
                          <div className="spinner-border" role="status">
                            <span className="visually-hidden">Sende...</span>
                          </div>
                        )}
                      </Col>
                    </Row>
                  </form>
                )}
              </Formik>
            </Col>
          </Row>
        </Container>
      </>
    );
  }
}
