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

import android.app.DatePickerDialog;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
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.XsDqGzZxListBean;
import com.example.administrator.zhapp.entity.XsDqgzYqQxSaveBean;
import com.example.administrator.zhapp.entity.XsResultBaseBean;
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.CommitDialog;
import com.example.administrator.zhapp.weight.InputLayout;
import com.example.administrator.zhapp.weight.ListDialog;
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.Calendar;
import java.util.List;


/**
 * 定期工作执行
 */
public class XsDqgzYqActivity extends BaseActivity {

    InputLayout ilJhmc;
    InputLayout ilBgyy;
    InputLayout ilXgr;
    InputLayout ilYq;
    InputLayout ilBgbc;
    Button btn_save;

    private XsDqGzZxListBean.DataBean dqgz;

    private void findView() {
        ilJhmc = findViewById(R.id.il_jhmc);
        ilBgyy = findViewById(R.id.il_bgyy);
        ilXgr = findViewById(R.id.il_xgr);
        ilYq = findViewById(R.id.il_yq);
        ilBgbc = findViewById(R.id.il_bgbc);
        btn_save = findViewById(R.id.btn_save);
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommitDialog dialog = new CommitDialog(context, new CommitDialog.CommitDialogListener() {
                    @Override
                    public void confirm() {
                        saveData();
                    }

                    @Override
                    public void cancel() {

                    }
                });
                dialog.setTvMsg("是否延期该工作?");
                dialog.show();
            }
        });
    }

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

    @Override
    protected void initView() {

        findView();
        setTitle("工作延期");
        setBack();

        dqgz = (XsDqGzZxListBean.DataBean) getIntent().getExtras().get(Contans.KEY_DQGZ);
        if (dqgz != null) {
            ilJhmc.setContent(dqgz.getJhmc());
        }

        ilBgbc.setTvOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                List<String> lists = new ArrayList<String>();
                lists.add("白班");
                lists.add("中班");
                lists.add("夜班");

                ListDialog dialog = new ListDialog(context, lists, new ListDialog.ListDialogListener() {
                    @Override
                    public void selectText(String str, int position) {
                        ilBgbc.setContent(str);
                    }
                });
                dialog.show();
            }
        });

        ilYq.setTvOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                Calendar c = Calendar.getInstance();

                DatePickerDialog dialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

                        int mYear = year;
                        int mMonth = month;
                        int mDay = dayOfMonth;

                        ilYq.setContent(new StringBuilder()
                                .append(mYear)
                                .append("-")
                                .append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1))
                                .append("-")
                                .append((mDay < 10) ? 0 + mDay : mDay).toString());

                    }
                }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));

                dialog.show();
            }
        });
    }

    @Override
    protected void initData() {

    }

    private void saveData() {

        final Request<String> requset = NoHttp.createStringRequest(Contans.IP + Contans.XSDJ, 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 bean = GsonUtils.GsonToBean(response.get(), XsResultBaseBean.class);
                    if (bean != null) {
                        showToast(bean.getMsg());
                        if (bean.getState() == 1) {
                            finish();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

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

            }
        });

    }

    private String createJson() {

        XsDqgzYqQxSaveBean bean = new XsDqgzYqQxSaveBean();
        bean.setAction("DQGZ_ZXBG_SET");
        bean.setZc((String) SPUtils.get(context, Contans.BZBH, ""));
        bean.setZxid(dqgz.getZxid());
        bean.setBglx("1");
        bean.setBgms(ilBgyy.getContent());
        bean.setBgxgr(ilXgr.getContent());
        bean.setBgyq(ilYq.getContent());
        bean.setBgbc(ilBgbc.getContent());
        bean.setSqr((String) SPUtils.get(context, Contans.USERNAME, ""));

        String json = GsonUtils.GsonString(bean);
        return json;
    }
}
