FormEditor.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //
  2. // FormEditor.swift
  3. // NexilisLite
  4. //
  5. // Created by Qindi on 20/05/22.
  6. //
  7. import UIKit
  8. import NotificationBannerSwift
  9. import nuSDKService
  10. class FormEditor: UIViewController {
  11. var jsonData = ""
  12. var dataMessage: [String: Any?] = [:]
  13. var dataPerson: [String: String?] = [:]
  14. var isAorR: Int?
  15. var dateApproved = ""
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. let containerView = UIView()
  19. self.view.addSubview(containerView)
  20. containerView.backgroundColor = .white
  21. containerView.anchor(top: self.view.topAnchor, left: self.view.leftAnchor, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, paddingTop: 50, paddingLeft: 40, paddingBottom: 100, paddingRight: 40)
  22. let viewTitle = UIView()
  23. viewTitle.backgroundColor = .mainColor
  24. containerView.addSubview(viewTitle)
  25. viewTitle.anchor(top: containerView.topAnchor, left: containerView.leftAnchor, right: containerView.rightAnchor, height: 40)
  26. if let json = try! JSONSerialization.jsonObject(with: jsonData.data(using: String.Encoding.utf8)!, options: []) as? [String: Any] {
  27. let title = UILabel()
  28. title.text = (json["form_title"] as! String).replacingOccurrences(of: "+", with: " ")
  29. title.font = .systemFont(ofSize: 15.0, weight: .bold)
  30. title.textColor = .white
  31. viewTitle.addSubview(title)
  32. title.anchor(centerX: viewTitle.centerXAnchor, centerY: viewTitle.centerYAnchor)
  33. if checkForm(json: json){
  34. buildRorAView(json: json, viewTitle: viewTitle, containerView: containerView)
  35. } else {
  36. buildWaitingApprovalView(json: json, viewTitle: viewTitle, containerView: containerView)
  37. }
  38. let buttonClose = UIButton()
  39. buttonClose.frame.size = CGSize(width: 50, height: 50)
  40. buttonClose.layer.cornerRadius = 25
  41. buttonClose.clipsToBounds = true
  42. buttonClose.setBackgroundImage(UIImage(systemName: "xmark.circle"), for: .normal)
  43. buttonClose.tintColor = .mainColor
  44. buttonClose.actionHandle(controlEvents: .touchUpInside,
  45. ForAction:{() -> Void in
  46. self.dismiss(animated: true, completion: nil)
  47. })
  48. self.view.addSubview(buttonClose)
  49. buttonClose.anchor(top: containerView.bottomAnchor, centerX: self.view.centerXAnchor, width: 50, height: 50)
  50. }
  51. }
  52. private func checkForm(json: [String: Any]) -> Bool {
  53. var result = false
  54. // Database.shared.database?.inTransaction({ (fmdb, rollback) in
  55. // if let dataForm = Database().getRecords(fmdb: fmdb, query: "select sq_no, created_date from FORM where form_id = '\(dataMessage["reff_id"] as! String)'"), dataForm.next() {
  56. // result = true
  57. // isAorR = Int(dataForm.int(forColumnIndex: 0))
  58. // dateApproved = dataForm.string(forColumnIndex: 1)!
  59. // }
  60. // })
  61. return result
  62. }
  63. private func buildRorAView(json: [String: Any], viewTitle: UIView, containerView: UIView) {
  64. if isAorR != nil {
  65. let viewStatus = UIView()
  66. containerView.addSubview(viewStatus)
  67. viewStatus.anchor(top: viewTitle.bottomAnchor, left: containerView.leftAnchor, right: containerView.rightAnchor, height: 20)
  68. let textStatus = UILabel()
  69. viewStatus.addSubview(textStatus)
  70. textStatus.anchor(centerX: viewStatus.centerXAnchor, centerY: viewStatus.centerYAnchor)
  71. textStatus.font = .systemFont(ofSize: 14, weight: .bold)
  72. textStatus.textColor = .white
  73. let imageProvince = UIImageView()
  74. imageProvince.image = UIImage(systemName: "doc.text.image")
  75. imageProvince.tintColor = .mainColor
  76. imageProvince.contentMode = .scaleToFill
  77. containerView.addSubview(imageProvince)
  78. imageProvince.anchor(top: viewStatus.bottomAnchor, left: containerView.leftAnchor, paddingTop: 15, paddingLeft: 10, width: 20, height: 20)
  79. let province = UILabel()
  80. province.text = "Province".localized()
  81. province.font = .systemFont(ofSize: 14.0, weight: .bold)
  82. province.textColor = .gray
  83. containerView.addSubview(province)
  84. province.anchor(top: viewStatus.bottomAnchor, left: imageProvince.rightAnchor, right: containerView.rightAnchor, paddingTop: 10, paddingLeft: 10)
  85. let provinceFill = UILabel()
  86. provinceFill.text = json["province"] as? String
  87. provinceFill.font = .systemFont(ofSize: 14.0)
  88. provinceFill.textColor = .black
  89. containerView.addSubview(provinceFill)
  90. provinceFill.anchor(top: province.bottomAnchor, left: imageProvince.rightAnchor, right: containerView.rightAnchor, paddingLeft: 10)
  91. let imageClub = UIImageView()
  92. imageClub.image = UIImage(systemName: "doc.text.image")
  93. imageClub.tintColor = .mainColor
  94. imageClub.contentMode = .scaleToFill
  95. containerView.addSubview(imageClub)
  96. imageClub.anchor(top: provinceFill.bottomAnchor, left: containerView.leftAnchor, paddingTop: 15, paddingLeft: 10, width: 20, height: 20)
  97. let club = UILabel()
  98. club.text = "Club".localized()
  99. club.font = .systemFont(ofSize: 14.0, weight: .bold)
  100. club.textColor = .gray
  101. containerView.addSubview(club)
  102. club.anchor(top: provinceFill.bottomAnchor, left: imageClub.rightAnchor, right: containerView.rightAnchor, paddingTop: 10, paddingLeft: 10)
  103. let clubFill = UILabel()
  104. clubFill.text = json["club"] as? String
  105. clubFill.font = .systemFont(ofSize: 14.0)
  106. clubFill.textColor = .black
  107. containerView.addSubview(clubFill)
  108. clubFill.anchor(top: club.bottomAnchor, left: imageClub.rightAnchor, right: containerView.rightAnchor, paddingLeft: 10)
  109. let imageAorR = UIImageView()
  110. imageAorR.image = UIImage(systemName: "person.fill")
  111. imageAorR.tintColor = .mainColor
  112. imageAorR.contentMode = .scaleToFill
  113. containerView.addSubview(imageAorR)
  114. imageAorR.anchor(top: clubFill.bottomAnchor, left: containerView.leftAnchor, paddingTop: 15, paddingLeft: 10, width: 20, height: 20)
  115. let descAorR = UILabel()
  116. descAorR.font = .systemFont(ofSize: 14.0, weight: .bold)
  117. descAorR.textColor = .gray
  118. containerView.addSubview(descAorR)
  119. descAorR.anchor(top: clubFill.bottomAnchor, left: imageAorR.rightAnchor, right: containerView.rightAnchor, paddingTop: 10, paddingLeft: 10)
  120. let name = UILabel()
  121. name.text = User.getData(pin: User.getMyPin())?.fullName
  122. name.font = .systemFont(ofSize: 14.0)
  123. name.textColor = .black
  124. containerView.addSubview(name)
  125. name.anchor(top: descAorR.bottomAnchor, left: imageAorR.rightAnchor, right: containerView.rightAnchor, paddingLeft: 10)
  126. let imageDateAorR = UIImageView()
  127. imageDateAorR.image = UIImage(systemName: "person.fill")
  128. imageDateAorR.tintColor = .mainColor
  129. imageDateAorR.contentMode = .scaleToFill
  130. containerView.addSubview(imageDateAorR)
  131. imageDateAorR.anchor(top: name.bottomAnchor, left: containerView.leftAnchor, paddingTop: 15, paddingLeft: 10, width: 20, height: 20)
  132. let dateAorR = UILabel()
  133. dateAorR.text = "Time".localized()
  134. dateAorR.font = .systemFont(ofSize: 14.0, weight: .bold)
  135. dateAorR.textColor = .gray
  136. containerView.addSubview(dateAorR)
  137. dateAorR.anchor(top: name.bottomAnchor, left: imageAorR.rightAnchor, right: containerView.rightAnchor, paddingTop: 10, paddingLeft: 10)
  138. let dateLabel = UILabel()
  139. let stringDate = dateApproved.isEmpty ? "\(Date().currentTimeMillis())" : dateApproved
  140. let date = Date(milliseconds: Int64(stringDate)!)
  141. let formatter = DateFormatter()
  142. formatter.dateFormat = "dd MMM yyyy, HH:mm"
  143. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  144. dateLabel.text = formatter.string(from: date as Date)
  145. dateLabel.font = .systemFont(ofSize: 14.0)
  146. dateLabel.textColor = .black
  147. containerView.addSubview(dateLabel)
  148. dateLabel.anchor(top: dateAorR.bottomAnchor, left: imageAorR.rightAnchor, right: containerView.rightAnchor, paddingLeft: 10)
  149. if isAorR == 0 {
  150. viewStatus.backgroundColor = .systemRed
  151. textStatus.text = "Rejected".localized()
  152. descAorR.text = "Rejected by".localized()
  153. } else {
  154. viewStatus.backgroundColor = .systemGreen
  155. textStatus.text = "Approved".localized()
  156. descAorR.text = "Approved by".localized()
  157. }
  158. }
  159. }
  160. private func buildWaitingApprovalView(json: [String: Any], viewTitle: UIView, containerView: UIView) {
  161. let imageRequested = UIImageView()
  162. imageRequested.image = UIImage(systemName: "person.fill")
  163. imageRequested.tintColor = .mainColor
  164. imageRequested.contentMode = .scaleToFill
  165. containerView.addSubview(imageRequested)
  166. imageRequested.anchor(top: viewTitle.bottomAnchor, left: containerView.leftAnchor, paddingTop: 15, paddingLeft: 10, width: 20, height: 20)
  167. let requestedBy = UILabel()
  168. requestedBy.text = "Requested by".localized()
  169. requestedBy.font = .systemFont(ofSize: 14.0, weight: .bold)
  170. requestedBy.textColor = .gray
  171. containerView.addSubview(requestedBy)
  172. requestedBy.anchor(top: viewTitle.bottomAnchor, left: imageRequested.rightAnchor, right: containerView.rightAnchor, paddingTop: 10, paddingLeft: 10)
  173. let name = UILabel()
  174. name.text = dataPerson["name"] as? String
  175. name.font = .systemFont(ofSize: 14.0)
  176. name.textColor = .black
  177. containerView.addSubview(name)
  178. name.anchor(top: requestedBy.bottomAnchor, left: imageRequested.rightAnchor, right: containerView.rightAnchor, paddingLeft: 10)
  179. let imageClubType = UIImageView()
  180. imageClubType.image = UIImage(systemName: "doc.text.image")
  181. imageClubType.tintColor = .mainColor
  182. imageClubType.contentMode = .scaleToFill
  183. containerView.addSubview(imageClubType)
  184. imageClubType.anchor(top: name.bottomAnchor, left: containerView.leftAnchor, paddingTop: 15, paddingLeft: 10, width: 20, height: 20)
  185. let clubType = UILabel()
  186. clubType.text = "Club Type".localized()
  187. clubType.font = .systemFont(ofSize: 14.0, weight: .bold)
  188. clubType.textColor = .gray
  189. containerView.addSubview(clubType)
  190. clubType.anchor(top: name.bottomAnchor, left: imageClubType.rightAnchor, right: containerView.rightAnchor, paddingTop: 10, paddingLeft: 10)
  191. let clubTypeFill = UILabel()
  192. clubTypeFill.text = json["club_type"] as? String
  193. clubTypeFill.font = .systemFont(ofSize: 14.0)
  194. clubTypeFill.textColor = .black
  195. containerView.addSubview(clubTypeFill)
  196. clubTypeFill.anchor(top: clubType.bottomAnchor, left: imageClubType.rightAnchor, right: containerView.rightAnchor, paddingLeft: 10)
  197. let imageProvince = UIImageView()
  198. imageProvince.image = UIImage(systemName: "doc.text.image")
  199. imageProvince.tintColor = .mainColor
  200. imageProvince.contentMode = .scaleToFill
  201. containerView.addSubview(imageProvince)
  202. imageProvince.anchor(top: clubTypeFill.bottomAnchor, left: containerView.leftAnchor, paddingTop: 15, paddingLeft: 10, width: 20, height: 20)
  203. let province = UILabel()
  204. province.text = "Province".localized()
  205. province.font = .systemFont(ofSize: 14.0, weight: .bold)
  206. province.textColor = .gray
  207. containerView.addSubview(province)
  208. province.anchor(top: clubTypeFill.bottomAnchor, left: imageProvince.rightAnchor, right: containerView.rightAnchor, paddingTop: 10, paddingLeft: 10)
  209. let provinceFill = UILabel()
  210. provinceFill.text = json["province"] as? String
  211. provinceFill.font = .systemFont(ofSize: 14.0)
  212. provinceFill.textColor = .black
  213. containerView.addSubview(provinceFill)
  214. provinceFill.anchor(top: province.bottomAnchor, left: imageProvince.rightAnchor, right: containerView.rightAnchor, paddingLeft: 10)
  215. let imageClub = UIImageView()
  216. imageClub.image = UIImage(systemName: "doc.text.image")
  217. imageClub.tintColor = .mainColor
  218. imageClub.contentMode = .scaleToFill
  219. containerView.addSubview(imageClub)
  220. imageClub.anchor(top: provinceFill.bottomAnchor, left: containerView.leftAnchor, paddingTop: 15, paddingLeft: 10, width: 20, height: 20)
  221. let club = UILabel()
  222. club.text = "Club".localized()
  223. club.font = .systemFont(ofSize: 14.0, weight: .bold)
  224. club.textColor = .gray
  225. containerView.addSubview(club)
  226. club.anchor(top: provinceFill.bottomAnchor, left: imageClub.rightAnchor, right: containerView.rightAnchor, paddingTop: 10, paddingLeft: 10)
  227. let clubFill = UILabel()
  228. clubFill.text = json["club"] as? String
  229. clubFill.font = .systemFont(ofSize: 14.0)
  230. clubFill.textColor = .black
  231. containerView.addSubview(clubFill)
  232. clubFill.anchor(top: club.bottomAnchor, left: imageClub.rightAnchor, right: containerView.rightAnchor, paddingLeft: 10)
  233. let buttonReject = UIButton()
  234. buttonReject.backgroundColor = .systemRed
  235. buttonReject.setImage(UIImage(systemName: "xmark"), for: .normal)
  236. buttonReject.tintColor = .white
  237. buttonReject.setTitle("Reject".localized(), for: .normal)
  238. buttonReject.titleLabel?.font = .boldSystemFont(ofSize: 15)
  239. buttonReject.layer.cornerRadius = 8
  240. containerView.addSubview(buttonReject)
  241. buttonReject.anchor(left: containerView.leftAnchor, bottom: containerView.bottomAnchor, paddingLeft: 10, paddingBottom: 10, width: (self.view.bounds.width / 2) - 55, height: 50)
  242. buttonReject.tag = 0
  243. buttonReject.addTarget(self, action: #selector(rejectApproveForm(_:)), for: .touchUpInside)
  244. let buttonAccept = UIButton()
  245. buttonAccept.backgroundColor = .systemGreen
  246. buttonAccept.setImage(UIImage(systemName: "checkmark"), for: .normal)
  247. buttonAccept.tintColor = .white
  248. buttonAccept.setTitle("Approve".localized(), for: .normal)
  249. buttonAccept.titleLabel?.font = .boldSystemFont(ofSize: 15)
  250. buttonAccept.layer.cornerRadius = 8
  251. containerView.addSubview(buttonAccept)
  252. buttonAccept.anchor(bottom: containerView.bottomAnchor, right: containerView.rightAnchor, paddingBottom: 10, paddingRight: 10, width: (self.view.bounds.width / 2) - 55, height: 50)
  253. buttonAccept.tag = 1
  254. buttonAccept.addTarget(self, action: #selector(rejectApproveForm(_:)), for: .touchUpInside)
  255. }
  256. @objc func rejectApproveForm(_ sender: UIButton) {
  257. let refId = dataMessage["reff_id"] as? String
  258. let isApprove = sender.tag
  259. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  260. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  261. imageView.tintColor = .white
  262. let banner = FloatingNotificationBanner(title: "Check your connection".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  263. banner.show()
  264. return
  265. }
  266. DispatchQueue.global().async { [self] in
  267. let idMe = User.getMyPin()!
  268. let resp = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getFormApproval(p_f_pin: idMe, p_ref_id: refId ?? "", p_approve: "\(isApprove)", p_note: "", p_sign: ""))
  269. if resp != nil {
  270. // Database.shared.database?.inTransaction({ (fmdb, rollback) in
  271. // do {
  272. // _ = try Database.shared.insertRecord(fmdb: fmdb, table: "FORM", cvalues: [
  273. // "form_id" : refId ?? "",
  274. // "name" : "",
  275. // "created_date" : "\(Date().currentTimeMillis())",
  276. // "created_by" : dataPerson["f_pin"]!!,
  277. // "sq_no" : isApprove,
  278. // ], replace: true)
  279. // } catch {
  280. // rollback.pointee = true
  281. // //print(error)
  282. // }
  283. // })
  284. DispatchQueue.main.async {
  285. self.view.subviews.forEach({ $0.removeFromSuperview() })
  286. self.viewDidLoad()
  287. }
  288. } else {
  289. DispatchQueue.main.async {
  290. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  291. imageView.tintColor = .white
  292. let banner = FloatingNotificationBanner(title: "Unable to access servers".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  293. banner.show()
  294. }
  295. }
  296. }
  297. }
  298. }