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


import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
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.activity.BaseActivity;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.Gzqk;
import com.example.administrator.zhapp.entity.GzqkInfo;
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.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 xaqksbActivity extends BaseActivity {

    TextView tvInput;
    ListView lv;
    TextView tvNodata;

    private CommonAdapter<Gzqk> adapter;
    private List<Gzqk> datas;

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

    @Override
    protected void initView() {
        tvInput = findViewById(R.id.tv_input);
        lv = findViewById(R.id.lv);
        tvNodata = findViewById(R.id.tv_nodata);
        tvInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, XaqksblrActivity.class);
                startActivityForResult(intent, 200);
            }
        });

        setBack();
        setTitle("卸氨情况上报");

        datas = new ArrayList<>();
        requestData();

    }

    @Override
    protected void initData() {

    }

    private void setLvAdapter() {

        if (adapter == null) {

            adapter = new CommonAdapter<Gzqk>(context, R.layout.gzqk_item, datas) {
                @Override
                protected void convert(ViewHolder viewHolder, Gzqk item, int position) {
                    viewHolder.setText(R.id.tv_inputname, "录入人(" + (item.getLrrname().isEmpty() ? item.getLrr() : item.getLrrname()) + ")");
                    viewHolder.setText(R.id.tv_starttime, item.getGgtime_s());
//                    viewHolder.setText(R.id.tv_gzdd, item.getGgplace());
//                    viewHolder.setText(R.id.tv_ms, item.getGgcontent());
                    viewHolder.setText(R.id.tv_cbs, item.getCbs());
                    viewHolder.setText(R.id.tv_bcztt, item.getIssave());

                }
            };

            lv.setAdapter(adapter);
            lv.setEmptyView(tvNodata);

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(xaqksbActivity.this, XaqksblrActivity.class);
                    intent.putExtra("gzqk", datas.get(position));
                    startActivityForResult(intent, 200);
                }
            });
        } else {
            adapter.notifyDataSetChanged();
        }

    }

    private void requestData() {

        Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.GZQKSBLIST, RequestMethod.POST);
        request.setDefineRequestBodyForJson(getJson((String) SPUtils.get(context, Contans.USERNAME, "")));
        NohttpUtils.getInstance().add(this, 0, request, callback, true, true, "加载中...");

    }

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

            System.out.println("返回数据" + response.get());

            try {
                GzqkInfo info = GsonUtils.GsonToBean(response.get(), GzqkInfo.class);

                if (info != null) {
                    if (!info.getTotal().equals("0")) {
                        datas.clear();
                        datas.addAll(info.getRows());
                        setLvAdapter();
                    }
                } else {
                    showToast("数据出错");
                }
            } catch (Exception e) {
                e.printStackTrace();
                showToast("数据出错");
            }
        }

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

        }
    };

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

        if (requestCode == 200) {
            switch (resultCode) {
                //保存返回
                case 100:
                    requestData();
                    break;
                //录入返回
                case 200:
                    requestData();
                    break;
            }
        }

    }

    private String getJson(String lrr) {
        String json = "{\"Rows\": [{\"s_ggtime_s\": \"\",\"e_ggtime_s\": \"\",\"bm\": \"\",\"lrr\": \"" + lrr + "\",\"rwstate\": \"0\",\"selecttype\": \"1\"} ],\"Total\": 1}";
        return json;
    }

}
