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

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.R;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.KcpdHjInfo;
import com.example.administrator.zhapp.fragment.BaseFragment;
import com.example.administrator.zhapp.utils.GsonUtils;
import com.example.administrator.zhapp.utils.HttpListener;
import com.example.administrator.zhapp.utils.NohttpUtils;
import com.example.administrator.zhapp.utils.SPUtils;
import com.example.administrator.zhapp.utils.UiUtlis;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;
import java.util.ArrayList;
import java.util.List;


public class PdxxFragment extends BaseFragment {

    ListView lv;
    TextView tvPch;
    TextView tvYpzwz;

    private String pdid;
    private List<KcpdHjInfo.KcpdHj> hjs;
    private CommonAdapter<KcpdHjInfo.KcpdHj> adapter;
    private View headView;
    private String pch;

    public PdxxFragment() {
    }

    public static PdxxFragment newInstance(String pdid, String pch) {
        PdxxFragment fragment = new PdxxFragment();
        Bundle args = new Bundle();
        args.putString("pdid", pdid);
        args.putString("pch", pch);
        fragment.setArguments(args);
        return fragment;
    }

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

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

    @Override
    protected void initView() {
        lv = view.findViewById(R.id.lv);
        tvPch = view.findViewById(R.id.tv_pch);
        tvYpzwz = view.findViewById(R.id.tv_ypzwz);


        hjs = new ArrayList<>();
        headView = View.inflate(context, R.layout.kcpdhj_item, null);
        headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
        requestData();
    }

    @Override
    protected void initData() {

    }


    private void requestData() {
        Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.PDCX, RequestMethod.GET);
        request.set("userid", (String) SPUtils.get(context, Contans.USERNAME, ""));
        request.set("pdid", pdid);
        NohttpUtils.getInstance().add(getActivity(), 0, request, callback, true, true, "加载中...");
    }

    private HttpListener<String> callback = new HttpListener<String>() {
        @Override
        public void onSucceed(int what, Response<String> response) {
            KcpdHjInfo info = GsonUtils.GsonToBean(response.get(), KcpdHjInfo.class);
            if (info != null) {
                double total = UiUtlis.getNum(info.getTotal());
                if (total != 0) {
                    hjs.clear();
                    hjs.addAll(info.getRows());
                    setList();
                }
            }
        }

        @Override
        public void onFailed(int what, Response<String> response) {

        }
    };

    private void setList() {

        if (adapter == null) {

            if (hjs.size() >= 2) {
                tvPch.setText(pch);
                tvYpzwz.setText(hjs.get(0).getYps() + "/" + hjs.get(0).getZps());
                hjs.remove(0);
                adapter = new CommonAdapter<KcpdHjInfo.KcpdHj>(context, R.layout.kcpdhj_item, hjs) {
                    @Override
                    protected void convert(ViewHolder viewHolder, KcpdHjInfo.KcpdHj item, int position) {
                        viewHolder.setText(R.id.tv_xh, (position + 1) + "");
                        viewHolder.setText(R.id.tv_hj, item.getBinnum());
                        viewHolder.setText(R.id.tv_ypd, item.getYps());
                        viewHolder.setText(R.id.tv_zs, item.getZps());
                    }
                };
                lv.addHeaderView(headView, null, false);
                lv.setAdapter(adapter);
            } else {
                tvPch.setText(pch);
                tvYpzwz.setText(hjs.get(0).getYps() + "/" + hjs.get(0).getZps());
            }

        } else {
            adapter.notifyDataSetChanged();
        }
    }
}
