package com.example.beijing_daxing;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.beijing_daxing.base.BaseActivity;
import com.example.beijing_daxing.base.MyRecultCall;
import com.example.beijing_daxing.base.MyResult;
import com.example.beijing_daxing.been.LedgerRfid;
import com.example.beijing_daxing.databinding.ActivityBfactivityBinding;
import com.example.beijing_daxing.uitls.SharedPreferencesUtils;
import com.example.beijing_daxing.vm.BFWm;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.uhf.api.cls.Reader;

import java.util.List;

import cn.pda.serialport.Tools;

public class BFActivity extends BaseActivity {
    private BFWm wm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityBfactivityBinding dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_bfactivity);
        wm=new BFWm();
        dataBinding.setVm(wm);
        setPower(17);
    }

    @Override
    protected void sanRfid(List<Reader.TAGINFO> epcs) {
        var epc = epcs.get(0);
        byte[] epcId = epc.EpcId;
        String epc1 = Tools.Bytes2HexString(epcId, epcId.length);
        wm.setEpc(epc1);
        OkGo.<MyResult>post(url+"/bf/select")
                .tag(this)
                .params("epc",epc1)
                .execute(new MyRecultCall(dialog,this){
                    @Override
                    public void onSuccess(Response<MyResult> response) {
                        super.onSuccess(response);
                        var body = response.body();
                        if (body.getCode()==0){
                            LedgerRfid ledgerRfid=gson.fromJson(body.getData().toString(),LedgerRfid.class);
                            wm.setPcode(ledgerRfid.getBatchCode());
                            wm.setState(ledgerRfid.getDictLabel());
                        }else {
                            wm.clearText();
                            Toast.makeText(BFActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    }
                });
    }

    public void bf_submit(View view){
        if (wm.getState().equals("正常")){
            OkGo.<MyResult>post(url+"/bf/submit")
                    .params("epc",wm.getEpc())
                    .params("remark",wm.getRemark())
                    .params("user", SharedPreferencesUtils.getstring("user","test-user"))
                    .execute(new MyRecultCall(dialog,this){
                        @Override
                        public void onSuccess(Response<MyResult> response) {
                            super.onSuccess(response);
                            var body = response.body();
                            if (body.getCode()==0){
                                Toast.makeText(BFActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                finish();
                            }else {
                                Toast.makeText(BFActivity.this,body.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

        }else {
            Toast.makeText(this, "轮挡已报废，无需操作", Toast.LENGTH_SHORT).show();
        }
    }
}