//
//  ConditionalSearchView.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/12/29.
//

import Foundation
import DDControlsKit_Private
import DDAutoUIKit_Private
import DDColorKit_Private

class ConditionalSearchView : DDView {
    public var titleLabel : DDLabel
    public var scrollView : DDScrollView
    public var scrollContentView : DDView
    public var bottomView : DDView
    public var resetButton : DDButton
    public var searchButton : DDButton
    public var exitButton : DDButton
    
    public let orderInfoTitleLabel : DDLabel
    public let orderCodeTextFiled : DDTextField
    public let licenseTextField : DDTextField
    public let carOwnerTextField : DDTextField
    
    public let contractTitleLabel : DDLabel
    public let contractTextFiled : DDTextField
    public let contractTypeButton : DDButton

    public let dateTitleLabel : DDLabel
    public let fromDateButton : DDButton
    public let toDateButton : DDButton
    
    public let serviceTitleLabel : DDLabel
    public let serviceTextField : DDTextField
    public let serviceAppointButton : DDButton
    
    public let controlBackgroundColor = UIColor.hex("000000").alpha(0.1)
    public let noneColor = UIColor.hex("000000").alpha(0.2)
    public let valueColor = UIColor.black
    public let noneFont = UIFont.mediumFont(14)
    public let valueFont = UIFont.mediumFont(14)
    
    let layerView = UIView()
    let borderLayer1 = CAGradientLayer()
    let bottomSafeArea : CGFloat = 0.0
    
    public override init(frame: CGRect) {
        titleLabel = DDLabel.dd_init(withText: "订单筛选", font: .mediumFont(17), textColor: .hex("000000").alpha(0.8))
        bottomView = DDView.init()
        scrollView = DDScrollView.init()
        scrollContentView = DDView.init()
        resetButton = DDButton.dd_initCustom()
        searchButton = DDButton.dd_initCustom()
        exitButton = DDButton.dd_initCustom()

        orderInfoTitleLabel = DDLabel.dd_init(withText: "任务信息", font: .mediumFont(16), textColor: .hex("000000").alpha(0.8))
        orderCodeTextFiled = DDTextField.init()
        licenseTextField = DDTextField.init()
        carOwnerTextField = DDTextField.init()
        
        contractTitleLabel = DDLabel.dd_init(withText: "合同信息", font: .mediumFont(16), textColor: .hex("000000").alpha(0.8))
        contractTextFiled = DDTextField.init()
        contractTypeButton = DDButton.dd_initCustom()
        
        dateTitleLabel = DDLabel.dd_init(withText: "事发时间", font: .mediumFont(16), textColor: .hex("000000").alpha(0.8))
        fromDateButton = DDButton.dd_initCustom()
        toDateButton = DDButton.dd_initCustom()
        
        serviceTitleLabel = DDLabel.dd_init(withText: "服务内容", font: .mediumFont(16), textColor: .hex("000000").alpha(0.8))
        serviceTextField = DDTextField.init()
        serviceAppointButton = DDButton.dd_initCustom()
        super.init(frame: frame)
        
        backgroundColor = .white
        resetButton.setTitle("重置", for: .normal)
        resetButton.titleLabel?.font = .mediumFont(15)
        resetButton.setTitleColor(.hex("000000"), for: .normal)
        resetButton.dd_setBackgroundColor(controlBackgroundColor)
        searchButton.setTitle("搜索", for: .normal)
        searchButton.titleLabel?.font = .mediumFont(15)
        searchButton.dd_setBackgroundColor(.hex("2A5094"))
        searchButton.layer.cornerRadius = auto(20)
        layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]
        layer.masksToBounds = true
        layer.cornerRadius = auto(16)
 
        addSubview(bottomView)
        bottomView.snp.remakeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(auto(80))
            make.bottom.equalTo(0)
        }
        
        resetButton.layer.maskedCorners = [.layerMinXMinYCorner,.layerMinXMaxYCorner]
        resetButton.layer.masksToBounds = true
        resetButton.layer.cornerRadius = auto(20)
        bottomView.addSubview(resetButton)
        searchButton.layer.maskedCorners = [.layerMaxXMinYCorner,.layerMaxXMaxYCorner]
        searchButton.layer.masksToBounds = true
        searchButton.layer.cornerRadius = auto(20)
        bottomView.addSubview(searchButton)
        
        resetButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(snp.centerX)
            make.height.equalTo(auto(40))
            make.width.equalTo(auto(170))
        }
        
        searchButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(snp.centerX)
            make.width.equalTo(auto(170))
            make.height.equalTo(auto(40))
        }
        
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(auto(10))
            make.centerX.equalToSuperview()
        }

        borderLayer1.colors = [UIColor.hex("D9D9D9").alpha(0.6).cgColor, UIColor.hex("B2B2B2").alpha(0.8).cgColor, UIColor.hex("D9D9D9").alpha(0.6).cgColor]
        borderLayer1.locations = [0, 0.5, 1]
        borderLayer1.startPoint = CGPoint(x: 0, y: 0.5)
        borderLayer1.endPoint = CGPoint(x: 1, y: 0.5)
        layerView.layer.addSublayer(borderLayer1)
        addSubview(layerView)
        layerView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(auto(10))
            make.width.equalTo(auto(324))
            make.height.equalTo(0.5)
            make.centerX.equalToSuperview()
        }
        
        exitButton.setImage(UIImage(named: "conditional_search_exit"), for: .normal)
        addSubview(exitButton)
        exitButton.snp.makeConstraints { make in
            make.centerY.equalTo(titleLabel)
            make.right.equalToSuperview().offset(-auto(15))
            make.width.height.equalTo(auto(20))
        }
        
        addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.top.equalTo(layerView.snp.bottom).offset(auto(10)).priority(.high)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(bottomView.snp.top)
        }
        
        scrollView.addSubview(scrollContentView)
        scrollContentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalToSuperview()
        }

        scrollContentView.addSubview(orderInfoTitleLabel)
        orderInfoTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(auto(10))
            make.top.equalTo(scrollContentView)
        }
        
        orderCodeTextFiled.backgroundColor = controlBackgroundColor
        orderCodeTextFiled.layer.cornerRadius = auto(6)
        orderCodeTextFiled.tintColor = .gray
        orderCodeTextFiled.attributedPlaceholder = NSAttributedString(string: "内部编号/外部编号", attributes: [.foregroundColor : noneColor,.font : UIFont.mediumFont(14)])
        orderCodeTextFiled.textAlignment = .center
        orderCodeTextFiled.font = valueFont
        scrollContentView.addSubview(orderCodeTextFiled)
        orderCodeTextFiled.snp.makeConstraints { make in
            make.left.equalTo(orderInfoTitleLabel).priority(.high)
            make.top.equalTo(orderInfoTitleLabel.snp.bottom).offset(auto(10))
            make.right.equalTo(snp.centerX).offset(-auto(5))
            make.height.equalTo(auto(30))
        }
        
        licenseTextField.backgroundColor = controlBackgroundColor
        licenseTextField.layer.cornerRadius = auto(6)
        licenseTextField.tintColor = .gray
        licenseTextField.attributedPlaceholder = NSAttributedString(string: "车架/车牌/单证", attributes: [.foregroundColor : noneColor,.font : UIFont.mediumFont(14)])
        licenseTextField.textAlignment = .center
        licenseTextField.font = valueFont
        scrollContentView.addSubview(licenseTextField)
        licenseTextField.snp.makeConstraints { make in
            make.right.equalTo(-auto(10)).priority(.high)
            make.top.equalTo(orderCodeTextFiled)
            make.left.equalTo(snp.centerX).offset(auto(5))
            make.height.equalTo(orderCodeTextFiled)
        }
        
        carOwnerTextField.backgroundColor = controlBackgroundColor
        carOwnerTextField.layer.cornerRadius = auto(6)
        carOwnerTextField.tintColor = .gray
        carOwnerTextField.attributedPlaceholder = NSAttributedString(string: "车主/报案人姓名/电话", attributes: [.foregroundColor : noneColor,.font : UIFont.mediumFont(14)])
        carOwnerTextField.textAlignment = .center
        carOwnerTextField.font = valueFont
        scrollContentView.addSubview(carOwnerTextField)
        carOwnerTextField.snp.makeConstraints { make in
            make.top.equalTo(orderCodeTextFiled.snp.bottom).offset(auto(10))
            make.left.equalTo(orderCodeTextFiled)
            make.width.equalTo(orderCodeTextFiled)
            make.height.equalTo(orderCodeTextFiled)
        }
        
        scrollContentView.addSubview(contractTitleLabel)
        contractTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(orderInfoTitleLabel)
            make.top.equalTo(carOwnerTextField.snp.bottom).offset(auto(10))
        }
        
        contractTextFiled.backgroundColor = controlBackgroundColor
        contractTextFiled.layer.cornerRadius = auto(6)
        contractTextFiled.tintColor = .gray
        contractTextFiled.attributedPlaceholder = NSAttributedString(string: "合同名称", attributes: [.foregroundColor : noneColor,.font : UIFont.mediumFont(14)])
        contractTextFiled.textAlignment = .center
        contractTextFiled.font = valueFont
        scrollContentView.addSubview(contractTextFiled)
        contractTextFiled.snp.makeConstraints { make in
            make.top.equalTo(contractTitleLabel.snp.bottom).offset(auto(10))
            make.left.equalTo(orderCodeTextFiled)
            make.height.equalTo(orderCodeTextFiled)
            make.width.equalTo(orderCodeTextFiled)
        }
        
        contractTypeButton.titleLabel?.textAlignment = .center
        contractTypeButton.backgroundColor = controlBackgroundColor
        contractTypeButton.layer.cornerRadius = auto(6)
        contractTypeButton.setTitleColor(noneColor, for: .normal)
        contractTypeButton.titleLabel?.font = .mediumFont(14)
        contractTypeButton.setTitle("合同类型", for: .normal)
        contractTypeButton.titleLabel?.font = valueFont
        let contractTypeImageView = DDImageView.init(image: UIImage(named: "conditional_search_down_arrow"))
        contractTypeButton.addSubview(contractTypeImageView)
        contractTypeImageView.snp.makeConstraints { make in
            make.right.equalTo(-auto(10))
            make.centerY.equalToSuperview()
        }
        scrollContentView.addSubview(contractTypeButton)
        contractTypeButton.snp.makeConstraints { make in
            make.right.equalTo(licenseTextField.snp.right)
            make.top.equalTo(contractTextFiled)
            make.height.width.equalTo(contractTextFiled)
        }
        
        scrollContentView.addSubview(dateTitleLabel)
        dateTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(orderInfoTitleLabel)
            make.top.equalTo(contractTextFiled.snp.bottom).offset(auto(10))
        }
        
        fromDateButton.titleLabel?.textAlignment = .center
        fromDateButton.backgroundColor = controlBackgroundColor
        fromDateButton.layer.cornerRadius = auto(6)
        fromDateButton.setTitleColor(noneColor, for: .normal)
        fromDateButton.setTitle("开始日期", for: .normal)
        fromDateButton.titleLabel?.font = valueFont
        scrollContentView.addSubview(fromDateButton)
        fromDateButton.snp.makeConstraints { make in
            make.top.equalTo(dateTitleLabel.snp.bottom).offset(auto(10))
            make.left.equalTo(auto(10)).priority(.high)
            make.height.equalTo(orderCodeTextFiled)
            make.right.equalTo(snp.centerX).offset(-auto(10))
        }
        
        let middleLine = DDView()
        middleLine.backgroundColor = noneColor
        scrollContentView.addSubview(middleLine)
        middleLine.snp.makeConstraints { make in
            make.centerY.equalTo(fromDateButton)
            make.height.equalTo(2)
            make.width.equalTo(auto(10))
            make.centerX.equalToSuperview()
        }
        
        toDateButton.titleLabel?.textAlignment = .center
        toDateButton.backgroundColor = controlBackgroundColor
        toDateButton.layer.cornerRadius = auto(6)
        toDateButton.setTitleColor(noneColor, for: .normal)
        toDateButton.setTitle("结束日期", for: .normal)
        toDateButton.titleLabel?.font = valueFont
        scrollContentView.addSubview(toDateButton)
        toDateButton.snp.makeConstraints { make in
            make.right.equalTo(licenseTextField.snp.right).priority(.high)
            make.top.equalTo(fromDateButton)
            make.height.width.equalTo(fromDateButton)
            make.left.equalTo(snp.centerX).offset(auto(10))
        }
        
        scrollContentView.addSubview(serviceTitleLabel)
        serviceTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(orderInfoTitleLabel)
            make.top.equalTo(fromDateButton.snp.bottom).offset(auto(10))
        }
        
        serviceTextField.backgroundColor = controlBackgroundColor
        serviceTextField.layer.cornerRadius = auto(6)
        serviceTextField.tintColor = .gray
        serviceTextField.attributedPlaceholder = NSAttributedString(string: "服务内容", attributes: [.foregroundColor : UIColor.hex("000000").alpha(0.2),.font : UIFont.mediumFont(14)])
        serviceTextField.textAlignment = .center
        serviceTextField.font = valueFont
        scrollContentView.addSubview(serviceTextField)
        serviceTextField.snp.makeConstraints { make in
            make.top.equalTo(serviceTitleLabel.snp.bottom).offset(auto(10)).priority(.high)
            make.left.equalTo(orderCodeTextFiled)
            make.height.equalTo(orderCodeTextFiled)
            make.width.equalTo(orderCodeTextFiled)
            make.bottom.equalTo(scrollContentView.snp.bottom)
        }
        
        serviceAppointButton.titleLabel?.textAlignment = .center
        serviceAppointButton.backgroundColor = controlBackgroundColor
        serviceAppointButton.layer.cornerRadius = auto(6)
        serviceAppointButton.setTitleColor(noneColor, for: .normal)
        serviceAppointButton.setTitle("是否预约", for: .normal)
        serviceAppointButton.titleLabel?.font = valueFont
        let serviceAppointImageView = DDImageView.init(image: UIImage(named: "conditional_search_down_arrow"))
        serviceAppointButton.addSubview(serviceAppointImageView)
        serviceAppointImageView.snp.makeConstraints { make in
            make.right.equalTo(-auto(10))
            make.centerY.equalToSuperview()
        }
        scrollContentView.addSubview(serviceAppointButton)
        serviceAppointButton.snp.makeConstraints { make in
            make.right.equalTo(licenseTextField.snp.right)
            make.top.equalTo(serviceTextField)
            make.height.width.equalTo(serviceTextField)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func resetAction() {
        contractTypeButton.setTitle("合同类型", for: .normal)
        contractTypeButton.setTitleColor(noneColor, for: .normal)
        
        fromDateButton.setTitle("开始日期", for: .normal)
        fromDateButton.setTitleColor(noneColor, for: .normal)
        
        toDateButton.setTitle("结束日期", for: .normal)
        toDateButton.setTitleColor(noneColor, for: .normal)
        
        serviceAppointButton.setTitle("是否预约", for: .normal)
        serviceAppointButton.setTitleColor(noneColor, for: .normal)
        
        orderCodeTextFiled.text = nil
        licenseTextField.text = nil
        carOwnerTextField.text = nil
        
        contractTextFiled.text = nil
        
        serviceTextField.text = nil
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        borderLayer1.frame = layerView.bounds
    }
    
    func setButtonValue(button: DDButton?, value: String?) {
        button?.setTitle(value, for: .normal)
        button?.setTitleColor(valueColor, for: .normal)
    }
    
    func remakeBottomViewConstraints(bottomSafeArea: CGFloat) {
        bottomView.snp.remakeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(auto(80))
            make.bottom.equalTo(-bottomSafeArea)
        }
    }
}
