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


import android.app.DatePickerDialog;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
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.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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class GzqkckActivity extends BaseActivity {

    ListView lv;
    TextView tvNodata;
    TextView tvStarttime;
    TextView tvEndtime;
    EditText etBm;
    Button btnQuery;
    Button btnReset;

    private CommonAdapter<Gzqk> adapter;
    private List<Gzqk> datas;
    private Calendar c = Calendar.getInstance();

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

    @Override
    protected void initView() {
        lv = findViewById(R.id.lv);
        tvNodata = findViewById(R.id.tv_nodata);
        tvStarttime = findViewById(R.id.tv_starttime);
        tvEndtime = findViewById(R.id.tv_endtime);
        etBm = findViewById(R.id.et_bm);
        btnQuery = findViewById(R.id.btn_query);
        btnReset = findViewById(R.id.btn_reset);
        tvStarttime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog dialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

                        c.set(year, month, dayOfMonth);
                        tvStarttime.setText(new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()));
                    }
                }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
                dialog.show();
            }
        });
        tvEndtime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog dialog1 = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        c.set(year, month, dayOfMonth);
                        tvEndtime.setText(new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()));
                    }
                }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));

                dialog1.show();
            }
        });
        btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UiUtlis.hideInput(etBm, context);
                String startTime = tvStarttime.getText().toString();
                String endTime = tvEndtime.getText().toString();
                String bm = etBm.getText().toString();
                requestData(startTime, endTime, bm);
            }
        });
        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvStarttime.setText("");
                tvEndtime.setText("");
                etBm.setText("");
                requestData();
            }
        });

        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_gzdd, item.getGgplace());
                    viewHolder.setText(R.id.tv_cbs, item.getCbs());
                    viewHolder.setText(R.id.tv_bcztt, item.getIssave());
                    viewHolder.setText(R.id.tv_starttime, item.getGgtime_s());
                    viewHolder.setText(R.id.tv_czr, item.getCuropername());
                    if (item.getRwstate().equals("1")) {
                        viewHolder.setText(R.id.tv_gzzt, "该工单已关闭");

                    } else if (item.getRwstate().equals("2")) {
                        viewHolder.setText(R.id.tv_gzzt, "离开");
                        viewHolder.setTextColorRes(R.id.tv_gzzt, R.color.red);
                        viewHolder.setText(R.id.tv_ztsj, "离开时间:" + item.getTbtime());

                    } else if (item.getRwstate().equals("3")) {
                        viewHolder.setText(R.id.tv_gzzt, "现场");
                        viewHolder.setTextColorRes(R.id.tv_gzzt, R.color.green);
                        viewHolder.setText(R.id.tv_ztsj, "进入时间:" + item.getTbtime());

                    } else {
                        viewHolder.setText(R.id.tv_gzzt, "未开始工作");
                        viewHolder.setText(R.id.tv_ztsj, "");
                        viewHolder.setTextColorRes(R.id.tv_gzzt, R.color.gray);
                    }
                }
            };

            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(GzqkckActivity.this, GzqkgbActivity.class);
                    intent.putExtra("gzqk", datas.get(position));
                    intent.putExtra("isClose", false);
                    startActivity(intent);
                }
            });
        } else {

            adapter.setNewData(datas);

        }
    }

    private void requestData() {
        requestData("", "", "");
    }

    private void requestData(String startTime, String endTime, String bm) {
        Request<String> request = NoHttp.createStringRequest(Contans.IP+ Contans.GZQKSBLIST, RequestMethod.
                POST);
        request.setDefineRequestBodyForJson(getJson((String) SPUtils.get(context, Contans.USERNAME, ""), startTime, endTime, bm));
        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) {

            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 {
                        datas.clear();
                        setLvAdapter();
                    }
                } else {
                    showToast("暂无数据");
                }
            } catch (Exception e) {
                e.printStackTrace();
                showToast("数据出错");
            }
        }

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

        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        requestData("", "", "");
    }

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