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

import static org.litepal.crud.DataSupport.updateAll;

import android.content.ContentValues;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import com.example.administrator.zhapp.DBModel.XfDjjhRwqy;
import com.example.administrator.zhapp.DBModel.XfXcxm;
import com.example.administrator.zhapp.DBModel.XfXcxmjg;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.fragment.BaseFragment;
import com.example.administrator.zhapp.utils.SPUtils;
import com.example.administrator.zhapp.utils.UiUtlis;
import org.litepal.crud.DataSupport;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 消防保卫巡查管理-消防栓详情
 */
public class XfsDetaliFragment extends BaseFragment {

    TextView tvBh;
    TextView tvWz;
    TextView tvWp;
    EditText etJg;
    TextView tvZt;
    Button btnLast;
    Button btnSave;
    Button btnNext;
    TextView tvYjzj;
    LinearLayout llJg;

    private int item;
    private ArrayList<XfDjjhRwqy> xfss;
    private List<XfXcxm> xms;

    private List<XfXcxmjg> jgs;

    public XfsDetaliFragment() {
    }

    public static XfsDetaliFragment newInstance(ArrayList<XfDjjhRwqy> list, int item) {
        XfsDetaliFragment fragment = new XfsDetaliFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList("list", list);
        args.putInt("item", item);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            xfss = new ArrayList<>();
            xfss = getArguments().getParcelableArrayList("list");
            item = getArguments().getInt("item");
        }
    }


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

    @Override
    protected void initView() {
        tvBh = view.findViewById(R.id.tv_bh);
        tvWz = view.findViewById(R.id.tv_wz);
        tvWp = view.findViewById(R.id.tv_wp);
        etJg = view.findViewById(R.id.et_jg);
        tvZt = view.findViewById(R.id.tv_zt);
        btnLast = view.findViewById(R.id.btn_last);
        btnSave = view.findViewById(R.id.btn_save);
        btnNext = view.findViewById(R.id.btn_next);
        tvYjzj = view.findViewById(R.id.tv_yjzj);
        llJg = view.findViewById(R.id.ll_jg);

        btnLast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!btnNext.isEnabled()) {
                    btnNext.setEnabled(true);
                }
                --item;
                if (item == 0) {
                    btnLast.setEnabled(false);
                    item++;
                } else {
                    updataView(xfss.get(item - 1), item);
                }
            }
        });
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!btnLast.isEnabled()) {
                    btnLast.setEnabled(true);
                }
                int zcCount = 0;//正常个数
                XfDjjhRwqy mhqc = xfss.get(item - 1);
                //保存检查结果,如果数据库中已经有该数据，就更新
                for (int i = 0; i < xms.size(); i++) {
                    View view1 = llJg.getChildAt(i);
                    RadioButton rb1 = (RadioButton) view1.findViewById(R.id.rb1);
                    boolean checked = rb1.isChecked();
                    if (checked) {
                        zcCount++;
                    }
                    XfXcxm xfXcxm = xms.get(i);
                    List<XfXcxmjg> jgs = DataSupport.where("xfid = ? and jhid = ? and ssid = ?", mhqc.getXFID(), xfXcxm.getJhid(), xfXcxm.getSsid()).find(XfXcxmjg.class);
                    if (jgs.size() == 0) {
                        XfXcxmjg jg = new XfXcxmjg();
                        jg.setXftype(xfXcxm.getXftype());
                        jg.setJhid(xfXcxm.getJhid());
                        jg.setSsid(xfXcxm.getSsid());
                        jg.setType1(xfXcxm.getType1());
                        jg.setType2(xfXcxm.getType2());
                        jg.setTypename(xfXcxm.getTypename());
                        jg.setXmid(xfXcxm.getXmid());
                        jg.setXfid(mhqc.getXFID());
                        jg.setJg(checked);//true 表示type1被选中，false 表示type2被选中
                        //xms.get(i).setTypejg(checked);
                        jg.save();
                    } else {
                        ContentValues values = new ContentValues();
                        values.put("jg", checked);
                        DataSupport.updateAll(XfXcxmjg.class, values, "xfid = ? and jhid = ? and ssid = ?", mhqc.getXFID(), xfXcxm.getJhid(), xfXcxm.getSsid());
                    }
                }

                //保存采集结果
                ContentValues values = new ContentValues();
                values.put("CJJG", UiUtlis.getText(etJg));
                values.put("checked", "1");
                values.put("cjr", (String) SPUtils.get(context, Contans.NAME,"测试"));
                values.put("cjsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                values.put("iszc", (zcCount == xms.size() ? 1 : 0) + "");
                DataSupport.updateAll(XfDjjhRwqy.class, values, "xfid = ?", mhqc.getXFID());
                ++item;
                xfss.get(item - 2).setCJJG(UiUtlis.getText(etJg));
                xfss.get(item - 2).setChecked(true);
                if (item > xfss.size()) {
                    item--;
                    updataView(xfss.get(item - 1), item);
                } else {
                    updataView(xfss.get(item - 1), item);
                }
            }
        });
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!btnLast.isEnabled()) {
                    btnLast.setEnabled(true);
                }
                ++item;
                if (item > xfss.size()) {
                    btnNext.setEnabled(false);
                    item--;
                } else {
                    updataView(xfss.get(item - 1), item);
                }
            }
        });

        xms = new ArrayList<>();
        jgs = new ArrayList<>();
        updataView(xfss.get(item - 1), item);
    }

    @Override
    public void initData() {

    }

    private void updataView(XfDjjhRwqy xfs, int item) {

        tvBh.setText(xfs.getBH());
        tvWz.setText(xfs.getXFWZ());
        tvWp.setText(xfs.getXHNUM());
        tvZt.setText(xfs.isChecked() ? "已检" : "未检");
        tvYjzj.setText(item + "/" + xfss.size());
        etJg.setText(xfs.getCJJG());

        //添加采集结果的View
        llJg.removeAllViews();
        jgs.clear();
        xms.clear();
        jgs = DataSupport.where("xmid = ? and jhid = ? and xfid = ?", xfs.getXMID(), xfs.getJHID(), xfs.getXFID()).find(XfXcxmjg.class);
        xms = DataSupport.where("xmid = ? and jhid = ?", xfs.getXMID(), xfs.getJHID()).find(XfXcxm.class);
        for (int i = 0; i < xms.size(); i++) {
            View view = LayoutInflater.from(context).inflate(R.layout.xfjg_layout, null, false);
            TextView tv = (TextView) view.findViewById(R.id.tv_typename);
            RadioButton rb1 = (RadioButton) view.findViewById(R.id.rb1);
            RadioButton rb2 = (RadioButton) view.findViewById(R.id.rb2);
            tv.setText(xms.get(i).getTypename() + "：");
            rb1.setText(xms.get(i).getType1());
            rb2.setText(xms.get(i).getType2());
            //如果结果中有数据，就显示结果中的数据
            if (jgs.size() != 0) {
                for (XfXcxmjg jg : jgs) {
                    if (jg.getSsid().equals(xms.get(i).getSsid())) {
                        if (jg.isJg()) {
                            rb1.setChecked(true);
                        } else {
                            rb2.setChecked(true);
                        }
                    }
                }
            }
            llJg.addView(view);
        }
    }

    public int getItem() {
        return item-1;
    }
}
