package com.lukouguoji.mit.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.lukouguoji.mit.R
import com.lukouguoji.mit.viewModel.MitInPackInfoViewModel
import com.lukouguoji.module_base.BaseActivity
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.router.ARouterConstants
import com.lukouguoji.module_base.util.Common


@Route(path = ARouterConstants.ACTIVITY_URL_MONITOR_IN_PACK_INFO)
class MitInPackInfoActivity : BaseActivity(), View.OnClickListener {
    private lateinit var viewModel: MitInPackInfoViewModel

    private lateinit var carNo: EditText
    private lateinit var carNoScan: ImageView

    //第一列
    private lateinit var fdateEdit: EditText
    private lateinit var fdepEdit: EditText


    //第二列
    private lateinit var fnoEdit: EditText
    private lateinit var fdestEdit: EditText


    //第三列
    private lateinit var countryTypeEdit: EditText

    private lateinit var submit: TextView

    @JvmField
    @Autowired
    var fid: Int = -9999

    @JvmField
    @Autowired
    var fdate: String? = null

    @JvmField
    @Autowired
    var fno: String? = null

    @JvmField
    @Autowired
    var countryName: String? = null

    @JvmField
    @Autowired
    var fdep: String? = null

    @JvmField
    @Autowired
    var fdest: String? = null

    private fun initView() {
        carNo = findViewById(R.id.carNo)
        carNoScan = findViewById(R.id.carNoScan)

        fdateEdit = findViewById(R.id.fdate)
        fdepEdit = findViewById(R.id.fdep)

        fnoEdit = findViewById(R.id.fno)
        fdestEdit = findViewById(R.id.fdest)

        countryTypeEdit = findViewById(R.id.countryType)

        submit = findViewById(R.id.submit)

        carNoScan.setOnClickListener(this)
        submit.setOnClickListener(this)


        //渲染数据
        fdateEdit.setText(fdate)
        fdepEdit.setText(fdep)
        fnoEdit.setText(fno)
        fdestEdit.setText(fdest)
        countryTypeEdit.setText(countryName)

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mit_in_pack_info)
        setBackArrow("进港卸机确认")
        initView()
        viewModel = ViewModelProvider(this).get(MitInPackInfoViewModel::class.java)

        //监听完成装板
        viewModel.completedObserver.observe(this) {
            loadingCancel()
            val status = it.getString("status")
            val data = it.getString("data")
            if (Constant.Result.succ == status) {
                if (data == "1") {
                    carNo.setText("")
                    Common.alertDialog(this, "完成装板！") { dialog ->
                        dialog.dismiss()
                    }
                } else if (data == "2") {
                    Common.alertDialog(this, "板车不存在！") { dialog ->
                        dialog.dismiss()
                    }

                } else if (data == "3") {
                    Common.alertDialog(this, "此板车已装板！") { dialog ->
                        dialog.dismiss()
                    }

                } else {
                    Common.alertDialog(this, "装板失败！") { dialog ->
                        dialog.dismiss()
                    }
                }
            } else {
//                Common.showToastLong(this, "收运失败！ ")
                Common.alertDialog(this, "装板失败！") { dialog ->
                    dialog.dismiss()
                }
            }
        }

    }

    override fun onClick(v: View?) {
        v?.let {
            when (it.id) {
                R.id.carNoScan -> {
                    scanCode(Constant.RequestCode.mit_in_pack_info_car_scan)
                }
                R.id.submit -> {
                    if (getAllEditData()) {
                        Common.secondConfirmDialog(this, "是否确认？") { dialog ->
                            viewModel.completed(fid, carNo.text.toString())
                            dialog.dismiss()
                            loading()
                        }
                    }
                }
                else -> {
                    Toast.makeText(this, "未找到对应操作", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    /**
     * 扫码返回的结果
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        // 扫描二维码/条码回传
        if (resultCode == RESULT_OK && data != null) {
            val content = data.getStringExtra(com.yzq.zxinglibrary.common.Constant.CODED_CONTENT)
            if (content == null) {
                Common.showToast(this, "条码错误！")
                return
            }
            when (requestCode) {
                Constant.RequestCode.mit_in_pack_info_car_scan -> {
                    carNo.setText("$content")
                }
            }
        }
    }

    /**
     * 同步viewModel的实体类数据
     */
    private fun getAllEditData(): Boolean {
        if (carNo.text.toString() == "") {
            Common.showToast(this, "请填写平板车编号！")
            return false
        }
        return true
    }
}