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

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
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.PdlbInfo;
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.rest.Request;
import com.yolanda.nohttp.rest.Response;
import java.util.ArrayList;
import java.util.List;

public class PdlbFragment extends BaseFragment {

    ListView lv;
    Spinner sp;
    ListView lv1;
    ListView lv2;
    private String pdid;

    private List<PdlbInfo.Pdlb> pdlbs;
    private List<PdlbInfo.Pdlb> wpdlbs;
    private List<PdlbInfo.Pdlb> bzcdlbs;

    private CommonAdapter<PdlbInfo.Pdlb> adapter;
    private CommonAdapter<PdlbInfo.Pdlb> wpdadapter;
    private CommonAdapter<PdlbInfo.Pdlb> bzcadapter;

    private View headView;

    public PdlbFragment() {
    }

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

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

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

    @Override
    protected void initView() {
        lv = (ListView) headView.findViewById(R.id.lv);
        sp = (Spinner) headView.findViewById(R.id.sp);
        lv1 = (ListView) headView.findViewById(R.id.lv1);
        lv2 = (ListView) headView.findViewById(R.id.lv2);

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


    @Override
    public void initData() {

        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    lv.setVisibility(View.VISIBLE);
                    lv1.setVisibility(View.INVISIBLE);
                    lv2.setVisibility(View.INVISIBLE);
                }

                if (position == 1) {
                    lv1.setVisibility(View.VISIBLE);
                    lv.setVisibility(View.INVISIBLE);
                    lv2.setVisibility(View.INVISIBLE);
                }

                if (position == 2) {
                    lv1.setVisibility(View.INVISIBLE);
                    lv.setVisibility(View.INVISIBLE);
                    lv2.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void requestData() {

        Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.PDCX);
        request.set("pdid", pdid);
        request.set("userid", (String) SPUtils.get(context, Contans.USERNAME, ""));
        request.set("sjtype", "2");
        NohttpUtils.getInstance().add(getActivity(), 0, request, callback, false, false, "");

        Request<String> request1 = NoHttp.createStringRequest(Contans.IP + Contans.PDCX);
        request1.set("pdid", pdid);
        request1.set("userid", (String) SPUtils.get(context, Contans.USERNAME, ""));
        request1.set("sjtype", "3");
        NohttpUtils.getInstance().add(getActivity(), 1, request1, callback, false, false, "");

        Request<String> request2 = NoHttp.createStringRequest(Contans.IP + Contans.PDCX);
        request2.set("pdid", pdid);
        request2.set("userid", (String) SPUtils.get(context, Contans.USERNAME, ""));
        request2.set("sjtype", "4");
        NohttpUtils.getInstance().add(getActivity(), 2, request2, callback, false, false, "");
    }

    private HttpListener<String> callback = new HttpListener<String>() {
        @Override
        public void onSucceed(int what, Response<String> response) {

            switch (what) {
                case 0:
                    PdlbInfo info = GsonUtils.GsonToBean(response.get(), PdlbInfo.class);
                    if (info != null) {
                        if (!info.getTotal().equals("0")) {
                            pdlbs.addAll(info.getRows());
                            setListView();
                        }
                    }
                    break;
                case 1:

                    PdlbInfo info2 = GsonUtils.GsonToBean(response.get(), PdlbInfo.class);
                    if (info2 != null) {
                        if (!info2.getTotal().equals("0")) {
                            wpdlbs.addAll(info2.getRows());
                            setWpdListView();
                        }
                    }
                    break;

                case 2:

                    PdlbInfo info3 = GsonUtils.GsonToBean(response.get(), PdlbInfo.class);
                    if (info3 != null) {
                        if (!info3.getTotal().equals("0")) {
                            bzcdlbs.addAll(info3.getRows());
                            setBzcListView();
                        }
                    }
                    break;
            }
        }

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

        }
    };

    private void setListView() {

        adapter = new CommonAdapter<PdlbInfo.Pdlb>(context, R.layout.pdlb_item, pdlbs) {
            @Override
            protected void convert(ViewHolder viewHolder, PdlbInfo.Pdlb item, int position) {

                viewHolder.setText(R.id.tv_xh, (position + 1) + "");
                viewHolder.setText(R.id.tv_hjh, item.getBinnum());
                viewHolder.setText(R.id.tv_wzbm, item.getItemnum());
                viewHolder.setText(R.id.tv_ms, item.getDescription());
                viewHolder.setText(R.id.tv_dw, item.getOrderunit());
                viewHolder.setText(R.id.tv_zmsl, item.getCurbal());
                viewHolder.setText(R.id.tv_pdsl, item.getPdsl());
                viewHolder.setText(R.id.tv_yksl, item.getYksl());
                viewHolder.setText(R.id.tv_xgzy, item.getIn6());
                viewHolder.setText(R.id.tv_dj, item.getLastcost());
                viewHolder.setText(R.id.tv_zj, item.getZj());
                viewHolder.setText(R.id.tv_bz, item.getBz());

                int color;

                if (item.getBz().equals("数量少")) {
                    color = Color.BLUE;
                } else if (item.getBz().equals("数量多")) {
                    color = Color.RED;
                } else if (item.getBz().equals("配件")) {
                    color = Color.YELLOW;
                } else {
                    //正常
                    color = Color.GRAY;
                }

                viewHolder.setTextColor(R.id.tv_xh, color);
                viewHolder.setTextColor(R.id.tv_hjh, color);
                viewHolder.setTextColor(R.id.tv_wzbm, color);
                viewHolder.setTextColor(R.id.tv_ms, color);
                viewHolder.setTextColor(R.id.tv_dw, color);
                viewHolder.setTextColor(R.id.tv_zmsl, color);
                viewHolder.setTextColor(R.id.tv_pdsl, color);
                viewHolder.setTextColor(R.id.tv_yksl, color);
                viewHolder.setTextColor(R.id.tv_xgzy, color);
                viewHolder.setTextColor(R.id.tv_dj, color);
                viewHolder.setTextColor(R.id.tv_zj, color);
                viewHolder.setTextColor(R.id.tv_bz, color);
            }
        };
        lv.addHeaderView(headView, null, false);
        lv.setAdapter(adapter);
    }

    private void setBzcListView() {

        bzcadapter = new CommonAdapter<PdlbInfo.Pdlb>(context, R.layout.pdlb_item, bzcdlbs) {
            @Override
            protected void convert(ViewHolder viewHolder, PdlbInfo.Pdlb item, int position) {

                viewHolder.setText(R.id.tv_xh, (position + 1) + "");
                viewHolder.setText(R.id.tv_hjh, item.getBinnum());
                viewHolder.setText(R.id.tv_wzbm, item.getItemnum());
                viewHolder.setText(R.id.tv_ms, item.getDescription());
                viewHolder.setText(R.id.tv_dw, item.getOrderunit());
                viewHolder.setText(R.id.tv_zmsl, item.getCurbal());
                viewHolder.setText(R.id.tv_pdsl, item.getPdsl());
                viewHolder.setText(R.id.tv_yksl, item.getYksl());
                viewHolder.setText(R.id.tv_xgzy, item.getIn6());
                viewHolder.setText(R.id.tv_dj, item.getLastcost());
                viewHolder.setText(R.id.tv_zj, item.getZj());
                viewHolder.setText(R.id.tv_bz, item.getBz());

                double yksl = UiUtlis.getNum(item.getYksl());

                int color;

                if (yksl > 0) {
                    color = Color.RED;
                } else if (yksl < 0) {
                    color = Color.BLUE;
                } else if (item.getBz().equals("配件")) {
                    color = Color.YELLOW;
                } else {
                    color = Color.GRAY;
                }

//                if (item.getBz().equals("数量少")) {
//                    color = Color.BLUE;
//                } else if (item.getBz().equals("数量多")) {
//                    color = Color.RED;
//                } else if (item.getBz().equals("配件")) {
//                    color = Color.YELLOW;
//                } else {
//                    //正常
//                    color = Color.GRAY;
//                }

                viewHolder.setTextColor(R.id.tv_xh, color);
                viewHolder.setTextColor(R.id.tv_hjh, color);
                viewHolder.setTextColor(R.id.tv_wzbm, color);
                viewHolder.setTextColor(R.id.tv_ms, color);
                viewHolder.setTextColor(R.id.tv_dw, color);
                viewHolder.setTextColor(R.id.tv_zmsl, color);
                viewHolder.setTextColor(R.id.tv_pdsl, color);
                viewHolder.setTextColor(R.id.tv_yksl, color);
                viewHolder.setTextColor(R.id.tv_xgzy, color);
                viewHolder.setTextColor(R.id.tv_dj, color);
                viewHolder.setTextColor(R.id.tv_zj, color);
                viewHolder.setTextColor(R.id.tv_bz, color);
            }
        };
        lv2.addHeaderView(headView, null, false);
        lv2.setAdapter(bzcadapter);
    }

    private void setWpdListView() {

        wpdadapter = new CommonAdapter<PdlbInfo.Pdlb>(context, R.layout.pdlb_item, wpdlbs) {
            @Override
            protected void convert(ViewHolder viewHolder, PdlbInfo.Pdlb item, int position) {

                viewHolder.setText(R.id.tv_xh, (position + 1) + "");
                viewHolder.setText(R.id.tv_hjh, item.getBinnum());
                viewHolder.setText(R.id.tv_wzbm, item.getItemnum());
                viewHolder.setText(R.id.tv_ms, item.getDescription());
                viewHolder.setText(R.id.tv_dw, item.getOrderunit());
                viewHolder.setText(R.id.tv_zmsl, item.getCurbal());
                viewHolder.setText(R.id.tv_pdsl, "");
                viewHolder.setText(R.id.tv_yksl, "");
                viewHolder.setText(R.id.tv_xgzy, item.getIn6());
                viewHolder.setText(R.id.tv_dj, item.getLastcost());
                viewHolder.setText(R.id.tv_zj, item.getZj());
                viewHolder.setText(R.id.tv_bz, item.getBz());
            }
        };
        lv1.addHeaderView(headView, null, false);
        lv1.setAdapter(wpdadapter);
    }
}
