CNLiveShopCouponCreateViewController.swift 8.75 KB
//
//  CNLiveShopCouponCreateViewController.swift
//  metaCode
//
//  Created by zx on 2023/12/12.
//

import Foundation
import SnapKit

///跳转类型
enum CNLivePushCreateCouponType:Int {
    //创建优惠券
    case create
    ///编辑优惠券
    case edit
}

class CNLiveShopCouponCreateViewController:CNLiveBaseViewController,UITableViewDelegate,UITableViewDataSource, CNLiveShopCouponButtonClickDelegate{
    let kCNLiveCreateCouponCellIdentifier = "kCNLiveCreateCouponCellIdentifier"
    let kCNLiveCreateCouponTableViewHeaderFooterViewID = "kCNLiveCreateCouponTableViewHeaderFooterViewID"
    
    lazy var bottomView :CNLiveShopCouponCreateBottomView = {
        let bottomView = CNLiveShopCouponCreateBottomView()
        bottomView.delegate = self
        return bottomView
    }()

    lazy var tableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
//        tableView.emptyDataSetSource = self
//        tableView.emptyDataSetDelegate = self
        tableView.separatorStyle = .none
        tableView.backgroundColor = HexColor("#F6F6F6")
        tableView.register(CNLiveCreateCouponTableViewCell.self, forCellReuseIdentifier: kCNLiveCreateCouponCellIdentifier)
        tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: kCNLiveCreateCouponTableViewHeaderFooterViewID)
        return tableView
    }()

    var dataSource:Array<Array<CNLiveShopCouponModel>>?
    var couponTitle:String?
    var totalNum:String?
    var maxMoney:String?
    var lessMoney:String?
    var startTime:String?
    var endTime:String?
    var goodsId:String?
    var hiddenGoodsLine:Bool = false
    var couponModel:CNLiveShopCouponListModel?
    var type:CNLivePushCreateCouponType = .create
    init(couponListModel:CNLiveShopCouponListModel?,type:CNLivePushCreateCouponType){
        super.init(nibName: nil, bundle: nil)
        self.couponModel = couponListModel
        self.type = type
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = CNLiveShopCouponViewModel.responseCouponCreateWithModel(listModel: self.couponModel)
        let model = self.dataSource![1][0]
        if model.section == 1{
            self.hiddenGoodsLine = true
        }else{
            self.hiddenGoodsLine = false
        }
        self.view.backgroundColor = HexColor("#F6F6F6")
        navigationBarHidden = false
        titleName = "发布优惠券"
        self.view.addSubview(self.tableView)
        self.view.addSubview(self.bottomView)
        self.bottomView.snp.makeConstraints { make in
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
            make.height.equalTo(50)
        }
        self.tableView.snp.makeConstraints { make in
            make.left.right.equalTo(self.view)
            make.top.equalTo(self.view).offset(KNavigationHeight)
            make.bottom.equalTo(self.bottomView.snp.top)
        }
    }
    // MARK: - DSShopCouponButtonClickDelegate
    func selectedItemButton(section:Int){
        switch section{
        case 0:
            self.navigationController?.popViewController(animated: true)
            break
        case 1:
            let model1 = self.dataSource![1][0]
            let model2 = self.dataSource![1][2]
            let model3 = self.dataSource![1][3]
            let model4 = self.dataSource![1][4]
            if self.type == .create{
                CNLiveShopCouponViewModel.requestCreateCouponWithTitle(title: self.couponTitle ?? "", filled: self.maxMoney ?? "", discount: self.lessMoney ?? "", startTime: self.startTime ?? "", endTime: self.endTime ?? "", state: String(format: "%d", model2.section), isShow: String(format: "%d", model4.section), receiveNum: String(format: "%d", model3.section), totalNum: self.totalNum ?? "", type: model1.section, goodsId: self.goodsId ?? "") { result, status in
                    if status == .success{
                        if let ret = result as? Dictionary<String, Any> {
                            if ret["code"] as! String  != "0" && ret["code"] as! String  != "200"{
                                showMessage(message: ret["message"] as! String)
                                return
                            }
                        }
                        
                        showMessage(message: "发布成功")
                        self.navigationController?.popViewController(animated: true)
                    }else{
                        showMessage(message: "发布失败")
                    }
                }
            }else{
                CNLiveShopCouponViewModel.requestEditCouponWithModel(model: self.couponModel!, title: (self.couponTitle?.count ?? 0>0 ? self.couponTitle! : self.couponModel!.title)!, state: String(format: "%d", model2.section), isShow: String(format: "%d", model4.section), receiveNum: String(format: "%d", model3.section)) { result, status in
                    if status == .success{
                        showMessage(message: "发布成功")
                        self.navigationController?.popViewController(animated: true)
                    }else{
                        showMessage(message: "发布失败")
                    }
                }
            }
            break
        default:
            break
        }
    }
    // MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 1 && indexPath.row == 1 && self.hiddenGoodsLine{
            return 0
        }
        return 50
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0{
            return 0
        }
        return 5
    }
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.view.endEditing(true)
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: kCNLiveCreateCouponTableViewHeaderFooterViewID)
        view?.contentView.backgroundColor = HexColor("#F6F6F6")
        return view
    }
    // MARK: - UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let listArr = self.dataSource![section]
        return listArr.count
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.dataSource!.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                
        guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveCreateCouponCellIdentifier) as? CNLiveCreateCouponTableViewCell
        else {
            let cell = CNLiveCreateCouponTableViewCell()
            return cell
        }
        if self.type == .edit{
            if (indexPath.section == 0 && indexPath.row == 0) || (indexPath.section == 1 && (indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 4)) {
                cell.editEnabled = true
            }else{
                cell.editEnabled = false
            }
        }else{
            cell.editEnabled = true
        }
        cell.model = self.dataSource![indexPath.section][indexPath.row]
        cell.textFieldChangeBlock = {section,  number in
            switch section {
            case 1:
                self.couponTitle = number
                break
            case 3:
                self.totalNum = number
                break
            case 1001:
                self.maxMoney = number
                break
            case 1002:
                self.lessMoney = number
                break
            default:
                break
            }
        }
        cell.dateSelectBlock = { id,date in
            let interval = date.timeIntervalSince1970
            if id == 4{
                self.startTime = String(format: "%f", interval)
            }else{
                self.endTime = String(format: "%f", interval+3600*24-1)
            }
        }
        if indexPath.row == 1 && indexPath.section == 1{
            if self.goodsId == nil && (self.couponModel?.goods_id == nil || self.couponModel?.goods_id == "0"){
                cell.goodsLabel.text = ""
            }
            cell.isHidden = self.hiddenGoodsLine
        }
        cell.couponGoodsHiddenBlock = { isHidden in
            self.hiddenGoodsLine = isHidden
            self.goodsId = nil
            tableView.reloadRows(at: [IndexPath(row: 1, section: 1)], with: .middle)
        }
        cell.couponGoodsBlock = { goodsId in
            self.goodsId = goodsId
        }
        return cell
    }
}