package cn.muchinfo.rma.view.base.home.reportquery

import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.data.HedgePlanData
import cn.muchinfo.rma.global.data.SpotReportData
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.*
import cn.muchinfo.rma.view.base.BaseActivity
import cn.muchinfo.rma.view.base.home.financial.InvoiceRegisterActivity
import cn.muchinfo.rma.view.base.home.hedge.HedgePlanViewHolder
import com.blankj.utilcode.util.ActivityUtils
import mtp.polymer.com.autowidget.adapter.BaseAdapter
import org.jetbrains.anko.*

/**
 * 现货报表-报表详情
 * @property type String?
 */
class SpotReportDetailsActivity : BaseActivity<ReportQueryViewModel>(){

    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout

    val wrstandardid by lazy {
        intent.getStringExtra("wrstandardid")
    }

    /**
     * 页面类型 1 日报表 2 月报表
     */
    val type by lazy {
        intent.getStringExtra("type")
    }

    /**
     * 日报表详情时的请求参数
     */
    val tradedate by lazy {
        intent.getStringExtra("tradedate")
    }

    /**
     * 月报表详情时的请求参数
     */
    val cycletime by lazy {
        intent.getStringExtra("cycletime")
    }

    private val unSubmitAdapter: BaseAdapter<SpotReportData, SpotReportDetailsViewHolder> =
        BaseAdapter { _, _ -> SpotReportDetailsViewHolder(this, viewModel, type ?: "1") }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            topBar {
                commonLeftButton()
                commonTitle {
                    text = if (type == "1"){
                        "现货日报表-详情"
                    }else{
                        "现货月报表-详情"
                    }
                }
            }

            if (type == "1"){
                SpotReportDayDetailsTitle()
                viewModel.qryReportDaySpotDetail(wrstandardid = wrstandardid ?: "",tradedate = tradedate ?: "")
            }else{
                SpotReportMonthDetailsTitle()
                viewModel.qryReportMonthSpotDetail(wrstandardid = wrstandardid ?: "",cycletime = cycletime ?: "")
            }

            statusLayout(contentBlock = {
                statusLayout = this
//                bindTaskStatus(viewModule.status)
                swipeToLoadLayout {
                    swipeToLayout = this
                    setEnableRefresh(true)
                    setEnableLoadMore(false)
                    setOnRefreshListener {
                        if (type == "1"){
                            viewModel.qryReportDaySpotDetail(wrstandardid = wrstandardid ?: "",tradedate = tradedate ?: "")
                        }else{
                            viewModel.qryReportMonthSpotDetail(wrstandardid = wrstandardid ?: "",cycletime = cycletime ?: "")
                        }
                    }
                    setEnableScrollContentWhenLoaded(false)
                    setEnableLoadMoreWhenContentNotFull(false)
                    recyclerView {
                        background = resources.getDrawable(R.color.white)
                        adapter = unSubmitAdapter
                    }.lparams(matchParent, matchParent)
                }
            }, emptyBlock = {
                emptyView(hint = resources.getString(R.string.now_no_data))
            }).lparams(matchParent, matchParent)

            viewModel.spotReportDetailsDataList.bindOptional(context) {
                if (it?.isEmpty() == true || it?.size == 0) {
                    statusLayout.showEmpty()
                } else {
                    if (swipeToLayout.getIsRefreshing()) {
                        swipeToLayout.finishRefresh()
                    }
                    unSubmitAdapter.setNewData(it)
                }
            }

        }
    }

}

/**
 * 日报表头部
 * @receiver _LinearLayout
 */
fun _LinearLayout.SpotReportDayDetailsTitle(){
    linearLayout {
        verticalLayout {
            gravity = Gravity.START
            textView {
                text = "发生时间"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginStart = autoSize(36)
                topMargin = autoSize(15)
            }
            textView {
                text = "商品型号"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginStart = autoSize(36)
            }
        }.lparams(autoSize(225), autoSize(100))

        verticalLayout {
            gravity = Gravity.CENTER
            textView {
                text = "合同类型"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginStart = autoSize(36)
            }
            textView {
                text = "合同编号"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginStart = autoSize(36)
            }
        }.lparams(autoSize(225), autoSize(100))

        verticalLayout {
            gravity = Gravity.CENTER
            textView {
                text = "操作类型"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginStart = autoSize(36)
            }
            textView {
                text = "值"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginStart = autoSize(36)
            }
        }.lparams(autoSize(225), autoSize(100))

        verticalLayout {
            gravity = Gravity.END
            textView {
                text = "操作人"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginEnd = autoSize(36)
                topMargin = autoSize(15)
            }
        }.lparams(autoSize(225), autoSize(100))
    }.lparams(matchParent, autoSize(100))
}

/**
 * 月报表头部
 * @receiver _LinearLayout
 */
fun _LinearLayout.SpotReportMonthDetailsTitle(){
    linearLayout {
        verticalLayout {
            gravity = Gravity.START
            textView {
                text = "日期"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginStart = autoSize(36)
                topMargin = autoSize(15)
            }

        }.lparams(autoSize(225), autoSize(100))

        verticalLayout {
            gravity = Gravity.CENTER
            textView {
                text = "采购定价量"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginStart = autoSize(36)
            }
            textView {
                text = "销售定价量"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginStart = autoSize(36)
            }
        }.lparams(autoSize(225), autoSize(100))

        verticalLayout {
            gravity = Gravity.CENTER
            textView {
                text = "采购确定量"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginStart = autoSize(36)
            }
            textView {
                text = "销售确定量"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginStart = autoSize(36)
            }
        }.lparams(autoSize(225), autoSize(100))

        verticalLayout {
            gravity = Gravity.END
            textView {
                text = "采购入库量"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginEnd = autoSize(36)
                topMargin = autoSize(15)
            }
            textView {
                text = "销售出库量"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginEnd = autoSize(36)
            }
        }.lparams(autoSize(225), autoSize(100))
    }.lparams(matchParent, autoSize(100))
}