@file:Suppress("DEPRECATION")

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

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.ViewEnumUtils
import cn.muchinfo.rma.global.data.FinancialManageData
import cn.muchinfo.rma.view.MyApplication
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.home.contract.emptyView
import com.blankj.utilcode.util.ToastUtils
import mtp.polymer.com.autowidget.dialog.createLoadingDialog
import mtp.polymer.com.autowidget.utils.bindTaskStatus
import org.jetbrains.anko.*

/**
 * 财务 - 款项审核/发票审核
 */
class PaymentReviewActivity : BaseActivity<FinancialViewModel>() {

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

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

    private val type by lazy { //type 为1是款项审核 2是发票审核 3是详情
        intent.getStringExtra("type")
    }

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            val margingPadding = autoSize(47)
            dialog.bindTaskStatus(context,viewModel.loadingDialogStatus)
            background = resources.getDrawable(R.color.segtabment_bg_color)
            topBar {
                commonLeftButton()
                commonTitle {
                    when (type) {
                        "1" -> {
                            text = "款项审核"
                        }
                        "2" -> {
                            text = "发票审核"
                        }
                        "3" -> {
                            text = "详情"
                        }
                    }
                }
            }

            frameLayout {
                scrollView {
                    verticalLayout {
                        verticalLayout {
                            background = resources.getDrawable(R.color.white)
                            textView {
                                text = "合同基本信息"
                                textColorInt = R.color.rma_blue_color
                                textSizeAuto = 34
                            }.lparams(wrapContent, wrapContent) {
                                marginStart = margingPadding
                                topMargin = autoSize(25)
                            }

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                linearLayout {
                                    textView {
                                        text = "合同编号"
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

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

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

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

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

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

                                    }.lparams(matchParent, wrapContent)

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

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

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

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

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                linearLayout {
                                    textView {
                                        if (type == "1") {
                                            text = "款项类型"
                                        } else if (type == "2") {
                                            text = "类型"
                                        }

                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            if (type == "1") {
                                                if (data.kxtype == "1") {
                                                    text = "付款申请"
                                                } else if (data.kxtype == "2") {
                                                    text = "退款申请"
                                                } else if (data.kxtype == "3") {
                                                    text = "付款/退款 申请"
                                                }
                                            } else if (type == "2") {
                                                if (data.contracttype == "1"){
                                                    text = "收票"
                                                }else if (data.contracttype == "2"){
                                                    text = "开票"
                                                }
                                            }
                                            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.applytime
                                            textSizeAuto = 24
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent){
                                            marginEnd = autoSize(47)
                                        }
                                        textView {
                                            visibility = View.GONE
                                            text = "00:00:00"
                                            textSizeAuto = 17
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)
                                    }.lparams(matchParent, wrapContent)

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

                            linearLayout {
                                gravity = Gravity.CENTER_VERTICAL
                                linearLayout {
                                    textView {
                                        if (type == "1"){
                                            text = "登记金额"
                                        }else if (type == "2"){
                                            text = "发票金额"
                                        }
                                        textColorInt = R.color.p_normal_summary_txt_color
                                        textSizeAuto = 29
                                    }.lparams(wrapContent, wrapContent) {
                                        marginStart = margingPadding
                                    }

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            if (type == "1"){
                                                if (data.payamount.isNullOrEmpty()){
                                                    text = data.deductamount
                                                }else{
                                                    text = data.payamount
                                                }
                                            }else if (type == "2"){
                                                text = data.invoiceAmount
                                            }
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)
                                        textView {
                                            text = "元"
                                            textSizeAuto = 17
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent)
                                    }.lparams(matchParent, wrapContent)

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

                                    linearLayout {
                                        emptyView()
                                        textView {
                                            text = data.applyname
                                            textSizeAuto = 29
                                            textColorInt = R.color.rma_black_33
                                        }.lparams(wrapContent, wrapContent) {
                                            marginEnd = autoSize(47)
                                        }

                                    }.lparams(matchParent, wrapContent)

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

                            linearLayout {
                                if (type == "1"){
                                    visibility = View.GONE
                                }else if (type == "2"){

                                }
                                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 {
                                            onThrottleFirstClick {
                                                ToastUtils.showLong("没附件，看个啥子？")
//                                                val path = data.value.guard {
//                                                    return@onThrottleFirstClick
//                                                }
//                                                PhotoUtils().showImageViewer(this@InvoiceRegistrationActivity, 1, arrayListOf(path))
                                            }
                                            text = "查看附件"
                                            textSizeAuto = 29
                                            textColorStr = "#FE9A68"
                                        }.lparams(wrapContent, wrapContent)

                                    }.lparams(matchParent, wrapContent)

                                }.lparams(autoSize(450), wrapContent)

                            }.lparams(matchParent, autoSize(60))

                            view { }.lparams(matchParent, autoSize(50))
                        }

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

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

                            }

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

                            }
                        }.lparams(matchParent, autoSize(227)) {
                            bottomMargin = autoSize(200)
                            topMargin = autoSize(20)
                        }
                    }
                }

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

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

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

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

        }
    }

}