import * as React from "react";
import { FieldsControl as CoreFieldsControl } from "../../../Controls/FieldsControl";
import { RecordField } from "src/Models";

export class FieldsControl extends React.Component<any, any> {
  constructor(props: any) {
    super(props);

    if (this.props.fields)
      this.props.fields.forEach((field: RecordField) => {

        if (field.id == 260) {
          field.extraData = {
            getUsersCondition: {
              organizationUnit: -1
            }
          }
        }

      });

  }

  private _onOrganizationUnitChange(field: any) {
    debugger;

    if (field.linkedUserField) {
      field.linkedUserField.extraData = {
        getUsersCondition: {
          organizationUnit: field.data
        }
      }
    }

    this.forceUpdate();
  }


  private _setColWidth(id: any): string {
    switch (id) {
      case 256: {
        return "ms-Grid-col ms-sm12 ms-lg12";
      }
      case 257: {
        return "ms-Grid-col ms-sm12 ms-lg6";
      }
      case 258: {
        return "ms-Grid-col ms-sm12 ms-lg6";
      }
      case 259: {
        return "ms-Grid-col ms-sm12 ms-lg6";
      }
      case 260: {
        return "ms-Grid-col ms-sm12 ms-lg6";
      }

      default: {
        return "ms-Grid-col ms-sm12 ms-lg12";
      }
    }
  }

  public render(): JSX.Element {

    if (this.props.fields) {
      let userField: RecordField;
      this.props.fields.forEach((field: RecordField) => {

        if (field.id == 260) {
          userField = field;
        }

        if (field.id == 264 || field.id == 265)
          field.hidden = true;

      });

      this.props.fields.forEach((field: any) => {
        if (field.id == 259) {
          field.onChanged = this._onOrganizationUnitChange.bind(this)

          field.linkedUserField = userField;
        }
      });

    }
    return (
      <div><CoreFieldsControl
        {...this.props} fields={this.props.fields}
        setColWidth={this._setColWidth.bind(this)}></CoreFieldsControl>
      </div>
    )
  }
}                        