import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {AgGridComponent} from '../../../components/ag-grid-component';
import {ICustomer} from '../../../model/entities/customer';
import {AppService} from '../../../services/app.service';
import {Const} from '../../../utils/const';
import {isNull} from 'util';

@Component({
  selector: 'app-customer-list',
  templateUrl: './customer-list.component.html',
  styleUrls: ['./customer-list.component.scss']
})

export class CustomerListComponent extends AgGridComponent implements OnInit {

    @Output() rowIsClicked: EventEmitter<any> = new EventEmitter<any>();
    @Output() createNewCustomer: EventEmitter<boolean> = new EventEmitter<boolean>();

    private customers: ICustomer[];
    public rowClassRules: {};
    public hasSalesRights: boolean;
    public usersById: {};

    constructor(public appService: AppService) {
        super();
    }

    ngOnInit() {
        this.usersById = this.appService.getConfig().vc_user_by_id;
        this.hasSalesRights = this.appService.userHasRole(Const.USER_ROLE_SALES);
        this.customers = [];

        this.defaultColDef = {
            resizable: true,
            sortable: true
        };

        this.columnDefs = [
            {headerName: 'ID', field: 'customer_id', width: 60 },
            {headerName: 'Status', field: 'active', width: 90 },
            {headerName: 'Name', field: 'customer_name' },
            {headerName: 'Name Zusatz', field: 'customer_name_additional' },
            {headerName: 'Alte PLP Kennung', field: 'old_plp_id' },
            {headerName: 'PLP Mediaberater', field: 'v_consultant' },
            {headerName: 'Straße', field: 'street' },
            {headerName: 'Hausnummer', field: 'street_no' },
            {headerName: 'PLZ', field: 'zip' },
            {headerName: 'Ort', field: 'city' },
            {headerName: 'Internetadresse', field: 'url', cellRenderer: this.urlRenderer },
            {headerName: 'Bemerkungen (Allgemein)', field: 'comment' }
        ];

        this.rowSelection = 'single';

        this.rowClassRules = {
            inactive(params) {
                return !params.data.active_state;
            }
        };
    }

    /**
     * Sets data
     */
    public setData(customers: ICustomer[]): void {
        this.customers = customers;
        this.usersById = this.appService.getConfig().vc_user_by_id;
        this.generateEntries();
    }

    /**
     * Generates AG grid entries for customer grid
     */
    public generateEntries(): void {
        this.rowData = [];
        if (!isNull(this.customers)) {
            for (let i = 0; i < this.customers.length; i++) {
                this.rowData.push({
                    customer_id: this.customers[i].id,
                    active_state: this.customers[i].active,
                    active: this.customers[i].active ? 'Aktiv' : 'Inaktiv',
                    customer_name: this.customers[i].name,
                    customer_name_additional: this.customers[i].name_additional,
                    old_plp_id: this.customers[i].old_plp_id,
                    v_consultant: !isNull(this.customers[i].consultant_user_id) ?
                        this.usersById[this.customers[i].consultant_user_id].firstname + ' ' + this.usersById[this.customers[i].consultant_user_id].lastname : null,
                    street: this.customers[i].street,
                    street_no: this.customers[i].street_no,
                    zip: this.customers[i].zip,
                    city: this.customers[i].city,
                    url: this.customers[i].url,
                    comment: this.customers[i].comment,
                });
            }
        }
    }

    /**
     * Row is clicked
     * @param e
     */
    public rowClicked(e: any) {
        this.rowIsClicked.emit(e);
    }

    /**
     * OnClick Create customer
     */
    public createCustomer(): void {
        this.createNewCustomer.emit(true);
    }

}
