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

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.data.ExposureReportData
import cn.muchinfo.rma.global.data.FinancialManageData
import cn.muchinfo.rma.global.data.FinancialReportData
import cn.muchinfo.rma.global.data.SpotReportData
import cn.muchinfo.rma.global.isBlankString
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.contract.emptyView
import cn.muchinfo.rma.view.base.home.financial.FinancialViewholder
import cn.muchinfo.rma.view.base.home.financial.InvoiceRegisterActivity
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.NumberUtils
import com.blankj.utilcode.util.TimeUtils
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.tab.QMUITabIndicator
import com.qmuiteam.qmui.widget.tab.QMUITabSegment
import mtp.polymer.com.autowidget.adapter.BaseAdapter
import mtp.polymer.com.autowidget.dialog.showDataSelectDialog
import org.jetbrains.anko.*
import org.jetbrains.anko.support.v4.viewPager
import java.text.SimpleDateFormat

/**
 * 报表查询
 */
class ReportQueryActivity : BaseActivity<ReportQueryViewModel>() {

    lateinit var segment: QMUITabSegment
    var selectedTabIndex: Int = 0
    private lateinit var viewPager: ViewPager

    /** tab标题 **/
    private val tabsArray: List<String> by lazy {
        arrayListOf("财务报表", "敞口报表", "现货报表")
    }

    /** 财务报表 **/
    private val financialReportUI by lazy { FinancialReportUI(this, viewModel) }

    /** 敞口报表 **/
    private val exposureReportUI by lazy { ExposureReportUI(this, viewModel) }

    /** 现货报表 **/
    private val spotReportUI by lazy { SpotReportUI(this, viewModel) }

    private val pagerAdapter: PagerAdapter by lazy {
        object : PagerAdapter() {

            override fun getPageTitle(position: Int): CharSequence? = tabsArray[position]

            override fun instantiateItem(container: ViewGroup, position: Int): Any {
                when (position) {
                    0 -> {
                        return financialReportUI.root.apply {//财务报表
                            container.addView(
                                this,
                                ViewGroup.LayoutParams(matchParent, matchParent)
                            )
                        }
                    }
                    1 -> {
                        return exposureReportUI.root.apply {//敞口报表
                            container.addView(
                                this,
                                ViewGroup.LayoutParams(matchParent, matchParent)
                            )
                        }
                    }
                    2 -> {
                        return spotReportUI.root.apply {//现货报表
                            container.addView(
                                this,
                                ViewGroup.LayoutParams(matchParent, matchParent)
                            )
                        }
                    }
                    else -> return super.instantiateItem(container, position)
                }
            }

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                (`object` as? View)?.let { container.removeView(it) }
            }

            override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object`

            override fun getCount(): Int = tabsArray.size

        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            topBar {
                commonLeftButton()
                commonTitle {
                    text = "报表查询"
                }
                commonMenuButton(R.mipmap.rma_search) {
                    val intent = Intent()
                    intent.setClass(context, InvoiceRegisterActivity::class.java)
                    ActivityUtils.startActivity(intent)
                }
            }


            tabSegment {
                segment = this
                background = resources.getDrawable(R.color.segtabment_bg_color)
                addOnTabSelectedListener(object :
                    QMUITabSegment.OnTabSelectedListener {
                    override fun onDoubleTap(index: Int) {}
                    override fun onTabReselected(index: Int) {}
                    override fun onTabUnselected(index: Int) {}
                    override fun onTabSelected(index: Int) {
                        selectedTabIndex = index
                    }
                })
                setIndicator(QMUITabIndicator(QMUIDisplayHelper.dp2px(context, 2), false, true))
                setDefaultTextSize(ConvertUtils.sp2px(16f), ConvertUtils.sp2px(16f))
            }.lparams(matchParent, autoSize(100))

            view {
                background = resources.getDrawable(R.color.main_hit_bg_color)
            }.lparams(matchParent, autoSize(1)) {
                marginStart = autoSize(32)
                marginEnd = autoSize(32)
            }

            viewPager {
                background = resources.getDrawable(R.color.white)
                viewPager = this
                adapter = pagerAdapter
            }.lparams(matchParent, matchParent)

            segment.setupWithViewPager(viewPager)
            segment.selectTab(selectedTabIndex)


        }
    }

}

/**
 * 财务报表页面
 * @property activity AppCompatActivity
 * @property viewModel ReportQueryViewModel
 * @constructor
 */
class FinancialReportUI(
    private val activity: AppCompatActivity,
    private val viewModel: ReportQueryViewModel
) {


    lateinit var segment: QMUITabSegment
    var selectedTabIndex: Int = 0
    private lateinit var viewPager: ViewPager

    //月报表时用到的列表
    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout
    private val financialReportMonthAdapter: BaseAdapter<FinancialReportData, FinancialMonthViewholder> =
        BaseAdapter { _, _ -> FinancialMonthViewholder(activity, viewModel) }

    /**
     * 查询所用的date 注意（页面显示时间规则yyyy-MM-dd,而接口请求规则yyyyMMdd，简直无fuck可说）
     */
    var tradedate: String = TimeUtils.getNowString(SimpleDateFormat("yyyy-MM-dd"))

    /** 查询时间textview **/
    lateinit var tradedatetextview: TextView

    /** tab标题 **/
    private val tabsArray: List<String> by lazy {
        arrayListOf("款项", "发票")
    }

    /** 款项 **/
    private val financialPaymentUI by lazy {
        FinancialPaymentUI(activity, viewModel)
    }

    /** 发票 **/
    private val financialInvoiceUI by lazy {
        FinancialInvoiceUI(activity, viewModel)
    }

    private val pagerAdapter: PagerAdapter by lazy {
        object : PagerAdapter() {

            override fun getPageTitle(position: Int): CharSequence? = tabsArray[position]

            override fun instantiateItem(container: ViewGroup, position: Int): Any {
                when (position) {
                    0 -> {
                        return financialPaymentUI.root.apply {//款项
                            container.addView(
                                this,
                                ViewGroup.LayoutParams(matchParent, matchParent)
                            )
                        }
                    }
                    1 -> {
                        return financialInvoiceUI.root.apply {//发票
                            container.addView(
                                this,
                                ViewGroup.LayoutParams(matchParent, matchParent)
                            )
                        }
                    }
                    else -> return super.instantiateItem(container, position)
                }
            }

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                (`object` as? View)?.let { container.removeView(it) }
            }

            override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object`

            override fun getCount(): Int = tabsArray.size

        }
    }

    @SuppressLint("SetTextI18n")
    @RequiresApi(Build.VERSION_CODES.N)
    val root = _FrameLayout(activity).apply {
        verticalLayout {
            viewModel.qryReportDayFinance(tradedate = tradedate.replace("-",""))
            linearLayout {
                gravity = Gravity.CENTER_VERTICAL
                linearLayout {
                    onThrottleFirstClick {
                        if (viewModel.isselectFinancialMonthOrDay.value == "1"){
                            viewModel.isselectFinancialMonthOrDay.postValue("2")
                        }else{
                            viewModel.isselectFinancialMonthOrDay.postValue("1")
                        }
                    }
                    backgroundResource = R.drawable.rma_time_click_bg
                    gravity = Gravity.CENTER_VERTICAL
                    textView {
                        viewModel.isselectFinancialMonthOrDay.bindOptional(context){
                            if (it == "1"){
                                text = "日报表"
                            }else if (it == "2"){
                                text = "月报表"
                            }
                        }
                        text = "日报表"
                        textColorInt = R.color.rma_hint_text_color_ccc
                        textSizeAuto = 31
                    }.lparams(wrapContent, wrapContent) {
                        marginStart = autoSize(18)
                    }

                    emptyView()

                    imageView {
                        imageResource = R.mipmap.rma_down
                    }.lparams(autoSize(25), autoSize(25))

                }.lparams(autoSize(288), autoSize(67)) {
                    marginStart = autoSize(36)
                }

                linearLayout {
                    onThrottleFirstClick {
                        activity.showDataSelectDialog(type = viewModel.isselectFinancialMonthOrDay.value.toString()) {
                            tradedate = this
                            tradedatetextview.text = this
                        }
                    }
                    backgroundResource = R.drawable.rma_time_click_bg
                    gravity = Gravity.CENTER_VERTICAL
                    textView {
                        tradedatetextview = this
                        text = TimeUtils.getNowString(SimpleDateFormat("yyyy-MM-dd"))
                        textColorInt = R.color.rma_hint_text_color_ccc
                        textSizeAuto = 31
                    }.lparams(wrapContent, wrapContent) {
                        marginStart = autoSize(18)
                    }

                    emptyView()

                    imageView {
                        imageResource = R.mipmap.rma_search_by_time
                    }.lparams(autoSize(38), autoSize(38))
                }.lparams(autoSize(370), autoSize(67)) {
                    marginStart = autoSize(12)
                }

                textView {
                    onThrottleFirstClick {
                        if (viewModel.isselectFinancialMonthOrDay.value == "1"){//日报表数据请求
                            viewModel.qryReportDayFinance(tradedate = tradedate.replace("-",""))
                            financialPaymentUI.queryRefrashByTime(tradeDate = tradedate)
                            financialInvoiceUI.queryRefrashByTime(tradeDate = tradedate)
                        }else{//月报表数据请求
                            viewModel.qryReportMonthFinance(cycletime = tradedate)
                        }
                    }
                    gravity = Gravity.CENTER
                    backgroundDrawable = createCommonBottomRoundBg(color = "#2481DD", corner = 5)
                    text = "查询"
                    textSizeAuto = 31
                    textColorInt = R.color.white
                }.lparams(autoSize(144), autoSize(67)) {
                    marginStart = autoSize(12)
                }

            }.lparams(matchParent, autoSize(100))

            /**
             * 报表页面头部页面
             */
            verticalLayout {

                backgroundResource = R.mipmap.rma_report_title_bg

                linearLayout {
                    linearLayout {
                        gravity = Gravity.START
                        textView {
                            text = "收款"
                            textColorInt = R.color.rma_report_title_color
                            textSizeAuto = 31
                        }.lparams(wrapContent, wrapContent) {
                            topMargin = autoSize(20)
                            marginStart = autoSize(25)
                        }
                    }.lparams(autoSize(207), autoSize(74))

                    linearLayout {
                        gravity = Gravity.START
                        textView {
                            text = "付款"
                            textColorInt = R.color.rma_report_title_color
                            textSizeAuto = 31
                        }.lparams(wrapContent, wrapContent) {
                            topMargin = autoSize(20)
                            marginStart = autoSize(25)
                        }
                    }.lparams(autoSize(207), autoSize(74))

                    linearLayout {
                        gravity = Gravity.START
                        textView {
                            text = "开票"
                            textColorInt = R.color.rma_report_title_color
                            textSizeAuto = 31
                        }.lparams(wrapContent, wrapContent) {
                            topMargin = autoSize(20)
                            marginStart = autoSize(25)
                        }
                    }.lparams(autoSize(207), autoSize(74))

                    linearLayout {
                        gravity = Gravity.START
                        textView {
                            text = "收票"
                            textColorInt = R.color.rma_report_title_color
                            textSizeAuto = 31
                        }.lparams(wrapContent, wrapContent) {
                            topMargin = autoSize(20)
                            marginStart = autoSize(25)
                        }
                    }.lparams(autoSize(207), autoSize(74))
                }.lparams(matchParent, autoSize(74))

                view {
                    background = resources.getDrawable(R.color.rma_report_title_color)
                }.lparams(matchParent, autoSize(1)) {
                    marginStart = autoSize(15)
                    marginEnd = autoSize(15)
                }

                linearLayout {
                    linearLayout {
                        gravity = Gravity.START
                        textView {
                            viewModel.financialReportTitleData.bindOptional(context) {
                                text = it?.collectmoneycount?.isBlankString() + "笔"
                            }
                            textColorInt = R.color.rma_black_33
                            textSizeAuto = 26
                        }.lparams(wrapContent, wrapContent) {
                            topMargin = autoSize(20)
                            marginStart = autoSize(25)
                        }
                    }.lparams(autoSize(207), autoSize(74))

                    linearLayout {
                        gravity = Gravity.START
                        textView {
                            viewModel.financialReportTitleData.bindOptional(context) {
                                text = it?.paymoneycount?.isBlankString() + "笔"
                            }

                            textColorInt = R.color.rma_black_33
                            textSizeAuto = 26
                        }.lparams(wrapContent, wrapContent) {
                            topMargin = autoSize(20)
                            marginStart = autoSize(25)
                        }
                    }.lparams(autoSize(207), autoSize(74))

                    linearLayout {
                        gravity = Gravity.START
                        textView {
                            viewModel.financialReportTitleData.bindOptional(context) {
                                text = it?.collectinvoicecount?.isBlankString() + "笔"
                            }
                            textColorInt = R.color.rma_black_33
                            textSizeAuto = 26
                        }.lparams(wrapContent, wrapContent) {
                            topMargin = autoSize(20)
                            marginStart = autoSize(25)
                        }
                    }.lparams(autoSize(207), autoSize(74))

                    linearLayout {
                        gravity = Gravity.START
                        textView {
                            viewModel.financialReportTitleData.bindOptional(context) {
                                text = it?.payinvoicecount?.isBlankString() + "笔"
                            }
                            textColorInt = R.color.rma_black_33
                            textSizeAuto = 26
                        }.lparams(wrapContent, wrapContent) {
                            topMargin = autoSize(20)
                            marginStart = autoSize(25)
                        }
                    }.lparams(autoSize(207), autoSize(74))
                }.lparams(matchParent, autoSize(70))

                linearLayout {
                    linearLayout {
                        gravity = Gravity.START
                        textView {
                            viewModel.financialReportTitleData.bindOptional(context) {
                                text = it?.collectmoneyamount?.isBlankString()
                            }
                            textColorInt = R.color.rma_black_33
                            textSizeAuto = 26
                        }.lparams(wrapContent, wrapContent) {
                            topMargin = autoSize(20)
                            marginStart = autoSize(25)
                        }
                    }.lparams(autoSize(207), autoSize(74))

                    linearLayout {
                        gravity = Gravity.START
                        textView {
                            viewModel.financialReportTitleData.bindOptional(context) {
                                text = it?.paymoneyamount?.isBlankString()
                            }
                            textColorInt = R.color.rma_black_33
                            textSizeAuto = 26
                        }.lparams(wrapContent, wrapContent) {
                            topMargin = autoSize(20)
                            marginStart = autoSize(25)
                        }
                    }.lparams(autoSize(207), autoSize(74))

                    linearLayout {
                        gravity = Gravity.START
                        textView {
                            viewModel.financialReportTitleData.bindOptional(context) {
                                text = it?.collectinvoiceamount?.isBlankString()
                            }
                            textColorInt = R.color.rma_black_33
                            textSizeAuto = 26
                        }.lparams(wrapContent, wrapContent) {
                            topMargin = autoSize(20)
                            marginStart = autoSize(25)
                        }
                    }.lparams(autoSize(207), autoSize(74))

                    linearLayout {
                        gravity = Gravity.START
                        textView {
                            viewModel.financialReportTitleData.bindOptional(context) {
                                text = it?.payinvoiceamount?.isBlankString()
                            }
                            textColorInt = R.color.rma_black_33
                            textSizeAuto = 26
                        }.lparams(wrapContent, wrapContent) {
                            topMargin = autoSize(20)
                            marginStart = autoSize(25)
                        }
                    }.lparams(autoSize(207), autoSize(74))
                }.lparams(matchParent, autoSize(70))


            }.lparams(matchParent, autoSize(210)) {
                marginStart = autoSize(36)
                marginEnd = autoSize(36)
            }
            /** 在选择日报表时显示此页面 **/
            verticalLayout {
                viewModel.isselectFinancialMonthOrDay.bindOptional(context){
                    visibility = if (it == "1"){
                        View.VISIBLE
                    }else{
                        View.GONE
                    }
                }
                tabSegment {
                    segment = this
                    background = resources.getDrawable(R.color.segtabment_bg_color)
                    addOnTabSelectedListener(object :
                        QMUITabSegment.OnTabSelectedListener {
                        override fun onDoubleTap(index: Int) {}
                        override fun onTabReselected(index: Int) {}
                        override fun onTabUnselected(index: Int) {}
                        override fun onTabSelected(index: Int) {
                            selectedTabIndex = index
                        }
                    })
                    setIndicator(QMUITabIndicator(QMUIDisplayHelper.dp2px(context, 2), false, true))
                    setDefaultTextSize(ConvertUtils.sp2px(16f), ConvertUtils.sp2px(16f))
                }.lparams(matchParent, autoSize(100))

                view {
                    background = resources.getDrawable(R.color.main_hit_bg_color)
                }.lparams(matchParent, autoSize(1)) {
                    marginStart = autoSize(32)
                    marginEnd = autoSize(32)
                }

                viewPager {
                    background = resources.getDrawable(R.color.white)
                    viewPager = this
                    adapter = pagerAdapter
                }.lparams(matchParent, matchParent)

                segment.setupWithViewPager(viewPager)
                segment.selectTab(selectedTabIndex)
            }.lparams(matchParent, matchParent)

            /** 在选择月报表时显示此页面 **/
            verticalLayout {
                viewModel.isselectFinancialMonthOrDay.bindOptional(context){
                    visibility = if (it == "2"){
                        View.VISIBLE
                    }else{
                        View.GONE
                    }
                }
                FinancialReportUiTitle()

                statusLayout(contentBlock = {
                    statusLayout = this
//                bindTaskStatus(viewModule.status)
                    swipeToLoadLayout {
                        swipeToLayout = this
                        setEnableRefresh(true)
                        setEnableLoadMore(false)
                        setOnRefreshListener {

                        }
                        setEnableScrollContentWhenLoaded(false)
                        setEnableLoadMoreWhenContentNotFull(false)
                        recyclerView {
                            background = resources.getDrawable(R.color.white)
                            adapter = financialReportMonthAdapter
                        }.lparams(matchParent, matchParent)
                    }
                }, emptyBlock = {
                    emptyView(hint = resources.getString(R.string.now_no_data))
                }).lparams(matchParent, matchParent)


            }.lparams(matchParent, matchParent)
        }
    }
}

/**
 * 敞口报表页面
 * @property activity AppCompatActivity
 * @property viewModel ReportQueryViewModel
 * @constructor
 */
class ExposureReportUI(
    private val activity: AppCompatActivity,
    private val viewModel: ReportQueryViewModel
) {

    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout
    private val exposureReportAdapter: BaseAdapter<ExposureReportData, ExposureReportViewholder> =
        BaseAdapter { _, _ -> ExposureReportViewholder(activity, viewModel) }

    /**
     * 查询所用的date 注意（页面显示时间规则yyyy-MM-dd,而接口请求规则yyyyMMdd，简直无fuck可说）
     */
    var tradedate: String = TimeUtils.getNowString(SimpleDateFormat("yyyy-MM-dd"))

    /** 查询时间textview **/
    lateinit var tradedatetextview: TextView

    @RequiresApi(Build.VERSION_CODES.N)
    val root = _FrameLayout(activity).apply {
        verticalLayout {
            viewModel.qryReportDayExposure(tradedate = tradedate.replace("-",""))
            linearLayout {
                gravity = Gravity.CENTER_VERTICAL
                linearLayout {
                    backgroundResource = R.drawable.rma_time_click_bg
                    gravity = Gravity.CENTER_VERTICAL
                    textView {
                        text = "日报表"
                        textColorInt = R.color.rma_hint_text_color_ccc
                        textSizeAuto = 31
                    }.lparams(wrapContent, wrapContent) {
                        marginStart = autoSize(18)
                    }
                    emptyView()

                }.lparams(autoSize(288), autoSize(67)) {
                    marginStart = autoSize(36)
                }

                linearLayout {
                    onThrottleFirstClick {
                        activity.showDataSelectDialog {
                            tradedate = this
                            tradedatetextview.text = this
                        }
                    }
                    backgroundResource = R.drawable.rma_time_click_bg
                    gravity = Gravity.CENTER_VERTICAL
                    textView {
                        tradedatetextview = this
                        text = TimeUtils.getNowString(SimpleDateFormat("yyyy-MM-dd"))
                        textColorInt = R.color.rma_hint_text_color_ccc
                        textSizeAuto = 31
                    }.lparams(wrapContent, wrapContent) {
                        marginStart = autoSize(18)
                    }

                    emptyView()

                    imageView {
                        imageResource = R.mipmap.rma_search_by_time
                    }.lparams(autoSize(38), autoSize(38))
                }.lparams(autoSize(370), autoSize(67)) {
                    marginStart = autoSize(12)
                }

                textView {
                    onThrottleFirstClick {
                        viewModel.qryReportDayExposure(tradedate = tradedate.replace("-",""))
                    }
                    gravity = Gravity.CENTER
                    backgroundDrawable = createCommonBottomRoundBg(color = "#2481DD", corner = 5)
                    text = "查询"
                    textSizeAuto = 31
                    textColorInt = R.color.white
                }.lparams(autoSize(144), autoSize(67)) {
                    marginStart = autoSize(12)
                }

            }.lparams(matchParent, autoSize(100))

            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(97))

                verticalLayout {
                    gravity = Gravity.CENTER
                    textView {
                        text = "现货数量"
                        textSizeAuto = 24
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent)

                    textView {
                        text = "应套保比例"
                        textSizeAuto = 24
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent)
                }.lparams(autoSize(225), autoSize(97))

                verticalLayout {
                    gravity = Gravity.CENTER
                    textView {
                        text = "期货应套保量"
                        textSizeAuto = 24
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent)

                    textView {
                        text = "期货数量"
                        textSizeAuto = 24
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent)
                }.lparams(autoSize(225), autoSize(97))

                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(97))
            }.lparams(matchParent, autoSize(97))

            statusLayout(contentBlock = {
                statusLayout = this
//                bindTaskStatus(viewModule.status)
                swipeToLoadLayout {
                    swipeToLayout = this
                    setEnableRefresh(false)
                    setEnableLoadMore(false)
                    setOnRefreshListener {

                    }
                    setEnableScrollContentWhenLoaded(false)
                    setEnableLoadMoreWhenContentNotFull(false)
                    recyclerView {
                        background = resources.getDrawable(R.color.white)
                        adapter = exposureReportAdapter
                    }.lparams(matchParent, matchParent)
                }
            }, emptyBlock = {
                emptyView(hint = resources.getString(R.string.now_no_data))
            }).lparams(matchParent, matchParent)

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

/**
 * 现货报表页面
 * @property activity AppCompatActivity
 * @property viewModel ReportQueryViewModel
 * @constructor
 */
class SpotReportUI(
    private val activity: AppCompatActivity,
    private val viewModel: ReportQueryViewModel
) {

    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout
    private val spotReportAdapter: BaseAdapter<SpotReportData, SpotReportViewHolder> =
        BaseAdapter { _, _ -> SpotReportViewHolder(activity, viewModel) }

    /**
     * 查询所用的date 注意（页面显示时间规则yyyy-MM-dd,而接口请求规则yyyyMMdd，简直无fuck可说）
     */
    var tradedate: String = TimeUtils.getNowString(SimpleDateFormat("yyyy-MM-dd"))

    /** 查询时间textview **/
    lateinit var tradedatetextview: TextView

    val root = _FrameLayout(activity).apply {
        verticalLayout {
            viewModel.qryReportDaySpot(tradedate = tradedate.replace("-",""))
            linearLayout {
                gravity = Gravity.CENTER_VERTICAL
                linearLayout {
                    onThrottleFirstClick {
                        if (viewModel.isselectSpotMonthOrDay.value == "1"){
                            viewModel.isselectSpotMonthOrDay.postValue("2")
                        }else{
                            viewModel.isselectSpotMonthOrDay.postValue("1")
                        }
                    }
                    backgroundResource = R.drawable.rma_time_click_bg
                    gravity = Gravity.CENTER_VERTICAL
                    textView {
                        viewModel.isselectSpotMonthOrDay.bindOptional(context){
                            if (it == "1"){
                                text = "日报表"
                            }else if (it == "2"){
                                text = "月报表"
                            }
                        }
                        text = "日报表"
                        textColorInt = R.color.rma_hint_text_color_ccc
                        textSizeAuto = 31
                    }.lparams(wrapContent, wrapContent) {
                        marginStart = autoSize(18)
                    }

                    emptyView()

                    imageView {
                        imageResource = R.mipmap.rma_down
                    }.lparams(autoSize(25), autoSize(25))

                }.lparams(autoSize(288), autoSize(67)) {
                    marginStart = autoSize(36)
                }

                linearLayout {
                    onThrottleFirstClick {
                        activity.showDataSelectDialog(type = viewModel.isselectSpotMonthOrDay.value.toString()) {
                            tradedate = this
                            tradedatetextview.text = this
                        }
                    }
                    backgroundResource = R.drawable.rma_time_click_bg
                    gravity = Gravity.CENTER_VERTICAL
                    textView {
                        tradedatetextview = this
                        text = TimeUtils.getNowString(SimpleDateFormat("yyyy-MM-dd"))
                        textColorInt = R.color.rma_hint_text_color_ccc
                        textSizeAuto = 31
                    }.lparams(wrapContent, wrapContent) {
                        marginStart = autoSize(18)
                    }

                    emptyView()

                    imageView {
                        imageResource = R.mipmap.rma_search_by_time
                    }.lparams(autoSize(38), autoSize(38))
                }.lparams(autoSize(370), autoSize(67)) {
                    marginStart = autoSize(12)
                }

                textView {
                    onThrottleFirstClick {
                        if (viewModel.isselectSpotMonthOrDay.value == "1"){
                            viewModel.qryReportDaySpot(tradedate = tradedate.replace("-",""))
                        }else{
                            viewModel.qryReportMonthSpot(tradedate = tradedate.replace("-",""))
                        }
                    }
                    gravity = Gravity.CENTER
                    backgroundDrawable = createCommonBottomRoundBg(color = "#2481DD", corner = 5)
                    text = "查询"
                    textSizeAuto = 31
                    textColorInt = R.color.white
                }.lparams(autoSize(144), autoSize(67)) {
                    marginStart = autoSize(12)
                }

            }.lparams(matchParent, autoSize(100))

            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(97))

                verticalLayout {
                    gravity = Gravity.CENTER
                    textView {
                        text = "采购定价量"
                        textSizeAuto = 24
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent)

                    textView {
                        text = "销售定价量"
                        textSizeAuto = 24
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent)
                }.lparams(autoSize(225), autoSize(97))

                verticalLayout {
                    gravity = Gravity.CENTER
                    textView {
                        text = "采购确定量"
                        textSizeAuto = 24
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent)

                    textView {
                        text = "销售确定量"
                        textSizeAuto = 24
                        textColorInt = R.color.hint_text_color
                    }.lparams(wrapContent, wrapContent)
                }.lparams(autoSize(225), autoSize(97))

                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(97))
            }.lparams(matchParent, autoSize(97))

            statusLayout(contentBlock = {
                statusLayout = this
//                bindTaskStatus(viewModule.status)
                swipeToLoadLayout {
                    swipeToLayout = this
                    setEnableRefresh(false)
                    setEnableLoadMore(false)
                    setOnRefreshListener {

                    }
                    setEnableScrollContentWhenLoaded(false)
                    setEnableLoadMoreWhenContentNotFull(false)
                    recyclerView {
                        background = resources.getDrawable(R.color.white)
                        adapter = spotReportAdapter
                    }.lparams(matchParent, matchParent)
                }
            }, emptyBlock = {
                emptyView(hint = resources.getString(R.string.now_no_data))
            }).lparams(matchParent, matchParent)

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

/**
 * 财务报表-款项UI
 * @property activity AppCompatActivity
 * @property viewModel ReportQueryViewModel
 * @constructor
 */
class FinancialPaymentUI(
    private val activity: AppCompatActivity,
    private val viewModel: ReportQueryViewModel
) {

    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout
    private val financialReportDayAdapter: BaseAdapter<FinancialReportData, FinancialDayViewHolder> =
        BaseAdapter { _, _ -> FinancialDayViewHolder(activity, viewModel,"1") }

    fun queryRefrashByTime(tradeDate : String){
        viewModel.qryReportDayFinanceKx(tradedate = tradeDate.replace("-",""))
    }

    val root = _FrameLayout(activity).apply {
        verticalLayout {
            viewModel.qryReportDayFinanceKx(tradedate = TimeUtils.getNowString(SimpleDateFormat("yyyyMMdd")))
            FinanciclReportDayTitleUI(type = "1")
            statusLayout(contentBlock = {
                statusLayout = this
//                bindTaskStatus(viewModule.status)
                swipeToLoadLayout {
                    swipeToLayout = this
                    setEnableRefresh(false)
                    setEnableLoadMore(false)
                    setOnRefreshListener {

                    }
                    setEnableScrollContentWhenLoaded(false)
                    setEnableLoadMoreWhenContentNotFull(false)
                    recyclerView {
                        background = resources.getDrawable(R.color.white)
                        adapter = financialReportDayAdapter
                    }.lparams(matchParent, matchParent)
                }
            }, emptyBlock = {
                emptyView(hint = resources.getString(R.string.now_no_data))
            }).lparams(matchParent, matchParent)

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

/**
 * 财务报表-发票UI
 * @property activity AppCompatActivity
 * @property viewModel ReportQueryViewModel
 * @constructor
 */
class FinancialInvoiceUI(
    private val activity: AppCompatActivity,
    private val viewModel: ReportQueryViewModel
) {

    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout
    private val financialReportDayAdapter: BaseAdapter<FinancialReportData, FinancialDayViewHolder> =
        BaseAdapter { _, _ -> FinancialDayViewHolder(activity, viewModel,"1") }

    fun queryRefrashByTime(tradeDate : String){
        viewModel.qryReportDayFinanceFp(tradedate = tradeDate.replace("-",""))
    }

    val root = _FrameLayout(activity).apply {
        verticalLayout {
            viewModel.qryReportDayFinanceFp(tradedate = TimeUtils.getNowString(SimpleDateFormat("yyyyMMdd")))
            FinanciclReportDayTitleUI(type = "2")
            statusLayout(contentBlock = {
                statusLayout = this
//                bindTaskStatus(viewModule.status)
                swipeToLoadLayout {
                    swipeToLayout = this
                    setEnableRefresh(false)
                    setEnableLoadMore(false)
                    setOnRefreshListener {

                    }
                    setEnableScrollContentWhenLoaded(false)
                    setEnableLoadMoreWhenContentNotFull(false)
                    recyclerView {
                        background = resources.getDrawable(R.color.white)
                        adapter = financialReportDayAdapter
                    }.lparams(matchParent, matchParent)
                }
            }, emptyBlock = {
                emptyView(hint = resources.getString(R.string.now_no_data))
            }).lparams(matchParent, matchParent)

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

/**
 * 财务报表日报表头部
 * type  1 款项 2 发票
 */
fun _LinearLayout.FinanciclReportDayTitleUI(type : String){
    linearLayout {
        verticalLayout {
            gravity = Gravity.START
            textView {
                text = if (type == "1"){ "款项发生时间" }else{ "发票发生时间" }
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent){
                marginStart = autoSize(36)
                topMargin = autoSize(15)
            }
        }.lparams(autoSize(225), autoSize(97))

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

        verticalLayout {
            gravity = Gravity.CENTER
            textView {
                text = "类型"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent)
            textView {
                text = "金额"
                textSizeAuto = 24
                textColorInt = R.color.hint_text_color
            }.lparams(wrapContent, wrapContent)
        }.lparams(autoSize(225), autoSize(97))

        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(97))
    }.lparams(matchParent, autoSize(97))
}

/**
 * 财务报表月报表头部
 * @receiver _LinearLayout
 */
fun _LinearLayout.FinancialReportUiTitle() {
    linearLayout {
        verticalLayout {
            textView {
                text = "日期"
                textSizeAuto = 24
                textColorInt = R.color.text_hit_color
            }.lparams(wrapContent, wrapContent)
        }.lparams(autoSize(180), autoSize(97))

        verticalLayout {
            gravity = Gravity.END
            textView {
                text = "收款笔数"
                textSizeAuto = 24
                textColorInt = R.color.text_hit_color
            }.lparams(wrapContent, wrapContent)

            textView {
                text = "收款金额"
                textSizeAuto = 24
                textColorInt = R.color.text_hit_color
            }.lparams(wrapContent, wrapContent)
        }.lparams(autoSize(180), autoSize(97))

        verticalLayout {
            gravity = Gravity.END
            textView {
                text = "付款笔数"
                textSizeAuto = 24
                textColorInt = R.color.text_hit_color
            }.lparams(wrapContent, wrapContent)

            textView {
                text = "付款金额"
                textSizeAuto = 24
                textColorInt = R.color.text_hit_color
            }.lparams(wrapContent, wrapContent)
        }.lparams(autoSize(180), autoSize(97))

        verticalLayout {
            gravity = Gravity.END
            textView {
                text = "收票笔数"
                textSizeAuto = 24
                textColorInt = R.color.text_hit_color
            }.lparams(wrapContent, wrapContent)

            textView {
                text = "收票金额"
                textSizeAuto = 24
                textColorInt = R.color.text_hit_color
            }.lparams(wrapContent, wrapContent)
        }.lparams(autoSize(180), autoSize(97))

        verticalLayout {
            gravity = Gravity.END
            textView {
                text = "开票笔数"
                textSizeAuto = 24
                textColorInt = R.color.text_hit_color
            }.lparams(wrapContent, wrapContent)

            textView {
                text = "开票金额"
                textSizeAuto = 24
                textColorInt = R.color.text_hit_color
            }.lparams(wrapContent, wrapContent)
        }.lparams(autoSize(180), autoSize(97))
    }.lparams(matchParent, autoSize(97))
}