package com.grkj.iscs_mc.view.fragment

import android.os.Bundle
import androidx.navigation.NavOptions
import androidx.navigation.fragment.navArgs
import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.databinding.FragmentMaterialInspectionSignatureBinding
import com.grkj.iscs_mc.extentions.navigateTo
import com.grkj.iscs_mc.extentions.navigateUp
import com.grkj.iscs_mc.extentions.serialNo
import com.grkj.iscs_mc.util.ImgUtil
import com.grkj.iscs_mc.util.NetApi
import com.grkj.iscs_mc.view.base.BaseFragment

/**
 * 物资检查签名页
 */
class MaterialInspectionSignatureFragment :
    BaseFragment<FragmentMaterialInspectionSignatureBinding>() {

    private var mPlanId: Long? = null

    override val viewBinding: FragmentMaterialInspectionSignatureBinding
        get() = FragmentMaterialInspectionSignatureBinding.inflate(layoutInflater)

    override fun initView() {
        mBinding?.cbBack?.setOnClickListener {
            navigateUp()
        }

        mBinding?.cbClear?.setOnClickListener {
            mBinding?.sv?.destroyDrawingCache()
            mBinding?.sv?.clear()
        }

        val args: MaterialInspectionSignatureFragmentArgs by navArgs()
        mPlanId = args.planId

        mBinding?.cbConfirm?.setOnClickListener {
            val bitmap = mBinding?.sv?.drawingCache
            // 页面回传图片，暂时取消，改成直接上传后端
//            val bundle = Bundle()
//            bundle.putString("bitmap", ImgUtil.imageToBase64(bitmap!!))
//
//            navigateTo(
//                R.id.action_inspection_signature_fragment_to_inspection_plan_fragment,
//                bundle,
//                NavOptions.Builder()
//                    .setLaunchSingleTop(true)
//                    .setPopUpTo(R.id.material_inspection_plan_fragment, true, false)
//                    .build()
//            )

            mPlanId?.let {
                NetApi.insertSign(it, requireContext().serialNo(), ImgUtil.imageToBase64(bitmap!!)) {
                    if (it) {
                        navigateUp()
                    }
                }
            }

        }
    }
}