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

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.inputmethod.EditorInfo
import android.widget.CheckBox
import android.widget.CompoundButton
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.protobuf.protoclasses.ErmcpMI1
import cn.muchinfo.rma.view.autoWidget.*
import cn.muchinfo.rma.view.base.BaseActivity
import cn.muchinfo.rma.view.base.home.contract.emptyView
import com.blankj.utilcode.util.ToastUtils
import com.google.protobuf.ByteString
import mtp.polymer.com.autowidget.dialog.createLoadingDialog
import mtp.polymer.com.autowidget.utils.bindTaskStatus
import org.jetbrains.anko.*
import org.json.JSONObject

/**
 * 结算登记页面
 */
class SettlementRegistrationActivity : BaseActivity<RegistrationViewModel>() {

    /** 确定量的输入框 **/
    lateinit var determine_edittext: EditText

    /** 其他费用 **/
    lateinit var other_price_edittext: EditText

    /** 保证金追加选择 **/
    lateinit var margin_add_checkbox: CheckBox

    /** 保证金减少选择 **/
    lateinit var margin_min_checkbox: CheckBox

    /** 保证金输入框 **/
    lateinit var margin_price: EditText

    /** 金额追加选择 **/
    lateinit var amount_add_checkbox: CheckBox

    /** 金额退还选择 **/
    lateinit var amount_min_checkbox: CheckBox

    /** 金额输入框 **/
    lateinit var amount_edittext: EditText

    /** 备注信息 **/
    lateinit var ramask_edittext: EditText

    /** 确定量选择框 **/
    lateinit var determine_checkbox: CheckBox

    /** 其他费用选择框 **/
    lateinit var other_amount_checkbox: CheckBox

    /** 调整保证金选择框 **/
    lateinit var margin_checkbox: CheckBox

    /** 调整金额 **/
    lateinit var amount_checkbox: CheckBox

    //type 为1是采购 2是销售
    private val type by lazy {
        intent.getStringExtra("type")
    }

    //传进来的数据
    private val data by lazy {
        intent.getParcelableExtra<ContractData>("data") as ContractData
    }

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

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            val margingPadding = autoSize(47)
            dialog.bindTaskStatus(context, viewModel.loadingDialogStatus)
            background = resources.getDrawable(R.color.main_hit_bg_color)
            //页面标题
            topBar {
                commonLeftButton()
                commonTitle {
                    text = "结算登记"
                }
            }

            relativeLayout {
                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.spotcontractid
                                            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.deliverygoodsname
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.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.qty
                                            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) {
                                            marginEnd = margingPadding
                                        }
                                    }.lparams(matchParent, wrapContent)
                                }.lparams(autoSize(450), wrapContent)
                            }.lparams(matchParent, autoSize(60))

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                linearLayout {
                                    textView {
                                        text = if (type == "1") {
                                            "销售方"
                                        } else {
                                            "采购方"
                                        }
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            text = data.accountname
                                            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.price
                                            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) {
                                            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 = margingPadding
                                marginEnd = margingPadding
                                topMargin = autoSize(30)
                            }

                            textView {
                                text = "合同基本信息"
                                textColorInt = R.color.rma_blue_color
                                textSizeAuto = 34
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = margingPadding
                                topMargin = margingPadding
                            }

                            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 = data.pricedqty + data.enumdicname
                                            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.reckonadjustamount + "元"
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.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 = data.reckonrealqty + data.enumdicname
                                            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.margin + "元"
                                            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 {
                                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 = data.reckonotheramount + "元"
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)

                                    }.lparams(matchParent, wrapContent)

                                }.lparams(autoSize(450), wrapContent)

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

                        verticalLayout {
                            backgroundDrawable = createRoundRectDrawable("#ffffff", 14)
                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                textView {
                                    text = "本次结算信息"
                                    textSizeAuto = 34
                                    textColorInt = R.color.rma_blue_color
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(34)
                                }
                                imageView {
                                    imageResource = R.mipmap.rma_zhushi
                                }.lparams(autoSize(36), autoSize(36)) {
                                    marginStart = autoSize(29)
                                }
                            }.lparams(matchParent, autoSize(60))

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                checkBox {
                                    determine_checkbox = this
                                    buttonDrawable = null
                                    background =
                                        resources.getDrawable(R.drawable.rma_check_backgrond)
                                    setOnCheckedChangeListener { p0, isCheck ->
                                        if (isCheck.not()) {
                                            determine_edittext.setText("")
                                        }
                                        determine_edittext.setEnabled(isCheck)
                                    }
                                }.lparams(autoSize(43), autoSize(43)) {
                                    marginStart = autoSize(35)
                                }

                                textView {
                                    text = "确定量"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(30)
                                }


                                linearLayout {
                                    background = resources.getDrawable(R.drawable.rma_item_click_bg)

                                    editText {
                                        setEnabled(false)
                                        determine_edittext = this
                                        setPadding(0, 10, 0, 0)
                                        setSingleLine(true)
                                        gravity = Gravity.CENTER
                                        hint = "请输入确定量"
                                        background = null
                                        inputType = EditorInfo.TYPE_CLASS_NUMBER
                                        setDecimalInputType()
                                        hintColorStr = "#CCCCCC"
                                        textSizeAuto = 31
                                        textColorStr = "#333333"
                                    }.lparams(matchParent, matchParent) {

                                    }
                                }.lparams(autoSize(432), autoSize(72)) {
                                    marginStart = autoSize(110)
                                }

                                textView {
                                    text = "吨"
                                    textSizeAuto = 29
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(28)
                                }
                            }.lparams(matchParent, autoSize(80))

                            linearLayout {
                                textView {
                                    text = "待确定量："
                                    textSizeAuto = 29
                                    textColorInt = R.color.text_hit_color
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(316)
                                }

                                textView {
                                    text = "20"
                                    textSizeAuto = 29
                                    textColorInt = R.color.rma_item_click_color
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(15)
                                }

                                textView {
                                    text = "吨"
                                    textSizeAuto = 29
                                    textColorInt = R.color.text_hit_color
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(10)
                                }

                                emptyView()

                                textView {
                                    text = "全部登记"
                                    textSizeAuto = 29
                                    textColorInt = R.color.rma_green_color
                                }.lparams(wrapContent, wrapContent) {
                                    marginEnd = autoSize(105)
                                }
                            }.lparams(matchParent, wrapContent) {
                                topMargin = autoSize(10)
                            }
                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                //其他费用选择
                                checkBox {
                                    other_amount_checkbox = this
                                    buttonDrawable = null
                                    background =
                                        resources.getDrawable(R.drawable.rma_check_backgrond)
                                    setOnCheckedChangeListener { compoundButton, b ->
                                        if (b.not()) {
                                            other_price_edittext.setText("")
                                        }
                                        other_price_edittext.isEnabled = b
                                    }
                                }.lparams(autoSize(43), autoSize(43)) {
                                    marginStart = autoSize(35)
                                }

                                textView {
                                    text = "其他费用"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(30)
                                }


                                linearLayout {
                                    background = resources.getDrawable(R.drawable.rma_item_click_bg)

                                    editText {
                                        other_price_edittext = this
                                        isEnabled = false
                                        setPadding(0, 10, 0, 0)
                                        setSingleLine(true)
                                        gravity = Gravity.CENTER
                                        hint = "0.00"
                                        background = null
                                        inputType = EditorInfo.TYPE_CLASS_TEXT
                                        setDecimalInputType()
                                        hintColorStr = "#CCCCCC"
                                        textSizeAuto = 31
                                        textColorStr = "#333333"
                                    }.lparams(matchParent, matchParent) {

                                    }
                                }.lparams(autoSize(432), autoSize(72)) {
                                    marginStart = autoSize(80)
                                }

                                textView {
                                    text = "元"
                                    textSizeAuto = 29
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(28)
                                }
                            }.lparams(matchParent, autoSize(80)) {
                                topMargin = autoSize(10)
                            }


                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                //调整保证金
                                checkBox {
                                    margin_checkbox = this
                                    buttonDrawable = null
                                    background =
                                        resources.getDrawable(R.drawable.rma_check_backgrond)
                                    setOnCheckedChangeListener { compoundButton, isCheck ->
                                        if (isCheck.not()) {
                                            margin_price.setText("")
                                            margin_min_checkbox.isChecked = false
                                            margin_add_checkbox.isChecked = false
                                        }
                                        if (isCheck) {
                                            margin_add_checkbox.isChecked = true
                                        }
                                        margin_add_checkbox.isEnabled = isCheck
                                        margin_min_checkbox.isEnabled = isCheck
                                        margin_price.isEnabled = isCheck
                                    }
                                }.lparams(autoSize(43), autoSize(43)) {
                                    marginStart = autoSize(35)
                                }

                                textView {
                                    text = "调整保证金"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(30)
                                }

                                checkBox {
                                    isEnabled = false
                                    margin_add_checkbox = this
                                    buttonDrawable = null
                                    background =
                                        resources.getDrawable(R.drawable.rma_check_cercle_backgrond)
                                    setOnCheckedChangeListener { compoundButton, isClick ->
                                        if (isClick) {
                                            margin_min_checkbox.isChecked = false
                                        }
                                    }
                                }.lparams(autoSize(41), autoSize(41)) {
                                    marginStart = autoSize(54)
                                }

                                textView {
                                    text = "追加"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(15)
                                }

                                checkBox {
                                    isEnabled = false
                                    margin_min_checkbox = this
                                    buttonDrawable = null
                                    background =
                                        resources.getDrawable(R.drawable.rma_check_cercle_backgrond)
                                    setOnCheckedChangeListener { compoundButton, isClick ->
                                        if (isClick) {
                                            margin_add_checkbox.isChecked = false
                                        }
                                    }
                                }.lparams(autoSize(41), autoSize(41)) {
                                    marginStart = autoSize(100)
                                }

                                textView {
                                    text = "退还"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(15)
                                }

                            }.lparams(matchParent, autoSize(80)) {
                                topMargin = autoSize(50)
                            }

                            linearLayout {
                                linearLayout {
                                    background = resources.getDrawable(R.drawable.rma_item_click_bg)

                                    editText {
                                        margin_price = this
                                        isEnabled = false
                                        setPadding(0, 10, 0, 0)
                                        setSingleLine(true)
                                        gravity = Gravity.CENTER
                                        hint = "0.00"
                                        background = null
                                        inputType = EditorInfo.TYPE_CLASS_NUMBER
                                        setDecimalInputType()
                                        hintColorStr = "#CCCCCC"
                                        textSizeAuto = 31
                                        textColorStr = "#333333"
                                    }.lparams(matchParent, matchParent) {

                                    }
                                }.lparams(autoSize(432), autoSize(72)) {
                                    marginStart = autoSize(316)
                                }

                                textView {
                                    text = "元"
                                    textSizeAuto = 29
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(28)
                                }

                            }.lparams(matchParent, autoSize(80)) {
                                topMargin = autoSize(25)
                            }


                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                //调整金额
                                checkBox {
                                    amount_checkbox = this
                                    buttonDrawable = null
                                    background =
                                        resources.getDrawable(R.drawable.rma_check_backgrond)
                                    setOnCheckedChangeListener { _, isClick ->
                                        if (isClick.not()) {
                                            amount_min_checkbox.isChecked = false
                                            amount_add_checkbox.isChecked = false
                                            amount_edittext.setText("")
                                        }
                                        if (isClick) {
                                            amount_add_checkbox.isChecked = true
                                        }
                                        amount_add_checkbox.isEnabled = isClick
                                        amount_min_checkbox.isEnabled = isClick
                                        amount_edittext.isEnabled = isClick
                                    }
                                }.lparams(autoSize(43), autoSize(43)) {
                                    marginStart = autoSize(35)
                                }

                                textView {
                                    text = "调整金额"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(30)
                                }

                                checkBox {
                                    isEnabled = false
                                    amount_add_checkbox = this
                                    buttonDrawable = null
                                    background =
                                        resources.getDrawable(R.drawable.rma_check_cercle_backgrond)
                                    setOnCheckedChangeListener { compoundButton, isClick ->
                                        if (isClick) {
                                            amount_min_checkbox.isChecked = false
                                        }
                                    }
                                }.lparams(autoSize(41), autoSize(41)) {
                                    marginStart = autoSize(84)
                                }

                                textView {
                                    text = "追加"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(15)
                                }

                                checkBox {
                                    isEnabled = false
                                    amount_min_checkbox = this
                                    buttonDrawable = null
                                    background =
                                        resources.getDrawable(R.drawable.rma_check_cercle_backgrond)
                                    setOnCheckedChangeListener { compoundButton, isClick ->
                                        if (isClick) {
                                            amount_add_checkbox.isChecked = false
                                        }
                                    }
                                }.lparams(autoSize(41), autoSize(41)) {
                                    marginStart = autoSize(100)
                                }

                                textView {
                                    text = "退还"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(15)
                                }

                            }.lparams(matchParent, autoSize(80)) {
                                topMargin = autoSize(25)
                            }



                            linearLayout {
                                linearLayout {
                                    background = resources.getDrawable(R.drawable.rma_item_click_bg)

                                    editText {
                                        isEnabled = false
                                        amount_edittext = this
                                        setPadding(0, 10, 0, 0)
                                        setSingleLine(true)
                                        gravity = Gravity.CENTER
                                        hint = "0.00"
                                        background = null
                                        inputType = EditorInfo.TYPE_CLASS_NUMBER
                                        hintColorStr = "#CCCCCC"
                                        textSizeAuto = 31
                                        textColorStr = "#333333"
                                    }.lparams(matchParent, matchParent) {

                                    }
                                }.lparams(autoSize(432), autoSize(72)) {
                                    marginStart = autoSize(316)
                                }

                                textView {
                                    text = "元"
                                    textSizeAuto = 29
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(28)
                                }

                            }.lparams(matchParent, autoSize(80)) {
                                topMargin = autoSize(25)
                            }


                            linearLayout {
                                textView {
                                    text = "备注"
                                    textSizeAuto = 31
                                    textColorInt = R.color.rma_black_33
                                }.lparams(wrapContent, wrapContent) {
                                    marginStart = autoSize(108)
                                }

                                linearLayout {
                                    background = resources.getDrawable(R.drawable.rma_item_click_bg)
                                    editText {
                                        ramask_edittext = this
                                        setPadding(0, 10, 0, 0)
                                        hint = "请输入备注信息"
                                        gravity = Gravity.LEFT or Gravity.TOP
                                        background = null
                                        inputType = EditorInfo.TYPE_CLASS_TEXT
                                        hintColorStr = "#CCCCCC"
                                        textSizeAuto = 31
                                        textColorStr = "#333333"
                                    }.lparams(matchParent, matchParent) {

                                    }
                                }.lparams(autoSize(432), autoSize(220)) {
                                    marginStart = autoSize(148)
                                }
                            }.lparams(matchParent, autoSize(220)) {
                                topMargin = autoSize(25)
                                bottomMargin = autoSize(40)
                            }

                        }.lparams(matchParent, wrapContent) {
                            marginStart = autoSize(24)
                            marginEnd = autoSize(24)
                            topMargin = autoSize(24)
                            bottomMargin = autoSize(200)
                        }


                    }.lparams(autoSize(900), matchParent)
                }

                verticalLayout {
                    view { }.lparams(autoSize(1), 0, 1f)
                    linearLayout {
                        background = resources.getDrawable(R.color.white)
                        gravity = Gravity.CENTER_VERTICAL

                        textView {
                            onThrottleFirstClick {
                                if (checkUpdate().not()) {
                                    return@onThrottleFirstClick
                                }
                                viewModel.orderPerationContract(
                                    info = getPerationContractInfo(),
                                    operateType = 1,
                                    remark = ramask_edittext.text.toString()
                                ) {
                                    finish()
                                }
                            }
                            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
                    }
                }.lparams(matchParent, matchParent)
            }
        }
    }

    /**
     * 获取合同操作info
     * @return ErmcpMI1.ContractOperateApplyInfo
     */
    private fun getPerationContractInfo(): ErmcpMI1.ContractOperateApplyInfo {
        val json = JSONObject()
        if (determine_edittext.text.toString().isNotEmpty()) {
            json.put("ReckonRealQty", determine_edittext.text.toString().toDouble())
        }
        if (other_price_edittext.text.toString().isNotEmpty()) {
            json.put("ReckonOtherAmount", other_price_edittext.text.toString().toDouble())
        }
        if (margin_checkbox.text.toString().isNotEmpty()) {
            if (margin_add_checkbox.isChecked) {
                json.put("AddMargin", margin_price.text.toString().toDouble())
            } else if (margin_min_checkbox.isChecked) {
                json.put("DecMargin", margin_price.text.toString().toDouble())
            }
        }
        if (amount_edittext.text.toString().isNotEmpty()) {
            json.put("ReckonAdjustAmount", -amount_edittext.text.toString().toDouble())
        }
        val contractInfobuilder = ErmcpMI1.ContractOperateApplyInfo.newBuilder()

        contractInfobuilder.relatedID = data.spotcontractid?.toLong() ?: 0// 现货合同ID
        contractInfobuilder.operateApplyType = 2//结算
        contractInfobuilder.detailJson = ByteString.copyFrom(json.toString().toByteArray())

        return contractInfobuilder.build()
    }

    /**
     * 判断条件（确定量，其他费用，调整保证金，调整金额最少选择一个）
     * @return Boolean
     */
    private fun checkUpdate(): Boolean {
        if (determine_checkbox.isChecked.not() && other_amount_checkbox.isChecked.not() && margin_checkbox.isChecked.not() && amount_checkbox.isChecked.not()) {
            ToastUtils.showLong("本次结算至少需要勾选一项信息填写")
            return false
        }

        if (determine_checkbox.isChecked) {
            if (determine_edittext.text.toString().isNullOrEmpty()) {
                ToastUtils.showLong("请输入确定量")
                return false
            }
        }

        if (other_amount_checkbox.isChecked) {
            if (other_price_edittext.text.toString().isNullOrEmpty()) {
                ToastUtils.showLong("请输入其他费用")
                return false
            }
        }

        if (margin_checkbox.isChecked) {
            if (margin_price.text.toString().isNullOrEmpty()) {
                ToastUtils.showLong("请输入调整保证金")
                return false
            }
        }

        if (amount_checkbox.isChecked) {
            if (amount_edittext.text.toString().isNullOrEmpty()) {
                ToastUtils.showLong("请输入调整金额")
                return false
            }
        }

        return true
    }
}