import React, { Component } from 'react';
import { withNamespaces } from 'react-i18next';
import DynamicFormBuilder from './DynamicForm';
import { connect } from 'react-redux';

class RealEstateRequestPreviewForm extends Component {
  constructor(props) {
    super(props);
    this.responseData = {};
    this.storeData = {};
    this.documentData = {};
    this.state = {
      isLoad: false,
      isProceed: false,
      template: [],
      isNoRecFound: false
    };
  }

  generateTemplate() {
    this.parentData = {
      "pagetitle": "",
      "form": [],
      "table": [],
      "button": []
    };
    let parentDataKeys = Object.keys(this.props.responseData);
    let inputData = this.props.responseData;
    let self = this;
    parentDataKeys.forEach(function (pdata) {
      self.parentData.pagetitle = pdata;
      let childDataKeys = Object.keys(inputData[pdata]);
      childDataKeys.forEach(function (value) {
        if (Object.prototype.toString.call(inputData[pdata][value]) === '[object Array]') {
          let tableobjKeys = Object.keys(inputData[pdata][value][0]);
          let tableHeader = [];
          let tableRow = [];
          tableobjKeys.forEach(function (keyvalue, index) {
            let headerobj = {
              "id": keyvalue + index,
              "labelName": keyvalue,
              "isVisible": true,
              "position": 2,
              "class": "col-md-3"
            };
            let rowobj = {
              "id": "td" + keyvalue,
              "labelName": keyvalue,
              "keyCode": keyvalue,
              "type": "label",
              "value": "",
              "isVisible": true,
              "class": "col-md-3"
            };
            tableHeader.push(headerobj);
            tableRow.push(rowobj);
          });
          let tableList = {
            "title": "Address",
            "tabHeader": tableHeader,
            "tabData": tableRow
          };
          let tableObj = {
            "title": "",
            "viewType": "defaultGrid",
            "keyCode": "AddressDetails",
            "gridClass": "col-lg-12 col-md-12",
            "panelClass": "",
            "tablelist": tableList
          };
          self.parentData.table.push(tableObj);
        } else {
          let objKeys = Object.keys(inputData[pdata][value]);
          let inputformdata = [];
          objKeys.forEach(function (data) {
            let obj = {
              "id": data,
              "name": data,
              "keyCode": data,
              "labelName": data,
              "type": "label",
              "placeholder": "Enter Application No",
              "showAsterisk": false,
              "minlength": "1",
              "maxlength": "",
              "disabled": false,
              "readOnly": "true",
              "isVisible": true,
              "model": "",
              "displayField": "value",
              "value": "",
              "validations": {},
              "position": 1,
              "class": "col-lg-3 col-md-2 col-sm-6 col-xm-12"
            };
            inputformdata.push(obj);
          });
          let objData = {
            "title": "",
            "gridClass": "col-lg-12 col-md-12 col-sm-12",
            "panelClass": "panel-theme-no-bg",
            "formgroup": {
              "title": value,
              "form-col": 3,
              "input": inputformdata
            }
          };
          self.parentData.form.push(objData);
        }
      });
    });
    this.setState({
      template: this.parentData,
      isLoad: true
    });
  }

  componentDidMount() {
    if (this.props.element === "" || this.props.element.length === 0) {
      this.generateTemplate();
    } else {
      this.setState({
        template: this.props.element,
        isLoad: true
      });
    }
  }

  render() {
    //window.scrollTo(0, 0);
    const {
      template,
      isLoad
    } = this.state;
    let RenderForm;

    if (isLoad) {
      RenderForm = /*#__PURE__*/React.createElement(DynamicFormBuilder, {
        lang: "en",
        isAcknowldege: this.props.isAcknowldege,
        element: template,
        responseData: this.props.responseData,
        viewType: this.props.viewType,
        viewAction: this.props.viewAction,
        onAppFormChange: this.props.onAppFormChange,
        onAppFormSubmit: this.props.onAppFormSubmit,
        onAppBtnClick: this.props.onAppBtnClick,
        isUploadPresent: this.props.isUploadPresent,
        storeData: this.props.storeData,
        isEditTable: this.props.isEditTable,
        langCheckValue: "ar",
        labelDirectionStyle: "ltr",
        labelDiretionStylesList: {
          "en": "ltr",
          "ar": "rtl"
        }
      });
    }

    return /*#__PURE__*/React.createElement("div", null, RenderForm);
  }

}

const mapStateToProps = state => ({});

const mapDispatchToProps = {};
export default withNamespaces()(connect(mapStateToProps, mapDispatchToProps)(RealEstateRequestPreviewForm));