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

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
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.Ajhxzrwqy;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.MipcaActivityCapture;
import com.example.administrator.zhapp.activity.ajhxcgl.AjhxcDetaliActivity;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.DlbInfo;
import com.example.administrator.zhapp.entity.SbdjInfo;
import com.example.administrator.zhapp.fragment.BaseFragment;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.List;

/**
 * 安健环巡查管理-安健环巡查
 */
public class Sajhlv2Fragment extends BaseFragment {

    ListView lv;
    TextView tvNodata;
    Button btnSys;

    private ArrayList<SbdjInfo> list;
    private CommonAdapter<DlbInfo> adapter;

    private ArrayList<Ajhxzrwqy> rwqys;
    private List<DlbInfo> infos;
    private String ewm = "";
    private boolean nfcorewm;

    private String tag;

    private View headview;

    public Sajhlv2Fragment() {
    }

    /**
     * 初始化
     *
     * @param param1
     * @param param2
     * @return
     */
    public static Sajhlv2Fragment newInstance(String param1, String param2) {
        Sajhlv2Fragment fragment = new Sajhlv2Fragment();
        Bundle args = new Bundle();
        args.putString("tag", param1);
        fragment.setArguments(args);
        return fragment;
    }

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

        Bundle bundle = getActivity().getIntent().getExtras();
        if (bundle != null) {
            nfcorewm = bundle.getBoolean(Contans.NFCOREWM);
            ewm = bundle.getString(Contans.KEY_BQBM);
            ewm = ewm == null ? "" : ewm;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
//            visibity();
        }
    }

    //显示界面
    private void visibity() {
        getDataInSql();
    }

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

    @Override
    protected void initView() {

        lv = view.findViewById(R.id.lv);
        btnSys = view.findViewById(R.id.btn_sys);
        tvNodata = view.findViewById(R.id.tv_nodata);

        headview = View.inflate(context, R.layout.baxc_item, null);
        TextView tv = (TextView) headview.findViewById(R.id.tv_d);
        tv.setText("检查内容");
        headview.findViewById(R.id.head).setVisibility(View.VISIBLE);

        btnSys.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MipcaActivityCapture.class);
                startActivityForResult(intent, 1);
            }
        });

    }

    public void initData() {

        list = new ArrayList<>();
        rwqys = new ArrayList<>();
        infos = new ArrayList<>();
        if (!ewm.isEmpty()) {
            getDataInSql();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

//        switch (requestCode) {
//            case 1:
//                if (resultCode == Activity.RESULT_OK) {
//                    Bundle bundle = data.getExtras();
//                    ewm = bundle.getString("result");
//                    nfcorewm = true;
//                    getDataInSql();
//                    if (tag.equals("ajh")) {
//                        AjhAndSbjdActivity ajh = (AjhAndSbjdActivity) getActivity();
//                        ajh.setList(ewm);
//                    }
//                }
//                break;
//        }
    }

    private void getDataInSql() {
        rwqys.clear();
        infos.clear();

        if (nfcorewm) {
            ContentValues values = new ContentValues();
            values.put("smfx", 1);
            DataSupport.updateAll(Ajhxzrwqy.class, values, "bqbm = ?", ewm);
            List<Ajhxzrwqy> rwqy = DataSupport.where("bqbm = ?", ewm).find(Ajhxzrwqy.class);
            if (rwqy.size() != 0) {
                rwqys.addAll(rwqy);
            }
        } else {
            ContentValues values = new ContentValues();
            values.put("smfx", 0);
            DataSupport.updateAll(Ajhxzrwqy.class, values, "nfcbh = ?", ewm);
            List<Ajhxzrwqy> rwqy = DataSupport.where("nfcbh = ?", ewm).find(Ajhxzrwqy.class);
            if (rwqy.size() != 0) {
                rwqys.addAll(rwqy);
            }
        }
        for (int i = 0; i < rwqys.size(); i++) {
            DlbInfo info = new DlbInfo();
            info.setXh(i + 1);
            info.setDian(rwqys.get(i).getJCNY());
            info.setStatu(rwqys.get(i).isChecked());
            infos.add(info);
        }

        if (adapter == null) {
            adapter = new CommonAdapter<DlbInfo>(context, R.layout.baxc_item, infos) {
                @Override
                protected void convert(ViewHolder viewHolder, DlbInfo item, int position) {
                    viewHolder.setText(R.id.tv_xh, item.getXh() + "");
                    viewHolder.setText(R.id.tv_d, item.getDian());
                    viewHolder.setText(R.id.tv_zt, item.isStatu() ? "已检" : "未检");
                }
            };
            lv.addHeaderView(headview, null, false);
            lv.setEmptyView(tvNodata);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(getActivity(), AjhxcDetaliActivity.class);
                    intent.putParcelableArrayListExtra(Contans.KEY_SBDJLIST, rwqys);
                    intent.putExtra(Contans.NFCOREWM, nfcorewm);
                    intent.putExtra(Contans.KEY_BQBM, ewm);
                    intent.putExtra("item", position - 1);
                    startActivity(intent);
                }
            });
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    public void dataFromNFC(String nfcbh) {
        nfcorewm = false;
        ewm = nfcbh;
        getDataInSql();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!ewm.isEmpty()) {
            getDataInSql();
        }
    }
}