/**
 * HRView.js — ماژول نیروی انسانی
 */

import BaseView from "../../core/BaseView.js";
import { viewRegistry } from "../../core/registries.js";
import HrEmployeesSection   from "./sections/HrEmployeesSection.js";
import HrDepartmentsSection from "./sections/HrDepartmentsSection.js";
import HrPositionsSection   from "./sections/HrPositionsSection.js";
import HrAttendanceSection  from "./sections/HrAttendanceSection.js";
import HrLeavesSection      from "./sections/HrLeavesSection.js";
import HrMapSection         from "./sections/HrMapSection.js";

const SECTIONS = [
    { key:"employees",   icon:"👥", title:"کارمندان",     desc:"اطلاعات کامل + موقعیت", Class: HrEmployeesSection },
    { key:"departments", icon:"🏢", title:"بخش‌ها",        desc:"ساختار سازمانی",        Class: HrDepartmentsSection },
    { key:"positions",   icon:"📋", title:"سمت‌ها",        desc:"شغل و سطح",             Class: HrPositionsSection },
    { key:"attendance",  icon:"⏰", title:"حضور غیاب",    desc:"ورود/خروج + GPS",       Class: HrAttendanceSection },
    { key:"leaves",      icon:"🌴", title:"مرخصی‌ها",     desc:"درخواست و تأیید",       Class: HrLeavesSection },
    { key:"map",         icon:"🗺️", title:"نقشه پرسنل",   desc:"محل سکونت/کار",         Class: HrMapSection },
];

export default class HRView extends BaseView {

    static title = "نیروی انسانی";
    useContextBadge = true;

    constructor(props = {}) {
        super(props);
        this.api      = props.api;
        this._active  = "employees";
        this._sections = {};
    }

    async template() {
        const cards = SECTIONS.map(s => `
            <div class="sp-nav-card ${s.key === this._active ? "sp-nav-card--active" : ""}"
                 data-section="${s.key}" role="button" tabindex="0">
                <span class="sp-nav-card__icon">${s.icon}</span>
                <div class="sp-nav-card__title">${s.title}</div>
                <div class="sp-nav-card__desc">${s.desc}</div>
            </div>
        `).join("");

        const panels = SECTIONS.map(s => `
            <div class="acc-section ${s.key === this._active ? "acc-section--active" : ""}"
                 data-panel="${s.key}" data-ref="panel-${s.key}"></div>
        `).join("");

        return `
            <div class="sp-section-nav" data-ref="navBar">${cards}</div>
            <div>${panels}</div>
        `;
    }

    async afterRender() {
        this.ref("navBar").querySelectorAll(".sp-nav-card").forEach(c => {
            this.on(c, "click", () => this._switch(c.dataset.section));
        });
        await this._mount(this._active);
    }

    async _switch(key) {
        if (key === this._active) return;
        this.ref("navBar").querySelectorAll(".sp-nav-card").forEach(c =>
            c.classList.toggle("sp-nav-card--active", c.dataset.section === key)
        );
        this.container.querySelectorAll(".acc-section").forEach(p =>
            p.classList.toggle("acc-section--active", p.dataset.panel === key)
        );
        this._active = key;
        await this._mount(key);
    }

    async _mount(key) {
        if (this._sections[key]) return;
        const def   = SECTIONS.find(s => s.key === key);
        const panel = this.ref(`panel-${key}`);
        if (!def || !panel) return;
        const sec = new def.Class({ eventBus: this.eventBus, store: this.store, tab: this.tab, api: this.api });
        await sec.render(panel);
        this._sections[key] = sec;
    }

    destroy() {
        Object.values(this._sections).forEach(s => s?.destroy?.());
        super.destroy();
    }
}

viewRegistry.register("hr", HRView);
