@{
    ViewData["Title"] = "Dashboard";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<div  class="container" id="invUser">
    <div class="row">
        <div class="text-center">
            <p><h1 class="display-4">Inventory User Dashboard</h1></p>
            <p v-show="currentUserCompanyDept.departmentName"><h2 class="display-6">Store: {{ currentUserCompanyDept.departmentName }}</h2></p>
        </div>
    </div>
    @await Html.PartialAsync("~/Areas/Inventory/Views/_InventoryPartialUser.cshtml")
</div>
@section Scripts {
    @{
        await Html.RenderPartialAsync("_ValidationScriptsPartial");
    }
<script>
    $(function () {
            app.mount('#invUser');

            $('.closeModal').on('click', function () {
                // Show the modal with the ID 'addManufacturerModal'.
                $('.modal').modal('hide');
            });
    });
    const app = Vue.createApp({
        data() {
            return {
                currentUser: null,
                currentUserCompanyDept: {
                    departmentName: null,
                    departmentId: null
                },
                reportData: null,
            }
        },
        mounted() {
            this.fetchUser();
        },
        methods: {
            async fetchUser() {
                try {
                    const response = await fetch(`/IdentityAPI/GetUserInformation/`, {
                        method: 'POST',
                    });
                    if (response.ok) {
                        const data = await response.json();
                        this.currentUser = data?.userInfo || null;
                        const companyDeptData = await this.currentUser.department;
                        const userRole = this.currentUser.role;
                        if(userRole == "SuperAdmin" || userRole == "SystemAdmin"){
                            this.currentUserCompanyDept = {departmentId : 0, departmentName : "All"}
                            this.fetchInventoryReport(0);
                        }
                        else{
                            this.currentUserCompanyDept = companyDeptData;
                            this.fetchInventoryReport(companyDeptData.departmentId);
                        }
                    }
                    else {
                        console.error(`Failed to fetch user: ${response.statusText}`);
                    }
                }
                catch (error) {
                    console.error('There was a problem with the fetch operation:', error);
                }
            },
            async fetchInventoryReport(deptId){
                try {
                    const response = await fetch(`/InvMainAPI/GetInventoryReport/` + deptId, {
                        method: 'POST',
                    });
                    if (response.ok) {
                        const data = await response.json();
                        this.reportData = data;
                    }
                    else {
                        console.error(`Failed to fetch user: ${response.statusText}`);
                    }
                }
                catch (error) {
                    console.error('There was a problem with the fetch operation:', error);
                }
            },
        },
    });
</script>
}