package com.example.administrator.zhapp.fragment.ajhxcgl;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.example.administrator.zhapp.DBModel.DjjhRwQy;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.ajhxcgl.AjhAndSbjdActivity;
import com.example.administrator.zhapp.activity.sbxdjgl.ScjsjActivity;
import com.example.administrator.zhapp.fragment.BaseFragment;
import com.example.administrator.zhapp.utils.UiUtlis;

/**
 * 安健环巡查管理-采集
 */
public class CJFragment extends BaseFragment {

    TextView tv_wz;
    TextView tv_bw;
    TextView tv_dmc;
    TextView tv_sbzt;
    TextView textView3;
    TextView tv_cs;
    TextView tv_cx;
    TextView tv_bs;
    TextView tv_bx;
    EditText et_jg;
    TextView tv_yjzj;
    TextView tv_zt;
    TextView tv_dw;
    EditText etFxnr;
    LinearLayout llFxnr;

    RadioButton rb1;
    RadioButton rb2;
    RadioButton rb3;
    RadioGroup rg;
    TextView tv_bz;
    Button btn_next;



    private boolean isEdit;

    private DjjhRwQy info;

    private int zj;
    private int index;

    private double cxsx = 0;//超限上限
    private double cxxx = 0;//超限下限
    private double bjsx = 0;//报警上限
    private double bjxx = 0;//报警下限

    public CJFragment() {

    }

    public static CJFragment newInstance(boolean b, DjjhRwQy info, int zj, int index) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("edit", b);
        bundle.putParcelable("testinfo", 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("testinfo");
            zj = bundle.getInt("zj");
            index = bundle.getInt("index");
        }
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_cj;
    }

    @Override
    protected void initView() {
        tv_wz = view.findViewById(R.id.tv_wz);
        tv_bw = view.findViewById(R.id.tv_bw);
        tv_dmc = view.findViewById(R.id.tv_dmc);
        tv_sbzt = view.findViewById(R.id.tv_sbzt);
        textView3 = view.findViewById(R.id.textView3);
        tv_cs = view.findViewById(R.id.tv_cs);
        tv_cx = view.findViewById(R.id.tv_cx);
        tv_bs = view.findViewById(R.id.tv_bs);
        tv_bx = view.findViewById(R.id.tv_bx);
        et_jg = view.findViewById(R.id.et_jg);
        tv_yjzj = view.findViewById(R.id.tv_yjzj);
        tv_zt = view.findViewById(R.id.tv_zt);
        tv_dw = view.findViewById(R.id.tv_dw);
        etFxnr = view.findViewById(R.id.et_fxnr);
        llFxnr = view.findViewById(R.id.ll_fxnr);

        rb1 = view.findViewById(R.id.rb1);
        rb2 = view.findViewById(R.id.rb2);
        rb3 = view.findViewById(R.id.rb3);
        rg = view.findViewById(R.id.rg);
        tv_bz = view.findViewById(R.id.tv_bz);

        btn_next = view.findViewById(R.id.btn_next);

        et_jg.setEnabled(isEdit);
        updata(info, index, zj);

        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //下一条
                if (getActivity() instanceof ScjsjActivity) {
                    ScjsjActivity activity = (ScjsjActivity) getActivity();
                    activity.getSjcjNext();
                }
                if (getActivity() instanceof AjhAndSbjdActivity) {
                    AjhAndSbjdActivity activity = (AjhAndSbjdActivity) getActivity();
                    activity.getSjcjNext();
                }
            }
        });
    }

    @Override
    public void initData() {

        et_jg.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!s.toString().trim().isEmpty()) {
                    double jg = UiUtlis.getNum(s.toString().trim());
                    if (jg > cxsx || jg < cxxx || jg > bjsx || jg < bjxx) {
                        showToast("采集结果超限");
                        llFxnr.setVisibility(View.VISIBLE);
                    } else {
                        llFxnr.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }


    /**
     * 更新数据
     */
    public void updata(DjjhRwQy info, int item, int size) {
        tv_dmc.setText(info.getPOINTNAME());
        tv_bw.setText(info.getMEAPOS());
        tv_wz.setText(info.getDESCRIPTION());
        et_jg.setText("");
        tv_bs.setText(info.getUPPERWARNING());
        tv_bx.setText(info.getLOWERWARNING());
        tv_cs.setText(info.getUPPERACTION());
        tv_cx.setText(info.getLOWERACTION());
        tv_sbzt.setText(info.getMEASTATUS());
        tv_zt.setText(info.isChecked() ? "已检" : "未检");
        tv_yjzj.setText(item + "/" + size);
        et_jg.setText(info.getCJJG());
        tv_dw.setText(info.getUNITOFMEASURE());
        etFxnr.setText(info.getFxnr());
        tv_bz.setText(info.getMEASTANDARD());


        if (info.isChecked()) {
            if (info.isBYZT()) {
                rb3.setChecked(true);
                rb1.setChecked(false);
                rb2.setChecked(false);
            } else {
                rb1.setChecked(info.isSBZT());
                rb2.setChecked(!rb1.isChecked());
                rb3.setChecked(false);
            }
        } else {
            rb1.setChecked(true);
            rb2.setChecked(false);
            rb3.setChecked(false);
        }

//        rb1.setChecked(info.isSBZT());
//        rb2.setChecked(!rb1.isChecked());
//        rb3.setChecked(info.isBYZT());

        cxsx = UiUtlis.getNum(info.getUPPERACTION());
        cxxx = UiUtlis.getNum(info.getLOWERACTION());
        bjsx = UiUtlis.getNum(info.getUPPERWARNING());
        bjxx = UiUtlis.getNum(info.getLOWERWARNING());

        if (info.getFxnr() == null) {
            llFxnr.setVisibility(View.GONE);
        } else {
            double jg = UiUtlis.getNum(info.getCJJG());
            if (jg > cxsx || jg < cxxx || jg > bjsx || jg < bjxx) {
                llFxnr.setVisibility(View.VISIBLE);
            } else {
                llFxnr.setVisibility(View.GONE);
            }
        }
    }


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

    public String getFxnr() {
        return etFxnr.getText().toString().trim();
    }

    public boolean getRB1() {

        boolean flag = true;
        if (rb1.isChecked()) {
            flag = true;
        } else if (rb2.isChecked()) {
            flag = false;
        } else if (rb3.isChecked()) {
            flag = true;
        }
        return flag;
    }

    public boolean getRB3() {
        return rb3.isChecked();
    }
}
