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

class Controller extends Section {
    constructor($element, $) {
        super($element, $);
        this.dms = {
            files: [],
            hasFile: false,
            hasFileAttached: false
        };
    }

    get client() {
        return this._client;
    }

    set client(value) {
        this._client = value;

        if (value) {
            this.setDefaultParams();
            this.getAllowedContentTypes();
        }
    }

    getAllowedContentTypes() {
        this.$http.get('DmsContainers/allowedContentTypes').then(res => {
            const contentTypes = res.data.join(', ');
            this.allowedContentTypes = contentTypes;
        });
    }

    get contentTypesInfo() {
        return this.$t('ContentTypesInfo', {
            allowedContentTypes: this.allowedContentTypes
        });
    }

    setDefaultParams() {
        const params = {filter: {
            where: {code: 'paymentsLaw'}
        }};
        this.$http.get('DmsTypes/findOne', {params}).then(res => {
            const dmsType = res.data && res.data;
            const companyId = this.vnConfig.companyFk;
            const warehouseId = this.vnConfig.warehouseFk;
            const defaultParams = {
                reference: this.client.id,
                warehouseId: warehouseId,
                companyId: companyId,
                dmsTypeId: dmsType.id,
                description: this.$t('ClientFileDescription', {
                    dmsTypeName: dmsType.name,
                    clientId: this.client.id,
                    clientName: this.client.name
                }).toUpperCase()
            };

            this.dms = Object.assign(this.dms, defaultParams);
        });
    }

    onSubmit() {
        const query = `clients/${this.client.id}/uploadFile`;
        const options = {
            method: 'POST',
            url: query,
            params: this.dms,
            headers: {
                'Content-Type': undefined
            },
            transformRequest: files => {
                const formData = new FormData();

                for (let i = 0; i < files.length; i++)
                    formData.append(files[i].name, files[i]);

                return formData;
            },
            data: this.dms.files
        };
        this.$http(options).then(res => {
            if (res) {
                this.vnApp.showSuccess(this.$t('Data saved!'));
                this.$.watcher.updateOriginalData();
                this.$state.go('client.card.dms.index');
            }
        });
    }

    onFileChange(files) {
        let hasFileAttached = false;

        if (files.length > 0)
            hasFileAttached = true;

        this.$.$applyAsync(() => {
            this.dms.hasFileAttached = hasFileAttached;
        });
    }
}

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

ngModule.vnComponent('vnClientDmsCreate', {
    template: require('./index.html'),
    controller: Controller,
    bindings: {
        client: '<'
    }
});