diff --git a/src/app/profilemanagement/sustainability/sustainability.ts b/src/app/profilemanagement/sustainability/sustainability.ts index 49d7293..fcb175d 100644 --- a/src/app/profilemanagement/sustainability/sustainability.ts +++ b/src/app/profilemanagement/sustainability/sustainability.ts @@ -1153,7 +1153,7 @@ onDeleteGoal(goal: Goal): void { expiry_date: formatDateTime(expiryDate), // description: this.certificationsForm.value.description, // impact: this.certificationsForm.value.impact, - file_id: 1, // file id from upload + file_id: this.certificationsForm.value.image, };  this.profileService.saveCertification(payload).subscribe({ @@ -1190,8 +1190,9 @@ loadCertifications( certificateTitle: item.mcscd_certtitle, startDate: item.mcscdappliedon, expiryDate: item.mcscd_expiry, - image: 'assets/images/profilemanagement/default_cert.svg', - id: item.memcompsuszonecertdtls_pk + image: item.imagesrc, + id: item.memcompsuszonecertdtls_pk, + mcscd_memcompfiledtls_fk: item.mcscd_memcompfiledtls_fk }));  // THIS is what appends rows @@ -1462,15 +1463,23 @@ onDeleteInitiative(initiative: any): void { certificateTitle: cert.certificateTitle, startDate: cert.startDate ? new Date(cert.startDate) : null, expiryDate: cert.expiryDate ? new Date(cert.expiryDate) : null, + file_id: cert.mcscd_memcompfiledtls_fk, // description: cert.description ?? '', // impact: cert.impact ?? '' });  // 2️⃣ Show image preview if exists - if (cert.image && cert.image !== 'assets/images/profilemanagement/default_cert.svg') { - this.certificationsFilePreview = cert.image; - this.certificationsFile = cert.image; - } + console.log(cert , 'cert') + if (cert?.mcscd_memcompfiledtls_fk) { + const value = cert.mcscd_memcompfiledtls_fk.toString(); + const selectedFiles = Array.isArray(value) ? value : value.split(','); + console.log(selectedFiles, "selectedFiles"); + this.certificationsDrvInput = { + fileMstPk: this.certificationsDrvInput?.fileMstPk ?? 1, + selectedFilesPk: selectedFiles + }; + + }  // 3️⃣ Store PK for update this.editCertificationId = cert.id; diff --git a/src/app/profilemanagement/market-presence/market-presence.ts b/src/app/profilemanagement/market-presence/market-presence.ts index 4338267..c095d98 100644 --- a/src/app/profilemanagement/market-presence/market-presence.ts +++ b/src/app/profilemanagement/market-presence/market-presence.ts @@ -273,9 +273,17 @@ export class MarketPresence implements OnInit, AfterViewInit, OnChanges { } break; case 1: - + // ✅ Ensure countries are loaded before loading global offices if (!this.isGlobalOfficesLoaded) { - this.loadGlobalOffices(); + if (this.country.length === 0) { + // Countries not loaded yet, load them first + this.loadCountries(() => { + this.loadGlobalOffices(); + }); + } else { + // Countries already loaded, just load global offices + this.loadGlobalOffices(); + } this.isGlobalOfficesLoaded = true; } else { if (this.globalOffices?.length > 0) { @@ -812,6 +820,7 @@ export class MarketPresence implements OnInit, AfterViewInit, OnChanges {  // 2. Set PK and Close other panels this.editBranchPk = branch.memcompmplocationdtls_pk; + console.log('[BranchOffice] editBranchOffice - Setting editBranchPk:', this.editBranchPk); this.branchOffices.forEach(b => b.isOpen = false);  // 3. Open this panel (triggers onbranchPanelOpened which handles patching) @@ -1296,6 +1305,13 @@ export class MarketPresence implements OnInit, AfterViewInit, OnChanges { // Helper to copy office address to postal address copyGlobalOfficeToPostalAddress(office: any): void { const form = office.form; +  + // ✅ Don't copy if cities haven't been loaded yet + if (!office.cityes || office.cityes.length === 0) { + console.log(`[GlobalOffice] Skipping copy - cities not loaded yet`); + return; + } +  // Copy address form.patchValue({ postaladdress: form.get('officeaddress')?.value, @@ -1395,11 +1411,16 @@ export class MarketPresence implements OnInit, AfterViewInit, OnChanges { const postalStatePk = Number(officeData.mcmpld_ispostaladdr == 1 ? officeData.mcmpld_statemst_fk : officeData.mcmpld_postalstatemst_fk); const postalCityPk = Number(officeData.mcmpld_ispostaladdr == 1 ? officeData.mcmpld_citymst_fk : officeData.mcmpld_postalcitymst_fk);  + console.log(`[LoadGlobalOffices] Office ${office.id}: countryPk=${countryPk}, statePk=${statePk}, cityPk=${cityPk}`); + if (countryPk) { // 1. Fetch States + console.log(`[LoadGlobalOffices] Fetching states for country ${countryPk}...`); this.profileService.getStates(countryPk).subscribe((sRes: any) => { + console.log(`[LoadGlobalOffices] States response for office ${office.id}:`, sRes); if (sRes.status && Array.isArray(sRes.data)) { office.states = sRes.data.map((s: any) => ({ ...s, value: Number(s.value) })); + console.log(`[LoadGlobalOffices] States loaded for office ${office.id}: ${office.states.length}`); }  // ✅ Patch State ONLY after options are loaded @@ -1408,9 +1429,14 @@ export class MarketPresence implements OnInit, AfterViewInit, OnChanges {  // 2. Fetch Cities (Office) - dependent on State if (statePk) { + console.log(`[LoadGlobalOffices] Fetching cities for state ${statePk}...`); this.profileService.getCities(statePk).subscribe((cRes: any) => { + console.log(`[LoadGlobalOffices] Cities response for office ${office.id}:`, cRes); if (cRes.status && Array.isArray(cRes.data)) { office.cityes = cRes.data.map((c: any) => ({ ...c, value: Number(c.value) })); + console.log(`[LoadGlobalOffices] Cities loaded for office ${office.id}: ${office.cityes.length}`); + } else { + console.error(`[LoadGlobalOffices] Invalid cities response for office ${office.id}`); } // ✅ Patch City ONLY after options are loaded office.form.patchValue({ city: cityPk }, { emitEvent: false }); @@ -1419,6 +1445,7 @@ export class MarketPresence implements OnInit, AfterViewInit, OnChanges { // If terms checked, postal cities = office cities if (officeData.mcmpld_ispostaladdr == 1) { office.postalCities = [...office.cityes]; + console.log(`[LoadGlobalOffices] Postal cities synced for office ${office.id}: ${office.postalCities.length}`); office.form.patchValue({ postalState: statePk, postalCity: cityPk @@ -1427,9 +1454,11 @@ export class MarketPresence implements OnInit, AfterViewInit, OnChanges { } else if (postalStatePk) { // Terms not checked, but we have a postal state // Fetch Postal Cities specifically + console.log(`[LoadGlobalOffices] Fetching postal cities for state ${postalStatePk}...`); this.profileService.getCities(postalStatePk).subscribe((pcRes: any) => { if (pcRes.status && Array.isArray(pcRes.data)) { office.postalCities = pcRes.data.map((c: any) => ({ ...c, value: Number(c.value) })); + console.log(`[LoadGlobalOffices] Postal cities loaded for office ${office.id}: ${office.postalCities.length}`); } office.form.patchValue({ postalState: postalStatePk, @@ -1443,8 +1472,12 @@ export class MarketPresence implements OnInit, AfterViewInit, OnChanges {  this.cdRef.detectChanges(); }); + } else { + console.error(`[LoadGlobalOffices] No statePk for office ${office.id}`); } }); + } else { + console.error(`[LoadGlobalOffices] No countryPk for office ${office.id}`); }  form.disable(); @@ -1668,17 +1701,89 @@ export class MarketPresence implements OnInit, AfterViewInit, OnChanges { this.storage.warningpermision(); return; } +  + console.log(`[GlobalOffice] Edit clicked for office ${office.id}`); + console.log(`[GlobalOffice] Office object:`, office); + console.log(`[GlobalOffice] Cities array:`, office.cityes); + console.log(`[GlobalOffice] Postal cities array:`, office.postalCities); +  this.globalOffices.forEach(o => o.isOpen = false); office.isEditing = true; office.isOpen = true; office.form.enable();  - // ✅ Enforce checkbox logic immediately - console.log(`[GlobalOffice] Edit clicked for ${office.id}. States: ${office.states?.length}, Cities: ${office.cityes?.length}`); - if (office.form.get('terms')?.value) { - console.log(`[GlobalOffice] Terms checked, syncing postal address...`); - this.copyGlobalOfficeToPostalAddress(office); + // ✅ Re-patch state and city from officeData since they might be lost when form was disabled + if (office.officeData) { + const statePk = Number(office.officeData.mcmpld_statemst_fk); + const cityPk = Number(office.officeData.mcmpld_citymst_fk); + const postalStatePk = Number(office.officeData.mcmpld_ispostaladdr == 1 ? office.officeData.mcmpld_statemst_fk : office.officeData.mcmpld_postalstatemst_fk); + const postalCityPk = Number(office.officeData.mcmpld_ispostaladdr == 1 ? office.officeData.mcmpld_citymst_fk : office.officeData.mcmpld_postalcitymst_fk); +  + console.log(`[GlobalOffice] Re-patching from officeData: statePk=${statePk}, cityPk=${cityPk}`); +  + office.form.patchValue({ + state: statePk, + city: cityPk, + postalState: postalStatePk, + postalCity: postalCityPk + }, { emitEvent: false }); + } + + // ✅ ALWAYS fetch cities when editing to ensure they're available + const stateId = office.form.get('state')?.value; + const postalStateId = office.form.get('postalState')?.value; + const termsChecked = office.form.get('terms')?.value; +  + console.log(`[GlobalOffice] State ID: ${stateId}, Postal State ID: ${postalStateId}, Terms: ${termsChecked}`); + console.log(`[GlobalOffice] States: ${office.states?.length}, Cities: ${office.cityes?.length}, Postal Cities: ${office.postalCities?.length}`); +  + if (stateId) { + console.log(`[GlobalOffice] Fetching cities for state ${stateId}...`); + this.profileService.getCities(Number(stateId)).subscribe({ + next: (cRes: any) => { + console.log(`[GlobalOffice] Cities API response:`, cRes); + if (cRes.status && Array.isArray(cRes.data)) { + office.cityes = cRes.data.map((c: any) => ({ ...c, value: Number(c.value) })); + console.log(`[GlobalOffice] Cities fetched and mapped: ${office.cityes.length}`); +  + // If checkbox is checked, sync postal cities + if (termsChecked) { + console.log(`[GlobalOffice] Terms checked, syncing postal address...`); + this.copyGlobalOfficeToPostalAddress(office); + } else if (postalStateId && postalStateId !== stateId) { + // Fetch postal cities separately if different state + console.log(`[GlobalOffice] Fetching postal cities for state ${postalStateId}...`); + this.profileService.getCities(Number(postalStateId)).subscribe({ + next: (pcRes: any) => { + if (pcRes.status && Array.isArray(pcRes.data)) { + office.postalCities = pcRes.data.map((c: any) => ({ ...c, value: Number(c.value) })); + console.log(`[GlobalOffice] Postal cities fetched: ${office.postalCities.length}`); + this.cdRef.detectChanges(); + } + }, + error: (err) => { + console.error(`[GlobalOffice] Error fetching postal cities:`, err); + } + }); + } else if (termsChecked) { + // Same state, just copy + office.postalCities = [...office.cityes]; + } + this.cdRef.detectChanges(); + } else { + console.error(`[GlobalOffice] Invalid cities response:`, cRes); + office.cityes = []; + } + }, + error: (err) => { + console.error(`[GlobalOffice] Error fetching cities:`, err); + office.cityes = []; + } + }); + } else { + console.log(`[GlobalOffice] No state ID found, cannot fetch cities`); } +  this.cdRef.detectChanges(); }  @@ -1752,42 +1857,86 @@ export class MarketPresence implements OnInit, AfterViewInit, OnChanges { onPanelOpened(office: any): void { office.isOpen = true;  - // Load states for this office's country when expanded - // Load states only if not already loaded - const countryPk = office.form.get('country')?.value; - if (countryPk && (!office.states || office.states.length === 0)) { - console.log(`[GlobalOffice] onPanelOpened: Fetching states for ${countryPk}`); - this.profileService.getStates(Number(countryPk)).subscribe((res: any) => { - if (res.status && Array.isArray(res.data)) { - office.states = res.data.map((s: any) => ({ ...s, value: Number(s.value) })); - } else { - office.states = []; - } - this.cdRef.detectChanges(); - - const statePk = office.form.get('state')?.value; - if (statePk) { - // We might need to fetch cities if states were just loaded and we have a state value - // But wait, if we have a state value, cities should have been loaded too? - // Let's just fetch cities if they are empty - this.profileService.getCities(Number(statePk)).subscribe((cityRes: any) => { - if (cityRes.status && Array.isArray(cityRes.data)) { - office.cityes = cityRes.data.map((c: any) => ({ ...c, value: Number(c.value) })); - } else { - office.cityes = []; - } + // ✅ Re-patch state and city from officeData since they might be lost when form was disabled + if (office.officeData) { + const countryPk = Number(office.officeData.mcmpld_countrymst_fk); + const statePk = Number(office.officeData.mcmpld_statemst_fk); + const cityPk = Number(office.officeData.mcmpld_citymst_fk); + const postalStatePk = Number(office.officeData.mcmpld_ispostaladdr == 1 ? office.officeData.mcmpld_statemst_fk : office.officeData.mcmpld_postalstatemst_fk); + const postalCityPk = Number(office.officeData.mcmpld_ispostaladdr == 1 ? office.officeData.mcmpld_citymst_fk : office.officeData.mcmpld_postalcitymst_fk); +  + console.log(`[GlobalOffice] onPanelOpened: Re-patching from officeData: countryPk=${countryPk}, statePk=${statePk}, cityPk=${cityPk}`); + console.log(`[GlobalOffice] onPanelOpened: Postal: postalStatePk=${postalStatePk}, postalCityPk=${postalCityPk}`); +  + // ✅ FIRST: Enable/disable form BEFORE patching values + if (office.isSaved && !office.isEditing) { + console.log(`[GlobalOffice] onPanelOpened: Disabling form (isSaved=${office.isSaved}, isEditing=${office.isEditing})`); + office.form.disable(); // 👁 view only if already saved and not in edit mode + } else { + console.log(`[GlobalOffice] onPanelOpened: Enabling form (isSaved=${office.isSaved}, isEditing=${office.isEditing})`); + office.form.enable(); // Ensure it's enabled for new offices or when editing + } +  + // ✅ Fetch states if not loaded + if (!office.states || office.states.length === 0) { + console.log(`[GlobalOffice] onPanelOpened: Fetching states for country ${countryPk}`); + this.profileService.getStates(countryPk).subscribe((sRes: any) => { + if (sRes.status && Array.isArray(sRes.data)) { + office.states = sRes.data.map((s: any) => ({ ...s, value: Number(s.value) })); + console.log(`[GlobalOffice] onPanelOpened: States loaded: ${office.states.length}`); +  + // Patch state AFTER states are loaded + office.form.patchValue({ state: statePk }, { emitEvent: false }); + console.log(`[GlobalOffice] onPanelOpened: State patched to form: ${statePk}`); this.cdRef.detectChanges(); - }); - } - }); - } else { - // States already loaded, check cities - const statePk = office.form.get('state')?.value; - if (statePk && (!office.cityes || office.cityes.length === 0)) { - console.log(`[GlobalOffice] onPanelOpened: Fetching cities for ${statePk}`); + } + }); + } else { + // States already loaded, just patch + console.log(`[GlobalOffice] onPanelOpened: States already loaded: ${office.states.length}`); + console.log(`[GlobalOffice] onPanelOpened: office.states array:`, office.states.map((s: any) => s.value)); + console.log(`[GlobalOffice] onPanelOpened: Patching state: ${statePk}`); +  + office.form.patchValue({ state: statePk }, { emitEvent: false }); +  + console.log(`[GlobalOffice] onPanelOpened: Form state value after patch:`, office.form.get('state')?.value); + this.cdRef.detectChanges(); + } +  + // Patch city and postal values + office.form.patchValue({ + city: cityPk, + postalState: postalStatePk, + postalCity: postalCityPk + }, { emitEvent: false }); + console.log(`[GlobalOffice] onPanelOpened: City and postal values patched`); +  + // ✅ ALWAYS fetch cities when panel opens to ensure they're available + if (statePk) { + console.log(`[GlobalOffice] onPanelOpened: Fetching cities for state ${statePk}`); this.profileService.getCities(Number(statePk)).subscribe((cityRes: any) => { if (cityRes.status && Array.isArray(cityRes.data)) { office.cityes = cityRes.data.map((c: any) => ({ ...c, value: Number(c.value) })); + console.log(`[GlobalOffice] onPanelOpened: Cities loaded: ${office.cityes.length}`); +  + // Re-patch city AFTER cities are loaded + office.form.patchValue({ city: cityPk }, { emitEvent: false }); + console.log(`[GlobalOffice] onPanelOpened: City re-patched after loading: ${cityPk}`); +  + // ✅ Sync postal cities AFTER cities are loaded + if (offi