package cn.muchinfo.rma.view.base.procurement.detailsviewholder

import android.graphics.Typeface
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.ViewEnumUtils
import cn.muchinfo.rma.global.data.BusinessData
import cn.muchinfo.rma.global.data.FinancialManageData
import cn.muchinfo.rma.global.isBlankNumber
import cn.muchinfo.rma.global.isBlankString
import cn.muchinfo.rma.global.isShowTimeString
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.autoSize
import cn.muchinfo.rma.view.autoWidget.textColorInt
import cn.muchinfo.rma.view.autoWidget.textSizeAuto
import mtp.polymer.com.autowidget.adapter.BaseViewHolder
import org.jetbrains.anko.*

/**
 * 合同详情的款项记录和发票记录
 * @property activity AppCompatActivity
 * @property type String
 * @property itemSize IntArray
 * @constructor
 */
class InvoiceRecordViewHolder(private val activity: AppCompatActivity, private val type: String) :
    BaseViewHolder<FinancialManageData>(activity) {
    override val itemSize: IntArray = intArrayOf(matchParent, wrapContent)

    override fun _FrameLayout.createContentView() {
        linearLayout {
            verticalLayout {
                gravity = Gravity.CENTER
                linearLayout {
                    /** 款项登记时间/发票审核时间 **/
                    textView {
                        data.bindOptional(context) {
                            text = it?.applytime?.isShowTimeString("yyyyMM/dd HH:mm")
                        }
                        textColorInt = R.color.rma_black_33
                        textSizeAuto = 29
                        typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                    }.lparams(wrapContent, wrapContent) {
                        marginStart = autoSize(35)
                        topMargin = autoSize(25)
                    }

                }.lparams(matchParent, wrapContent)
                /** 款项审核时间/发票审核时间 **/
                textView {
                    data.bindOptional(context) {
                        text = it?.audittime?.isShowTimeString("yyyyMM/dd HH:mm")
                    }
                    textSizeAuto = 22
                    textColorInt = R.color.hint_text_color
                }.lparams(wrapContent, wrapContent) {
                    marginStart = autoSize(35)
                    topMargin = autoSize(15)
                }

            }.lparams(autoSize(315), autoSize(120))

            verticalLayout {
                gravity = Gravity.CENTER
                /** 款项类型/发票类型 **/
                textView {
                    data.bindOptional(context) {
                        data.bindOptional(context) {
                            if (type == "1") {
                                if (it?.kxtype == "1") {
                                    text = "付款"
                                } else if (it?.kxtype == "2") {
                                    text = "退款"
                                } else if (it?.kxtype == "3") {
                                    text = "付款/退款"
                                }
                            } else {
                                if (it?.contracttype == "1") {
                                    text = "收票"
                                } else if (it?.contracttype == "2") {
                                    text = "开票"
                                }
                            }

                        }

                    }
                    textSizeAuto = 29
                    textColorInt = R.color.rma_black_33
                    maxLines = 1
                    ellipsize = TextUtils.TruncateAt.END
                }.lparams(wrapContent, wrapContent) {
                    topMargin = autoSize(25)
                }
                /** 金额 **/
                textView {
                    data.bindOptional(context) {
                        if (type == "1") {
                            if (it?.kxtype == "1") {
                                text = it.payamount
                            } else if (it?.kxtype == "2") {
                                text = it.deductamount
                            } else if (it?.kxtype == "3") {
                                text = it.payamount + "/" + it.deductamount
                            }
                        } else {
                            text = it?.invoiceAmount
                        }
                    }
                    textSizeAuto = 22
                    textColorInt = R.color.hint_text_color
                }.lparams(wrapContent, wrapContent) {
                    topMargin = autoSize(15)
                }

            }.lparams(autoSize(195), autoSize(120))

            /**  申请人 **/
            verticalLayout {
                gravity = Gravity.CENTER
                textView {
                    data.bindOptional(context) {
                        text = it?.applyname?.isBlankString()
                    }
                    textSizeAuto = 29
                    textColorInt = R.color.rma_black_33
                    maxLines = 1
                    ellipsize = TextUtils.TruncateAt.END
                }.lparams(wrapContent, wrapContent) {
                    topMargin = autoSize(25)
                }
                /** 审核人 **/
                textView {
                    data.bindOptional(context) {
                        text = it?.auditname?.isBlankString()
                    }
                    textSizeAuto = 22
                    textColorInt = R.color.hint_text_color
                }.lparams(wrapContent, wrapContent) {
                    topMargin = autoSize(15)
                }
            }.lparams(autoSize(195), autoSize(120))

            verticalLayout {
                gravity = Gravity.CENTER
                /** 状态 */
                textView {
                    data.bindOptional(context) {
                        text = ViewEnumUtils.getBusinessManagementStatus(it?.applystatus ?: "")
                    }
                    textSizeAuto = 29
                    textColorInt = R.color.rma_black_33
                    maxLines = 1
                    ellipsize = TextUtils.TruncateAt.END
                }.lparams(wrapContent, wrapContent) {
                    topMargin = autoSize(25)
                }

            }.lparams(autoSize(195), autoSize(120))

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

}