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


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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.StatusInfo;
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.example.administrator.zhapp.weight.DateTimePickDialog;
import com.yolanda.nohttp.Binary;
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.Date;

public class GzqksblrActivity extends BaseActivity implements DateTimePickDialog.CommitClickListener {

    EditText etCbs;
    EditText etGzdd;
    TextView tvStarttime;
    EditText etMs;
    Button btnSave;

    DateTimePickDialog dialog;

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

    @Override
    protected void initView() {
        etCbs = findViewById(R.id.et_cbs);
        etGzdd = findViewById(R.id.et_gzdd);
        tvStarttime = findViewById(R.id.tv_starttime);
        etMs = findViewById(R.id.et_ms);
        btnSave = findViewById(R.id.btn_save);
        tvStarttime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = new DateTimePickDialog(context, "取消", "确定", GzqksblrActivity.this);
                dialog.show();
            }
        });
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UiUtlis.isEmpty(etGzdd)) {
                    showToast("工作地点不能为空");
                    return;
                }

                if (TextUtils.isEmpty(tvStarttime.getText().toString())) {
                    showToast("工作开始时间不能为空");
                    return;
                }

                if (UiUtlis.isEmpty(etMs)) {
                    showToast("工作内容描述不能为空");
                    return;
                }

                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();
            }
        });

        setBack();
        setTitle("工作情况上报录入");
    }

    @Override
    protected void initData() {

    }

    private void saveData() {

        Request<String> requset = NoHttp.createStringRequest(Contans.IP + Contans.GZQKSBSAVE, RequestMethod.POST);
        requset.setDefineRequestBodyForJson(getJson());
        NohttpUtils.getInstance().add(this, 0, requset, callback, true, true, "保存中...");

    }

    public void sendPush() {
        Request<String> request = NoHttp.createStringRequest(Contans.IP, RequestMethod.GET);
        request.set("name", (Binary) SPUtils.get(context, Contans.NAME, ""));
        NohttpUtils.getInstance().add(this, 2, 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 {
                switch (what) {
                    case 0:
                        StatusInfo info = GsonUtils.GsonToBean(response.get(), StatusInfo.class);
                        if (info != null) {
                            if (info.getTotal() != 0) {
                                if (info.getRows().get(0).getStatus().equals("1")) {
//                                    showToast("保存成功");
                                    showToast("保存成功");
                                    setResult(100);
                                    finish();
                                }
                            } else {
                                showToast("保存失败");
                            }
                        } else {
                            showToast("数据出错");
                        }
                        break;

                    case 1:
//                        sendPush();

                        break;

                }
            } catch (Exception e) {
                e.printStackTrace();
                showToast("数据出错");
            }
        }

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

    @Override
    public void Click(String outPutDate) {
        tvStarttime.setText(outPutDate);
        dialog.dismiss();
    }

    private String getJson() {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Gzqk info = new Gzqk();
        info.setId("");
        info.setGgtime_s(tvStarttime.getText().toString());
        info.setGgplace(UiUtlis.getText(etGzdd));
        info.setGgcontent(UiUtlis.getText(etMs));
        info.setLrr((String) SPUtils.get(context, Contans.USERNAME, ""));
        info.setAddtime(format.format(new Date()));
        info.setBm((String) SPUtils.get(context, Contans.BZMC, ""));
        info.setLrrname((String) SPUtils.get(context, Contans.NAME, ""));
        info.setCbs(UiUtlis.getText(etCbs));
        String json = "{\"Rows\":[" + GsonUtils.GsonString(info) + "],\"Total\":1}";
        return json;
    }
}
