package com.rehome.sgbaxj.activity.sbxj;


import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.rehome.sgbaxj.DBModel.Profession;
import com.rehome.sgbaxj.R;
import com.rehome.sgbaxj.adapter.CommonAdapter;
import com.rehome.sgbaxj.adapter.ViewHolder;
import com.rehome.sgbaxj.base.BaseActivity3;
import com.rehome.sgbaxj.bean.MyUploadCheckedBean;
import com.rehome.sgbaxj.bean.StatisticsBean;
import com.rehome.sgbaxj.bean.XsRequestInfo;
import com.rehome.sgbaxj.contans.Contans;
import com.rehome.sgbaxj.utils.GsonUtils;
import com.rehome.sgbaxj.utils.HttpListener;
import com.rehome.sgbaxj.utils.NoProgresshttpUtils;
import com.rehome.sgbaxj.utils.NohttpUtils;
import com.rehome.sgbaxj.utils.SPUtils;
import com.rehome.sgbaxj.weight.ListDialog;
import com.google.gson.Gson;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class XJStatisticsActivity extends BaseActivity3 {

    ListView lv;
    TextView tvNodata;
    TextView tv_zc;
    TextView checked_count;
    TextView tv_total;
    TextView tv_hasCheck;
    TextView tv_ljds;
    TextView tv_hasCheck_rate;
    LinearLayout tv_my_upload;


    private View headView;
    private TextView tv_djrw;
    private CommonAdapter<StatisticsBean.DataBean> adapter;
    private List<MyUploadCheckedBean.DataBean> dataBeans = new ArrayList<>();
    private List<String> listPro = new ArrayList<>();
    private List<Profession> professionList = new ArrayList<>();
    ListDialog listDialog;

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

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_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);
        tv_total = findViewById(R.id.tv_total);
        tv_hasCheck = findViewById(R.id.tv_hasCheck);
        tv_ljds = findViewById(R.id.tv_ljds);
        tv_hasCheck_rate = findViewById(R.id.tv_hasCheck_rate);
        tv_my_upload = findViewById(R.id.tv_my_upload);

        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        username = (String) SPUtils.get(context, Contans.USERID, "");
        lv.setEmptyView(tvNodata);
        headView = View.inflate(this, R.layout.checked_status_xsgz_item, null);
        headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
        lv.addHeaderView(headView, null, false);
        tv_djrw = headView.findViewById(R.id.tv_djrw);
        if(type!=null&&type.equals("YX")){
            String zyid = (String) SPUtils.get(context, Contans.ZY, "");
            if(zyid!=null&&(!zyid.equals(""))){
                List<String> zyids = this.stringToList(zyid);
                Log.i("app","---------");
                Log.i("app",new Gson().toJson(zyids));
                Log.i("app",zyids.get(0));
                if(zyids.size()>0){
                    this.professionId=zyids.get(0);
                }
            }

            initToolbar("巡检统计", "选择专业", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(type.equals("YX")){
                        listDialog.show();;
                    }
                }
            });


            professionList = DataSupport.findAll(Profession.class);
            if(professionList!=null&&professionList.size()>0){
                for(Profession profession : professionList){
                    listPro.add(profession.getZymc());
                }
            }
            if(listPro!=null&&listPro.size()>0){
                listDialog = new ListDialog(context, listPro, new ListDialog.ListDialogListener() {
                    @Override
                    public void selectText(String text, int position) {
                        if(professionList!=null&&professionList.size()>0){
                            Profession profession = professionList.get(position);
                            Log.i("app",profession.getZyid());
                            getStatisticsData(profession.getZyid());
                        }
                    }
                });
                listDialog.setTvTitle("请选择专业");
            }
        }else{
            initToolbar("巡检统计", "", new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }

        tv_my_upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(XJStatisticsActivity.this, XJStatisticsMyUploadActivity.class);
                intent.putExtra("type",type);
                startActivity(intent);
            }
        });
    }

    public void initData() {
        getMyUploadData();
        getStatisticsData(null);
        //setListData();
    }

    private List<String> stringToList(String strs) {
        String[] str = strs.split(",");
        return Arrays.asList(str);
    }

    //获取本人上传的巡检数据
    private void getMyUploadData() {
        //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 = GsonUtils.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()));
                    XJStatisticsActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(type.equals("BA")){
                                tv_zc.setText("保安");
                            }
                            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+"值");
                                    }
                                }
                            }
                        }
                    });
                }
            }

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

            }
        });
    }

    //获取本人上传的巡检数据
    private void getStatisticsData(String zyid) {
        if(type!=null&&type.equals("YX")){
            if(zyid!=null){
                this.professionId=zyid;
            }
        }

        Log.i("app",Contans.IP + Contans.GET_STATISTICS_DATA);
        Request<String> requestxs = NoHttp.createStringRequest(Contans.IP + Contans.GET_STATISTICS_DATA, RequestMethod.POST);
        requestxs.setDefineRequestBodyForJson(createJsonStatistics());
        requestxs.setReadTimeout(30000);

        NohttpUtils.getInstance().add(this, 1, requestxs, new HttpListener<String>() {

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

                Log.i("app","----------------");
                Log.i("app",response.get());

                StatisticsBean bean = GsonUtils.GsonToBean(response.get(), StatisticsBean.class);
                if(bean!=null&&bean.getState()==1){
                    Log.i("app",String.valueOf(bean.getData().size()));
                    Log.i("app",new Gson().toJson(bean.getData()));
                    List<StatisticsBean.DataBean> data = bean.getData();
                    XJStatisticsActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(data!=null&&data.size()>0){

                                int countZds = 0;
                                int countYjds = 0;
                                int countLjds = 0;
                                for (StatisticsBean.DataBean item: data) {
                                    String zds = item.getZds();
                                    String yjds = item.getYjds();
                                    String ljds = item.getLjds();
                                    countZds = countZds + Integer.parseInt(zds);
                                    countYjds = countYjds + Integer.parseInt(yjds);
                                    countLjds = countLjds + Integer.parseInt(ljds);
                                }
                                tv_total.setText(String.valueOf(countZds));
                                tv_hasCheck.setText(String.valueOf(countYjds));
                                tv_ljds.setText(String.valueOf(countLjds));
                                java.text.DecimalFormat df=new java.text.DecimalFormat("##.#%");//传入格式模板
                                String rate=df.format((float)(countYjds)/(float)countZds);
                                tv_hasCheck_rate.setText(rate);

                                setListData(data);
                            }
                        }
                    });
                }
            }

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

            }
        });
    }

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

                @Override
                protected void convert(ViewHolder viewHolder, StatisticsBean.DataBean item, int position) {
                    viewHolder.setText(R.id.tv_xh, item.getZds());
                    viewHolder.setText(R.id.tv_qymc, item.getQymc());
                    viewHolder.setText(R.id.tv_djrw, item.getYjds());
                    //viewHolder.setTextColor(R.id.tv_djrw, Color.parseColor("#00CD00"));//已经检查完成绿色
                }
            };
            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);
        Log.i("app",json);
        return json;
    }
    private String createJsonStatistics() {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("XJ_STATISTICS_CHECKED");
        if(type!=null&&type.equals("YX")){
            info.setZy(this.professionId);
        }else{
            info.setZy(type);
        }
        String json = GsonUtils.GsonString(info);
        Log.i("app",json);
        return json;
    }
}