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

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.sbxdjgl.SxcdjActivity;
import com.example.administrator.zhapp.adapter.XzjhAdapter;
import com.example.administrator.zhapp.entity.XzjhInfo;
import com.example.administrator.zhapp.fragment.BaseFragment;
import java.util.ArrayList;
import java.util.List;


/**
 * 设备巡点检管理-质控点
 */
public class ZkdFragment extends BaseFragment {

    ListView lv;
    Button btn_sc;
    Button btn_xz;

    private SxcdjActivity mActivity;
    private XzjhAdapter adapter;
    private List<XzjhInfo> list;
    private CheckBox cb;
    private View headView;
    private View head;

    public ZkdFragment() {
    }

    public static ZkdFragment newInstance() {
        ZkdFragment fragment = new ZkdFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

    @Override
    protected void initView() {
        lv = view.findViewById(R.id.lv);
        btn_xz = view.findViewById(R.id.btn_xz);
        btn_sc = view.findViewById(R.id.btn_sc);

        mActivity = (SxcdjActivity) getActivity();
        headView = View.inflate(context, R.layout.xzjh_item, null);
        head = headView.findViewById(R.id.head);
        head.setVisibility(View.VISIBLE);
        TextView yjzj = (TextView) headView.findViewById(R.id.tv_jhlx);
        yjzj.setText("已检/总计");
        cb = (CheckBox) headView.findViewById(R.id.cb);
        cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (cb.isChecked()) {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).setChecked(true);
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).setChecked(false);
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });

        btn_xz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast("下载");
            }
        });

        btn_sc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast("上传");
            }
        });
    }

    public void initData() {

        list = new ArrayList<>();

        XzjhInfo info = new XzjhInfo(false, 1, "GGH修检", "0/5");
        XzjhInfo info1 = new XzjhInfo(false, 2, "吸收塔修检", "0/4");
        XzjhInfo info2 = new XzjhInfo(false, 3, "增压风机修检", "0/15");

        list.add(info);
        list.add(info1);
        list.add(info2);

        //setListData();

    }

    private void setListData() {
        adapter = new XzjhAdapter(context, null, new XzjhAdapter.CallBack() {
            @Override
            public void Click(View view) {

                CheckBox checkBox = (CheckBox) view;
                int index = (int) checkBox.getTag();

                list.get(index).setChecked(checkBox.isChecked());

                int count = 0;
                for (int a = 0; a < list.size(); a++) {
                    if (list.get(a).isChecked()) {
                        count++;
                    }
                }
                cb.setChecked(count == list.size());
                adapter.notifyDataSetChanged();

            }
        });
        lv.addHeaderView(headView, null, false);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                list.get(i - 1).setChecked(!list.get(i - 1).isChecked());
                int count = 0;
                for (int a = 0; a < list.size(); a++) {
                    if (list.get(a).isChecked()) {
                        count++;
                    }
                }
                cb.setChecked(count == list.size());
                adapter.notifyDataSetChanged();
            }
        });
    }
}
