//
//  PLJobView.swift
//  IOS_study
//
//  Created by CC-star on 2025/6/13.
//

import SwiftUI
struct PLJobView: View {
   @Environment(PLJobViewModel.self) var vm
   @State var showActiveDialog = false
   @State var showRemoveDialog = false
   @State var showRemoveAlert = false
   @Binding var naviPath : NavigationPath
   var job: PLJob { vm.job }
   var isActive:Bool{ vm.job.isActive }
   var titleAD: String { isActive ? "" : "[已下线]" }
   @Environment(HUD.self) var hud
   var body: some View {
      List {
         //头部视图
         VStack(alignment: .leading,spacing: kStackSpacing) {
            //第一层
            HStack {
               VStack(alignment: .leading) {
                  HStack {
                     Text(job.business)
                        .labelBG2()
                     if job.updatedAt.daysFromNow <= 7 { Text("新到").labelWA() }
                     Text("3人想去")
                        .size13s()
                  }
                  Text(job.title + titleAD).tc().size18b()
               }
               Spacer()
               Text("\(job.hourlyWage)").accent().headline()
            }.padding(.bottom,-2)
            //第二层
            HStack{
               Text(job.placeName)
               
               if job.tax == "不报税" {
                  Text(job.tax).accentBorder()
               } else if job.tax == "报税" {
                  Text(job.tax).redBorder()
               }
               
               Text("\(job.updatedAt.toStr)发布").secondary()
            }
            //第三层
            HStack(spacing: kStackSpacing){
               HStack {
                  HStack(spacing: kIconSpacing) {
                     //icon
                     Image("station").iconStyle()
                     Text("\(job.province)-\(job.city)").semib()
                     Button {
                        if let url = URL(string: "https://maps.google.com/?q=\(job.province)\(job.city)"){
                           UIApplication.shared.open(url)
                        }
                     } label: {
                        Text("导航")
                     }.btnStyle()
                  }
                  HStack(spacing: kIconSpacing) {
                     Image("want").iconStyle()
                     Text("招\(job.wantNum)人")
                  }
                  HStack {
                     Button("编辑") {
                        vm.draftJob = job
                        naviPath.append(Navi1.PLJobToEdit)
                     }.buttonStyle(.borderless).accent()
                     Button(isActive ? "下线" : "上线") {
                       showActiveDialog = true
                     }.buttonStyle(.borderless).accent()
                        .confirmationDialog(isActive ? "确定下线吗" : "确定上线吗", isPresented: $showActiveDialog, titleVisibility: .visible) {
                           Button("确定") {
                              toggleJobStatus()
                           }
                           Button("再想想", role: .cancel) {  }
                        }
                     Button("删除") {
                       showRemoveDialog = true
                     }.buttonStyle(.borderless).accent()
                        .confirmationDialog("确定删除吗", isPresented: $showRemoveDialog, titleVisibility: .visible) {
                           Button("确定", role: .destructive) {
                              showRemoveAlert = true
                           }
                           Button("再想想", role: .cancel) {  }
                        }.alert("删除之后不可恢复", isPresented: $showRemoveAlert) {
                           Button("确定", role: .destructive) { removeJob() }
                           Button("取消", role: .cancel) {  }
                        }
                     
                  }
               }
            }
         }.textStyle2().cellStyle()
         //公司
         Button {
            
         } label: {
            HStack(spacing: kIconSpacing){
               Image("company").iconStyle()
               Text(job.companyName).size16semib().oneLineStyleMiddle()
               Spacer()
               Group {
                  Text("更多工作")
                  Image(systemName: "chevron.right")
               }.secondary()
            }
         }.textStyle().cellStyle().buttonStyle(.borderless)
         
         //工作详情
         VStack(alignment: .leading,spacing:kStackSpacingS) {
            //公司时间
            VStack(alignment: .leading,spacing: kStackSpacingS) {
               HStack(spacing: kIconSpacing) {
                  Image("time").iconStyle()
                  Text("工作时间").subTStyle()
               }
               
               HStack(spacing: 2) {
                  Text("\(job.startHour)")
                  Text(":").conlonStyle()
                  Text(job.startMin == 0 ? "00" : "\(job.startMin)")
                  Text("-").conlonStyle()
                  Text("\(job.endHour)")
                  Text(":").conlonStyle()
                  Text(job.endMin == 0 ? "00" : "\(job.endMin)")
               }
               
               if job.has2 {
                  HStack(spacing: 2) {
                     Text("\(job.startHour2)")
                     Text(":").conlonStyle()
                     Text(job.startMin2 == 0 ? "00" : "\(job.startMin2)")
                     Text("-").conlonStyle()
                     Text("\(job.endHour2)")
                     Text(":").conlonStyle()
                     Text(job.endMin2 == 0 ? "00" : "\(job.endMin2)")
                  }
                  
               }
               
//               Text("\(job.startHour)-\(job.endHour)").labelBG()
//               if job.has2 {
//                  Text("\(job.startHour2)-\(job.endHour2)").labelBG()
//               }
            }
            
            //            Divider()
            exDivider()
            if !job.workContent.isEmpty {
               //工作内容
               VStack(alignment: .leading,spacing: kStackSpacingS) {
                  HStack(spacing: kIconSpacing) {
                     Image("content").iconStyle()
                     Text("工作内容").subTStyle()
                  }
                  Text(job.workContent).allowMulti()
               }
               exDivider()
            }
            //工作要求
            VStack(alignment: .leading,spacing: kStackSpacingS) {
               HStack(spacing: kIconSpacing) {
                  Image("need").iconStyle()
                  Text("工作要求").subTStyle()
               }
               HStack {
                  Text(job.needExp).labelWAL()
                  Text(job.needEdu).labelWAL()
               }
               HStack {
                  Text(job.needLan).labelWAL()
                  Text(job.needFrame).labelWAL()
               }
               if !job.otherNeed.isEmpty {
                  Text(job.otherNeed).allowMulti()
               }
            }
            
            //公式福利
            exDivider()
            VStack(alignment: .leading,spacing: kStackSpacingS) {
               HStack(spacing: kIconSpacing) {
                  Image("benefit").iconStyle()
                  Text("公式福利").subTStyle()
               }
               HStack {
                  if job.noOverTime {
                     Text("不加班").labelWAL()
                  }
                  if job.moveFee {
                     Text("交通费").labelWAL()
                  }
               }
               //更多福利
               if !job.otherBenefit.isEmpty {
                  Text(job.otherBenefit).allowMulti()
               }
            }
         }.textStyle2().cellStyle()
         
         
         //应聘电话
         VStack(alignment: .leading,spacing:kStackSpacingS){
            Text("应聘方式（\(job.contactType)）").subTStyleL()
            HStack {
               Text("\(job.contact)").size20b().tc().allowMulti()
               Spacer()
               Button("复制"){
                  UIPasteboard.general.string = job.contact
                  hud.show("复制成功")
                  //todo弹窗提示
               }.btnStyle().padding(6)
               
               
               //                Button {
               //                   UIPasteboard.general.string = "0423-1230-1241"
               //                   //todo弹窗提示
               //                   }
               //                } label: {
               //                   Text("复制")
               //                }.btnStyle().padding(6)
            }
            Text("若问起，请说是在“测试软件”上看到的").size14().purple()
         }.cellStyle()
         
            listBottomEView()
         
      }.listStyle().navigationTitle("长期兼职").navigationBarTitleDisplayMode(.inline).navigationBarBackButtonHidden()
      //顶部返回键
         .toolbar {
            ToolbarItem(placement: .topBarLeading) {
               Button {
                  if !naviPath.isEmpty {//检查是否为最后一页
                     naviPath.removeLast()//返回上一页
                  }
               } label: {
                  Image(systemName: "chevron.left").secondary().padding(.horizontal,6)
               }.btnStyle()
            }
         }
   }
}

//#Preview {
//   PLJobView(naviPath: .constant(NavigationPath()))
//}

