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


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
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.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;

import static com.example.administrator.zhapp.contans.Contans.IP;

public class GzqkgbActivity extends BaseActivity {

    TextView tvLrr;
    TextView tvInputtime;
    TextView tvBm;
    TextView tvCbs;
    TextView tvGzdd;
    TextView tvStarttime;
    TextView tvMs;
    Button btnClose;
    Button btnLk;
    Button btnJr;
    TextView tvRz;

    private Gzqk gzqk;
    private String issave;
    private String status;


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

    @Override
    protected void initView() {
        tvLrr = findViewById(R.id.tv_lrr);
        tvInputtime = findViewById(R.id.tv_inputtime);
        tvBm = findViewById(R.id.tv_bm);
        tvCbs = findViewById(R.id.et_cbs);
        tvGzdd = findViewById(R.id.et_gzdd);
        tvStarttime = findViewById(R.id.tv_starttime);
        tvMs = findViewById(R.id.tv_ms);
        btnClose = findViewById(R.id.btn_close);
        btnLk = findViewById(R.id.btn_lk);
        btnJr = findViewById(R.id.btn_jr);
        tvRz = findViewById(R.id.tv_rz);
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (issave.equals("已保存")) {
                    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();
                            requestData("1");
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

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

                } else if (issave.equals("未保存")) {
                    showToast("该工作状态还未保存");
                }
            }
        });


        setBack();
        setTitle("变更工作状态");
        gzqk = (Gzqk) getIntent().getExtras().getSerializable("gzqk");
        status = gzqk.getRwstate();
        issave = gzqk.getIssave();
        String bzbm = (String) SPUtils.get(context, Contans.BZMC, "");
        if (bzbm.equals("安全监察分部")) {
            btnJr.setVisibility(View.GONE);
            btnLk.setVisibility(View.GONE);
            btnClose.setVisibility(View.GONE);
        }

        String USERNAME = (String) SPUtils.get(context, Contans.USERNAME, "");
        String CZRID = gzqk.getOperid();

        if (CZRID.equals(USERNAME)) {   //登陆人是操作人
            if (status.equals("0")) {
                btnLk.setVisibility(View.GONE);
            }

            if (status.equals("2")) {
                btnLk.setVisibility(View.GONE);
            }

            if (status.equals("3")) {
                btnJr.setVisibility(View.GONE);
            } else {
                btnJr.setVisibility(View.VISIBLE);
            }

            if (issave.equals("已保存")) {
            } else if (issave.equals("未保存")) {
                btnLk.setVisibility(View.GONE);
                btnClose.setVisibility(View.GONE);
            }
        } else {  //登陆人不是操作人

            if (status.equals("3")) {
                btnLk.setVisibility(View.GONE);
                btnJr.setVisibility(View.GONE);
                btnClose.setVisibility(View.GONE);
            }

            if (status.equals("0")) {
                btnLk.setVisibility(View.GONE);
            }
            if (status.equals("2")) {
                btnLk.setVisibility(View.GONE);
            }

        }


        if (gzqk != null) {
            tvLrr.setText(gzqk.getLrrname().isEmpty() ? gzqk.getLrr() : gzqk.getLrrname());
            tvInputtime.setText(gzqk.getAddtime());
            tvBm.setText(gzqk.getBm());
            tvGzdd.setText(gzqk.getGgplace());
            tvStarttime.setText(gzqk.getGgtime_s());
            tvMs.setText(gzqk.getGgcontent());
            tvCbs.setText(gzqk.getCbs());
        }
        btnJr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (issave.equals("已保存")) {
                    requestData("3");
                } else {
                    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();


                }
            }
        });
        btnLk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (issave.equals("已保存")) {
                    requestData("2");
                } else {
                    showToast("该工作状态还未保存");
                }
            }
        });
        tvRz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GzqkgbActivity.this, GzqkrzckActivity.class);
                intent.putExtra("fid", gzqk.getId());
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {

    }

    private void requestData(String rwstate) {
        String json = "{ \"Rows\": [ { \"id\": \"" + gzqk.getId() + "\",\"rwstate\": \"" + rwstate + "\", \"operid\": \"" + (String) SPUtils.get(context, Contans.USERNAME, "") + "\"} ],\"Total\": 1}";

        Request<String> request = NoHttp.createStringRequest(IP + Contans.GZQKSBGB, RequestMethod.POST);
        request.setDefineRequestBodyForJson(json);
        NohttpUtils.getInstance().add(this, 0, request, callback1, true, true, "提交中...");
    }

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

            try {
                StatusInfo info = GsonUtils.GsonToBean(response.get(), StatusInfo.class);
                if (info != null) {
                    if (info.getTotal() != 0) {
                        if (info.getRows().get(0).getStatus().equals("1")) {
                            showToast("提交成功");
                            finish();
                        }
                    } else {
                        showToast("提交失败");
                    }
                } else {
                    showToast("数据出错");
                }
            } catch (Exception e) {
                e.printStackTrace();
                showToast("数据出错");
            }
        }

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

        }
    };

    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, "保存中...");

    }


    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("保存成功");
                                    requestData("3");
                                    setResult(100);
                                    finish();
                                }
                            } else {
                                showToast("保存失败");
                            }
                        } else {
                            showToast("数据出错");
                        }
                        break;
                    case 1:
                        break;

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

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

    private String getJson() {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Gzqk info = new Gzqk();
        info.setId(gzqk.getId());
        info.setGgtime_s(gzqk.getGgtime_s());
        info.setGgplace(gzqk.getGgplace());
        info.setGgcontent(gzqk.getGgcontent());
        info.setLrr(gzqk.getLrr());
        info.setAddtime(format.format(new Date()));
        info.setBm((String) SPUtils.get(context, Contans.BZMC, ""));
        info.setLrrname(gzqk.getLrrname());
        info.setCbs(gzqk.getCbs());
        info.setWonum(gzqk.getWonum());
        info.setWrdesc(gzqk.getWrdesc());
        info.setPjdesc(gzqk.getPjdesc());
        String json = "{\"Rows\":[" + GsonUtils.GsonString(info) + "],\"Total\":1}";
        return json;
    }

}
