//
//  XinCaiGouViewController.swift
//  GeliBusinessPlatform
//
//  Created by junye lu on 2020/4/29.
//  Copyright © 2020 junye lu. All rights reserved.
//

import UIKit

class XinCaiGouViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource,GLAlertSelectViewDelegate,AddGuiGeCellDelegate{
    
    var glSelectView:GLAlertSelectView? = nil
    
    @IBOutlet weak var lineView: UIView!
    @IBAction func btnAction(_ sender: Any) {
        //sender.tag 0:提交 1:提交并新增
    }
    
    @IBOutlet weak var listView: UITableView!
    
    var isEdit:Bool?
    //测试数据
    var data:NSDictionary? = nil
    var gouWuType:Int? //购物类型
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //编辑状态
        //        isEdit = true
        //        data = NSDictionary()
        //
        
        navbar.title = "新增采购单"
        self.view.addSubview(navbar)
        
        listView.separatorStyle = .none
        SetTopFrameView(view: listView, btmView: lineView)
        listView.register(UINib(nibName: "TitleAndTFCell", bundle: nil), forCellReuseIdentifier: "TitleAndTFCell")
        listView.register(UINib(nibName: "TitleAndBtnCell", bundle: nil), forCellReuseIdentifier: "TitleAndBtnCell")
        listView.register(UINib(nibName: "AddGuiGeCell", bundle: nil), forCellReuseIdentifier: "AddGuiGeCell")
        listView.register(UINib(nibName: "RuKuJiaGeCell", bundle: nil), forCellReuseIdentifier: "RuKuJiaGeCell")
        listView.register(UINib(nibName: "DaiKeXiaDanGouWuCell", bundle: nil), forCellReuseIdentifier: "DaiKeXiaDanGouWuCell")
    }
    //MARK: - CELL DELEGATE
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 1 {
            let view = UIView(frame: CGRect(x: 0, y: 0, width: fullScreenWidth, height: 36))
            view.backgroundColor = UIColor.white
            let title = UILabel()
            title.text = "商品清单"
            title.font = UIFont.boldSystemFont(ofSize: 15)
            title.textColor = UIColor(named: "标题字颜色")
            view.addSubview(title)
            title.sizeToFit()
            title.snp.makeConstraints { (make) in
                make.left.equalTo(15)
                make.height.equalTo(21)
                make.width.equalTo(title.bounds.size.width)
                make.bottom.equalTo(0)
            }
            let ti1 = UILabel()
            ti1.text = "(仅支持采购一种商品)"
            ti1.font = UIFont.boldSystemFont(ofSize: 11)
            ti1.textColor = UIColor(named: "灰色字体颜色")
            view.addSubview(ti1)
            ti1.sizeToFit()
            ti1.snp.makeConstraints { (make) in
                make.left.equalTo(title.snp_right)
                make.height.equalTo(15)
                make.width.equalTo(ti1.bounds.size.width)
                make.bottom.equalTo(0)
            }
            return view
        }
        return UIView()
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 1 {
            return 38
        }
        return 0.001
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if isEdit == true /*&& 有商品 */{
            if section == 1 {
                return 87
            }
        }
        return 10
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        if isEdit == true && data != nil {
            if section == 1 {
                let view = UIView(frame: CGRect(x: 0, y: 0, width: fullScreenWidth, height: 87))
                let bgV = UIView()
                bgV.backgroundColor = UIColor.white
                view.addSubview(bgV)
                bgV.snp.makeConstraints { (make) in
                    make.top.left.right.equalTo(0)
                    make.height.equalTo(77)
                }
                let line = UIView()
                line.backgroundColor = UIColor(named: "灰色分界线")
                view.addSubview(line)
                line.snp.makeConstraints { (make) in
                    make.top.right.equalTo(0)
                    make.left.equalTo(15)
                    make.height.equalTo(1)
                }
                
                let countLbl = UILabel()
                countLbl.text = "abc"
                countLbl.font = UIFont.boldSystemFont(ofSize: 13)
                countLbl.textColor = UIColor(named: "蓝色字体颜色")
                countLbl.sizeToFit()
                view.addSubview(countLbl)
                countLbl.snp.makeConstraints { (make) in
                    make.right.equalTo(-15)
                    make.top.equalTo(15)
                    make.height.equalTo(18.5)
                    make.width.equalTo(countLbl.bounds.size.width)
                }
                let countTLbl = UILabel()
                countTLbl.text = "商品合计:"
                countTLbl.font = UIFont.systemFont(ofSize: 13)
                countTLbl.textAlignment = .right
                countTLbl.textColor = UIColor(named: "标题字颜色")
                view.addSubview(countTLbl)
                countTLbl.snp.makeConstraints { (make) in
                    make.right.equalTo(countLbl.snp_left)
                    make.left.top.equalTo(15)
                    make.height.equalTo(18.5)
                }
                
                let countLbl2 = UILabel()
                countLbl2.text = "a123123123bc"
                countLbl2.font = UIFont.systemFont(ofSize: 13)
                countLbl2.textColor = UIColor(named: "蓝色字体颜色")
                countLbl2.sizeToFit()
                view.addSubview(countLbl2)
                countLbl2.snp.makeConstraints { (make) in
                    make.right.equalTo(-15)
                    make.top.equalTo(countTLbl.snp_bottom).offset(10)
                    make.height.equalTo(18.5)
                    make.width.equalTo(countLbl2.bounds.size.width)
                }
                
                let countTLbl2 = UILabel()
                countTLbl2.text = "订单合计:"
                countTLbl2.font = UIFont.systemFont(ofSize: 13)
                countTLbl2.textAlignment = .right
                countTLbl2.textColor = UIColor(named: "标题字颜色")
                view.addSubview(countTLbl2)
                countTLbl2.snp.makeConstraints { (make) in
                    make.right.equalTo(countLbl2.snp_left)
                    make.centerY.equalTo(countLbl2.snp_centerY)
                    make.height.equalTo(18.5)
                }
                
                return view
            }
        }
        let view = UIView(frame: CGRect(x: 0, y: 0, width: fullScreenWidth, height: 10))
        return view
    }
    let p1Arr = ["采购单号:","采购日期:","供应商:","采购人:","制单人:","采购类型:"]
    let p3Arr = ["采购数量:","采购单价:","采购金额:"]
    
    let hold1Arr = ["请输入采购单号","请选择采购日期","请选择供应商","请输入采购人姓名","请输入制单人姓名","请选择采购类型"]
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:return 6
        case 1:return 1
        default:return 3
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 1 {
            return 123.5
        }
        return 49
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            if indexPath.row == 2{//跳转供应商选择
                let vc = GYSListViewController()
                vc.barTitle = "请选择供应商"
                self.navigationController?.pushViewController(vc, animated: true)
            }
            if indexPath.row == 5 {
                if glSelectView == nil {
                    let view = GLAlertSelectView(frame: self.view.bounds)
//                    view.isMultipleSelect = false//是否多选
                    view.selectNum = gouWuType!
                    view.delegate = self
                    view.titleLbl.text = "请选择购物类型"
                    view.dataArr = ["普通订单","预售下单"]
                    self.view.addSubview(view)
                    glSelectView = view
                }
            }
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
        case 0:
            if indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 5 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "TitleAndBtnCell") as! TitleAndBtnCell
                cell.nameLbl.text = p1Arr[indexPath.row]
                cell.contentLbl.text = hold1Arr[indexPath.row]
                cell.btn.isUserInteractionEnabled = false
                return cell
            }
            let cell = tableView.dequeueReusableCell(withIdentifier: "TitleAndTFCell") as! TitleAndTFCell
            cell.nameLbl.text = p1Arr[indexPath.row]
            cell.textTF.placeholder = hold1Arr[indexPath.row]
            return cell
        case 1:
            if data != nil{//有数据
                let cell = tableView.dequeueReusableCell(withIdentifier: "DaiKeXiaDanGouWuCell") as! DaiKeXiaDanGouWuCell
                cell.delImg.isHidden = true
                cell.delBtn.isHidden = true
                cell.changeBtn.isHidden = false
                return cell
            }
            let cell = tableView.dequeueReusableCell(withIdentifier: "AddGuiGeCell") as! AddGuiGeCell
            cell.imgBtn.setImage(UIImage(named: "tianjiashangpin"), for: .normal)
            cell.delegate = self
            return cell
        default:
            if indexPath.row == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "TitleAndTFCell") as! TitleAndTFCell
                cell.nameLbl.text = p3Arr[indexPath.row]
                cell.textTF.placeholder = "请输入采购数量"
                return cell
            }else{
                let cell = tableView.dequeueReusableCell(withIdentifier: "RuKuJiaGeCell") as! RuKuJiaGeCell
                cell.nameLbl.text = p3Arr[indexPath.row]
                return cell
            }
        }
    }
    //MARK: - 自定义Delegate
    
    func AddGuiGeCellClick(cell: AddGuiGeCell) {
        print("AddGuiGeCellClick")
    }
    
    func GLAlertSelectViewClick(selectNum: Int, view: GLAlertSelectView) {
        print("单选",selectNum)
        glSelectView?.removeFromSuperview()
        glSelectView = nil
    }
    
    func GLAlertMoreSelectViewClick(sender: NSArray, view:UIView) {
        print("多选",sender)
        glSelectView?.removeFromSuperview()
        glSelectView = nil
    }
    func GLAlertSelectViewClose(sender: UIButton) {
        glSelectView?.removeFromSuperview()
        glSelectView = nil
    }
}