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

import android.os.Bundle
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.ContractData
import cn.muchinfo.rma.view.autoWidget.*
import cn.muchinfo.rma.view.base.BaseActivity
import mtp.polymer.com.autowidget.dialog.createLoadingDialog
import mtp.polymer.com.autowidget.utils.bindTaskStatus
import org.jetbrains.anko.*

class ContractDetailsActivity : BaseActivity<ContractViewModel>(){

    /**
     * 详情内的操作类型 1是单独显示详情 2 可以审核通过，审核拒绝  3是撤销  4是正常完结
     */
    private val type by lazy {
        intent.getStringExtra("type")
    }

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

    lateinit var remarskText : EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            createLoadingDialog("").bindTaskStatus(context,viewModel.loadingDialogStatus)
            topBar {
                commonLeftButton()
                commonTitle {
                    text = "采购合同详情"
                }
            }

            frameLayout {
                scrollView {
                    verticalLayout {
                        linearLayout {
                            gravity = Gravity.CENTER_VERTICAL
                            if (data.contracctstatus == "3"){
                                background = resources.getDrawable(R.color.rma_pass_status)//rma_pass_status已通过背景色
                            }else{
                                background = resources.getDrawable(R.color.rma_details_status)//rma_pass_status已通过背景色
                            }
                            imageView {
                                if (data.contracctstatus == "3"){
                                    imageResource = R.mipmap.rma_pass_status
                                }else{
                                    imageResource = R.mipmap.rma_wait_status
                                }

                            }.lparams(autoSize(38), autoSize(38)) {
                                marginStart = autoSize(37)
                            }
                            textView {
                                text = ViewEnumUtils.getContractStatus(data.contracctstatus ?: "")
                                textSizeAuto = 31
                                textColorInt = R.color.rma_yellow_text_color
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(20)
                            }
                        }.lparams(matchParent, autoSize(60))

                        baseInformationView("基本信息")

                        contractDetailOneView(data = data)

                        baseInformationView("现货信息")

                        contractDetailTwoView(data = data)

                        baseInformationView("价格信息")

                        contractDetailThreeView(data = data)

                        baseInformationView("其他信息")

                        contractDetailFourView(data = data)

                        view {
                            background = resources.getDrawable(R.color.main_hit_bg_color)
                        }.lparams(matchParent, autoSize(20))

                        linearLayout {
                            if (type == "2"){
                                visibility = View.VISIBLE
                            }else{
                                visibility = View.GONE
                            }
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "审核意见"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(59)
                            }

                            editText {
                                remarskText = this
                                hint = "请输入审核意见"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_TEXT
                                hintColorStr = "#CCCCCC"
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(matchParent, autoSize(260)) {
                                marginStart = autoSize(80)
                            }
                        }.lparams(matchParent, autoSize(136)) {
                            bottomMargin = autoSize(200)
                        }
                    }
                }

                linearLayout {
                    if (type == "1"){
                        visibility = View.GONE
                    }else{
                        visibility =View.VISIBLE
                    }
                    background = resources.getDrawable(R.color.white)
                    gravity = Gravity.CENTER_VERTICAL
                    textView {
                        onThrottleFirstClick {
                            viewModel.orderContract(OperateType = "4",Remark = remarskText.text.toString(),SpotContractID = data.spotcontractid ?: ""){
                                finish()
                            }
                        }
                        if (type == "2"){
                            visibility = View.VISIBLE
                        }else{
                            visibility = View.GONE
                        }
                        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.orderContract(OperateType = "3",Remark = remarskText.text.toString(),SpotContractID = data.spotcontractid ?: ""){
                                finish()
                            }
                        }
                        if (type == "2"){
                            visibility = View.VISIBLE
                        }else{
                            visibility = View.GONE
                        }
                        gravity = Gravity.CENTER
                        backgroundResource = R.mipmap.rma_submit_bg
                        text = "审核通过"
                        textColorInt = R.color.white
                        textSizeAuto = 38
                    }.lparams(autoSize(419), autoSize(119)){
                        marginStart = autoSize(13)
                    }

                    textView {
                        onThrottleFirstClick {
                            viewModel.orderContract(OperateType = "5",Remark = remarskText.text.toString(),SpotContractID = data.spotcontractid ?: ""){
                                finish()
                            }
                        }
                        visibility = if (type == "3"){
                            View.VISIBLE
                        }else{
                            View.GONE
                        }
                        gravity = Gravity.CENTER
                        backgroundResource = R.mipmap.rma_submit_bg
                        text = "撤销"
                        textColorInt = R.color.white
                        textSizeAuto = 38
                    }.lparams(autoSize(780), autoSize(119)){
                        marginStart = autoSize(59)
                    }

                    textView {
                        onThrottleFirstClick {
                            viewModel.orderContract(OperateType = "6",Remark = remarskText.text.toString(),SpotContractID = data.spotcontractid ?: ""){
                                finish()
                            }
                        }
                        visibility = if (type == "4"){
                            View.VISIBLE
                        }else{
                            View.GONE
                        }
                        gravity = Gravity.CENTER
                        backgroundResource = R.mipmap.rma_submit_bg
                        text = "正常完结"
                        textColorInt = R.color.white
                        textSizeAuto = 38
                    }.lparams(autoSize(780), autoSize(119)){
                        marginStart = autoSize(59)
                    }

                }.lparams(matchParent, autoSize(144)){
                    gravity = Gravity.BOTTOM
                }
            }
        }
    }

}