"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var forms_1 = require("@angular/forms");
var DailogCreateCustomerComponent = /** @class */ (function () {
    function DailogCreateCustomerComponent(localStorageService, customerService) {
        this.localStorageService = localStorageService;
        this.customerService = customerService;
        this.createCustomer = new core_1.EventEmitter();
        this.ignoreCustomerForm = new core_1.EventEmitter();
        this.showCustomerCreateSpinner = new core_1.EventEmitter();
        this.unsavedCustomerForm = false;
        this.showSpinner = false;
        this.showMsg = false;
        this.status = '';
        this.message = '';
        this.customerModel = {
            In_CustomerInfo: {
                DealerID: '',
                MD__c: '',
                Owner: '',
                Personal_UCID__c: '',
                RecordType: '',
                Dealer_GC_Code__c: "GC0011430",
                Dealer_GS_Code__c: "GS0014313",
                StouchUniqueId: '',
                Address_Type__c: '',
                District__c: '',
                Gender__c: '',
                General_Personal_Info_Mandatory__c: '',
                LastName: '',
                MBK_Data_Source__c: '',
                PersonBirthdate: '',
                PersonEmail: '',
                PersonHomePhone: '',
                PersonMailingAddress: '',
                PersonMailingAddressline2: '',
                PersonMailingCity: '',
                PersonMailingPostalCode: '',
                PersonMailingState: '',
                PersonMobilePhone: '',
                Person_Agree_Test_Driving_Optional__c: '',
                Person_Info_Custom_Service_Optional__c: '',
                Targeted_Advert_Info_Optional_STCCC__c: '',
                Work_Phone__c: '',
                Town: ''
            }
        };
        this.reqheaderdataLeads = {
            'x-api-key': '1be8403f-fe61-42a6-ad5f-90ff196e41ef'
        };
    }
    DailogCreateCustomerComponent.prototype.ngOnInit = function () {
        this.customerForm = new forms_1.FormGroup({
            dataSource: new forms_1.FormControl('', forms_1.Validators.required),
            email: new forms_1.FormControl('', [forms_1.Validators.required, this.patternValidator(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)]),
            lastName: new forms_1.FormControl('', forms_1.Validators.required),
            gender: new forms_1.FormControl('male', forms_1.Validators.required),
            countryCode: new forms_1.FormControl('+61', forms_1.Validators.required),
            cityCode: new forms_1.FormControl('', forms_1.Validators.required),
            areaCode: new forms_1.FormControl('', forms_1.Validators.required),
            number: new forms_1.FormControl('', forms_1.Validators.required),
            state: new forms_1.FormControl('', forms_1.Validators.required)
        });
        this.getCustomerDetailData();
    };
    DailogCreateCustomerComponent.prototype.ngOnChanges = function (changes) {
        if (changes.customerLabels) {
            this.customerLabels = changes.customerLabels.currentValue;
        }
    };
    DailogCreateCustomerComponent.prototype.patternValidator = function (regexp) {
        return function (control) {
            var value = control.value;
            if (value === '') {
                return null;
            }
            return !regexp.test(value) ? { 'patternInvalid': { regexp: regexp } } : null;
        };
    };
    DailogCreateCustomerComponent.prototype.validateAllFields = function (formGroup) {
        var _this = this;
        Object.keys(formGroup.controls).forEach(function (field) {
            var control = formGroup.get(field);
            if (control instanceof forms_1.FormControl) {
                control.markAsTouched({ onlySelf: true });
            }
            else if (control instanceof forms_1.FormGroup) {
                _this.validateAllFields(control);
            }
        });
    };
    DailogCreateCustomerComponent.prototype.getCustomerDetailData = function () {
        this.customerDetailData = this.localStorageService.getLocalStorageData("customerData");
        console.log("getCustomerDetailData", this.customerDetailData);
    };
    DailogCreateCustomerComponent.prototype.onCustomerCreateFormSubmit = function () {
        var _this = this;
        var obj = this.customerForm.value;
        this.validateAllFields(this.customerForm);
        this.customerModel.In_CustomerInfo.MBK_Data_Source__c = obj.dataSource;
        this.customerModel.In_CustomerInfo.PersonEmail = obj.email;
        this.customerModel.In_CustomerInfo.LastName = obj.lastName;
        this.customerModel.In_CustomerInfo.Gender__c = obj.gender;
        this.customerModel.In_CustomerInfo.PersonMobilePhone = obj.countryCode + obj.cityCode + obj.areaCode + obj.number;
        this.customerModel.In_CustomerInfo.PersonMailingState = obj.state;
        this.customerModel.In_CustomerInfo.Personal_UCID__c = 'KEC000441119000';
        if (this.customerForm.valid) {
            this.unsavedCustomerForm = false;
            this.showSpinner = true;
            var body = this.customerModel;
            this.customerService.postCustomer(body, this.reqheaderdataLeads).subscribe(function (data) {
                _this.showSpinner = false;
                _this.ignoreChanges();
            }, function (error) {
                _this.status = 'error';
                _this.message = 'Customer creation failed.';
            });
        }
    };
    DailogCreateCustomerComponent.prototype.goBackToCustomerSearch = function () {
        if (this.customerForm.dirty) {
            this.unsavedCustomerForm = true;
        }
        else {
            this.ignoreChanges();
        }
    };
    DailogCreateCustomerComponent.prototype.cancelChanges = function () {
        this.unsavedCustomerForm = false;
    };
    DailogCreateCustomerComponent.prototype.ignoreChanges = function () {
        this.ignoreCustomerForm.emit(true);
    };
    __decorate([
        core_1.Output()
    ], DailogCreateCustomerComponent.prototype, "createCustomer", void 0);
    __decorate([
        core_1.Output()
    ], DailogCreateCustomerComponent.prototype, "ignoreCustomerForm", void 0);
    __decorate([
        core_1.Output()
    ], DailogCreateCustomerComponent.prototype, "showCustomerCreateSpinner", void 0);
    __decorate([
        core_1.Input()
    ], DailogCreateCustomerComponent.prototype, "customerLabels", void 0);
    DailogCreateCustomerComponent = __decorate([
        core_1.Component({
            selector: 'app-dailog-create-customer',
            templateUrl: './dailog-create-customer.component.html',
            styleUrls: ['./dailog-create-customer.component.scss']
        })
    ], DailogCreateCustomerComponent);
    return DailogCreateCustomerComponent;
}());
exports.DailogCreateCustomerComponent = DailogCreateCustomerComponent;
//# sourceMappingURL=dailog-create-customer.component.js.map