//
//  PostPLJobIView.swift
//  IOS_study
//
//  Created by CC-star on 2025/6/28.
//

import SwiftUI

struct PostPLJobIView: View {
   var isEditing = false //默认新增工作
   
   @AppStorage("workPlaceName") var workPlaceName = ""
   @AppStorage("workProvince") var workProvince = ""
   @AppStorage("workCity") var workCity = ""
   @AppStorage("workContact") var workContact = ""
   @Environment(PLJobViewModel.self) var vm
   @Environment(HUD.self) var hud
   @Environment(TabBarViewModel.self) var tabbarVM
   @Binding var naviPath: NavigationPath
   @State var WordNum20 = 20
   @State var WordNum50 = 50
   @State var WordNum100 = 100
   @State var WordNum200 = 200
   @State var WordNum300 = 300
   @State var WordNum500 = 500

   var draftJob: PLJob{ vm.draftJob }
   
   var incalidMsg: String? {
      if draftJob.title.isBlank {
         return "工作名称未写"
      }
      if draftJob.hourlyWage < 20 || draftJob.hourlyWage > 1000{
         return "基本时给太低或太高"
      }
      if draftJob.placeName.isBlank {
         return "工作地点未填"
      }
      if draftJob.city.isBlank {
         return "城市未填"
      }
      if draftJob.contact.isBlank {
         return "联系方式未填"
      }
      if draftJob.title.count > WordNum50 { return "工作名称不能超过\($WordNum50)" }
      if draftJob.otherTime.count > WordNum100 { return "时间说明不能超过\($WordNum100)" }
      if draftJob.otherNeed.count > WordNum300 { return "工作要求不能超过\($WordNum300)" }
      if draftJob.workContent.count > WordNum500 { return "工作内容不能超过\($WordNum500)" }
      if draftJob.otherBenefit.count > WordNum300 { return "工作福利不能超过\($WordNum300)" }
      if draftJob.placeName.count > WordNum50 { return "工作地点名称不能超过\($WordNum50)" }
      if draftJob.city.count > WordNum20 { return "工作地城市不能超过\($WordNum20)" }
      if draftJob.contact.count > WordNum200 { return "联系方式不能超过\($WordNum200)" }
      return nil
   }
  
   var body: some View {
      @Bindable var vm = vm
      ScrollView(showsIndicators: false) {
         VStack(spacing: kStackSpacingS) {
            // MARK: - 基本信息
            VStack(alignment: .leading,spacing: kStackSpacing) {
               HStack {
                  VStack(alignment: .leading) {
                     Text("工作业种").littleTStyle()
                     Picker("工作业种", selection: $vm.draftJob.business) {
                        ForEach(kBusinessArr, id: \.self){
                           Text($0)
                        }
                     }.pStyle()
                  }
                  
                  VStack(alignment: .leading) {
                     Text("工作名称").littleTStyle()
                     TextField("IOS程序员", text: $vm.draftJob.title).tfStyle()
                  }
               }
               VStack(alignment: .leading) {
                  Text("是否报税").littleTStyle()
                  Picker("是否报税", selection: $vm.draftJob.tax){
                     ForEach(kTaxArr, id: \.self){
                        Text($0)
                     }
                  }.pickerStyle(.segmented)
               }.padding(.bottom,5)
               HStack {
                  Text("基本时给：").littleTStyle()
                  TextField("", value: $vm.draftJob.hourlyWage, formatter: NumberFormatter())
                     .size18ab().multilineTextAlignment(.center).kerning(2)
                     .keyboardType(.numberPad).tfStyle().frame(width: 88)
                  Text("请填写最低时给").size13s()
               }
               HStack {
                  Text("招聘人数：").littleTStyle()
                  Picker("招聘人数", selection: $vm.draftJob.wantNum) { ForEach(kWantNumArr, id: \.self){
                     Text("\($0)人")//字符串插值
                  }
                  }.pStyle()
                  Text("请填写最多招聘人数").size13s()
               }
               
            }.padding().bg()
            // MARK: - 工作时间
            VStack(alignment: .leading,spacing: kStackSpacingS) {
               Text("工作时间").littleTStyle()
               HStack {
                  HStack(spacing: 3) {
                     Picker("起始小时", selection: $vm.draftJob.startHour) { ForEach(0..<25) {Text("\($0)")}
                     }.pStyle()
                     Text(":").conlonStyle()
                     Picker("起始分钟", selection: $vm.draftJob.startMin) { ForEach([0,30], id: \.self) {Text($0 == 0 ? "00":"\($0)")}
                     }.pStyle()
                     
                     Image(systemName: "minus")
                     
                     Picker("结束小时", selection: $vm.draftJob.endHour) { ForEach(0..<25) {Text("\($0)")}
                     }.pStyle()
                     Text(":").conlonStyle()
                     Picker("结束分钟", selection: $vm.draftJob.endMin) { ForEach([0,30], id: \.self) {Text($0 == 0 ? "00":"\($0)")}
                     }.pStyle()
                  }
                  Button {
                     withAnimation {
                        vm.draftJob.has2.toggle()
                     }
                  } label: {
                     Image(systemName: vm.draftJob.has2 ? "minus.circle.fill" : "plus.circle.fill")
                        .foregroundStyle(vm.draftJob.has2 ? .red : .accent)
                  }.font(.title2).padding(.horizontal,3)
               }
               
               if(vm.draftJob.has2){
                  HStack(spacing: 3) {
                     Picker("第二个起始小时", selection: $vm.draftJob.startHour2) { ForEach(0..<25) {Text("\($0)")}
                     }.pStyle()
                     Text(":").conlonStyle()
                     Picker("第二个起始分钟2", selection: $vm.draftJob.startMin2) { ForEach([0,30], id: \.self) {Text($0 == 0 ? "00":"\($0)")}
                     }.pStyle()
                     
                     Image(systemName: "minus")
                     
                     Picker("第二个结束小时2", selection: $vm.draftJob.endHour2) { ForEach(0..<25) {Text("\($0)")}
                     }.pStyle()
                     Text(":").conlonStyle()
                     Picker("第二个结束分钟2", selection: $vm.draftJob.endMin2) { ForEach([0,30], id: \.self) {Text($0 == 0 ? "00":"\($0)")}
                     }.pStyle()
                  }
               }
               
               Text("追加说明").littleTStyle()
               TextField("具体时间段可以详谈", text: $vm.draftJob.otherTime, axis: .vertical).tfStyleMultiS()
            }.push(to: .leading).padding().bg()
            // MARK: - 工作要求
            VStack(alignment: .leading,spacing: kStackSpacing) {
               HStack {
                  VStack(alignment: .leading) {
                     Text("经验要求").littleTStyle()
                     Picker("经验要求", selection: $vm.draftJob.needExp) { ForEach(kNeedExpArr, id: \.self) {Text($0)}
                     }.pStyle()
                  }
                  VStack(alignment: .leading) {
                     Text("学历要求").littleTStyle()
                     Picker("学历要求", selection: $vm.draftJob.needEdu) { ForEach(kNeedEduArr, id: \.self) {Text($0)}
                     }.pStyle()
                  }
               }
               HStack {
                  VStack(alignment: .leading) {
                     Text("语言要求").littleTStyle()
                     Picker("学历要求", selection: $vm.draftJob.needLan) { ForEach(kNeedLanArr, id: \.self) {Text($0)}
                     }.pStyle()
                  }
                  VStack(alignment: .leading) {
                     Text("框架要求").littleTStyle()
                     Picker("学历要求", selection: $vm.draftJob.needFrame) { ForEach(kNeedFrameArr, id: \.self) {Text($0)}
                     }.pStyle()
                  }
               }
               VStack(alignment: .leading) {
                  Text("更多要求/加分项（选填）").littleTStyle()
                  TextField("", text: $vm.draftJob.otherNeed, axis: .vertical).tfStyleMultiM()
               }
            }.padding().bg()
            // MARK: - 工作内容
            VStack(alignment: .leading) {
               Text("工作内容（选填）").littleTStyle()
               TextField("", text: $vm.draftJob.workContent, axis: .vertical).tfStyleMultiM()

            }.padding().bg()
            // MARK: - 公司福利
            VStack(alignment: .leading,spacing: kStackSpacing) {
               Text("公司福利").littleTStyle()
               HStack {
                  Text("交通费报销").onTapGesture {
                     vm.draftJob.moveFee.toggle()
                  }.toggleStyle(isTap: vm.draftJob.moveFee)
                  Text("不加班").onTapGesture {
                     vm.draftJob.noOverTime.toggle()
                  }.toggleStyle(isTap: vm.draftJob.noOverTime)
               }
               VStack(alignment: .leading) {
                  Text("更多福利（选填）").littleTStyle()
                  TextField("", text: $vm.draftJob.otherBenefit, axis: .vertical).tfStyleMultiL()
               }
            }.padding().bg()
            // MARK: - 工作地点
            VStack(alignment: .leading,spacing: kStackSpacing) {
               VStack(alignment: .leading) {
                  Text("工作地名称").littleTStyle()
                  TextField("", text: $vm.draftJob.placeName, axis: .vertical).tfStyle()
                  
                  if !workPlaceName.isEmpty {
                     HStack(spacing: 3) {
                        Text("上次填的：").size13s()
                        Button {
                           vm.draftJob.placeName = workPlaceName
                        } label: {
                           Text(workPlaceName).labelBG()
                        }
                     }
                  }
               }
               HStack {
                  Text("省份").littleTStyle()
                  Picker("省份", selection: $vm.draftJob.province) { ForEach(kProvinceArr, id: \.self) {Text($0)}
                  }.pStyle()
                  if !workProvince.isEmpty {
                     HStack(spacing: 3) {
                        Text("上次填的：").size13s()
                        Button {
                           vm.draftJob.province = workProvince
                        } label: {
                           Text(workProvince).labelBG()
                        }
                     }
                  }
                  
               }
               HStack {
                  Text("城市").littleTStyle()
                  TextField("", text: $vm.draftJob.city, axis: .vertical).tfStyle().frame(width: 115)
                  if !workCity.isEmpty {
                     HStack(spacing: 3) {
                        Text("上次填的：").size13s()
                        Button {
                           vm.draftJob.city = workCity
                        } label: {
                           Text(workCity).labelBG()
                        }
                     }
                  }
               }
//               Button {
//                  workProvince = province
//                  workCity = city
//                  workProvince = province
//               } label: {
//                  Text("存本地")
//               }.btnStyle()
//
            }.padding().bg()
            // MARK: - 应聘方式
            VStack(alignment: .leading,spacing: kStackSpacing){
               Text("联系方式/应聘方式").littleTStyle()
               HStack {
                  Picker("联系方式种类", selection: $vm.draftJob.contactType) {
                     ForEach(kContactTypeArr, id: \.self) {Text($0)}
                  }.pStyle()
                  TextField("", text: $vm.draftJob.contact, axis: .vertical).tfStyle()
               }
               if !workContact.isEmpty {
                  HStack(spacing: 3) {
                     Text("上次填的：").size13s()
                     Button {
                        vm.draftJob.contact = workContact
                     } label: {
                        Text(workContact).labelBG()
                     }
                  }
               }
               
               Text("据调查，99%的人才都喜欢使用微信联系").size13().red()
            }.padding().bg()
            
            // MARK: - 提交按钮
            Button {
//               hud.show("提交成功")
               postJob()
            } label: {
               Text("提交").appleStyle()
            }.padding().bg()
         }.tc().bg2()
         postBottomEView().bg()
      }
   }
}

#Preview {
   PostPLJobIView(naviPath: .constant(NavigationPath()))
}
