import * as React from "react";
import { OrganizationService, CachingService } from "src/Services";
import { OrganizationUnit } from "src/Models";
import { IComboBoxOption } from "office-ui-fabric-react";
import { TreeComboBox } from "src/Controls";

export class OrganizationalUnitsForTransferTreeComboBox extends React.Component<any, any> {
    private _organizationService: OrganizationService = new OrganizationService();
    private _options: Array<IComboBoxOption> = new Array<IComboBoxOption>()
    private _cachingService: CachingService = new CachingService();
    private _loggedInUser: any;

    constructor(props: any) {
        super(props);

        this._loggedInUser = this._cachingService.read("LoggedInUser")

        this.state = {
            options: this._options
        }

        if (this.props.correspondence) {
            //debugger;
            let departmentId: number = -1;
            if (this.props.correspondence.fields) {
                this.props.correspondence.fields.forEach((field: any) => {
                    if (field.id == 165)
                        departmentId = field.data;
                })
            }

            if (departmentId == -1) {
                departmentId = this._loggedInUser.department
            }
            if (departmentId > -1)
                this._loadOrganizationalUnits(departmentId);
        }
    }

    private _loadOrganizationalUnits(correspondenceDepartmentId: number) {
        this._organizationService.listOrganizationsForTransfer(correspondenceDepartmentId).then((organizationUnits: OrganizationUnit[]) => {
            this._convertOrgChartToOptions(organizationUnits, 0, []);

            this.setState({ options: this._options })
        });
    }

    private _convertOrgChartToOptions(tree: OrganizationUnit[], level: number, parents: number[]): void {
        //debugger;
        if(tree) {
            tree.forEach(item => {
                let _option: any = {
                    "key": item.id,
                    "text": item.name, "level": level, "id": item.id,
                    'parents': parents,
                    "parentId": item.parentOrganizationUnitId,
                    "item": item
                };
                if (item.organizationUnits && item.organizationUnits.length > 0) {
                    let itemParents = parents.slice(0);
                    _option.parent = true;
                    this._options.push(_option
                    );
                    itemParents.push(item.id);
                    this._convertOrgChartToOptions(item.organizationUnits, level + 1, itemParents.slice(0))
                }
                else {
                    this._options.push(_option);
                }
            })
        }
    }

    public render(): JSX.Element {
        //debugger;
        OrganizationalUnitsForTransferTreeComboBox
        return (
            <div><TreeComboBox
                {...this.props} options={this.state.options}></TreeComboBox>
            </div>
        )
    }
}