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

import android.content.DialogInterface;
import android.content.Intent;

import androidx.appcompat.app.AlertDialog;

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.BaseActivity;
import com.example.administrator.zhapp.adapter.XzjhAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.XzjhInfo;
import com.example.administrator.zhapp.utils.UiUtlis;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备巡点检管理-质控点   已经取消和这个模块
 */

public class SzkdActivity extends BaseActivity {

    ListView lv;
    Button btn_hf;
    Button btn_sm;
    Button btn_del;

    private List<XzjhInfo> list;
    private View headView;
    private TextView tv_jh;
    private TextView tv_lx;
    private XzjhAdapter adapter;

    @Override
    public int getContentViewID() {
        return R.layout.activity_szkd;
    }

    protected void initView() {
        lv = findViewById(R.id.lv);
        btn_hf = findViewById(R.id.btn_hf);
        btn_sm = findViewById(R.id.btn_sm);
        btn_del = findViewById(R.id.btn_del);
        btn_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast("删除");
            }
        });
        btn_sm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SzkdActivity.this, SdlbActivity.class);
                intent.putExtra(Contans.KEY_FLAG, Contans.ZKDLB);
                startActivity(intent);
            }
        });
        btn_hf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast("恢复");
            }
        });
    }

    public void initData() {

        setTitle("质控点");
        setBack();

        list = new ArrayList<>();
        XzjhInfo info = new XzjhInfo(false, 1, "脱硫GGH检修", "0/5");
        XzjhInfo info2 = new XzjhInfo(false, 1, "浆液循环汞检修", "0/9");
        XzjhInfo info3 = new XzjhInfo(false, 1, "吸收塔搅拌检修", "0/21");
        list.add(info);
        list.add(info2);
        list.add(info3);

        headView = View.inflate(this, R.layout.xzjh_item, null);
        headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
        final CheckBox cb = (CheckBox) headView.findViewById(R.id.cb);
        cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setChecked(cb.isChecked());
                }
                adapter.notifyDataSetChanged();
            }
        });
        tv_jh = (TextView) headView.findViewById(R.id.tv_jhmc);
        tv_lx = (TextView) headView.findViewById(R.id.tv_jhlx);
        tv_jh.setText("项目名称");
        tv_lx.setText("已检/总计");
        adapter = new XzjhAdapter(this, 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) {

                AlertDialog.Builder builder = new AlertDialog.Builder(SzkdActivity.this);
                builder.setCancelable(false);
                builder.setTitle("提示");
                builder.setMessage("是否要浏览项目下的详细信息");
                builder.setNegativeButton(UiUtlis.getString(context, R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        Intent intent = new Intent(SzkdActivity.this, SqdActivity.class);
                        intent.putExtra(Contans.KEY_IS_EDIT, false);
                        startActivity(intent);
                    }
                });
                builder.show();
            }
        });
    }
}
