package com.bjzc.zjdxj.activity.sbxdj;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import com.bjzc.zjdxj.DBModel.QYDDATABean;
import com.bjzc.zjdxj.R;
import com.bjzc.zjdxj.base.BaseFragment;


public class CJFragment extends BaseFragment {
    TextView tv_bw;
    TextView tv_dmc;
    EditText et_jg;
    TextView tv_yjzj;
    TextView tv_zt;
    TextView tvSb;
    TextView tvBz;
    ScrollView sv;
    TextView tvZq;
    RadioButton rbzc;
    RadioButton rbbzc;
    RadioGroup RGZC;
    TextView textviewTy;
    TextView tvDjnr;
    EditText etYcFxnr;
    LinearLayout ycmsLl;


    private boolean isEdit;
    private QYDDATABean info;
    private int zj;
    private int index;
    private String writebleinfo;
    public static CJFragment newInstance(boolean b, QYDDATABean info, int zj, int index) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("edit", b);
        bundle.putParcelable("info", info);
        bundle.putInt("zj", zj);
        bundle.putInt("index", index);
        CJFragment fragment = new CJFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            isEdit = bundle.getBoolean("edit");
            info = bundle.getParcelable("info");
            zj = bundle.getInt("zj");
            index = bundle.getInt("index");
        }
    }
    @Override
    public int getContentViewId() {
        return R.layout.fragment_cj;
    }
    @Override
    protected void initView() {
        tv_bw = view.findViewById(R.id.tv_bw);
        tv_dmc = view.findViewById(R.id.tv_dmc);
        et_jg = view.findViewById(R.id.et_jg);
        tv_yjzj = view.findViewById(R.id.tv_yjzj);
        tv_zt = view.findViewById(R.id.tv_zt);
        tvSb = view.findViewById(R.id.tv_sb);
        tvBz = view.findViewById(R.id.tv_bz);
        sv = view.findViewById(R.id.sv);
        tvZq = view.findViewById(R.id.tv_zq);
        rbzc = view.findViewById(R.id.rbzc);
        rbbzc = view.findViewById(R.id.rbbzc);
        RGZC = view.findViewById(R.id.RGZC);
        textviewTy = view.findViewById(R.id.textview_ty);
        tvDjnr = view.findViewById(R.id.tv_djnr);
        etYcFxnr = view.findViewById(R.id.et_yc_fxnr);
        ycmsLl = view.findViewById(R.id.ycms_ll);

        et_jg.setEnabled(isEdit);
        updata(info, index, zj);
    }
    @Override
    public void initData() {
        RGZC.setOnCheckedChangeListener((radioGroup, i) -> {
            if (i == R.id.rbzc) {
                et_jg.setText("正常");
                ycmsLl.setVisibility(View.GONE);
            } else if (i == R.id.rbbzc) {
                et_jg.setText("异常");
                ycmsLl.setVisibility(View.VISIBLE);
            }
        });
    }
    void updateState(String value) {
        et_jg.setText(value);
    }
    void updateFXNR(String value) { etYcFxnr.setText(value); }
    void updatecheck(Boolean value) {
        if (value) {
            tv_zt.setText("已检");
        } else {
            tv_zt.setText("未检");
        }
    }
    @SuppressLint("SetTextI18n")
    void updata(QYDDATABean info, int item, int size) {
        tvSb.setText(info.getSBMC());
        tv_bw.setText(info.getBJMC());
        tv_dmc.setText(info.getDMC());
        tvDjnr.setText(info.getDJNR());
        tvBz.setText(info.getBZZ());
        tv_zt.setText(info.isChecked() ? "已检" : "未检");
        tv_yjzj.setText(item + "/" + size);

        writebleinfo = info.getLRFS();


        if (info.getLRFS().equals("0")) {     //当时编辑状态的是 不需要按钮
            et_jg.setVisibility(View.VISIBLE);
            RGZC.setVisibility(View.GONE);
            textviewTy.setVisibility(View.GONE);
            ycmsLl.setVisibility(View.GONE);
            etYcFxnr.setText("");
            if (info.getCJJG() == null) {
                et_jg.setText("");
            } else {
                switch (info.getSBMCSTATE()) {
                    case "已运行":
                        et_jg.setText(info.getCJJG());
                        break;
                    case "已停用":
                        et_jg.setText("已停用");
                        et_jg.setVisibility(View.GONE);
                        textviewTy.setVisibility(View.VISIBLE);
                        break;
                    case "备用":
                        et_jg.setText("备用");
                        textviewTy.setText("备用");
                        et_jg.setVisibility(View.GONE);
                        textviewTy.setVisibility(View.VISIBLE);
                        break;
                    default:
                        et_jg.setText(info.getCJJG());
                        break;
                }
            }
        } else if (info.getLRFS().equals("1")) {   //不是编辑状态的时候

            et_jg.setVisibility(View.GONE);
            textviewTy.setVisibility(View.GONE);
            rbzc.setVisibility(View.VISIBLE);
            rbbzc.setVisibility(View.VISIBLE);
            RGZC.setVisibility(View.VISIBLE);
            rbzc.setText("正常");
            rbbzc.setText("异常");
            rbzc.setChecked(true);
            rbbzc.setChecked(false);
            etYcFxnr.setText("");

            if (info.getCJJG() == null) {
                et_jg.setText("正常");
                rbzc.setChecked(true);
                rbbzc.setChecked(false);
                ycmsLl.setVisibility(View.GONE);
            } else {
                switch (info.getCJJG()) {
                    case "已运行":
                        et_jg.setText("正常");
                        rbzc.setChecked(true);
                        rbbzc.setChecked(false);
                        ycmsLl.setVisibility(View.GONE);
                        break;
                    case "已停用":
                        textviewTy.setVisibility(View.VISIBLE);
                        et_jg.setText("已停用");
                        RGZC.setVisibility(View.GONE);
                        textviewTy.setVisibility(View.VISIBLE);
                        ycmsLl.setVisibility(View.GONE);
                        break;
                    case "异常":
                        et_jg.setText("异常");
                        rbbzc.setChecked(true);
                        rbzc.setChecked(false);
                        ycmsLl.setVisibility(View.VISIBLE);
                        etYcFxnr.setText(info.getFXNR());
                        break;
                    case "备用":
                        textviewTy.setVisibility(View.VISIBLE);
                        ycmsLl.setVisibility(View.GONE);
                        et_jg.setText("备用");
                        textviewTy.setText("备用");
                        RGZC.setVisibility(View.GONE);
                        ycmsLl.setVisibility(View.GONE);
                        break;
                    default:
                        et_jg.setText(info.getCJJG());
                        rbzc.setText(info.getCJJG());
                        rbzc.setChecked(true);
                        rbbzc.setChecked(false);
                        break;
                }
            }
        }
    }

    public void getBlueToothData(String bluetoothdata) {
        if (writebleinfo.equals("0")){
            et_jg.setText(bluetoothdata);
        }else if (writebleinfo.equals("1")){

        }
    }
    public String getCJJG() { return et_jg.getText().toString().trim(); }
    public String getYc_Fxnr() { return etYcFxnr.getText().toString().trim();
    }
}
