//
//  OrderListAllView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/24.
//

import SwiftUI

struct OrderListAllView: View {
    
    @StateObject var viewModel = OrderListAllViewModel()
    
    var body: some View {
        NavigationView {
            BaseView(isLoading: $viewModel.isLoading) {
                VStack(spacing: 0) {
                    HStack(spacing: 0) {
                        Button {
                            AppState.shared.back()
                        } label: {
                            Image("ic_back")
                                .resizable()
                                .frame(width: 20, height: 20)
                            
                            Text("콘텐츠 보관함")
                                .font(.custom(Font.bold.rawValue, size: 18.3))
                                .foregroundColor(Color(hex: "eeeeee"))
                        }
                        
                        Spacer()
                    }
                    .padding(.horizontal, 13.3)
                    .frame(height: 50)
                    .background(Color.black)
                    
                    HStack(spacing: 0) {
                        Text("총  ")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(Color(hex: "eeeeee"))
                        
                        Text("\(viewModel.totalCount)")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(Color(hex: "dd4500"))
                        
                        Text(" 개")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(Color(hex: "eeeeee"))
                        
                        Spacer()
                    }
                    .padding(.horizontal, 13.3)
                    .padding(.top, 13.3)
                    
                    ScrollViewReader { reader in
                        ScrollView(.vertical, showsIndicators: false) {
                            LazyVStack(spacing: 10.7) {
                                ScrollerToTop(reader: reader, scrollOnChange: $viewModel.scrollToTop)
                                
                                ForEach(0..<viewModel.orderList.count, id: \.self) { index in
                                    let item = viewModel.orderList[index]
                                    NavigationLink {
                                        ContentDetailView(contentId: item.contentId)
                                    } label: {
                                        OrderListItemView(item: item)
                                            .contentShape(Rectangle())
                                            .padding(.horizontal, 13.3)
                                            .padding(.top, 6.7)
                                            .onAppear {
                                                if index == viewModel.orderList.count - 1 {
                                                    viewModel.getOrderList()
                                                }
                                            }
                                    }
                                }
                            }
                        }
                    }
                    .padding(.top, 13.3)
                }
                .onAppear {
                    viewModel.getOrderList()
                }
                .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .bottom, autohideIn: 2) {
                    HStack {
                        Spacer()
                        Text(viewModel.errorMessage)
                            .padding(.vertical, 13.3)
                            .frame(width: screenSize().width - 66.7, alignment: .center)
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .background(Color(hex: "3bb9f1"))
                            .foregroundColor(Color.white)
                            .multilineTextAlignment(.leading)
                            .cornerRadius(20)
                            .padding(.bottom, 66.7)
                        Spacer()
                    }
                }
            }
        }
    }
}
