package com.bjzc.bhzhgd.activity.sbxj;


import static com.bjzc.bhzhgd.utils.GsonUtils.GsonToBean;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.bjzc.bhzhgd.R;
import com.bjzc.bhzhgd.adapter.CommonAdapter;
import com.bjzc.bhzhgd.adapter.ViewHolder;
import com.bjzc.bhzhgd.base.BaseActivity3;
import com.bjzc.bhzhgd.bean.MyUploadCheckedBean;
import com.bjzc.bhzhgd.bean.XsRequestInfo;
import com.bjzc.bhzhgd.contans.Contans;
import com.bjzc.bhzhgd.utils.GsonUtils;
import com.bjzc.bhzhgd.utils.HttpListener;
import com.bjzc.bhzhgd.utils.NoProgresshttpUtils;
import com.bjzc.bhzhgd.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 XJStatisticsMyUploadActivity extends BaseActivity3 {


    ListView lv;
    TextView tvNodata;
    TextView tv_zc;
    TextView checked_count;

    private View headView;
    private TextView tv_djrw;
    private CommonAdapter<MyUploadCheckedBean.DataBean> adapter;
    private List<MyUploadCheckedBean.DataBean> dataBeans = new ArrayList<>();

    //private ArrayList<XSJJHDataBean> xsjjhDataBeanArrayList = new ArrayList<>();//点检记录列表
    private String type = null;
    private String username;

    @Override
    public int getLayoutId() {
        return R.layout.activity_myupload_checked;
    }


    @Override
    public void initView() {
        lv = findViewById(R.id.lv);
        tvNodata = findViewById(R.id.tv_nodata);
        tv_zc = findViewById(R.id.tv_zc);
        checked_count = findViewById(R.id.checked_count);


        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        username = (String) SPUtils.get(context, Contans.USERID, "");
        lv.setEmptyView(tvNodata);
        headView = View.inflate(this, R.layout.my_upload_checked_status_xsgz_item, null);
        headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
        tv_djrw = headView.findViewById(R.id.tv_djrw);
        initToolbar("我的上传", "", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }

    public void initData() {
        requestDatas();
    }

    //获取本人上传的巡检数据
    private void requestDatas() {
        //Log.i("app",Contans.IP + Contans.GET_MY_UPLOAD_CHECKED);
        Request<String> requestxs = NoHttp.createStringRequest(Contans.IP + Contans.GET_MY_UPLOAD_CHECKED, RequestMethod.POST);
        requestxs.setDefineRequestBodyForJson(createJson());

        NoProgresshttpUtils.getInstance().add(this, 0, requestxs, new HttpListener<String>() {

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

//                Log.i("app","----------------");
//                Log.i("app",response.get());
                MyUploadCheckedBean bean = GsonToBean(response.get(), MyUploadCheckedBean.class);
                if(bean!=null&&bean.getState()==1){
                    List<MyUploadCheckedBean.DataBean> data = bean.getData();
//                    Log.i("app",bean.getMsg());
//                    Log.i("app",String.valueOf(bean.getState()));
//                    Log.i("app",String.valueOf(bean.getData().size()));
//                    Log.i("app",new Gson().toJson(bean.getData()));
                    XJStatisticsMyUploadActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(data!=null&&data.size()>0){
                                checked_count.setText(String.valueOf(data.size()));
                                String zc = data.get(0).getZc();
                                if(type.equals("BA")){
                                    tv_zc.setText("保安");
                                }else{
                                    if(zc!=null){
                                        tv_zc.setText(zc+"值");
                                    }
                                }
                                setListData(data);
                            }
                        }
                    });
                }
            }

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

            }
        });
    }

    private void setListData(List<MyUploadCheckedBean.DataBean> data) {
        adapter = new CommonAdapter<MyUploadCheckedBean.DataBean>(context, R.layout.my_checked_xsgz_item, data) {

            @Override
            protected void convert(ViewHolder viewHolder, MyUploadCheckedBean.DataBean item, int position) {
                viewHolder.setText(R.id.tv_xh, String.valueOf(position + 1));
                viewHolder.setText(R.id.tv_qymc, item.getJhmc()+"_"+item.getQymc());
                viewHolder.setText(R.id.tv_djrw, "已检");
                viewHolder.setTextColor(R.id.tv_djrw, Color.parseColor("#00CD00"));//已经检查完成绿色
            }
        };
        lv.addHeaderView(headView, null, false);
        lv.setAdapter(adapter);
    }

    private String createJson() {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("YX_XJGL_MY_UPLOAD");
        info.setUsername(username);
        info.setType(type);
        String json = GsonUtils.GsonString(info);
        showLog(json);
        return json;
    }
}