import { formscreen } from "./formscreen";
import { product_give_back_request } from "./product_give_back_request.js";
import { product_reservations_request } from "./product_reservations_request";
import { searcher } from "./searcher.js";

export class product_all_rents extends formscreen {
    #target;

    constructor(target) {
        super();

        this.#target = target;
    }

    get _name() {
        return _("all-rents");
    }

    get _has_submit() {
        return false;
    }

    #create_single(target) {
        const container = document.createElement("div");
        container.classList.add("reservation-info");
        
        if (target.phone_number !== null) {
            const phone_icon = document.createElement("span");
            phone_icon.classList.add("material-icons");
            phone_icon.innerText = "phone";

            const phone_number = document.createElement("span");
            phone_number.classList.add("numbers")
            phone_number.innerText = target.phone_number;

            const phone_number_container = document.createElement("p");
            phone_number_container.appendChild(phone_icon);
            phone_number_container.appendChild(phone_number);
            container.appendChild(phone_number_container);
        }

        if (target.email !== null) {
            const email_icon = document.createElement("span");
            email_icon.classList.add("material-icons");
            email_icon.innerText = "mail";

            const email = document.createElement("span");
            email.innerText = target.email;

            const email_container = document.createElement("p");
            email_container.appendChild(email_icon);
            email_container.appendChild(email);
            container.appendChild(email_container);
        }

        return container;
    }

    #create_single_button(target) {
        const button = document.createElement("button");
        button.classList.add("material-icons");
        button.classList.add("give-back-button");
        button.innerText = "save_alt";

        button.addEventListener("click", async () => {
            try {
                this._info = _("processing");

                const request = new product_give_back_request(target);
                const response = await request.connect();

                if (!response.result) {
                    throw new Error(_(response.cause));
                }

                this._refresh();
                searcher.reload();
            } catch (error) {
                this._error = String(error);
            }
        });

        return button;
    }

    _process() {
        return;
    }

    async _build_form() {
        try {
            this._info = _("loading");

            const request = new product_reservations_request(this.#target);
            const response = await request.connect();

            const list = document.createElement("div");
            list.classList.add("reservations-list");

            let empty = true;

            response.collection.forEach(count => {
                const item = document.createElement("div");
                item.classList.add("reservation");

                const left = this.#create_single(count);
                const right = this.#create_single_button(count);

                empty = false;

                item.appendChild(left);
                item.appendChild(right);
                list.appendChild(item);
            });
            
            this._append_child(list);
            
            if (empty) {
                this._success = "not-found-any-reservations";
            } else {
                this._clear_results();
            }
        } catch (error) {
            this._error = String(error);
        }
    }
}