import flet as ft
import datetime
import requests
import time
from pages.archive_in_page import ArchiveInPage
from config import API_BASE_URL

class OrdersInPage:
    def __init__(self, page: ft.Page, dashboard):
        self.page = page
        self.dashboard = dashboard

        self.selected_client_id = None

        self.all_clients = self.get_all_clients()
        self.filtered_clients = self.all_clients.copy()
        self.client_search_field = ft.TextField(label="Search Clients...", on_change=self.on_searching_client)
        self.client_results = ft.Column(
            spacing=10,
            controls=[
                ft.Container(
                    content=ft.Row(
                        controls=[ft.Text(client["name"], expand=True)],
                        expand=True
                    ),
                    bgcolor=ft.Colors.BLUE_100 if client["id"] == self.selected_client_id else ft.Colors.BLUE_50,
                    padding=10,
                    ink=True,
                    on_click=lambda e, c=client: self.on_client_selected(e, c),
                    border=ft.border.all(1, ft.Colors.GREY_300),
                    border_radius=10,
                    expand=True
                )
                for client in self.all_clients
            ]
        )

        self.all_addresses = self.get_all_addresses()
        self.selected_loading_address_id = None
        self.filtered_addresses = self.all_addresses.copy()
        self.loading_address_search_field = ft.TextField(label="Search Loading Address...", on_change=self.on_searching_loading_address)
        self.loading_address_results = ft.Column(
            spacing=10,
            controls=[
                ft.Container(
                    content=ft.Row(
                        controls=[
                            ft.Text(address["name"], expand=True),
                            ft.IconButton(icon=ft.Icons.LOCATION_PIN, on_click=lambda e, t=address: self.on_location_btn_click(t))
                        ],
                        expand=True
                    ),
                    bgcolor=ft.Colors.BLUE_50,
                    padding=10,
                    ink=True,
                    on_click=lambda e, t=address: self.on_loading_address_selected(e, t),
                    border=ft.border.all(1, ft.Colors.GREY_300),
                    border_radius=10,
                    expand=True
                )
                for address in self.all_addresses
            ]
        )

        self.loading_informations = ft.TextField(
            label="Loading Instructions",
            min_lines=3,
            max_lines=7,
            multiline=True,
            input_filter=ft.InputFilter(
                allow=True,
                regex_string=r"^[\x20-\x7E]*$", 
                replacement_string=""
            ),
        )

        self.loading_date = ft.TextField(label="Date", expand=True, read_only=True)

        self.loading_hour = ft.TextField(label="Hour", expand=True, read_only=True)

        self.loading = ft.ListView(
            spacing=10,
        )

        self.selected_unloading_address_id = None
        self.filtered_addresses_ul = self.all_addresses.copy()
        self.unloading_address_search_field = ft.TextField(label="Search Unloading Address...", on_change=self.on_searching_unloading_address)
        self.unloading_address_results = ft.Column(
            spacing=10,
            controls=[
                ft.Container(
                    content=ft.Row(
                        controls=[
                            ft.Text(address["name"], expand=True),
                            ft.IconButton(icon=ft.Icons.LOCATION_PIN, on_click=lambda e, t=address: self.on_location_btn_click(t))
                        ],
                        expand=True
                    ),
                    bgcolor=ft.Colors.BLUE_50,
                    padding=10,
                    ink=True,
                    on_click=lambda e, t=address: self.on_unloading_address_selected(e, t),
                    border=ft.border.all(1, ft.Colors.GREY_300),
                    border_radius=10,
                    expand=True
                )
                for address in self.all_addresses
            ]
        )

        self.unloading_informations = ft.TextField(
            label="Unloading Instructions",
            min_lines=3,
            max_lines=7,
            multiline=True,
            input_filter=ft.InputFilter(
                allow=True,
                regex_string=r"^[\x20-\x7E]*$", 
                replacement_string=""
            ),
        )

        self.unloading_date = ft.TextField(label="Date", expand=True, read_only=True)

        self.unloading_hour = ft.TextField(label="Hour", expand=True, read_only=True)

        self.unloading = ft.ListView(
            spacing=10,
        )
        
        self.ldm_quantity = ft.TextField( 
            value='13.6',
            expand=True, 
            keyboard_type=ft.KeyboardType.NUMBER, 
            input_filter=ft.InputFilter(allow=True, regex_string=r"^[0-9]*\.?[0-9]*$", replacement_string="")
        )

        self.kg_quantity = ft.TextField(
            expand=True, 
            keyboard_type=ft.KeyboardType.NUMBER, 
            input_filter=ft.InputFilter(allow=True, regex_string=r"^[0-9]*\.?[0-9]*$", replacement_string="")
        )

        self.track_reg_number = ft.TextField(
            label="Track Reg. Number",
            input_filter=ft.InputFilter(
                allow=True,
                regex_string=r"^[\x20-\x7E]*$", 
                replacement_string=""
            ),
        )

        self.trailer_reg_number = ft.TextField(
            label="Trailer Reg. Number",
            input_filter=ft.InputFilter(
                allow=True,
                regex_string=r"^[\x20-\x7E]*$", 
                replacement_string=""
            ),
        )

        self.received_price = ft.TextField(
            label="Price Received",
            keyboard_type=ft.KeyboardType.NUMBER, 
            input_filter=ft.InputFilter(allow=True, regex_string=r"^[0-9]*\.?[0-9]*$", replacement_string="")
        )
        self.expenses = ft.TextField(
            label="Expenses",
            keyboard_type=ft.KeyboardType.NUMBER, 
            input_filter=ft.InputFilter(allow=True, regex_string=r"^[0-9]*\.?[0-9]*$", replacement_string="")
        )

        self.error_message = ft.Text(color = ft.Colors.RED)

        self.loading_query = []
        self.loading_error_message = ft.Text(color= ft.Colors.RED)

        self.unloading_query = []
        self.unloading_error_message = ft.Text(color= ft.Colors.RED)

        self.product_description= ft.TextField(
            label="Description",
            multiline=True,
            min_lines=3,
            max_lines=5,
            input_filter=ft.InputFilter(
                allow=True,
                regex_string=r"^[\x20-\x7E]*$", 
                replacement_string=""
            ),
        )

        self.order_number = ft.TextField(
            label="Order Number",
            input_filter=ft.InputFilter(
                allow=True,
                regex_string=r"^[\x20-\x7E]*$", 
                replacement_string=""
            ),
        )

        self.file_picker = ft.FilePicker(on_result=self.on_file_result)
        self.page.overlay.append(self.file_picker)

        # --- Persistent date & time pickers (must be on page.overlay) ---
        self.loading_date_picker = ft.DatePicker(
            first_date=datetime.datetime(year=2000, month=10, day=1),
            last_date=datetime.datetime(year=2025, month=10, day=1),
            on_change=self.on_loading_date_click,
        )
        self.unloading_date_picker = ft.DatePicker(
            first_date=datetime.datetime(year=2000, month=10, day=1),
            last_date=datetime.datetime(year=2025, month=10, day=1),
            on_change=self.on_unloading_date_click,
        )
        self.loading_time_picker_start = ft.TimePicker(
            confirm_text="Confirm",
            error_invalid_text="Time out of range",
            help_text="Pick your time slot",
            on_change=self.on_loading_hour_click,
            time_picker_entry_mode=ft.TimePickerEntryMode.INPUT_ONLY,
        )
        self.loading_time_picker_end = ft.TimePicker(
            confirm_text="Confirm",
            error_invalid_text="Time out of range",
            help_text="Pick your time slot",
            on_change=self.on_loading_hour_click,
            time_picker_entry_mode=ft.TimePickerEntryMode.INPUT_ONLY,
        )
        self.unloading_time_picker_start = ft.TimePicker(
            confirm_text="Confirm",
            error_invalid_text="Time out of range",
            help_text="Pick your time slot",
            on_change=self.on_unloading_hour_click,
            time_picker_entry_mode=ft.TimePickerEntryMode.INPUT_ONLY,
        )
        self.unloading_time_picker_end = ft.TimePicker(
            confirm_text="Confirm",
            error_invalid_text="Time out of range",
            help_text="Pick your time slot",
            on_change=self.on_unloading_hour_click,
            time_picker_entry_mode=ft.TimePickerEntryMode.INPUT_ONLY,
        )
        # Attach to overlay
        self.page.overlay.extend([
            self.loading_date_picker,
            self.unloading_date_picker,
            self.loading_time_picker_start,
            self.loading_time_picker_end,
            self.unloading_time_picker_start,
            self.unloading_time_picker_end,
        ])
        #self.page.update()

        self.upload_order_btn = ft.Button(
            "Upload",
            icon=ft.Icons.UPLOAD_FILE,
            width=150,
            on_click=self.on_upload_document_btn_click
        )

        self.filename = ft.Text()

        self.currency = ft.Dropdown(
            editable=True,
            label="Currency",
            options=self.get_currency(),
            value="EURO",
        )

    def get_currency(self):
        token = self.page.client_storage.get("token")
        headers = {"Authorization": f"Bearer {token}"}
        response = requests.get(f"{API_BASE_URL}/currency/", headers=headers)
        currency_list = response.json() if response.status_code == 200 else []
         
        options = []
        for currency in currency_list:
            options.append(
                ft.DropdownOption(
                    key=currency['name'],
                    content=ft.Text(
                        value=currency['name'],
                    ),
                )
            )
        return options
        
    def _open_date_picker(self, picker):
        # Works on both newer and older Flet
        if hasattr(picker, "pick_date"):
            picker.pick_date()
        else:
            picker.open = True
            self.page.update()

    def _open_time_picker(self, picker):
        # Works on both newer and older Flet
        if hasattr(picker, "pick_time"):
            picker.pick_time()
        else:
            picker.open = True
            self.page.update()

        

    def on_upload_document_btn_click(self, e):
        # Ensure FilePicker is attached to the page (Portainer/Flet sometimes reuses page objects)
        if self.file_picker not in getattr(self.page.overlay, "controls", []):
            self.page.overlay.append(self.file_picker)
            self.page.update()
        self.file_picker.pick_files(
            allow_multiple=False,
            allowed_extensions=["png", "jpg", "jpeg", "pdf"]
        )

    def on_file_result(self, e: ft.FilePickerResultEvent):
        if not e.files:
            return
        file = e.files[0]
        # Build a safe filename: order_in_YYYYmmdd_HHMMSS_userid_originalname
        timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
        user_id = self.page.session.get("user_id")
        new_filename = f"order_in_{timestamp}_{user_id}_{file.name}"
        upload_url = self.page.get_upload_url(new_filename, 1000)
        self.file_picker.upload([ft.FilePickerUploadFile(file.name, upload_url=upload_url)])
        self.filename.value = new_filename
        self.filename.update()

        #import os
        #import shutil

        #source_path = os.path.join("uploads", new_filename)
        # destination_path = os.path.join("assets/images", new_filename)
        # try:
        #     time.sleep(2)
        #     shutil.move(source_path, destination_path)
        #     self.logo.src = f"images/{new_filename}"
        #     self.logo.update()
        #     self.logo_field.value = new_filename
        #     self.page.update()
        #     self.page.client_storage.set("logo_filename", new_filename)
        #     self.dashboard.logo.src = f"images/{new_filename}"
        #     self.dashboard.logo.update()
        # except Exception as err:
        #    self.message.value = f"Upload error: {err}"
        #    self.message.color = ft.Colors.RED
        #    self.page.update()


    def on_location_btn_click(self, destination):
        query = destination["address"].replace(", ", "+")
        maps_url = f"https://www.google.com/maps/search/?api=1&query={query}"
        self.page.launch_url(maps_url)

    def on_searching_client(self, e):
        query = e.control.value.lower()
        self.filtered_clients = [client for client in self.all_clients if query in client["name"].lower()]
        self.update_client_list(self.filtered_clients)

    def update_client_list(self, clients):
        self.client_results.controls.clear()
        for client in clients:
            self.client_results.controls.append(
                ft.Container(
                    content=ft.Row(
                        controls=[ft.Text(client["name"], expand=True)],
                        expand=True
                    ),
                    bgcolor=ft.Colors.BLUE_100 if client["id"] == self.selected_client_id else ft.Colors.BLUE_50,
                    padding=10,
                    ink=True,
                    on_click=lambda e, c=client: self.on_client_selected(e, c),
                    border=ft.border.all(1, ft.Colors.GREY_300),
                    border_radius=10,
                    expand=True
                )
            )
        self.client_results.update()

    def on_client_selected(self, e, client):
        self.selected_client_id = client["id"]
        self.client_search_field.value = client["name"]
        self.client_search_field.update()
        self.update_client_list(self.filtered_clients)

    def on_searching_loading_address(self, e):
        query = e.control.value.lower()
        self.filtered_addresses = [a for a in self.all_addresses if query in a["name"].lower()]
        self.update_loading_addresses_list(self.filtered_addresses)

    def update_loading_addresses_list(self, addresses):
        self.loading_address_results.controls.clear()
        for address in addresses:
            self.loading_address_results.controls.append(
                ft.Container(
                    content=ft.Row(
                        controls=[ft.Text(address["name"], expand=True)],
                        expand=True
                    ),
                    bgcolor=ft.Colors.BLUE_100 if address["id"] == getattr(self, "selected_loading_address_id", None) else ft.Colors.BLUE_50,
                    padding=10,
                    ink=True,
                    on_click=lambda e,a=address: self.on_loading_address_selected(e, a),
                    border=ft.border.all(1, ft.Colors.GREY_300),
                    border_radius=10,
                    expand=True
                )
            )
        self.loading_address_results.update()

    def on_loading_address_selected(self, e, address):
        self.selected_loading_address_id = address["id"]
        self.loading_address_search_field.value = address["name"]
        self.loading_address_search_field.update()
        self.update_loading_addresses_list(self.filtered_addresses)

    def on_loading_date_click(self, e):
        self.loading_date.value = e.control.value.strftime('%m/%d/%Y')
        self.loading_date.update()
        #print(self.loading_date.value)

    def on_loading_hour_click(self, e):
        if not self.loading_hour.value:
            self.loading_hour.value = str(e.control.value)
        else:
            self.loading_hour.value += f' - {e.control.value}'
        self.loading_hour.update()
        #print(self.loading_hour.value)

    def on_reset_loading_hour_btn_click(self, e):
        self.loading_hour.value = None
        self.loading_hour.update()

    def add_loading_point_btn_click(self, e):
        adr = None
        name = None
        #print(self.selected_loading_address_id)
        for _address in self.all_addresses:
            if _address['id'] == self.selected_loading_address_id:
                adr = ''
                street_and_number = _address["address"].split(" %")[0]
                postal_code = _address["address"].split(" %")[1]
                city = _address["address"].split(" %")[2]
                region_county = _address["address"].split(" %")[3]
                country = _address["address"].split(" %")[4]
                if len(street_and_number) > 0:
                    adr += street_and_number +', '
                if len(postal_code) > 0:
                    adr += postal_code +', '
                if len(city) > 0:
                    adr += city +', '
                if len(region_county) > 0:
                    adr += region_county +', '
                if len(country) > 0:
                    adr += country
                name = _address['name']
                #print(adr)
                #print(_address['address'])
                
        loading_informatins = self.loading_informations.value
        date = self.loading_date.value
        hour = self.loading_hour.value

        #create loading list
        address = {
                'loading_address_id': self.selected_loading_address_id,
                'loading_address_name': name,
                'loading_address': adr,
                'loading_informatins': loading_informatins,
                'loading_date': date,
                'loading_hour': hour
            }
        #print(address)

        if self.selected_loading_address_id == None:
            self.loading_error_message.value = "Please select loading point!"
            self.loading_error_message.update()
            return
        # if self.loading_informations.value == None or len(self.loading_informations.value) == 0:
        #     self.loading_error_message.value = "Add loading informations!"
        #     self.loading_error_message.update()
        #     return
        if self.loading_date.value == None or len(str(self.loading_date.value)) == 0:
            self.loading_error_message.value = "Add loading date!"
            self.loading_error_message.update()
            return
        # if self.loading_hour.value == None or len(str(self.loading_hour.value)) == 0:
        #     self.loading_error_message.value = "Add loading hour!"
        #     self.loading_error_message.update()
        #     return

        if self.selected_loading_address_id:
            self.loading_query.append(address)
            self.loading.controls.clear()
            self.loading.controls = self.create_loading_list(self.loading_query, self.on_delete_loading_address_btn_click)
            self.loading.update()       

            #reset to default
            self.selected_loading_address_id = None
            self.loading_informations.value = None
            self.loading_informations.update()
            self.loading_date.value = None
            self.loading_date.update()
            self.loading_hour.value = None
            self.loading_hour.update()
            self.loading_error_message.value = None
            self.loading_error_message.update()
        else:
            self.loading_error_message.value = "All fields of the loading address are required."
            self.loading_error_message.update()

    def on_searching_unloading_address(self, e):
        query = e.control.value.lower()
        self.filtered_addresses_ul = [a for a in self.all_addresses if query in a["name"].lower()]
        self.update_unloading_addresses_list(self.filtered_addresses_ul)

    def update_unloading_addresses_list(self, addresses):
        self.unloading_address_results.controls.clear()
        for address in addresses:
            self.unloading_address_results.controls.append(
                ft.Container(
                    content=ft.Row(
                        controls=[ft.Text(address["name"], expand=True)],
                        expand=True
                    ),
                    bgcolor=ft.Colors.BLUE_100 if address["id"] == getattr(self, "selected_unloading_address_id", None) else ft.Colors.BLUE_50,
                    padding=10,
                    ink=True,
                    on_click=lambda e,a=address: self.on_unloading_address_selected(e, a),
                    border=ft.border.all(1, ft.Colors.GREY_300),
                    border_radius=10,
                    expand=True
                )
            )
        self.unloading_address_results.update()

    def on_unloading_address_selected(self, e, address):
        self.selected_unloading_address_id = address["id"]
        self.unloading_address_search_field.value = address["name"]
        self.unloading_address_search_field.update()
        self.update_unloading_addresses_list(self.filtered_addresses_ul)

    def on_unloading_date_click(self, e):
        self.unloading_date.value = e.control.value.strftime('%m/%d/%Y')
        self.unloading_date.update()

    def on_unloading_hour_click(self, e):
        if not self.unloading_hour.value:
            self.unloading_hour.value = str(e.control.value)
        else:
            self.unloading_hour.value += f' - {e.control.value}'
        self.unloading_hour.update()
    
    def on_reset_unloading_hour_btn_click(self, e):
        self.unloading_hour.value = None
        self.unloading_hour.update()

    def add_unloading_point_btn_click(self, e):
        adr = None
        name = None
        #print(self.selected_unloading_address_id)
        for _address in self.all_addresses:
            if _address['id'] == self.selected_unloading_address_id:
                adr = ''
                street_and_number = _address["address"].split(" %")[0]
                postal_code = _address["address"].split(" %")[1]
                city = _address["address"].split(" %")[2]
                region_county = _address["address"].split(" %")[3]
                country = _address["address"].split(" %")[4]
                if len(street_and_number) > 0:
                    adr += street_and_number +', '
                if len(postal_code) > 0:
                    adr += postal_code +', '
                if len(city) > 0:
                    adr += city +', '
                if len(region_county) > 0:
                    adr += region_county +', '
                if len(country) > 0:
                    adr += country
                name = _address['name']
        unloading_informatins = self.unloading_informations.value
        date = self.unloading_date.value
        hour = self.unloading_hour.value
        address = {
            'unloading_address_id': self.selected_unloading_address_id,
            'unloading_address_name': name,
            'unloading_address': adr,
            'unloading_informatins': unloading_informatins,
            'unloading_date': date,
            'unloading_hour': hour
        }

        if self.selected_unloading_address_id == None:
            self.unloading_error_message.value = "Please select unloading point!"
            self.unloading_error_message.update()
            return
        # if self.unloading_informations.value == None or len(self.unloading_informations.value) == 0:
        #     self.unloading_error_message.value = "Add unloading informations!"
        #     self.unloading_error_message.update()
        #     return
        if self.unloading_date.value == None or len(str(self.unloading_date.value)) == 0:
            self.unloading_error_message.value = "Add unloading date!"
            self.unloading_error_message.update()
            return
        # if self.unloading_hour.value == None or len(str(self.unloading_hour.value)) == 0:
        #     self.unloading_error_message.value = "Add unloading hour!"
        #     self.unloading_error_message.update()
        #     return

        if self.selected_unloading_address_id:
            self.unloading_query.append(address)
            self.unloading.controls.clear()
            self.unloading.controls = self.create_unloading_list(self.unloading_query, self.on_delete_unloading_address_btn_click)
            self.unloading.update()
        
            #reset to default
            self.selected_unloading_address_id = None
            self.unloading_informations.value = None
            self.unloading_informations.update()
            self.unloading_date.value = None
            self.unloading_date.update()
            self.unloading_hour.value = None
            self.unloading_hour.update()
            self.unloading_error_message.value = None
            self.unloading_error_message.update()
        else:
            self.unloading_error_message.value = "All fields of the unloading address are required."
            self.unloading_error_message.update()

    def get_all_clients(self):
        token = self.page.client_storage.get("token")
        headers = {"Authorization": f"Bearer {token}"}
        response = requests.get(f"{API_BASE_URL}/clients/", headers=headers)
        if response.status_code == 200:
            return response.json()
        else:
            print("Failed to fetch clients:", response.status_code)
            return []
        
    def get_all_addresses(self):   
        token = self.page.client_storage.get("token")
        headers = {"Authorization": f"Bearer {token}"}
        response = requests.get(f"{API_BASE_URL}/destinations/", headers=headers)
        if response.status_code == 200:
            return response.json()
        else:
            print("Failed to fetch addresses:", response.status_code)
            return []
        
    def create_loading_list(self, items, on_click_handler):
        """Helper to create list items for a column."""
        return [
            ft.Container(
                content=ft.Row(
                    [
                        ft.Column(
                            [
                                ft.Text(
                                    item['loading_address_name'], 
                                    expand=True, 
                                    size=15, 
                                    weight=ft.FontWeight.BOLD
                                ),
                                ft.Text(
                                    item['loading_address'], 
                                    expand=True, 
                                    size=12,
                                ),
                                ft.Row(
                                    [
                                        ft.Text(
                                            f"Date: {item['loading_date']}", 
                                            expand=True, 
                                            size=12,
                                        ),
                                        ft.Text(
                                            f"Hour: {item['loading_hour']}", 
                                            expand=True, 
                                            size=12,
                                        )
                                    ]
                                )

                            ]
                        ),
                        ft.Row(
                            [
                                ft.FilledButton(
                                    "Delete", 
                                    bgcolor=ft.Colors.RED, 
                                    on_click=lambda e, id=item: on_click_handler(id),
                                    width=100
                                )
                            ]
                        )
                        
                    ],
                    alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
                ),
                width=300,
                bgcolor=ft.Colors.BLUE_50,
                padding=10,
                border_radius=8,
                border=ft.border.all(1, ft.Colors.GREY_300),
                #ink=True,  # To enable click effect
                #on_click=lambda e, id=item: on_click_handler(id),  # Attach the click handler
            )
            for item in items
        ]
    
    def on_delete_loading_address_btn_click(self, item):
        self.loading_query.remove(item)
        self.loading.controls.clear()
        self.loading.controls = self.create_loading_list(self.loading_query, self.on_delete_loading_address_btn_click)
        self.loading.update()

    def create_unloading_list(self, items, on_click_handler):
        """Helper to create list items for a column."""
        return [
            ft.Container(
                content=ft.Row(
                    [
                        ft.Column(
                            [
                                ft.Text(
                                    item['unloading_address_name'], 
                                    expand=True, 
                                    size=15, 
                                    weight=ft.FontWeight.BOLD
                                ),
                                ft.Text(
                                    item['unloading_address'], 
                                    expand=True, 
                                    size=12,
                                ),
                                ft.Row(
                                    [
                                        ft.Text(
                                            f"Date: {item['unloading_date']}", 
                                            expand=True, 
                                            size=12,
                                        ),
                                        ft.Text(
                                            f"Hour: {item['unloading_hour']}", 
                                            expand=True, 
                                            size=12,
                                        )
                                    ]
                                )

                            ]
                        ),
                        ft.Row(
                            [
                                ft.FilledButton(
                                    "Delete", 
                                    bgcolor=ft.Colors.RED, 
                                    on_click=lambda e, id=item: on_click_handler(id),
                                    width=100
                                )
                            ]
                        )
                        
                    ],
                    alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
                ),
                width=300,
                bgcolor=ft.Colors.BLUE_50,
                padding=10,
                border_radius=8,
                border=ft.border.all(1, ft.Colors.GREY_300),
                #ink=True,  # To enable click effect
                #on_click=lambda e, id=item: on_click_handler(id),  # Attach the click handler
            )
            for item in items
        ]

    def on_delete_unloading_address_btn_click(self, item):
        #print(item)
        #print(self.unloading_query)
        self.unloading_query.remove(item)
        self.unloading.controls.clear()
        self.unloading.controls = self.create_unloading_list(self.unloading_query, self.on_delete_unloading_address_btn_click)
        self.unloading.update()

    def get_user_data(self):
        try:
            token = self.page.client_storage.get("token")
            if not token:
                self.message.value = "Unauthorized: No token"
                return
            response = requests.get(f"{API_BASE_URL}/profile/", headers={"Authorization": f"Bearer {token}"})
            if response.status_code == 200:
                user_data = response.json()
                return user_data
            return None
        except Exception as e:
            return None
    
    def on_archive_btn_click(self, e):
        archive_page = ArchiveInPage(self.page, self.dashboard, self)
        self.dashboard.placeholder.content = archive_page.build()
        self.dashboard.placeholder.update() 

    def get_current_subscription_plan(self):
        try:
            token = self.page.client_storage.get("token")
            headers = {"Authorization": f"Bearer {token}"}
            response = requests.get(f"{API_BASE_URL}/subscription/", headers=headers)
            #print(response.text)
            return response.json()[-1] if response.status_code == 200 else None
        except Exception as e:
            print("Error loading subscription:", e)
            return None
        
    def on_save_btn_click(self, e):
        loading_addresses = []
        unloading_addresses = []
        for laddr in self.loading_query:
            laddr_copy = laddr.copy()
            #if isinstance(laddr_copy.get("loading_hour"), datetime.time):
            #    laddr_copy["loading_hour"] = laddr_copy["loading_hour"].strftime("%H:%M")
            loading_addresses.append(laddr_copy)

        for uaddr in self.unloading_query:
            uaddr_copy = uaddr.copy()
            #if isinstance(uaddr_copy.get("unloading_hour"), datetime.time):
            #    uaddr_copy["unloading_hour"] = uaddr_copy["unloading_hour"].strftime("%H:%M")
            unloading_addresses.append(uaddr_copy)

        saved_data = {
            'order_number': self.order_number.value,
            'client_id': self.selected_client_id,
            'products_description': self.product_description.value,
            'ldb_quantity': self.ldm_quantity.value,
            'kg_quantity': self.kg_quantity.value,
            'track_reg_number': self.track_reg_number.value,
            'trailer_reg_number': self.trailer_reg_number.value,
            'received_price': self.received_price.value,
            'loading_addresses': loading_addresses,
            'unloading_addresses': unloading_addresses,
            'file':self.filename.value,
            'expenses': self.expenses.value,
            'currency': self.currency.value
        }
        #print(saved_data)
        if self.order_number.value == None or len(self.order_number.value)==0:
            self.error_message.value = "Order number is mandatory!"
            self.error_message.color = ft.Colors.RED
            self.error_message.update()
            return
        if self.selected_client_id == None:
            self.error_message.value = "Please select the client!"
            self.error_message.color = ft.Colors.RED
            self.error_message.update()
            return
        if self.product_description.value == None or len(self.product_description.value)==0:
            self.error_message.value = "Please insert product description!"
            self.error_message.color = ft.Colors.RED
            self.error_message.update()
            return
        # if self.ldm_quantity.value == None or len(self.ldm_quantity.value)==0:
        #     self.error_message.value = "Please insert LDM!"
        #     self.error_message.color = ft.Colors.RED
        #     self.error_message.update()
        #     return
        # if self.kg_quantity.value == None or len(self.kg_quantity.value)==0:
        #     self.error_message.value = "Please insert KG!"
        #     self.error_message.color = ft.Colors.RED
        #     self.error_message.update()
        #     return
        if self.track_reg_number.value == None or len(self.track_reg_number.value)==0:
            self.error_message.value = "Please insert Track Registration Number!"
            self.error_message.color = ft.Colors.RED
            self.error_message.update()
            return
        if self.trailer_reg_number.value == None or len(self.trailer_reg_number.value)==0:
            self.error_message.value = "Please insert Trailer Registration Number!"
            self.error_message.color = ft.Colors.RED
            self.error_message.update()
            return
        if self.received_price.value == None or len(self.received_price.value)==0:
            self.error_message.value = "Please insert Price received!"
            self.error_message.color = ft.Colors.RED
            self.error_message.update()
            return
        if len(loading_addresses) == 0:
            self.error_message.value = "Please add loading point!"
            self.error_message.color = ft.Colors.RED
            self.error_message.update()
            return
        if len(unloading_addresses) == 0:
            self.error_message.value = "Please add unloading point!"
            self.error_message.color = ft.Colors.RED
            self.error_message.update()
            return

        # --- POST request to save the order in the database ---
        token = self.page.client_storage.get("token")
        headers = {"Authorization": f"Bearer {token}"}
        user_id = self.page.session.get("user_id")
        saved_data["user_id"] = user_id
        user = self.get_user_data()
        saved_data['terms'] = user['terms']

        try:
            response = requests.post(f"{API_BASE_URL}/orders_in/", json=saved_data, headers=headers)
            if response.status_code == 201:
                self.error_message.value = "Order saved successfully! You will be redirected to archive page."
                self.error_message.color = ft.Colors.GREEN
                self.error_message.update()
                time.sleep(3)
                archive_page = ArchiveInPage(self.page, self.dashboard, self)
                self.dashboard.placeholder.content = archive_page.build()
                self.dashboard.placeholder.update() 
            else:
                self.error_message.value = f"Failed to save order: {response.status_code} - {response.text}"
                self.error_message.update()
        except Exception as ex:
           self.error_message.value = f"Error: {str(ex)}"
           self.error_message.update()
    
    def get_client_access(self):
        token = self.page.client_storage.get("token")
        headers = {"Authorization": f"Bearer {token}"}
        response = requests.get(f"{API_BASE_URL}/profile/", headers=headers, timeout=10)
        user = response.json()
        if user['user_role'] == 'user':
            return True
        else:
            id = self.page.session.get("user_id")
            response = requests.get(f"{API_BASE_URL}/company_user/access/{id}", headers=headers)
            return True if response.json()['orders_in'] == 1 else False

    def build(self):
        self.save_btn = ft.FilledButton(
            "Save Order", 
            width=200, 
            on_click=self.on_save_btn_click,
        )
        self.save_row = ft.Row([],alignment=ft.MainAxisAlignment.CENTER)
        subscription = self.get_current_subscription_plan()
        if subscription:
            if subscription['status'] != 'expired':
                self.save_row.controls.append(self.save_btn)
        return ft.Container(
            ft.Column(
                [
                    ft.Row(
                        [
                            ft.Column(
                                [
                                    ft.Text('Create Order In', size=24, weight=ft.FontWeight.BOLD),
                                    ft.Row(
                                        [
                                            ft.Text("Number", size=18, weight=ft.FontWeight.BOLD),
                                            self.order_number,
                                            self.upload_order_btn,
                                            self.filename
                                        ]
                                    )
                                ], 
                                alignment=ft.MainAxisAlignment.START
                            ),
                            ft.ElevatedButton("Archive", on_click=self.on_archive_btn_click, width=150)
                        ], 
                        alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
                        vertical_alignment=ft.CrossAxisAlignment.START
                    ),
                    ft.Row(
                        [
                            ft.Column(
                                [
                                    ft.Text("Client", size=18, weight=ft.FontWeight.BOLD),
                                    self.client_search_field,
                                    ft.Container(
                                        content=ft.Container(
                                            content=ft.Column(
                                                controls=[self.client_results],
                                                scroll=ft.ScrollMode.ADAPTIVE,
                                            ),
                                            #clip_behavior=ft.ClipBehavior.,
                                            expand=True,
                                            padding=0,
                                        ),
                                        height=250 if len(self.client_results.controls) > 4 else None,
                                    )
                                ],
                                expand=True
                            )
                        ],
                        expand=True,
                        spacing=20,
                        vertical_alignment=ft.CrossAxisAlignment.START
                    ),
                    ft.Divider(),
                    ft.Row(
                        [
                            ft.Column(
                                [
                                    ft.Row(
                                        [
                                            ft.Text("Product Details", size=18, weight=ft.FontWeight.BOLD)
                                        ],
                                        alignment=ft.MainAxisAlignment.START
                                    ),
                                    ft.Column(
                                        [
                                            ft.Container(
                                                content = self.product_description,
                                                expand=True
                                            ),
                                            ft.Row(
                                                [
                                                    ft.Text(
                                                        value="LDM"
                                                    ),
                                                    self.ldm_quantity,  
                                                    ft.Text("   "),
                                                    ft.Text(
                                                        value="KG"
                                                    ),
                                                    self.kg_quantity,
                                                ],
                                                expand=True
                                            ) 
                                        ],
                                        expand=True,
                                        
                                    )
                                ],
                                expand=5
                            ),
                            ft.Column(
                                [
                                    ft.Row(
                                        [
                                            ft.Text("Truck / Trailer Info", size=18, weight=ft.FontWeight.BOLD)
                                        ],
                                        alignment=ft.MainAxisAlignment.START
                                    ),
                                    ft.Column(
                                        [
                                            self.track_reg_number,
                                            self.trailer_reg_number
                                        ],
                                        expand=True
                                    )
                                ],
                                expand=2.5
                            ),
                            ft.Column(
                                [
                                    ft.Row(
                                        [
                                            ft.Text("Price / Expenses", size=18, weight=ft.FontWeight.BOLD),
                                            self.currency
                                        ],
                                        alignment=ft.MainAxisAlignment.START
                                    ),
                                    self.received_price,
                                    self.expenses
                                ],
                                expand=2.5
                            )
                        ],
                        expand=True,
                        spacing=20,
                        vertical_alignment=ft.CrossAxisAlignment.START
                    ),
                    ft.Divider(),
                    ft.Row(
                        [
                            ft.Column(
                                [
                                    ft.Text("Loading Points", size=18, weight=ft.FontWeight.BOLD),
                                    ft.Column(
                                        [
                                            self.loading_address_search_field,
                                            ft.Container(
                                                content=ft.Container(
                                                    content=ft.Column(
                                                        controls=[self.loading_address_results],
                                                        scroll=ft.ScrollMode.ADAPTIVE
                                                    ),
                                                    expand=True,
                                                    padding=0,
                                                ),
                                                height=250 if len(self.filtered_addresses) > 4 else None,
                                            )
                                        ],
                                        expand=5
                                    ),
                                    ft.Container(
                                        content = self.loading_informations,
                                        expand = True
                                    ),
                                    ft.Row(
                                        [
                                            self.loading_date,
                                            ft.ElevatedButton(
                                                "Select date",
                                                icon=ft.Icons.CALENDAR_MONTH,
                                                on_click=lambda e: self._open_date_picker(self.loading_date_picker),
                                            )
                                        ],
                                        expand=True
                                    ),
                                    ft.Row(
                                        [
                                            self.loading_hour,
                                            ft.ElevatedButton(
                                                "Select hour - Start",
                                                icon=ft.Icons.CALENDAR_MONTH,
                                                on_click=lambda e: self._open_time_picker(self.loading_time_picker_start),
                                            ),
                                            ft.ElevatedButton(
                                                "Select hour - End",
                                                icon=ft.Icons.CALENDAR_MONTH,
                                                on_click=lambda e: self._open_time_picker(self.loading_time_picker_end),
                                            ),
                                            ft.ElevatedButton(
                                                "Reset",
                                                on_click = self.on_reset_loading_hour_btn_click
                                            ),
                                        ],
                                        expand=True
                                    ),
                                    self.loading_error_message,
                                    ft.Row(
                                        [
                                            ft.ElevatedButton(
                                                "Add Loading Point",
                                                on_click=self.add_loading_point_btn_click,
                                                icon=ft.Icons.ADD
                                            )
                                        ],
                                        alignment=ft.MainAxisAlignment.CENTER
                                    ),
                                    self.loading
                                ],
                                expand=5,
                                alignment=ft.MainAxisAlignment.START
                            ),
                            ft.Column(
                                [
                                    ft.Text("Unloading Points", size=18, weight=ft.FontWeight.BOLD),
                                    ft.Column(
                                        [
                                            self.unloading_address_search_field,
                                            ft.Container(
                                                content=ft.Container(
                                                    content=ft.Column(
                                                        controls=[self.unloading_address_results],
                                                        scroll=ft.ScrollMode.ADAPTIVE
                                                    ),
                                                    expand=True,
                                                    padding=0,
                                                ),
                                                height=250 if len(self.filtered_addresses_ul) > 4 else None,
                                            )
                                        ],
                                        expand=5
                                    ),
                                    ft.Container(
                                        content = self.unloading_informations,
                                        expand=True
                                    ),
                                    ft.Row(
                                        [
                                            self.unloading_date,
                                            ft.ElevatedButton(
                                                "Select date",
                                                icon=ft.Icons.CALENDAR_MONTH,
                                                on_click=lambda e: self._open_date_picker(self.unloading_date_picker),
                                            )
                                        ],
                                        expand=True
                                    ),
                                    ft.Row(
                                        [
                                            self.unloading_hour,
                                            ft.ElevatedButton(
                                                "Select hour - Start",
                                                icon=ft.Icons.CALENDAR_MONTH,
                                                on_click=lambda e: self._open_time_picker(self.unloading_time_picker_start),
                                            ),
                                            ft.ElevatedButton(
                                                "Select hour - End",
                                                icon=ft.Icons.CALENDAR_MONTH,
                                                on_click=lambda e: self._open_time_picker(self.unloading_time_picker_end),
                                            ),
                                            ft.ElevatedButton(
                                                "Reset",
                                                on_click = self.on_reset_unloading_hour_btn_click
                                            ),
                                        ],
                                        expand=True
                                    ),
                                    self.unloading_error_message,
                                    ft.Row(
                                        [
                                            ft.ElevatedButton(
                                                "Add Unloading Point",
                                                on_click=self.add_unloading_point_btn_click,
                                                icon=ft.Icons.ADD
                                            )
                                        ],
                                        alignment=ft.MainAxisAlignment.CENTER
                                    ),
                                    self.unloading
                                ],
                                expand=5,
                                alignment=ft.MainAxisAlignment.START
                            )
                        ],
                        expand=True,
                        spacing=20,
                        vertical_alignment=ft.CrossAxisAlignment.START
                    ),
                    ft.Row(
                        [
                            self.error_message,
                        ],
                        alignment=ft.MainAxisAlignment.CENTER
                    ),
                    self.save_row,
                ],
                expand=True,
                scroll=ft.ScrollMode.ADAPTIVE,
                spacing=20
            )
        ) if self.get_client_access() else ft.Container(
            content=ft.Column(
                [
                    ft.Row(
                        [
                            ft.Text("Create Order In", size=24, weight=ft.FontWeight.BOLD),
                        ],
                        alignment=ft.MainAxisAlignment.SPACE_BETWEEN
                    ),
                    ft.Row(
                        [
                            ft.Text(
                                "You do not have access to this page content", 
                                size=24, 
                                weight=ft.FontWeight.BOLD,
                                color=ft.Colors.RED
                            )
                        ],
                        alignment=ft.MainAxisAlignment.CENTER
                    ),
                    ft.Text("")
                ],
                alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
                expand=True
            ),
            expand=True
        )
    