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

import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.ViewEnumUtils
import cn.muchinfo.rma.global.data.BusinessData
import cn.muchinfo.rma.global.isBlankNumber
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 mtp.polymer.com.autowidget.dialog.createLoadingDialog
import mtp.polymer.com.autowidget.utils.bindTaskStatus
import org.jetbrains.anko.*

/**
 *  管理 - 结算审核页面
 */
class SettlementAuditActivity : BaseActivity<BusinessManagementViewModel>() {

    /**
     * 页面类型 type 1 审核 2 详情
     */
    private val type by lazy {
        intent.getStringExtra("type")
    }

    /** 输入的审核意见 **/
    lateinit var ramsk_edittext: EditText

    val data: BusinessData by lazy {
        intent.getParcelableExtra<BusinessData>("data") as BusinessData
    }

    private val dialog by lazy { createLoadingDialog(hintStr = "请求中...") }

    private lateinit var statusLayout: StatusLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            viewModel.queryBusinessDetails(data.relatedid ?: "")
            dialog.bindTaskStatus(context, viewModel.loadingDialogStatus)
            val margingPadding = autoSize(47)
            background = resources.getDrawable(R.color.segtabment_bg_color)

            topBar {
                commonLeftButton()
                commonTitle {
                    text = if (type == "1"){
                        "结算审核"
                    }else{
                        "结算详情"
                    }
                }
            }
            statusLayout(contentBlock = {
                statusLayout = this
                bindTaskStatus(viewModel.loadingDataDialogStatus)
                frameLayout {
                    scrollView {
                        verticalLayout {
                            verticalLayout {
                                background = resources.getDrawable(R.color.white)
                                textView {
                                    text = "合同基本信息"
                                    textColorInt = R.color.rma_blue_color
                                    textSizeAuto = 34
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = margingPadding
                                    topMargin = autoSize(25)
                                }
                                linearLayout {
                                    gravity = Gravity.CENTER_VERTICAL
                                    linearLayout {
                                        textView {
                                            text = "合同编号"
                                            textColorInt = R.color.p_normal_summary_txt_color
                                            textSizeAuto = 29
                                        }.lparams(wrapContent, wrapContent) {
                                            marginStart = margingPadding
                                        }

                                        linearLayout {
                                            emptyView()
                                            textView {
                                                text = ViewEnumUtils.getContractType(
                                                    data.contracttype ?: ""
                                                )
                                                textSizeAuto = 29
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent)
                                            textView {
                                                text = data.contractno
                                                textSizeAuto = 17
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent)
                                        }.lparams(matchParent, wrapContent)

                                    }.lparams(autoSize(450), wrapContent)
                                    linearLayout {
                                        textView {
                                            text = if (data.contracttype == "1") {
                                                "销售方"
                                            } else {
                                                "采购方"
                                            }
                                            textColorInt = R.color.p_normal_summary_txt_color
                                            textSizeAuto = 29
                                        }.lparams(wrapContent, wrapContent) {
                                            marginStart = margingPadding
                                        }

                                        linearLayout {
                                            emptyView()
                                            textView {
                                                text = if (data.contracttype == "1") {
                                                    data.sellusername
                                                } else {
                                                    data.buyusername
                                                }
                                                textSizeAuto = 29
                                                textColorInt = R.color.rma_black_33
                                                maxLines = 1
                                                maxEms = 9
                                                ellipsize = TextUtils.TruncateAt.END
                                            }.lparams(wrapContent, wrapContent) {
                                                marginEnd = margingPadding
                                            }
                                        }.lparams(matchParent, wrapContent)
                                    }.lparams(autoSize(450), wrapContent)
                                }.lparams(matchParent, autoSize(60)) {
                                    topMargin = autoSize(30)
                                }

                                linearLayout {
                                    gravity = Gravity.CENTER_VERTICAL
                                    linearLayout {
                                        textView {
                                            text = "定价类型"
                                            textColorInt = R.color.p_normal_summary_txt_color
                                            textSizeAuto = 29
                                        }.lparams(wrapContent, wrapContent) {
                                            marginStart = margingPadding
                                        }

                                        linearLayout {
                                            emptyView()
                                            textView {
                                                text =
                                                    ViewEnumUtils.getPriceType(data.pricetype ?: "")
                                                textSizeAuto = 29
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent)

                                        }.lparams(matchParent, wrapContent)

                                    }.lparams(autoSize(450), wrapContent)
                                    linearLayout {
                                        textView {
                                            text = "商品名称"
                                            textColorInt = R.color.p_normal_summary_txt_color
                                            textSizeAuto = 29
                                        }.lparams(wrapContent, wrapContent) {
                                            marginStart = margingPadding
                                        }

                                        linearLayout {
                                            emptyView()
                                            textView {
                                                text = data.goodsname
                                                textSizeAuto = 29
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent) {
                                                marginEnd = margingPadding
                                            }
                                        }.lparams(matchParent, wrapContent)
                                    }.lparams(autoSize(450), wrapContent)
                                }.lparams(matchParent, autoSize(60))

                                view {
                                    background =
                                        resources.getDrawable(R.color.rma_hint_line_color_eee)
                                }.lparams(matchParent, autoSize(1)) {
                                    marginStart = autoSize(36)
                                    marginEnd = autoSize(36)
                                    topMargin = autoSize(30)
                                }


                                textView {
                                    text = "待审核登记信息"
                                    textColorInt = R.color.rma_blue_color
                                    textSizeAuto = 34
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = margingPadding
                                    topMargin = autoSize(25)
                                }

                                linearLayout {
                                    gravity = Gravity.CENTER_VERTICAL
                                    linearLayout {
                                        textView {
                                            text = "结算量"
                                            textColorInt = R.color.p_normal_summary_txt_color
                                            textSizeAuto = 29
                                        }.lparams(wrapContent, wrapContent) {
                                            marginStart = margingPadding
                                        }

                                        linearLayout {
                                            emptyView()
                                            textView {
                                                viewModel.settlementDetails.bindOptional(context){
                                                    text = it?.reckonrealqty?.isBlankNumber()
                                                }
                                                textSizeAuto = 29
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent)
                                            textView {
                                                text = data.enumdicname
                                                textSizeAuto = 17
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent)
                                        }.lparams(matchParent, wrapContent)

                                    }.lparams(autoSize(450), wrapContent)
                                    linearLayout {
                                        textView {
                                            text = "调整金额"
                                            textColorInt = R.color.p_normal_summary_txt_color
                                            textSizeAuto = 29
                                        }.lparams(wrapContent, wrapContent) {
                                            marginStart = margingPadding
                                        }

                                        linearLayout {
                                            emptyView()
                                            textView {
                                                viewModel.settlementDetails.bindOptional(context){
                                                    text = it?.reckonadjustamount?.isBlankNumber()
                                                }
                                                textSizeAuto = 29
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent)
                                            textView {
                                                text = "元"
                                                textSizeAuto = 17
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent) {
                                                marginEnd = autoSize(47)
                                            }
                                        }.lparams(matchParent, wrapContent)

                                    }.lparams(autoSize(450), wrapContent)
                                }.lparams(matchParent, autoSize(60)) {
                                    topMargin = autoSize(25)
                                }

                                linearLayout {
                                    gravity = Gravity.CENTER_VERTICAL
                                    linearLayout {
                                        textView {
                                            text = "其他费用"
                                            textColorInt = R.color.p_normal_summary_txt_color
                                            textSizeAuto = 29
                                        }.lparams(wrapContent, wrapContent) {
                                            marginStart = margingPadding
                                        }

                                        linearLayout {
                                            emptyView()
                                            textView {
                                                viewModel.settlementDetails.bindOptional(context){
                                                    text = it?.reckonotheramount?.isBlankNumber()
                                                }
                                                textSizeAuto = 29
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent)
                                            textView {
                                                text = "元"
                                                textSizeAuto = 17
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent)
                                        }.lparams(matchParent, wrapContent)

                                    }.lparams(autoSize(450), wrapContent)
                                    linearLayout {
                                        textView {
                                            text = "登记时间"
                                            textColorInt = R.color.p_normal_summary_txt_color
                                            textSizeAuto = 29
                                        }.lparams(wrapContent, wrapContent) {
                                            marginStart = margingPadding
                                        }

                                        linearLayout {
                                            emptyView()
                                            textView {
                                                text = data.applytime
                                                textSizeAuto = 24
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent) {
                                                marginEnd = autoSize(47)
                                            }
                                            textView {
                                                visibility = View.GONE
                                                text = "00:00:00"
                                                textSizeAuto = 17
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent)
                                        }.lparams(matchParent, wrapContent)

                                    }.lparams(autoSize(450), wrapContent)
                                }.lparams(matchParent, autoSize(60))

                                linearLayout {
                                    gravity = Gravity.CENTER_VERTICAL
                                    linearLayout {
                                        textView {
                                            text = "调整保证金"
                                            textColorInt = R.color.p_normal_summary_txt_color
                                            textSizeAuto = 29
                                        }.lparams(wrapContent, wrapContent) {
                                            marginStart = margingPadding
                                        }

                                        linearLayout {
                                            emptyView()
                                            textView {
                                                viewModel.settlementDetails.bindOptional(context){
                                                    text = it?.decmargin?.isBlankNumber()
                                                }
                                                textSizeAuto = 29
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent)
                                            textView {
                                                text = "元"
                                                textSizeAuto = 17
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent)
                                        }.lparams(matchParent, wrapContent)

                                    }.lparams(autoSize(450), wrapContent)
                                    linearLayout {
                                        textView {
                                            text = "登记人"
                                            textColorInt = R.color.p_normal_summary_txt_color
                                            textSizeAuto = 29
                                        }.lparams(wrapContent, wrapContent) {
                                            marginStart = margingPadding
                                        }

                                        linearLayout {
                                            emptyView()
                                            textView {
                                                text = data.applyname?.isBlankString()
                                                textSizeAuto = 29
                                                textColorInt = R.color.rma_black_33
                                            }.lparams(wrapContent, wrapContent) {
                                                marginEnd = margingPadding
                                            }
                                        }.lparams(matchParent, wrapContent)
                                    }.lparams(autoSize(450), wrapContent)
                                }.lparams(matchParent, autoSize(60))
                            }

                            linearLayout {
                                visibility = if (type == "1"){
                                View.VISIBLE
                            }else{
                                View.GONE
                            }
                                background = resources.getDrawable(R.color.white)

                                textView {
                                    text = "审核意见"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(59)
                                    topMargin = autoSize(40)
                                }

                                editText {
                                    gravity = Gravity.TOP
                                    ramsk_edittext = this
                                    hint = "请输入审核意见"
                                    background = null
                                    inputType = EditorInfo.TYPE_CLASS_TEXT
                                    hintColorStr = "#CCCCCC"
                                    textSizeAuto = 31
                                    textColorStr = "#333333"
                                }.lparams(matchParent, autoSize(227)) {
                                    marginStart = autoSize(80)
                                    topMargin = autoSize(40)
                                }
                            }.lparams(matchParent, autoSize(227)) {
                                bottomMargin = autoSize(200)
                                topMargin = autoSize(20)
                            }
                        }
                    }


                    linearLayout {
                        visibility = if (type == "1"){
                                View.VISIBLE
                            }else{
                                View.GONE
                            }
                        background = resources.getDrawable(R.color.white)
                        gravity = Gravity.CENTER_VERTICAL
                        textView {
                            onThrottleFirstClick {
                                viewModel.contractOperaOrder(operateApplyID = data.operateapplyid?.toLong() ?: 0,operateType = 3,remark = ramsk_edittext.text.toString()){
                                    finish()
                                }
                            }
                            gravity = Gravity.CENTER
                            backgroundResource = R.mipmap.rma_save_info_bg
                            text = "审核拒绝"
                            textSizeAuto = 38
                            textColorInt = R.color.rma_star_color
                        }.lparams(autoSize(419), autoSize(119)) {
                            marginStart = autoSize(24)
                        }

                        textView {
                            onThrottleFirstClick {
                                viewModel.contractOperaOrder(operateApplyID = data.operateapplyid?.toLong() ?: 0,operateType = 2,remark = ramsk_edittext.text.toString()){
                                    finish()
                                }
                            }
                            gravity = Gravity.CENTER
                            backgroundResource = R.mipmap.rma_submit_bg
                            text = "审核通过"
                            textColorInt = R.color.white
                            textSizeAuto = 38
                        }.lparams(autoSize(419), autoSize(119)) {
                            marginStart = autoSize(13)
                        }

                        view {
                            background = resources.getDrawable(R.color.rma_hint_line_color_eee)
                        }.lparams(matchParent, autoSize(1)) {
                            marginStart = autoSize(36)
                            marginEnd = autoSize(36)
                            topMargin = autoSize(30)
                        }

                    }.lparams(matchParent, autoSize(144)) {
                        gravity = Gravity.BOTTOM
                    }
                }
            }, emptyBlock = {
                emptyView(hint = resources.getString(R.string.now_no_data))
            }).lparams(matchParent, matchParent)

        }
    }

}