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

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import com.example.administrator.zhapp.BaseListViewAdapter.CommonAdapter;
import com.example.administrator.zhapp.BaseListViewAdapter.ViewHolder;
import com.example.administrator.zhapp.DBModel.XfBaxcRwqy;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.fragment.BaseFragment;
import java.util.ArrayList;
import java.util.List;

/**
 * 消防保卫巡查管理-治安巡逻签到点
 */
public class XzaxlFragment extends BaseFragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    ListView lv;

    private View headView;

    private List<XfBaxcRwqy> rwqys;

    private CommonAdapter<XfBaxcRwqy> adapter;

    public XzaxlFragment() {
    }

    /**
     */
    public static XzaxlFragment newInstance(String param1, ArrayList<XfBaxcRwqy> list) {
        XzaxlFragment fragment = new XzaxlFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putParcelableArrayList("list", list);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            rwqys = getArguments().getParcelableArrayList("list");
        }
    }

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

    @Override
    protected void initView() {
        context = getContext();
        lv = view.findViewById(R.id.lv);
    }

    public void initData() {

        headView = View.inflate(context, R.layout.baxc_item, null);
        headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
        TextView d = (TextView) headView.findViewById(R.id.tv_d);
        d.setText("巡逻签到点");
        lv.addHeaderView(headView, null, false);
        adapter = new CommonAdapter<XfBaxcRwqy>(context, R.layout.baxc_item, rwqys) {
            @Override
            protected void convert(ViewHolder viewHolder, XfBaxcRwqy item, int position) {
                viewHolder.setText(R.id.tv_xh, item.getQyname());
                viewHolder.setText(R.id.tv_d, item.getQywz());
                viewHolder.setText(R.id.tv_zt, item.isChecked() ? "已签到" : "未签到");

                int color;
                if (item.isChecked()) {
                    color = Color.RED;
                } else {
                    color = Color.GRAY;
                }

                viewHolder.setTextColor(R.id.tv_xh,color);
                viewHolder.setTextColor(R.id.tv_d,color);
                viewHolder.setTextColor(R.id.tv_zt,color);
            }
        };

        lv.setAdapter(adapter);
//        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
//
//                AlertDialog.Builder builder = new AlertDialog.Builder(context);
//                builder.setTitle("提示");
//                builder.setMessage("使用扫描巡更卡或者刷巡更卡进行签到");
//                builder.setNegativeButton("扫巡更卡", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int background) {
//                        rwqys.get(i - 1).setChecked(true);
//                        ContentValues values = new ContentValues();
//                        values.put("checked", 1);
//                        DataSupport.updateAll(XfBaxcRwqy.class, values, "xgid = ?", rwqys.get(i - 1).getXgid());
//                        adapter.notifyDataSetChanged();
//                    }
//                });
//
//                builder.setPositiveButton("刷巡更卡", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int i) {
//
//                    }
//                });
//                builder.show();
//            }
//        });
    }

    public void notifiDataChange() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
}
