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


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
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.StatusInfo;
import com.example.administrator.zhapp.entity.XagzqksbBean;
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.weight.DateTimePickDialog;
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 XaqksblrActivity extends BaseActivity implements DateTimePickDialog.CommitClickListener {


    DateTimePickDialog dialog;
    EditText etGznr;
    TextView tvStarttime;
    Button btnSave;
    EditText SBR;
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

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

    @Override
    protected void initView() {
        etGznr = findViewById(R.id.et_gznr);
        tvStarttime = findViewById(R.id.tv_starttime);
        btnSave = findViewById(R.id.btn_save);

        tvStarttime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = new DateTimePickDialog(context, "取消", "确定", XaqksblrActivity.this);
                dialog.show();
            }
        });

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

        setBack();
        setTitle("卸氨情况上报录入");
    }

    @Override
    protected void initData() {

    }

    private void saveData() {

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

    }

    private HttpListener<String> callback = new HttpListener<String>() {
        @Override
        public void onSucceed(int what, Response<String> response) {

            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("保存成功");
                                    setResult(100);
                                    finish();
                                }
                            } else {
                                showToast("保存失败");
                            }
                        } else {
                            showToast("数据出错");
                        }
                        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() {
        XagzqksbBean bean = new XagzqksbBean();
        List<XagzqksbBean.RowsBean> rowsBeanList = new ArrayList<>();
        XagzqksbBean.RowsBean rowsBean = new XagzqksbBean.RowsBean();
        rowsBean.setId("");
        rowsBean.setGgtime_s(tvStarttime.getText().toString());
        rowsBean.setGgcontent(etGznr.getText().toString());
        rowsBean.setLrr("");
        rowsBean.setAddtime(df.format(new Date()));
        rowsBean.setBm((String) SPUtils.get(context, Contans.BZMC, ""));
        rowsBean.setLrrname((String) SPUtils.get(context, Contans.NAME, ""));
        bean.setRows(rowsBeanList);
        bean.setTotal(1);
        String json = GsonUtils.GsonString(bean);
        return json;

    }
}
