package com.rehome.sgbaxj.activity.sbxj;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.rehome.sgbaxj.R;
import com.rehome.sgbaxj.adapter.XsjhqyWorkXsAdapter;
import com.rehome.sgbaxj.bean.XsResultBaseBean;
import com.rehome.sgbaxj.bean.XsSaveDataInfo;
import com.rehome.sgbaxj.bean.XsjhQyBean;
import com.rehome.sgbaxj.contans.Contans;
import com.rehome.sgbaxj.base.BaseActivity;
import com.rehome.sgbaxj.utils.GsonUtils;
import com.rehome.sgbaxj.utils.HttpListener;
import com.rehome.sgbaxj.utils.NohttpUtils;
import com.rehome.sgbaxj.utils.SPUtils;
import com.rehome.sgbaxj.weight.AutoListView;
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.Date;
import java.util.List;

/**
 * 巡视
 */
public class XscbqyWorkXsActivity extends BaseActivity {

    private static final String JHLX_XS = "1";//巡视
    private static final String JHLX_CB = "2";//抄表
    private static final String JHLX_DJ = "3";//定检

    AutoListView lv;
    Button btnCommit;

    private XsjhQyBean.DataBeanX qy;

    private String jhlx;

    private List<XsjhQyBean.DataBeanX.DataBean> qyds;
    private XsjhqyWorkXsAdapter adapterXs;

    private boolean isHis;

    private String smfx;

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

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

    @Override
    protected void initView() {

        lv = findViewById(R.id.lv);
        btnCommit = findViewById(R.id.btn_commit);
        jhlx = (String) getIntent().getExtras().get(Contans.KEY_JHLX);

        qy = (XsjhQyBean.DataBeanX) getIntent().getExtras().get(Contans.KEY_QY);
        smfx = getIntent().getExtras().getString(Contans.KEY_EWM_OR_NFC);
        isHis = getIntent().getExtras().getBoolean(Contans.KEY_IS_HIS, false);

        if (isHis) {
            btnCommit.setVisibility(View.GONE);
        }

        btnCommit.setVisibility(isHis ? View.GONE : View.VISIBLE);

        qyds = new ArrayList<>();

        if (qy != null) {
            setTitle(qy.getQymc());
            qyds.addAll(qy.getData());
            setAdapterXs();
        }
        setBack();
        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("你确定要提交吗?");
                builder.setTitle("提示");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        saveData();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.create();
                builder.show();
            }
        });
    }

    private void setAdapterXs() {

        if (adapterXs == null) {
            adapterXs = new XsjhqyWorkXsAdapter(context, qyds, isHis);
            View headView = View.inflate(context, R.layout.qy_work_xs_item, null);
            headView.findViewById(R.id.tv_input).setVisibility(View.VISIBLE);
            headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
            lv.addHeaderView(headView, null, false);
            lv.setAdapter(adapterXs);
        } else {
            adapterXs.notifyDataSetChanged();
        }
    }

    private void saveData() {


        final Request<String> requset = NoHttp.createStringRequest(Contans.IP + Contans.XSCB, RequestMethod.POST);

        requset.setDefineRequestBodyForJson(createJson());

        NohttpUtils.getInstance().add(this, 0, requset, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {

                try {
                    XsResultBaseBean xsResultBaseBean = GsonUtils.GsonToBean(response.get(), XsResultBaseBean.class);
                    if (xsResultBaseBean != null) {
                        showToast(xsResultBaseBean.getMsg());
                        if (xsResultBaseBean.getState() == 1) {
                            finish();
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

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

            }
        });
    }

    private String createJson() {

        XsSaveDataInfo info = new XsSaveDataInfo();
        info.setAction("XSCB_ZXJHD_SET");
        info.setZxid(qy.getZxid());
        info.setQybh(qy.getQybh());

        List<XsSaveDataInfo.DataBean> dataBeanList = new ArrayList<>();

        for (XsjhQyBean.DataBeanX.DataBean dataBean : qyds) {

            XsSaveDataInfo.DataBean bean = new XsSaveDataInfo.DataBean();
            bean.setDbh(dataBean.getDbh());//点编号
            bean.setCbsz(dataBean.getCbsz());//抄表数值
            bean.setDjsj(dataBean.getDjsj());//点检时间
            bean.setScid(dataBean.getScid());

            if (!TextUtils.isEmpty(dataBean.getCbsz())) {
                if (TextUtils.isEmpty(dataBean.getDjsj())) {
                    bean.setDjsj(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
                }
            } else {
                bean.setDjsj(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
                if (!TextUtils.isEmpty(dataBean.getSisData())) {
                    bean.setCbsz(dataBean.getSisData());
                }
            }

            bean.setZcmc((String) SPUtils.get(context, Contans.BZBH, ""));//班组

            bean.setCbr((String) SPUtils.get(context, Contans.USERID, ""));//抄表人
            bean.setFxnr("");//分析内容
            bean.setSmfx(smfx);//扫描方式
            dataBeanList.add(bean);
        }

        info.setData(dataBeanList);

        String json = GsonUtils.GsonString(info);


        Log.e("#########", json);

        return json;
    }
}
