import { Component } from '@angular/core';
import { MatCard, MatCardContent, MatCardTitle } from '@angular/material/card';
import { MatError, MatFormField, MatLabel } from '@angular/material/form-field';
import { MatInput } from '@angular/material/input';
import { MatButton } from '@angular/material/button';
import { ReactiveFormsModule, Validators } from '@angular/forms';
import { NgIf } from '@angular/common';
import { FileUploadButtonComponent } from '../../file-upload-button/file-upload-button.component';
import { ImagePreviewComponent } from '../../image-preview/image-preview.component';
import { TechlifyFormComponentInterface } from '../../base-model';
import * as i0 from "@angular/core";
import * as i1 from "../../core";
import * as i2 from "@angular/forms";
import * as i3 from "../../user";
import * as i4 from "../client.service";
import * as i5 from "@angular/common";
export class CompanyInformationComponent extends TechlifyFormComponentInterface {
    constructor(formValidatorService, formBuilder, currentUserService, clientService, alertService, location) {
        super(formValidatorService);
        this.formBuilder = formBuilder;
        this.currentUserService = currentUserService;
        this.clientService = clientService;
        this.alertService = alertService;
        this.location = location;
        this.errorMessages = {
            name: {
                required: 'The name field is required.'
            }
        };
        this.form = this.formBuilder.group({
            id: [''],
            name: ['', Validators.required],
            email: [''],
            phone: [''],
            address: [''],
            logo: [''],
            letterhead_image: [''],
            tin: ['']
        });
    }
    ngOnInit() {
        this.user = this.currentUserService.getUser();
        const client = this.user?.client;
        if (client) {
            this.form.patchValue({ ...client });
        }
    }
    save() {
        this.form.markAllAsTouched();
        if (this.form.invalid) {
            this.alertService.addAlert('Please check the form for errors.', 'error');
            return;
        }
        const data = { ...this.form.value };
        this.isWorking = true;
        this.clientService.update(data).subscribe({
            next: () => {
                this.isWorking = false;
                // update user
                this.currentUserService.getUserObservable(true).subscribe();
                this.alertService.addAlert('The company information saved successfully!', 'success');
            },
            error: () => (this.isWorking = false)
        });
    }
    onFileUploaded(uploadedFile, field) {
        if (uploadedFile && uploadedFile.length > 0) {
            this.form.get(field)?.patchValue(uploadedFile[0]?.item?.url);
        }
    }
    redirectBack() {
        this.location.back();
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: CompanyInformationComponent, deps: [{ token: i1.FormValidatorService }, { token: i2.FormBuilder }, { token: i3.CurrentUserService }, { token: i4.ClientService }, { token: i1.AlertService }, { token: i5.Location }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: CompanyInformationComponent, isStandalone: true, selector: "app-company-information", usesInheritance: true, ngImport: i0, template: "<div class=\"d-flex justify-content-center align-items-center\">\n  <mat-card style=\"width: 50%\">\n    <mat-card-content>\n      <mat-card-title>Company Information</mat-card-title>\n    </mat-card-content>\n    <mat-card-content class=\"mt-3\">\n      <form\n        [formGroup]=\"form\"\n        class=\"d-flex flex-wrap justify-content-start align-items-center gap-2\"\n        (submit)=\"save()\"\n      >\n        <mat-form-field style=\"width: calc(100%)\">\n          <mat-label>Name</mat-label>\n          <input matInput formControlName=\"name\">\n          <mat-error *ngIf=\"isFieldValid('name')\">\n            {{ getErrorMessage('name') }}\n          </mat-error>\n        </mat-form-field>\n\n        <mat-form-field style=\"width: calc(50% - 0.5rem)\">\n          <mat-label>Email</mat-label>\n          <input matInput formControlName=\"email\">\n        </mat-form-field>\n\n        <mat-form-field style=\"width: calc(50%)\">\n          <mat-label>Phone</mat-label>\n          <input matInput formControlName=\"phone\">\n        </mat-form-field>\n\n        <mat-form-field style=\"width: calc(100%)\">\n          <mat-label>Address</mat-label>\n          <textarea matInput formControlName=\"address\" rows=\"3\"></textarea>\n        </mat-form-field>\n\n        <mat-form-field style=\"width: calc(100%)\">\n          <mat-label>TIN</mat-label>\n          <input matInput formControlName=\"tin\">\n        </mat-form-field>\n\n        <div style=\"width: calc(50% - 0.5rem)\">\n          <p>Logo</p>\n          <app-file-upload-button\n            *ngIf=\"!form?.value?.logo\"\n            [config]=\"{ fileId: 'client-logo-' + this.form.value?.id, accept: 'image/*'}\"\n            (uploaded)=\"onFileUploaded($event, 'logo')\"\n          ></app-file-upload-button>\n          <app-image-preview\n            *ngIf=\"form.value?.logo\"\n            [path]=\"form.value?.logo\"\n            (removed)=\"form.get('logo')?.setValue('')\"\n          ></app-image-preview>\n        </div>\n\n        <div style=\"width: calc(50% - 0.5rem)\">\n          <p>Letterhead Image</p>\n          <app-file-upload-button\n            *ngIf=\"!form.value?.letterhead_image\"\n            [config]=\"{ fileId: 'client-letterhead-' + this.form.value?.id, accept: 'image/*'}\"\n            (uploaded)=\"onFileUploaded($event, 'letterhead_image')\"\n          ></app-file-upload-button>\n          <app-image-preview\n            *ngIf=\"form.value?.letterhead_image\"\n            [path]=\"form.value?.letterhead_image\"\n            (removed)=\"form.get('letterhead_image')?.setValue('')\"\n          ></app-image-preview>\n        </div>\n\n        <div style=\"width: calc(100%)\" class=\"d-flex justify-content-end align-items-center gap-3\">\n          <button [disabled]=\"isWorking\" mat-raised-button color=\"primary\" type=\"submit\">\n            Save\n          </button>\n          <button [disabled]=\"isWorking\" mat-flat-button type=\"button\" (click)=\"redirectBack()\">\n            Cancel\n          </button>\n        </div>\n      </form>\n    </mat-card-content>\n  </mat-card>\n</div>\n", styles: [""], dependencies: [{ kind: "directive", type: MatCardContent, selector: "mat-card-content" }, { kind: "component", type: MatCard, selector: "mat-card", inputs: ["appearance"], exportAs: ["matCard"] }, { kind: "directive", type: MatCardTitle, selector: "mat-card-title, [mat-card-title], [matCardTitle]" }, { kind: "component", type: MatFormField, selector: "mat-form-field", inputs: ["hideRequiredMarker", "color", "floatLabel", "appearance", "subscriptSizing", "hintLabel"], exportAs: ["matFormField"] }, { kind: "directive", type: MatLabel, selector: "mat-label" }, { kind: "directive", type: MatInput, selector: "input[matInput], textarea[matInput], select[matNativeControl],      input[matNativeControl], textarea[matNativeControl]", inputs: ["disabled", "id", "placeholder", "name", "required", "type", "errorStateMatcher", "aria-describedby", "value", "readonly"], exportAs: ["matInput"] }, { kind: "component", type: MatButton, selector: "    button[mat-button], button[mat-raised-button], button[mat-flat-button],    button[mat-stroked-button]  ", exportAs: ["matButton"] }, { kind: "ngmodule", type: ReactiveFormsModule }, { kind: "directive", type: i2.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i2.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i2.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i2.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i2.FormGroupDirective, selector: "[formGroup]", inputs: ["formGroup"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "directive", type: i2.FormControlName, selector: "[formControlName]", inputs: ["formControlName", "disabled", "ngModel"], outputs: ["ngModelChange"] }, { kind: "component", type: FileUploadButtonComponent, selector: "app-file-upload-button", inputs: ["config"], outputs: ["uploaded"] }, { kind: "directive", type: NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: MatError, selector: "mat-error, [matError]", inputs: ["id"] }, { kind: "component", type: ImagePreviewComponent, selector: "app-image-preview", inputs: ["path", "height"], outputs: ["removed"] }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: CompanyInformationComponent, decorators: [{
            type: Component,
            args: [{ selector: 'app-company-information', standalone: true, imports: [
                        MatCardContent,
                        MatCard,
                        MatCardTitle,
                        MatFormField,
                        MatLabel,
                        MatInput,
                        MatButton,
                        ReactiveFormsModule,
                        FileUploadButtonComponent,
                        NgIf,
                        MatError,
                        ImagePreviewComponent,
                    ], template: "<div class=\"d-flex justify-content-center align-items-center\">\n  <mat-card style=\"width: 50%\">\n    <mat-card-content>\n      <mat-card-title>Company Information</mat-card-title>\n    </mat-card-content>\n    <mat-card-content class=\"mt-3\">\n      <form\n        [formGroup]=\"form\"\n        class=\"d-flex flex-wrap justify-content-start align-items-center gap-2\"\n        (submit)=\"save()\"\n      >\n        <mat-form-field style=\"width: calc(100%)\">\n          <mat-label>Name</mat-label>\n          <input matInput formControlName=\"name\">\n          <mat-error *ngIf=\"isFieldValid('name')\">\n            {{ getErrorMessage('name') }}\n          </mat-error>\n        </mat-form-field>\n\n        <mat-form-field style=\"width: calc(50% - 0.5rem)\">\n          <mat-label>Email</mat-label>\n          <input matInput formControlName=\"email\">\n        </mat-form-field>\n\n        <mat-form-field style=\"width: calc(50%)\">\n          <mat-label>Phone</mat-label>\n          <input matInput formControlName=\"phone\">\n        </mat-form-field>\n\n        <mat-form-field style=\"width: calc(100%)\">\n          <mat-label>Address</mat-label>\n          <textarea matInput formControlName=\"address\" rows=\"3\"></textarea>\n        </mat-form-field>\n\n        <mat-form-field style=\"width: calc(100%)\">\n          <mat-label>TIN</mat-label>\n          <input matInput formControlName=\"tin\">\n        </mat-form-field>\n\n        <div style=\"width: calc(50% - 0.5rem)\">\n          <p>Logo</p>\n          <app-file-upload-button\n            *ngIf=\"!form?.value?.logo\"\n            [config]=\"{ fileId: 'client-logo-' + this.form.value?.id, accept: 'image/*'}\"\n            (uploaded)=\"onFileUploaded($event, 'logo')\"\n          ></app-file-upload-button>\n          <app-image-preview\n            *ngIf=\"form.value?.logo\"\n            [path]=\"form.value?.logo\"\n            (removed)=\"form.get('logo')?.setValue('')\"\n          ></app-image-preview>\n        </div>\n\n        <div style=\"width: calc(50% - 0.5rem)\">\n          <p>Letterhead Image</p>\n          <app-file-upload-button\n            *ngIf=\"!form.value?.letterhead_image\"\n            [config]=\"{ fileId: 'client-letterhead-' + this.form.value?.id, accept: 'image/*'}\"\n            (uploaded)=\"onFileUploaded($event, 'letterhead_image')\"\n          ></app-file-upload-button>\n          <app-image-preview\n            *ngIf=\"form.value?.letterhead_image\"\n            [path]=\"form.value?.letterhead_image\"\n            (removed)=\"form.get('letterhead_image')?.setValue('')\"\n          ></app-image-preview>\n        </div>\n\n        <div style=\"width: calc(100%)\" class=\"d-flex justify-content-end align-items-center gap-3\">\n          <button [disabled]=\"isWorking\" mat-raised-button color=\"primary\" type=\"submit\">\n            Save\n          </button>\n          <button [disabled]=\"isWorking\" mat-flat-button type=\"button\" (click)=\"redirectBack()\">\n            Cancel\n          </button>\n        </div>\n      </form>\n    </mat-card-content>\n  </mat-card>\n</div>\n" }]
        }], ctorParameters: () => [{ type: i1.FormValidatorService }, { type: i2.FormBuilder }, { type: i3.CurrentUserService }, { type: i4.ClientService }, { type: i1.AlertService }, { type: i5.Location }] });
//# sourceMappingURL=data:application/json;base64, 