//
//  OrderDetailPage.swift
//  Garmentoo
//
//  Created by ANGEL on 20/04/22.
//

import UIKit

class OrderDetailPage: UIViewController {

    @IBOutlet weak var statusLabel: UILabel!
    @IBOutlet weak var submitBtnOutlet: UIButton!
    @IBOutlet weak var ratingReminderView: UIView!
    
    var selectedCatIndex = 0
    
    @IBOutlet weak var itemTable: UITableView!
    @IBOutlet weak var tableHeight: NSLayoutConstraint!
    
    @IBOutlet weak var deliverDateLabel: UILabel!
    @IBOutlet weak var deliverAddressHeaderLbl: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if self.selectedCatIndex == 1
        {
            statusLabel.text = "Delivered"
            self.submitBtnOutlet.isHidden = true
            self.ratingReminderView.isHidden = false
            self.deliverDateLabel.isHidden = false
            self.deliverAddressHeaderLbl.text = "Delivered To"
        }
        else
        {
            statusLabel.text = "Ongoing"
            self.submitBtnOutlet.isHidden = false
            self.ratingReminderView.isHidden = true
            self.deliverDateLabel.isHidden = true
            self.deliverAddressHeaderLbl.text = "Delivery To"
        }
    }
    override func viewWillLayoutSubviews() {
        self.tableHeight.constant = self.itemTable.contentSize.height
    }
    @IBAction func backBtnTapped(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    @IBAction func finalBtnTapped(_ sender: Any) {
        if self.submitBtnOutlet.titleLabel?.text == "Track Your Order"
        {
           
            let destViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TrackingVC") as! TrackingVC
            destViewController.hidesBottomBarWhenPushed = true
            
            self.navigationController?.pushViewController(destViewController, animated: true)
        }
    }
    
    
}

extension OrderDetailPage : UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "orderDetailList")as! OrderDetailListTVC
        if self.selectedCatIndex == 0
        {
            cell.addReviewView.isHidden = true
        }
        else
        {
            cell.addReviewView.isHidden = false
        }
        cell.addReviewBtn.addTarget(self, action: #selector(reviewOrder(_:)), for: .touchUpInside)
        
        return cell
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        viewWillLayoutSubviews()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140
    }
    @objc func reviewOrder(_ sender: UIButton?)
    {
       
        let destViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AddOrderReviewVC") as! AddOrderReviewVC
        
        destViewController.hidesBottomBarWhenPushed = true
        
        self.navigationController?.pushViewController(destViewController, animated: true)
    }
    
}
