import { Component, OnInit } from "@angular/core";
import { UserService } from "src/app/services/[Link]";
import { Router } from "@angular/router";
import { CommerceService } from "src/app/services/[Link]";
import { ModalController } from "@ionic/angular";
import { CommerceModalPage } from "../commerce-modal/[Link]"; //
Adjust path as needed
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { UpdateCommerceModalPage } from "../update-commerce-modal/update-commerce-
[Link]"; // Adjust path as needed
import { CommerceDataService } from "src/app/services/[Link]"; // Adjust
path as needed
@Component({
selector: "app-commerces",
templateUrl: "./[Link]",
styleUrls: ["./[Link]"],
})
export class CommercesPage implements OnInit {
firstCommerce: any;
noCommerceMessage!: string;
commerceForm!: FormGroup;
villes: any[] = [];
villeName: string | null = null;
commerces: any[] = []; // Define commerces property
selectedCommerceId: number | null = null;
constructor(
private formBuilder: FormBuilder,
private userService: UserService,
private router: Router,
private commerceService: CommerceService,
private modalController: ModalController,
private commerceDataService: CommerceDataService
) {}
async ngOnInit() {
try {
[Link] = [Link]({
commercename: ["", [Link]],
services: ["", [Link]],
image_commerce: [""], // Assuming this field is optional
ville_id: [""],
// Add more form controls as needed for other fields
});
[Link] = await [Link]();
// Fetch businessOwnerId
const businessOwnerId = await [Link]();
if (businessOwnerId !== null) {
const commerces1 =
await [Link](
businessOwnerId
);
if ([Link] === 0) {
// No commerces belonging to the current user
} else {
const commerces =
await
[Link](
businessOwnerId
);
// Sort the commerces array by ID in ascending order
[Link]((a, b) => [Link] - [Link]);
// Now the commerces array is sorted by ID
[Link] = commerces;
[Link]("commerces", [Link]);
// Check if commerces array is empty
if ([Link] === 0) {
// If commerces array is empty, lock the app and navigate to '/villes-commerces'
[Link]();
// [Link](['/villes-commerces']);
} else {
// Call loadFirstCommerce method with the fetched commerces
[Link]([Link]);
}
}
}
} catch (error) {
[Link]("Error loading commerces:", error);
}
}
async loadFirstCommerce(commerces: any[]) {
try {
// Check if commerces array is not empty
if ([Link] > 0) {
[Link] = commerces[0];
[Link] = [Link];
// Fetch the ville name based on the ville_id of the first commerce
const villeId = [Link].ville_id;
const villeName = await [Link](
villeId
);
[Link]({
commercename: [Link],
services: [Link],
image_commerce: [Link].image_commerce || "",
business_owner_id: [Link].business_owner_id,
ville_id: villeName,
// Patch other form controls with corresponding commerce details
});
} else {
[Link] = "No commerce for the current business owner.";
}
} catch (error) {
[Link]("Error loading first commerce:", error);
}
}
lockApp() {
[Link](["/villes-commerces"]);
}
async openCommerceModal() {
try {
const businessOwnerId = await [Link]();
const modal = await [Link]({
component: CommerceModalPage,
componentProps: {
businessOwnerId: businessOwnerId,
},
});
// Handle modal dismissal if needed
[Link]().then((result) => {
[Link]("Modal dismissed:", result);
if ([Link] === "confirm") {
// Handle confirmation
[Link]("Modal dismissed with confirm role");
// For example, refresh commerce data
[Link]();
// Update commerces data after modal is dismissed
[Link]([Link]);
} else if ([Link] === "cancel") {
// Handle cancellation
[Link]("Modal dismissed with cancel role");
} else {
// Handle other dismissals
[Link](
"Modal dismissed with unexpected role:",
[Link]
);
}
});
await [Link]();
} catch (error) {
[Link]("Error opening commerce modal:", error);
}
}
// Example method to refresh commerce data after adding a new commerce
async refreshCommerceData() {
try {
const businessOwnerId = await [Link]();
if (businessOwnerId) {
const commerces =
await
[Link](
businessOwnerId
);
// Sort the commerces array by ID in ascending order
[Link]((a, b) => [Link] - [Link]);
// Now the commerces array is sorted by ID
[Link] = commerces;
// Set firstCommerce to the last item in the commerces array
if ([Link] > 0) {
[Link] = [Link][[Link] - 1];
// Repopulate form fields with updated data
[Link]([Link]);
[Link]([Link]);
}
}
} catch (error) {
[Link]("Error refreshing commerce data:", error);
// Handle error as needed
}
}
async refreshCommerceDataUp() {
try {
if ([Link]) {
const commerce = await [Link](
[Link]
);
if (!commerce) {
// If the commerce is not found, it means it has been deleted
// Clear the selected commerce ID and reset the form
[Link] = null;
[Link]();
} else {
// If the commerce still exists, update the form with its details
[Link](commerce);
}
[Link]([Link]);
}
} catch (error) {
[Link]("Error refreshing commerce data:", error);
// Handle error as needed
}
}
async updateVilleName(villeId: number) {
[Link] = await [Link](villeId);
}
async loadCommerceDetails(commerce: any) {
[Link] = [Link];
[Link] = commerce;
const villeId = [Link].ville_id;
const villeName = await [Link](villeId);
// Populate form with details of the selected commerce
[Link]({
commercename: [Link],
services: [Link],
image_commerce: commerce.image_commerce || "",
ville_id: villeName,
// Populate other form controls as needed
});
}
async openUpdateCommerceModal() {
try {
// Check if selectedCommerceId is defined
if (![Link]) {
[Link]("No commerce selected.");
return;
}
// Fetch the commerce corresponding to the selectedCommerceId
const selectedCommerce = await [Link](
[Link]
);
// Open the modal with the UpdateCommerceModalPage component
const modal = await [Link]({
component: UpdateCommerceModalPage,
componentProps: {
commerce: selectedCommerce, // Pass the selected commerce as props
},
});
// Handle modal dismissal if needed
[Link]().then((result) => {
[Link]("Modal dismissed:", result);
if ([Link] === "confirm") {
// Handle confirmation
[Link]("Modal dismissed with confirm role");
// For example, refresh commerce data
[Link]();
// Update commerces data after modal is dismissed
[Link]([Link]);
} else if ([Link] === "cancel") {
// Handle cancellation
[Link]("Modal dismissed with cancel role");
} else if ([Link] === "deleteConfirm") {
// Handle delete confirmation
[Link]("Modal dismissed with deleteConfirm role");
// Refresh commerce data specifically for delete confirmation
[Link]([Link]);
[Link]([Link]);
} else {
// Handle other dismissals
[Link](
"Modal dismissed with unexpected role:",
[Link]
);
}
});
// Present the modal
await [Link]();
} catch (error) {
[Link]("Error opening update commerce modal:", error);
}
}
// Method to refresh commerce data specifically for delete confirmation
refreshCommerceDataDel(deletedCommerceId: number) {
try {
[Link]("Refreshing commerce data for delete confirmation");
// Remove the deleted commerce from the commerces list
[Link] = [Link](
(commerce) => [Link] !== deletedCommerceId
);
// Check if there are still commerces in the array
if ([Link] > 0) {
// Set firstCommerce to the last commerce in the array
[Link] = [Link][[Link] - 1];
// Update selectedCommerceId
[Link] = [Link];
// Populate form with details of the last commerce
[Link]([Link]);
} else {
// If there are no more commerces, reset the form and display a message
[Link] = null;
[Link] = null;
[Link]();
[Link] = "No commerce for the current business owner.";
}
// After updating the commerces list and firstCommerce, you might want to perform
additional actions
// For example, update UI, trigger any side effects, etc.
// You can also call other methods or services as needed here
} catch (error) {
[Link](
"Error refreshing commerce data for delete confirmation:",
error
);
// Handle error as needed
}
}
}