//
//  TiXianDetailViewController.swift
//  xingchuangke
//
//  Created by Virgil on 2019/4/21.
//  Copyright © 2019 Virgil. All rights reserved.
//

import UIKit

class TiXianDetailViewController: BaseViewController, UITableViewDataSource, UITableViewDelegate, SDPhotoBrowserDelegate {

    var id = ""
    @IBOutlet weak var tableView: UITableView!

    var arrTitle = [["状态", "拒绝原因", "提现金额","应到账金额", "提现时间"], ["开户名", "银行卡号", "所属银行", "所属支行"], ["发票照片"], ["快递公司", "快递号"]]
    var arrContent = [["状态", "拒绝原因", "提现金额","应到账金额", "提现时间"], ["开户名", "银行卡号", "所属银行", "所属支行"], ["http://t8.baidu.com/it/u=3660968530,985748925&fm=191&app=48&size=h300&n=0&g=4n&f=JPEG?sec=1853310920&t=5e64af964be378c6c2a3b0acc65dfe24"], ["快递公司", "快递号"]]
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "提现详情"
        initNavLeftBackButton()

        let status = getIntValue(key: "status")
        if status == 3 {
            arrTitle = [["状态", "拒绝原因", "提现金额","应到账金额", "提现时间"], ["开户名", "银行卡号", "所属银行", "所属支行"], ["发票照片"], ["快递公司", "快递号"]]
            arrContent[0] = ["", getString(key: "remark"), "\(getDoubleValue(key: "amount_money"))","\(getDoubleValue(key: "realy_amount"))", getString(key: "createtime")]
        } else {
            arrTitle = [["状态", "提现金额","应到账金额", "提现时间"], ["开户名", "银行卡号", "所属银行", "所属支行"], ["发票照片"], ["快递公司", "快递号"]]
            arrContent[0] = ["", "\(getDoubleValue(key: "amount_money"))","\(getDoubleValue(key: "realy_amount"))", getString(key: "createtime")]
        }

        arrContent[1] = [getString(key: "bank_account_name"), getString(key: "bank_card_num"), getString(key: "bank_name"), getString(key: "bank_sub_branch")]
        arrContent[2] = [getString(key: "invoice_photo")]
        arrContent[3] = [getString(key: "express_name"), getString(key: "express_number")]

        tableView.register(UINib(nibName: "TiXianDetailTableViewCell", bundle: nil), forCellReuseIdentifier: "TiXianDetailTableViewCell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        tableView.estimatedRowHeight = 100

        tableView.reloadData()
    }

    // MARK: TABLEVLEW 实现
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.arrTitle.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.arrTitle[section].count
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 10))
        view.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xf7f7f9, alpha: 1.0)
        return view
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if arrTitle[indexPath.section][indexPath.row] == "发票照片" {
            return 87
        } else {
            return 55
        }
    }

    var imgTemp: UIImageView!
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TiXianDetailTableViewCell", for: indexPath as IndexPath) as! TiXianDetailTableViewCell
        cell.lblTitle.textColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x666666, alpha: 1.0)
        cell.lblTitle.text = arrTitle[indexPath.section][indexPath.row]

        if arrTitle[indexPath.section][indexPath.row] == "发票照片" {
            cell.btnImage.addTarget(self, action: #selector(centerBtnAction), for: .touchUpInside)
            cell.btnImage.isHidden = false
            imgTemp = cell.imgRight
            cell.imgRight.isHidden = false
            cell.lblContent.isHidden = true
            cell.imgRight.loadImage(imgUrl: arrContent[indexPath.section][indexPath.row], defaultImage: "")
            cell.imgRight.setCornerRadius(size: 4)
        } else {
            cell.btnImage.isHidden = true
            cell.imgRight.isHidden = true
            cell.lblContent.isHidden = false
            cell.lblContent.text = arrContent[indexPath.section][indexPath.row]
            if arrTitle[indexPath.section][indexPath.row] == "状态" {
                let status = getIntValue(key: "status")
                if status == 0 {
                    cell.lblContent.text = "待审核"
                    cell.lblContent.textColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x999999, alpha: 1.0)
                } else if status == 1 {
                    cell.lblContent.text = "待打款"
                    cell.lblContent.textColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xfc9005, alpha: 1.0)
                } else if status == 2 {
                    cell.lblContent.text = "已打款"
                    cell.lblContent.textColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x16b006, alpha: 1.0)
                } else if status == 3 {
                    cell.lblContent.text = "未通过"
                    cell.lblContent.textColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xff0000, alpha: 1.0)
                }
            }
        }
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //        let view = UIViewController()
        //        view.oneGoId = self.arrData[indexPath.row]["ItemId"] as! Int
        //        self.navigationController?.pushViewController(view, animated: true)
    }

    @IBAction func centerBtnAction(sender: UIButton) {
        let sd  =  SDPhotoBrowser()
        sd.tag = 0
        viewTopImage = sender.superview!
        arrImagePaths.removeAll()
        arrImagePaths.append(getString(key: "invoice_photo"))
        sd.sourceImagesContainerView = viewTopImage
        sd.imageCount = arrImagePaths.count
        sd.currentImageIndex = 0
        sd.delegate = self
        sd.show()
    }

    var viewTopImage = UIView()
    var arrImagePaths = [String]()
    // MARK: 图片
    func photoBrowser(_ browser: SDPhotoBrowser!, placeholderImageFor index: Int) -> UIImage! {
        return imgTemp.image!
    }
    func photoBrowser(_ browser: SDPhotoBrowser!, highQualityImageURLFor index: Int) -> URL! {
        return  URL(string: arrImagePaths[index])

    }
    func photoBrowser(_ browser: SDPhotoBrowser!, select index: Int) {

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: "TiXianDetailViewController", bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}