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 android.widget.TextView
import androidx.lifecycle.MutableLiveData
import cn.muchinfo.rma.R
import cn.muchinfo.rma.business.contract.SpotContractInfo
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.MTPEnums
import cn.muchinfo.rma.global.ViewEnumUtils
import cn.muchinfo.rma.global.data.ContractData
import cn.muchinfo.rma.global.data.WrStandardDetail
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.netManage.base.InteractiveException
import cn.muchinfo.rma.netManage.utils.UpLoadUtils
import cn.muchinfo.rma.view.autoWidget.*
import cn.muchinfo.rma.view.autoWidget.calendar.CalendarDialog
import cn.muchinfo.rma.view.autoWidget.photopicter.PhotoUtils
import cn.muchinfo.rma.view.base.BaseActivity
import cn.muchinfo.rma.view.base.app.Constant
import com.alibaba.fastjson.JSON
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.zhy.http.okhttp.callback.StringCallback
import mtp.polymer.com.autowidget.dialog.SelectData
import mtp.polymer.com.autowidget.dialog.creatBottomSheetDialog
import mtp.polymer.com.autowidget.dialog.createLoadingDialog
import mtp.polymer.com.autowidget.utils.TaskUiModel
import mtp.polymer.com.autowidget.utils.bindTaskStatus
import okhttp3.Call
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk25.coroutines.textChangedListener
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.io.File

/**
 * 新增采购合同
 */
class AddContractActivity : BaseActivity<ContractViewModel>() {

    private var uploadFile: String = ""//上传成功后服务给的图片地址
    private val imageString: MutableLiveData<String> =
        MutableLiveData<String>().apply { value = "" }//文件地址

    private val operationtype by lazy {
        intent.getStringExtra("operationtype")
    }

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

    /**
     * 合同编号
     */
    lateinit var contractNo: EditText

    /**
     * 标仓系数输入值
     */
    lateinit var coefficientWarehouse: EditText
    /** 商品规格 ***/
    lateinit var productSpecifications : EditText
    /** 数量 **/
    private lateinit var number_Edittext : EditText
    /** 输入价格 **/
    private lateinit var price_edittext : EditText
    /** 输入升贴水 **/
    private lateinit var liter_discount_edittext : EditText
    /** 一口价时的金额输入 **/
    private lateinit var price_edittext_one : TextView
    /** 输入暂定价 **/
    lateinit var temperary_edittext : EditText
    /** 保证金 **/
    lateinit var margin_edittext : EditText
    /** 备注 **/
    lateinit var remask_edittext : EditText
    /**
     * 本页面选择的合同类型
     */
    var contractType: MutableLiveData<SelectData> = MutableLiveData()

    /** 选择的客户 */
    val selectCustomer : MutableLiveData<SelectData> = MutableLiveData()

    /** 选择的现货商品类型 */
    val selectWrStandardData : MutableLiveData<SelectData> = MutableLiveData()

    /** 选择的型号类型 **/
    val selectWrStandModel : MutableLiveData<SelectData> = MutableLiveData()

    /** 选择的品牌 ***/
    val selectWrStandBrand : MutableLiveData<SelectData> = MutableLiveData()

    /**
     * 产品类型
     */
    val productType by lazy {
        arrayListOf(SelectData(MTPEnums.STANDARD_OF_THE_WAREHOUSE.toString(),"标准仓单"),
            SelectData(MTPEnums.SUCH_AS_THS.toString(),"等标"),
            SelectData(MTPEnums.NON_STANDARD.toString(),"非标")
        )
    }
    /** 选择的产品类型 **/
    val selectProductType : MutableLiveData<SelectData> = MutableLiveData()

    /**
     * 点价类型列表
     */
    val priceType by lazy {
         arrayListOf(SelectData(MTPEnums.PRICE.toString(),"一口价"),
            SelectData(MTPEnums.SOME_PRICE.toString(),"点价"),
            SelectData(MTPEnums.TEMPORARY_PRICE.toString(),"暂定价")
        )
    }

    /** 点价期 **/
    val midpointsPeriodTime : MutableLiveData<String> = MutableLiveData()

    /** 交收期 */
    val deliveryPeriodTime : MutableLiveData<String> = MutableLiveData()

    fun initData(){
        if (operationtype == "1"){//新增合同市需要初始化的数据
            contractType.postValue(SelectData(
                id = MTPEnums.CONTRACT_TYPE_BUY.toString(),
                value = "采购合同"
            ))//初始化一个合同类型
            viewModel.pricingType.postValue(SelectData(id = "1",value = "一口价"))//初始化一个价格类型
        }else{//修改或者重新提交时需要初始化的数据
            contractType.postValue(SelectData(id = data.contracttype.toString(),value = if (data.contracttype == "1"){
                "采购合同"
            }else{
                "销售合同"
            }))
            viewModel.pricingType.postValue(SelectData(id = data.pricetype.toString(),value = ViewEnumUtils.getPriceType(data.pricetype ?: "")))
            selectWrStandardData.postValue(SelectData(id = data.deliverygoodscode.toString(),value = data.deliverygoodsname.toString(),deliverygoodsid = data.deliverygoodsid.toString()))
            viewModel.queryWrStandardDetail(data.deliverygoodscode ?: "")
            selectProductType.postValue(SelectData(id = data.producttype.toString(),value = ViewEnumUtils.getProductType(data.producttype.toString())))

//            selectWrStandModel.postValue(SelectData(id = data.spotgoodsdesc.toString()))
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            createLoadingDialog(hintStr = "").bindTaskStatus(context,viewModel.loadingDialogStatus)
            background = resources.getDrawable(R.color.segtabment_bg_color)
            viewModel.queryUserInfo()
            viewModel.queryWrStandardList()
            initData()
            topBar {
                commonLeftButton()
                commonTitle {
                    text = "新增采购合同"
                }
            }

            frameLayout {
                scrollView {
                    verticalLayout {

                        baseInformationView("基本信息")

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL

                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }
                            textView {
                                text = "合同编号"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            editText {
                                contractNo = this
                                hint = "请输入合同编号"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_TEXT
//                    setDecimalInputType()
                                hintColorStr = "#CCCCCC"
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(matchParent, autoSize(132)) {
                                marginStart = autoSize(80)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL
                            onThrottleFirstClick {
                                val selectDataList = arrayListOf(
                                    SelectData(
                                        id = MTPEnums.CONTRACT_TYPE_BUY.toString(),
                                        value = "采购合同"
                                    ),
                                    SelectData(
                                        id = MTPEnums.CONTRACT_TYPE_SELL.toString(),
                                        value = "销售合同"
                                    )
                                )
                                creatBottomSheetDialog("请选择客户类型", selectDataList) {
                                    contractType.postValue(this)
                                }
                            }
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "合同类型"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            textView {
                                contractType.bindOptional(context) {
                                    text = it?.value
                                    textColorInt = R.color.rma_black_33
                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            imageView {
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL
                            onThrottleFirstClick {
                                if (contractType.value?.id == "-1") {
                                    val selectDataList = viewModel.setSelectCustomerList(viewModel.normalUserInfoList.value ?: arrayListOf())
                                    creatBottomSheetDialog("请选择客户", selectDataList) {
                                        selectCustomer.postValue(this)
                                    }
                                }
                            }
                            textView {
                                contractType.bindOptional(context) {
                                    visibility = if (it?.id == "1") {
                                        View.INVISIBLE
                                    } else {
                                        View.VISIBLE
                                    }
                                }
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                contractType.bindOptional(context) {
                                    text = "采  购  方"
                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            textView {
                                // FIXME 后期查询用户资料后需要填写用户名
                                contractType.bindOptional(context) {
                                    text = if (it?.id == "1") {
                                        GlobalDataCollection.instance?.userAccountData?.accountname
                                    } else {
                                        "请选择客户"
                                    }
                                }

                                selectCustomer.bindOptional(context){
                                    if (contractType.value?.id == "-1"){
                                        text = it?.value
                                    }

                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            imageView {
                                contractType.bindOptional(context) {
                                    visibility = if (it?.id == "1") {
                                        View.INVISIBLE
                                    } else {
                                        View.VISIBLE
                                    }
                                }
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            onThrottleFirstClick {
                                if (contractType.value?.id == "1") {
                                    val selectDataList = viewModel.setSelectCustomerList(viewModel.normalUserInfoList.value ?: arrayListOf())
                                    creatBottomSheetDialog("请选择客户", selectDataList) {
                                        selectCustomer.postValue(this)
                                    }
                                }
                            }
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                contractType.bindOptional(context) {
                                    visibility = if (it?.id == "-1") {
                                        View.INVISIBLE
                                    } else {
                                        View.VISIBLE
                                    }
                                }
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "销  售  方"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            textView {
                                // FIXME 后期查询用户资料后需要填写用户名
                                contractType.bindOptional(context) {
                                    text = if (it?.id == "-1") {
                                        GlobalDataCollection.instance?.userAccountData?.accountname
                                    } else {
                                        "请选择客户"
                                    }
                                }
                                selectCustomer.bindOptional(context){
                                    if (contractType.value?.id == "1"){
                                        text = it?.value
                                        textColorInt = R.color.rma_black_33
                                    }
                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            imageView {
                                contractType.bindOptional(context) {
                                    visibility = if (it?.id == "-1") {
                                        View.INVISIBLE
                                    } else {
                                        View.VISIBLE
                                    }
                                }
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL

                            textView {
                                textResource = R.string.business_license
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(59)
                            }

                            textView {
                                imageString.bindOptional(context) {
                                    visibility = if (it.isNullOrEmpty()) {
                                        View.VISIBLE
                                    } else {
                                        View.GONE
                                    }
                                }
                                visibility = View.VISIBLE
                                onThrottleFirstClick {
                                    PhotoUtils().showPicker(this@AddContractActivity) {
                                        it.firstOrNull()?.let {
                                            viewModel.loadingDialogStatus.value =
                                                TaskUiModel.inFlight()
                                            UpLoadUtils(this@AddContractActivity).uploadPicture(
                                                File(it),
                                                object : StringCallback() {
                                                    override fun onError(
                                                        call: Call,
                                                        e: Exception,
                                                        id: Int
                                                    ) {
                                                        LogUtils.e("hidahdashdsdasd", e.message)
                                                        viewModel.loadingDialogStatus.value =
                                                            TaskUiModel.failed(
                                                                InteractiveException(
                                                                    errorMessage = e.message ?: ""
                                                                )
                                                            )
                                                    }

                                                    override fun onResponse(
                                                        response: String,
                                                        id: Int
                                                    ) {
                                                        /*[{"originalName":"tem.jpg","fileName":"201907161657044712.jpg","fileSize":"0KB","fileType":"image/jpeg","filePath":"./uploadFile/20190716/201907161657044712.jpg","submitName":null}]*/
                                                        try {
                                                            viewModel.loadingDialogStatus.value =
                                                                TaskUiModel.success()
                                                            val jsonArray = JSONArray(response)
                                                            LogUtils.eTag(
                                                                "图片",
                                                                JSON.toJSONString(jsonArray)
                                                            )
                                                            for (i in 0 until jsonArray.length()) {
                                                                uploadFile =
                                                                    (jsonArray.get(i) as JSONObject).getString(
                                                                        "filePath"
                                                                    )
                                                                imageString.value = it
                                                                LogUtils.e("hidahdashd", uploadFile)
                                                            }
                                                        } catch (e: JSONException) {
                                                            e.printStackTrace()
                                                        }

                                                    }
                                                },
                                                10000.0,
                                                SPUtils.getInstance().getString(Constant.uploadUrl)
                                            )

                                        }
                                    }
                                }
                                gravity = Gravity.CENTER
                                textResource = R.string.update
                                textColorInt = R.color.rma_item_click_color
                                backgroundResource = R.drawable.rma_item_text_bg
                                textSizeAuto = 31
                            }.lparams(autoSize(134), autoSize(53)) {
                                marginStart = autoSize(80)
                            }

                            textView {
                                onThrottleFirstClick {
                                    val path = imageString.value.guard {
                                        return@onThrottleFirstClick
                                    }
                                    PhotoUtils().showImageViewer(this@AddContractActivity, 1, arrayListOf(path))
                                }
                                textResource = R.string.check_the_attachment
                                textColorInt = R.color.rma_yellow_text_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(52)
                            }
                        }.lparams(matchParent, autoSize(132))

                        baseInformationView("现货信息")

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL
                            onThrottleFirstClick {
                                creatBottomSheetDialog("请选择现货品种", viewModel.selectSpotVarietiesList(viewModel.wrStandardDataList.value ?: arrayListOf())) {
                                    selectWrStandardData.postValue(this)
                                    viewModel.queryWrStandardDetail(this.id)
                                }
                            }
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "现货品种"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            textView {
                                selectWrStandardData.bindOptional(context){
                                    text = it?.value
                                    textColorInt = R.color.rma_black_33
                                }
                                text = "请选择现货品种"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            imageView {
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL
                            onThrottleFirstClick {
                                creatBottomSheetDialog("请选择现货品种", productType) {
                                    selectProductType.postValue(this)
                                }
                            }
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "产品类型"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            textView {
                                selectProductType.bindOptional(context){
                                    text = it?.value
                                    textColorInt = R.color.rma_black_33
                                }
                                text = "请选择产品类型"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            imageView {
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "标仓系数"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            editText {

                                coefficientWarehouse = this
                                if (data.convertfactor?.isNotEmpty() == true){
                                    coefficientWarehouse.setText(data.convertfactor.toString())
                                }
                                hint = "请输入标仓系数"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_NUMBER
                                setDecimalInputType()
                                hintColorStr = "#CCCCCC"
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(matchParent, autoSize(132)) {
                                marginStart = autoSize(80)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL
                            onThrottleFirstClick {
                                if (viewModel.wrStandardDataList.value.isNullOrEmpty().not()) {
                                    creatBottomSheetDialog(
                                        "请选择型号",
                                        viewModel.selectWrStandardModel(
                                            viewModel.wrStandardDataDetail.value
                                                ?: WrStandardDetail()
                                        )
                                    ) {
                                        selectWrStandModel.postValue(this)
                                    }
                                }
                            }
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "型        号"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            textView {
                                selectWrStandModel.bindOptional(context){
                                    text = it?.value
                                    textColorInt = R.color.rma_black_33
                                }
                                text = "请选择型号"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            imageView {
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL
                            onThrottleFirstClick {
                                if (viewModel.wrStandardDataList.value.isNullOrEmpty().not()) {
                                    creatBottomSheetDialog(
                                        "请选择型号",
                                        viewModel.selectWrStandardBrand(
                                            viewModel.wrStandardDataDetail.value
                                                ?: WrStandardDetail()
                                        )
                                    ) {
                                        selectWrStandBrand.postValue(this)
                                    }
                                }
                            }
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "品        牌"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            textView {
                                selectWrStandBrand.bindOptional(context){
                                    text = it?.value
                                    textColorInt = R.color.rma_black_33
                                }
                                text = "请选择品牌"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            imageView {
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "商品规格"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            editText {
                                productSpecifications = this
                                hint = "请输入商品规格"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_TEXT
                                hintColorStr = "#CCCCCC"
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(matchParent, autoSize(132)) {
                                marginStart = autoSize(80)
                            }
                        }.lparams(matchParent, autoSize(132))

                        baseInformationView("价格信息")

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL
                            onThrottleFirstClick {
                                creatBottomSheetDialog("请选择定价类型", priceType) {
                                    viewModel.pricingType.postValue(this)
                                }
                            }
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "定价类型"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            textView {
                                viewModel.pricingType.bindOptional(context) {
                                    text = it?.value
                                    textColorInt = R.color.rma_black_33
                                }
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            imageView {
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL

                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

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

                            editText {
                                textChangedListener {
                                    afterTextChanged {
                                        if (it.toString().isNotEmpty()){
                                            if (price_edittext.text.toString().isNotEmpty()){
                                                price_edittext_one.text = price_edittext.text.toString().toDouble().times(number_Edittext.text.toString().toDouble()).toString()
                                            }
                                        }
                                    }
                                }
                                if (operationtype == "2"){
                                    setText(data.qty)
                                }
                                number_Edittext = this
                                hint = "请输入数量"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_NUMBER
                                setDecimalInputType()
                                hintColorStr = "#CCCCCC"
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(0, autoSize(132), 1f) {
                                marginStart = autoSize(80)
                            }

                            textView {
                                selectWrStandardData.bindOptional(context){
                                    text = it?.enumdicname
                                }
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginEnd = autoSize(38)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            viewModel.pricingType.bindOptional(context) {
                                if (it?.id == "1") {
                                    visibility = View.VISIBLE
                                } else {
                                    visibility = View.GONE
                                }
                            }
                            gravity = Gravity.CENTER_VERTICAL

                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }
                            textView {
                                text = "价        格"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            editText {
                                textChangedListener {
                                    afterTextChanged {
                                        if (it.toString().isNotEmpty()){
                                            if (number_Edittext.text.toString().isNotEmpty()){
                                                price_edittext_one.text = price_edittext.text.toString().toDouble().times(number_Edittext.text.toString().toDouble()).toString()
                                            }
                                        }
                                    }
                                }
                                price_edittext = this
                                if (operationtype == "2"){
                                    data.price.let {
                                        setText(it)
                                    }
                                }
                                hint = "请输入价格"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_NUMBER
                                hintColorStr = "#CCCCCC"
                                setDecimalInputType()
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(0, autoSize(132), 1f) {
                                marginStart = autoSize(80)
                            }

                            textView {
                                text = "元"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginEnd = autoSize(38)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            viewModel.pricingType.bindOptional(context) {
                                if (it?.id == "1") {
                                    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)
                            }

                            textView {
                                price_edittext_one = this
                                textColorInt = R.color.rma_black_33
                                textSizeAuto = 31
                                text = "0"
                            }.lparams(wrapContent, wrapContent){
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            textView {
                                text = "元"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginEnd = autoSize(38)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            viewModel.pricingType.bindOptional(context) {
                                if (it?.id == "3") {
                                    visibility = View.VISIBLE
                                } else {
                                    visibility = View.GONE
                                }
                            }
                            gravity = Gravity.CENTER_VERTICAL

                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }
                            textView {
                                text = "暂  定  价"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            editText {
                                temperary_edittext = this
                                hint = "请输入暂定价"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_TEXT
                                hintColorStr = "#CCCCCC"
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(0, autoSize(132), 1f) {
                                marginStart = autoSize(80)
                            }

                            textView {
                                text = "元"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginEnd = autoSize(38)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            viewModel.pricingType.bindOptional(context) {
                                if (it?.id == "1") {
                                    visibility = View.GONE
                                } else {
                                    visibility = View.VISIBLE
                                }
                            }
                            gravity = Gravity.CENTER_VERTICAL

                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }

                            textView {
                                text = "点价合约"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            textView {
                                text = "请选择点价合约"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            imageView {
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            viewModel.pricingType.bindOptional(context) {
                                if (it?.id == "1") {
                                    visibility = View.GONE
                                } else {
                                    visibility = View.VISIBLE
                                }
                            }
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "*"
                                textColorInt = R.color.rma_star_color
                                textSizeAuto = 31
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(37)
                            }
                            textView {
                                text = "升  贴  水"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(10)
                            }

                            editText {
                                liter_discount_edittext = this
                                hint = "请输入升贴水"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_TEXT
                                hintColorStr = "#CCCCCC"
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(0, autoSize(132), 1f) {
                                marginStart = autoSize(80)
                            }

                            textView {
                                text = "元"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginEnd = autoSize(38)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL

                            onThrottleFirstClick {
                                val timeDialog = CalendarDialog(context)
                                //以Dialog弹窗形式显示
                                timeDialog.window?.setGravity(Gravity.BOTTOM)
                                timeDialog.window?.setWindowAnimations(R.style.mystyle)
                                timeDialog.show()
                                timeDialog.setOnDialogCalendarListener { startDate, endDate ->
                                    midpointsPeriodTime.value = "$startDate~$endDate"
                                }
                            }
                            viewModel.pricingType.bindOptional(context) {
                                if (it?.id == "1") {
                                    visibility = View.GONE
                                } else {
                                    visibility = View.VISIBLE
                                }
                            }

                            textView {
                                text = "点  价  期"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(59)
                            }

                            textView {
                                midpointsPeriodTime.bindOptional(context){
                                    text = it
                                }
                                text = "请选择点价期"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            imageView {
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL
                            onThrottleFirstClick {
                                val timeDialog = CalendarDialog(context)
                                //以Dialog弹窗形式显示
                                timeDialog.window?.setGravity(Gravity.BOTTOM)
                                timeDialog.window?.setWindowAnimations(R.style.mystyle)
                                timeDialog.show()
                                timeDialog.setOnDialogCalendarListener { startDate, endDate ->
                                    deliveryPeriodTime.value = "$startDate~$endDate"
                                }
                            }
                            textView {
                                text = "交  收  期"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(59)
                            }

                            textView {
                                deliveryPeriodTime.bindOptional(context){
                                    text = it
                                }
                                text = "请选择交收期"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_hint_text_color_ccc
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(80)
                            }

                            emptyView()

                            imageView {
                                imageResource = R.mipmap.rma_more
                            }.lparams(autoSize(36), autoSize(36)) {
                                marginEnd = autoSize(25)
                            }
                        }.lparams(matchParent, autoSize(132))

                        baseInformationView("其他信息")

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "保  证  金"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(59)
                            }

                            editText {
                                margin_edittext = this
                                hint = "请输入保证金金额"
                                background = null
                                inputType = EditorInfo.TYPE_CLASS_NUMBER
                                hintColorStr = "#CCCCCC"
                                textSizeAuto = 31
                                textColorStr = "#333333"
                            }.lparams(matchParent, autoSize(132)) {
                                marginStart = autoSize(80)
                            }

                            textView {
                                text = "元"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginEnd = autoSize(38)
                            }
                        }.lparams(matchParent, autoSize(132))

                        viewItemUI()

                        linearLayout {
                            background = resources.getDrawable(R.color.white)
                            gravity = Gravity.CENTER_VERTICAL
                            textView {
                                text = "备        注"
                                textSizeAuto = 31
                                textColorInt = R.color.rma_black_33
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = autoSize(59)
                            }

                            editText {
                                remask_edittext = this
                                if (operationtype == "2"){
                                    setText(data.remark)
                                }
                                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 {
                    background = resources.getDrawable(R.color.white)
                    gravity = Gravity.CENTER_VERTICAL
                    textView {
                        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 {
                            if (checkUpdate().not()){
                                return@onThrottleFirstClick
                            }
                            viewModel.orderContract(contractInfo = getContractInfo(),OperateType = "2",Remark = remask_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)
                    }
                }.lparams(matchParent, autoSize(144)) {
                    gravity = Gravity.BOTTOM
                }
            }
        }
    }

    private fun getContractInfo() : SpotContractInfo{
        val contractInfo = SpotContractInfo()
        val userid = GlobalDataCollection.instance?.loginRsp?.userID ?: 0
        if (contractNo.text.toString().isNotEmpty()){
            contractInfo.ContractNo = contractNo.text.toString()
        }
        contractInfo.ContractType = contractType.value?.id?.toInt()!!
        contractInfo.UserID = userid
        if (contractType.value?.id == "1"){//采购合同
            contractInfo.BuyUserID = userid
            contractInfo.SellUserID = selectCustomer.value?.id?.toInt()!!
        }else{
            contractInfo.BuyUserID = selectCustomer.value?.id?.toInt()!!
            contractInfo.SellUserID = userid
        }
        contractInfo.SignDate = TimeUtils.getNowString()
        if (uploadFile.isNotEmpty()) {
            contractInfo.ContractAttachment = uploadFile
        }
        if (margin_edittext.text.toString().isNotEmpty()){
            contractInfo.ContractMargin = margin_edittext.text.toString().toDouble()
        }
        contractInfo.DeliveryGoodsID = selectWrStandardData.value?.deliverygoodsid?.toInt()!!
        contractInfo.WrStandardID = selectWrStandardData.value?.id?.toInt()!!
        contractInfo.SpotGoodsBrandID = selectWrStandBrand.value?.id?.toInt()!!
        contractInfo.ProductType = selectProductType.value?.id?.toInt()!!
        contractInfo.ConvertFactor = coefficientWarehouse.text.toString().toDouble()
        contractInfo.SpotGoodsModelID = selectWrStandModel.value?.id?.toInt()!!
        contractInfo.PriceType = viewModel.pricingType.value?.id?.toInt()!!
        contractInfo.Qty = number_Edittext.text.toString().toDouble()


        if (viewModel.pricingType.value?.id == "1"){//一口价时price为价格
            contractInfo.Price = price_edittext.text.toString().toDouble()
            contractInfo.Amount = price_edittext.text.toString().toDouble().times(number_Edittext.text.toString().toDouble())
        }else if (viewModel.pricingType.value?.id == "3"){//暂定价时price为暂定价
            contractInfo.Price = temperary_edittext.text.toString().toDouble()
            contractInfo.GoodsID = 1025//暂时写死一个//点价合约在点价和暂定价是需要传值
        }else{
            contractInfo.GoodsID = 1025//暂时写死一个
        }
        if (midpointsPeriodTime.value?.isNotEmpty() == true){//有点价时间
            contractInfo.StartDate = midpointsPeriodTime.value!!.split("~").get(0)
            contractInfo.EndDate = midpointsPeriodTime.value!!.split("~").get(1)
        }

        if (deliveryPeriodTime.value?.isNotEmpty() == true){//有交收时间
            contractInfo.DeliveryStartDate = deliveryPeriodTime.value!!.split("~").get(0)
            contractInfo.DeliveryEndDate = deliveryPeriodTime.value!!.split("~").get(1)
        }

        if (viewModel.pricingType.value?.id != "1"){
            contractInfo.PriceMove = liter_discount_edittext.text.toString().toDouble()
        }
        //商品规格
        contractInfo.SpotGoodsDesc = productSpecifications.text.toString()
        return contractInfo
    }

    private fun checkUpdate() : Boolean{
        if (contractNo.text.toString().isNullOrEmpty()){
            ToastUtils.showLong("请输入合同编号")
            return false
        }

        if (selectCustomer.value?.id.isNullOrEmpty()){
            ToastUtils.showLong("请选择客户")
            return false
        }
        if (selectWrStandardData.value?.id.isNullOrEmpty()){
            ToastUtils.showLong("请选择现货品种")
            return false
        }

        if (selectProductType.value?.id.isNullOrEmpty()){
            ToastUtils.showLong("请选择产品类型")
            return false
        }
        if (coefficientWarehouse.text.toString().isNullOrEmpty()){
            ToastUtils.showLong("请输入标仓系数")
            return false
        }
        if (selectWrStandModel.value?.id.isNullOrEmpty()){
            ToastUtils.showLong("请选择型号")
            return false
        }
        if (selectWrStandBrand.value?.id.isNullOrEmpty()){
            ToastUtils.showLong("请选择品牌")
            return false
        }
        if (productSpecifications.text.toString().isNullOrEmpty()){
            ToastUtils.showLong("请输入商品规格")
            return false
        }
        if (number_Edittext.text.toString().isNullOrEmpty()){
            ToastUtils.showLong("请输入数量")
            return false
        }
        if (viewModel.pricingType.value?.id == "1"){//判断一口价时需要输入的参数
            if (price_edittext.text.toString().isNullOrEmpty()){
                ToastUtils.showLong("请输入价格")
                return false
            }
            if (price_edittext_one.text.toString().isNullOrEmpty()){
                ToastUtils.showLong("请输入金额")
                return false
            }
        }else if (viewModel.pricingType.value?.id == "2"){//判断点价时需要输入的参数
            // TODO 添加判断点价合约

            if (liter_discount_edittext.text.toString().isNullOrEmpty()){
                ToastUtils.showLong("升贴水")
                return false
            }
        }else if (viewModel.pricingType.value?.id == "3"){//判断暂定价时需要输入的参数
            // TODO 添加判断点价合约
            if (temperary_edittext.text.toString().isNullOrEmpty()){
                ToastUtils.showLong("请输入暂定价")
                return false
            }
            if (liter_discount_edittext.text.toString().isNullOrEmpty()){
                ToastUtils.showLong("升贴水")
                return false
            }

        }
        return true
    }

}