package com.example.beijing_daxing;

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

import android.os.Bundle;
import android.util.Log;
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.ActivityFpInBinding;
import com.example.beijing_daxing.uitls.HexAscii;
import com.example.beijing_daxing.uitls.SharedPreferencesUtils;
import com.example.beijing_daxing.vm.FpInVm;
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 FpInActivity extends BaseActivity {
    private FpInVm vm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityFpInBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_fp_in);
        vm = new FpInVm();
        binding.setVm(vm);

    }

    @Override
    protected void onResume() {
        super.onResume();
        setPower(SharedPreferencesUtils.getInt("powerScrapIn",17));
        uhfLongerManager.setGen2session(true);
        uhfLongerManager.asyncStartReading();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        uhfLongerManager.setGen2session(false);
        uhfLongerManager.asyncStopReading();
    }

    @Override
    public void sanRfid(List<Reader.TAGINFO> epcs) {
        Reader.TAGINFO taginfo = epcs.get(0);
        var epc = HexAscii.hex2Str(Tools.Bytes2HexString(taginfo.EpcId,taginfo.EpcId.length));
        vm.setEpc(epc);
        OkGo.<MyResult>post(url+"/fpin/select")
                .tag(this).params("epc",epc)
                .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);
                            vm.setBichCode(ledgerRfid.getBatchCode());
                            vm.setLocation(ledgerRfid.getLocationType());
                            vm.setLocationCode(ledgerRfid.getLocationCode());
                            vm.setState(ledgerRfid.getDictLabel());
                            vm.setTime(ledgerRfid.getCreateTime());
                            vm.setList(ledgerRfid.getBaseLocationList());
                        }
                        Toast.makeText(FpInActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                });
    }

    public void fpin_submit(View view){
        // Log.e("TAG", "onSuccess:" +vm.toString() );
        var state = vm.getState();
        boolean b = state.equals("报废") || state.equals("维修");
        if (!b){
            Toast.makeText(this, "轮挡状态："+state+",不能使用废品入库功能", Toast.LENGTH_SHORT).show();
            return;
        }


        if (vm.getLocation().equals("仓库")){
            Toast.makeText(this, "轮挡状态已在仓库", Toast.LENGTH_SHORT).show();
            return;
        }
        OkGo.<MyResult>post(url+"/fpin/submit").tag(this)
                .params("epc",vm.getEpc())
                .params("localtionCode",vm.getLocationCode())
                .params("user", SharedPreferencesUtils.getstring("user",""))
                .params("bf",vm.getTime())
                .params("subCode",vm.getSubmitCode())
                .execute(new MyRecultCall(dialog,this){
                    @Override
                    public void onSuccess(Response<MyResult> response) {
                        super.onSuccess(response);
                        var body = response.body();
                        if (body.getCode()==0){
                            finish();
                            Toast.makeText(FpInActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        Toast.makeText(FpInActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                });


    }
}