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

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.XfXcmhqc;
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;
import static org.litepal.crud.DataSupport.updateAll;

/**
 * 消防保卫巡查管理-灭火器材
 */
public class MhqcDetaliFragment extends BaseFragment {


    TextView tvBh;
    TextView tvBw;
    TextView tvXh;
    TextView tvSl;
    TextView tvScrq;
    TextView tvYxrq;

    EditText etJg;
    TextView tvZt;
    Button btnLast;
    Button btnSave;
    Button btnNext;
    TextView tvYjzj;
    LinearLayout llJg;//用来动态添加数据

    private List<XfDjjhRwqy> mhqcs;
    private int item;

    private List<XfXcxmjg> jgs;

    private List<XfXcxm> xms;
    private List<XfXcmhqc> xfXcmhqcs;

    public MhqcDetaliFragment() {
    }

    public static MhqcDetaliFragment newInstance(ArrayList<XfDjjhRwqy> list, int item) {
        MhqcDetaliFragment fragment = new MhqcDetaliFragment();
        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) {
            mhqcs = new ArrayList<>();
            mhqcs = getArguments().getParcelableArrayList("list");
            item = getArguments().getInt("item");
        }
    }

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

    @Override
    protected void initView() {
        tvBh = view.findViewById(R.id.tv_bh);
        tvBw = view.findViewById(R.id.tv_bw);
        tvXh = view.findViewById(R.id.tv_xh);
        tvSl = view.findViewById(R.id.tv_sl);
        tvScrq = view.findViewById(R.id.tv_scrq);
        tvYxrq = view.findViewById(R.id.tv_yxrq);

        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++;
                    //showToast("已经到头了");
                } else {
                    updataView(mhqcs.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 = mhqcs.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 (rb1.isChecked()) {
                        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);
                        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) + "");//如果正常数量等于总数量，就表示正常
                int updatecount = DataSupport.updateAll(XfDjjhRwqy.class, values, "xfid = ?", mhqc.getXFID());
                if (updatecount != 0) {
                    showToast("保存成功");
                }
                ++item;
                mhqcs.get(item - 2).setCJJG(UiUtlis.getText(etJg));
                mhqcs.get(item - 2).setChecked(true);
                if (item > mhqcs.size()) {
                    item--;
                    updataView(mhqcs.get(item - 1), item);
                } else {
                    updataView(mhqcs.get(item - 1), item);
                }
            }
        });
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!btnLast.isEnabled()) {
                    btnLast.setEnabled(true);
                }
                ++item;
                if (item > mhqcs.size()) {
                    item--;
                    //showToast("到底了");
                    btnNext.setEnabled(false);
                } else {
                    updataView(mhqcs.get(item - 1), item);
                }
            }
        });


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

    @Override
    public void initData() {
    }

    private void updataView(XfDjjhRwqy mhqc, int item) {

        tvBh.setText(mhqc.getBH());
        tvBw.setText(mhqc.getXFWZ());
        tvXh.setText(mhqc.getXHNUM());
        tvZt.setText(mhqc.isChecked() ? "已检" : "未检");
        tvYjzj.setText(item + "/" + mhqcs.size());
        etJg.setText(mhqc.getCJJG());

        xfXcmhqcs.clear();
        xfXcmhqcs = DataSupport.where("xmid = ? and xfid = ?", mhqc.getXMID(), mhqc.getXFID()).find(XfXcmhqc.class);

        //设置时间
        StringBuilder scrq = new StringBuilder();
        StringBuilder yxrq = new StringBuilder();
        StringBuilder sl = new StringBuilder();
        for (int i = 0; i < xfXcmhqcs.size(); i++) {
            scrq.append(DateFormat(xfXcmhqcs.get(i).getScrq()) + "\n");
            yxrq.append(DateFormat(xfXcmhqcs.get(i).getYxrq()) + "\n");
            sl.append(i + 1 + "\n");
        }

        tvScrq.setText(scrq);
        tvYxrq.setText(yxrq);
        tvSl.setText(sl);

        //添加采集结果的View
        llJg.removeAllViews();
        xms.clear();
        jgs.clear();
        jgs = DataSupport.where("xmid = ? and jhid = ? and xfid = ?", mhqc.getXMID(), mhqc.getJHID(), mhqc.getXFID()).find(XfXcxmjg.class);
        xms = DataSupport.where("xmid = ? and jhid = ?", mhqc.getXMID(), mhqc.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);
            rb1.setText(xms.get(i).getType1());
            tv.setText(xms.get(i).getTypename() + "：");
            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);
        }
    }

    //转化日期格式
    private String DateFormat(String str) {

        String result = "";
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sf.parse(str);
            result = sf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

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