import ngModule from '../module';
import Section from 'salix/components/section';

export default class Controller extends Section {
    constructor($element, $) {
        super($element, $);
        this.worker = {companyFk: this.vnConfig.user.companyFk};
        this.$http.get(`WorkerConfigs/findOne`, {field: ['payMethodFk']}).then(res => {
            if (res.data) this.worker.payMethodFk = res.data.payMethodFk;
        });
    }

    onSubmit() {
        if (!this.worker.iban && !this.worker.bankEntityFk) {
            delete this.worker.iban;
            delete this.worker.bankEntityFk;
        }

        return this.$.watcher.submit().then(json => {
            this.$state.go('worker.card.basicData', {id: json.data.id});
        });
    }

    get ibanCountry() {
        if (!this.worker || !this.worker.iban) return false;

        let countryCode = this.worker.iban.substr(0, 2);

        return countryCode;
    }

    autofillBic() {
        if (!this.worker || !this.worker.iban) return;

        let bankEntityId = parseInt(this.worker.iban.substr(4, 4));
        let filter = {where: {id: bankEntityId}};

        this.$http.get(`BankEntities`, {filter}).then(response => {
            const hasData = response.data && response.data[0];

            if (hasData)
                this.worker.bankEntityFk = response.data[0].id;
            else if (!hasData)
                this.worker.bankEntityFk = null;
        });
    }

    generateCodeUser() {
        if (!this.worker.firstName || !this.worker.lastNames) return;

        const totalName = this.worker.firstName.concat(' ' + this.worker.lastNames).toLowerCase();
        const totalNameArray = totalName.split(' ');
        let newCode = '';

        for (let part of totalNameArray)
            newCode += part.charAt(0);

        this.worker.code = newCode.toUpperCase().slice(0, 3);
        this.worker.name = totalNameArray[0] + newCode.slice(1);

        if (!this.worker.companyFk)
            this.worker.companyFk = this.vnConfig.user.companyFk;
    }

    get province() {
        return this._province;
    }

    // Province auto complete
    set province(selection) {
        this._province = selection;

        if (!selection) return;

        const country = selection.country;

        if (!this.worker.countryFk)
            this.worker.countryFk = country.id;
    }

    get town() {
        return this._town;
    }

    // Town auto complete
    set town(selection) {
        this._town = selection;

        if (!selection) return;

        const province = selection.province;
        const country = province.country;
        const postcodes = selection.postcodes;

        if (!this.worker.provinceFk)
            this.worker.provinceFk = province.id;

        if (!this.worker.countryFk)
            this.worker.countryFk = country.id;

        if (postcodes.length === 1)
            this.worker.postcode = postcodes[0].code;
    }

    get postcode() {
        return this._postcode;
    }

    // Postcode auto complete
    set postcode(selection) {
        this._postcode = selection;

        if (!selection) return;

        const town = selection.town;
        const province = town.province;
        const country = province.country;

        if (!this.worker.city)
            this.worker.city = town.name;

        if (!this.worker.provinceFk)
            this.worker.provinceFk = province.id;

        if (!this.worker.countryFk)
            this.worker.countryFk = country.id;
    }

    onResponse(response) {
        this.worker.postcode = response.code;
        this.worker.city = response.city;
        this.worker.provinceFk = response.provinceFk;
    }
}

Controller.$inject = ['$element', '$scope'];

ngModule.vnComponent('vnWorkerCreate', {
    template: require('./index.html'),
    controller: Controller
});