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

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.BaseActivity;
import com.example.administrator.zhapp.adapter.XsDqgzZxListAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.XsDqGzZxListBean;
import com.example.administrator.zhapp.entity.XsRequestInfo;
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.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.List;


/**
 * 巡视定期工作执行
 */
public class XsDqgzZxActivity extends BaseActivity {

    ListView lv;
    private List<XsDqGzZxListBean.DataBean> datas;

    private XsDqgzZxListAdapter adapter;

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

    @Override
    protected void initView() {
        lv = findViewById(R.id.lv);
        setTitle("定期工作执行");
        setBack();
        datas = new ArrayList<>();
        requestDatas();
    }

    @Override
    protected void initData() {

    }

    private void requestDatas() {


        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 {
                    XsDqGzZxListBean xsDqGzListBean = GsonUtils.GsonToBean(response.get(), XsDqGzZxListBean.class);
                    if (xsDqGzListBean != null) {
                        if (xsDqGzListBean.getState() == 1) {
                            showToast(xsDqGzListBean.getMsg());
                            datas.clear();
                            datas.addAll(xsDqGzListBean.getData());
                            setAdapter();
                        } else {
                            showToast(xsDqGzListBean.getMsg());
                            datas.clear();
                            setAdapter();
                        }
                    } else {
                        showToast(R.string.data_error);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

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

            }
        });
    }

    private void setAdapter() {

        if (adapter == null) {
            adapter = new XsDqgzZxListAdapter(context, datas);

            lv.setAdapter(adapter);

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                    if (datas.get(position).getZxzt().equals("0") || datas.get(position).getZxzt().equals("3")) {

                        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 item) {

                                Intent intent = null;

                                switch (item) {
                                    case 0:
                                        intent = new Intent(XsDqgzZxActivity.this, XsDqgzWcActivity.class);
                                        break;
                                    case 1:
                                        intent = new Intent(XsDqgzZxActivity.this, XsDqgzYqActivity.class);
                                        break;
                                    case 2:
                                        intent = new Intent(XsDqgzZxActivity.this, XsDqgzQxActivity.class);
                                        break;
                                }

                                if (intent != null) {
                                    intent.putExtra(Contans.KEY_DQGZ, datas.get(position));
                                    startActivity(intent);
                                }
                            }
                        });
                        dialog.setTvTitle("选择操作");

                        dialog.show();
                    } else {
                        showToast("该工作不可变更");
                    }
                }
            });

        } else {
            adapter.notifyDataSetChanged();
        }
    }

    private String createJson() {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("DQGZ_ZXJL_GET");
        info.setZc((String) SPUtils.get(context, Contans.BZBH, ""));
        info.setJz(Contans.YXCB_ZY_ID);
        String json = GsonUtils.GsonString(info);
        return json;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        requestDatas();
    }

}
