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

import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.administrator.zhapp.BaseListViewAdapter.CommonAdapter;
import com.example.administrator.zhapp.BaseListViewAdapter.ViewHolder;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.BaseActivity;
import com.example.administrator.zhapp.adapter.SpinnerAdapter;
import com.example.administrator.zhapp.adapter.TlrkAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.FfjlInfo;
import com.example.administrator.zhapp.entity.HgInfo;
import com.example.administrator.zhapp.entity.LlrInfo;
import com.example.administrator.zhapp.entity.StatusInfo;
import com.example.administrator.zhapp.entity.WebServiceResult;
import com.example.administrator.zhapp.entity.WlffInfo;
import com.example.administrator.zhapp.utils.EditTextListener;
import com.example.administrator.zhapp.utils.GsonUtils;
import com.example.administrator.zhapp.utils.HttpListener;
import com.example.administrator.zhapp.utils.KsoapUtils;
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.WaitDialog;
import com.orhanobut.logger.Logger;
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;
import androidx.appcompat.app.AlertDialog;


/**
 * 移动仓库管理-退料入库
 */
public class TlrkActivity extends BaseActivity implements TlrkAdapter.CallBack {

    EditText et_gdh;
    Button btn_query;
    ListView lv;
    Spinner sp_hg;
    EditText et_hg;
    EditText et_tlsl;
    Button btn_save;
    Button btn_cancel;
    LinearLayout ll;
    EditText et_xm;
    Button btn_yth;
    ListView yth;
    Button btn_thjl;
    Button btnIt;

    private void findView() {
        et_gdh = findViewById(R.id.et_gdh);
        btn_query = findViewById(R.id.btn_query);
        lv = findViewById(R.id.lv);
        sp_hg = findViewById(R.id.sp_hg);
        et_hg = findViewById(R.id.et_hg);
        et_tlsl = findViewById(R.id.et_tlsl);
        btn_save = findViewById(R.id.btn_save);
        btn_cancel = findViewById(R.id.btn_cancel);
        ll = findViewById(R.id.ll);
        et_xm = findViewById(R.id.xm);
        btn_yth = findViewById(R.id.btn_yth);
        yth = findViewById(R.id.yth);
        btn_thjl = findViewById(R.id.btn_thjl);
        btnIt = findViewById(R.id.btn_it);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast(UiUtlis.getString(context, R.string.cancel));
            }
        });
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sp_hg.getChildCount() == 0) {
                    showToast("你没有选择项目");
                    return;
                }

                if (UiUtlis.isEmpty(et_tlsl)) {
                    showToast("退料数量不能为空");
                    return;
                }

                if (et_hg.isEnabled() && UiUtlis.isEmpty(et_hg)) {
                    showToast("货柜不能为空");
                    return;
                }

                matuReturn();
            }
        });
        btn_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UiUtlis.hideInput(et_gdh, context);
                if (!TextUtils.isEmpty(UiUtlis.getText(et_gdh)) || !TextUtils.isEmpty(UiUtlis.getText(et_xm))) {
                    sendRequestNohttp(UiUtlis.getString(context, R.string.loading));
                } else {
                    showToast("请输入工单号或项目号");
                }
            }
        });
        btn_yth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestAll();
                et_gdh.setText("");
                et_xm.setText("");
            }
        });
        btn_thjl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (seletcitem != -1 && lists.size() != 0 || yths.size() != 0) {
                    if (lv.getVisibility() == View.VISIBLE) {
                        requesthjl(lists.get(seletcitem).getItemnum());
                    } else {
                        requesthjl(yths.get(seletcitem).getItemnum());
                    }
                } else {
                    requesthjl(UiUtlis.getText(et_xm));
                }

            }
        });
        btnIt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_xm.setText("IT");
                et_xm.setSelection(2);
            }
        });
    }

    private View ythHeadView;
    private View headView;

    private WlffInfo info;
    private List<WlffInfo.Wlff> lists;
    private List<WlffInfo.Wlff> yths;
    private TlrkAdapter adapter;
    private LlrInfo.Llr llr;
    private EditTextListener etlisetener;

    private CommonAdapter<WlffInfo.Wlff> commonAdapter;

    private List<String> list;
    private List<HgInfo.Hg> hgs;
    private SpinnerAdapter<HgInfo.Hg> spAdapter;
    private String gdh;

    private WaitDialog loading;

    private List<FfjlInfo.Ffjl> thjls;

    private int seletcitem = -1;

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

    protected void initView() {
        findView();
    }

    public void initData() {

        setTitle("退料入库");
        setBack();

        loading = new WaitDialog(context, "加载中...");

        lists = new ArrayList<>();
        yths = new ArrayList<>();
        hgs = new ArrayList<>();
        thjls = new ArrayList<>();
        //et_gdh.setText("ZHP1363");

        ythHeadView = View.inflate(this, R.layout.yth_item, null);
        ythHeadView.findViewById(R.id.head).setVisibility(View.VISIBLE);
        ythHeadView.findViewById(R.id.cb).setEnabled(false);

        headView = View.inflate(this, R.layout.receive_list, null);
        headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
        headView.findViewById(R.id.ll_hj).setVisibility(View.VISIBLE);
        TextView tv = (TextView) headView.findViewById(R.id.tv_kjs);
        tv.setText("已退回数量");
        TextView tv1 = (TextView) headView.findViewById(R.id.tv_count);
        tv1.setText("数量");
        CheckBox cb = (CheckBox) headView.findViewById(R.id.cb_isprint);
        cb.setEnabled(false);
        headView.findViewById(R.id.cb_isprint).setEnabled(false);

        sp_hg.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                if (i == 1) {
                    et_hg.setEnabled(true);
                    et_hg.setHint("请输入货柜");
                } else {
                    et_hg.setEnabled(false);
                    et_hg.setHint("你已经选择了货柜");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

        etlisetener = new EditTextListener(TlrkActivity.this);
        et_tlsl.addTextChangedListener(etlisetener);
        et_tlsl.setEnabled(false);
        list = new ArrayList<>();
    }

    private void sendRequestNohttp(String msg) {
        Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.TLRK);
        request.set("wonum", UiUtlis.getText(et_gdh));
        request.set("itemnum", UiUtlis.getText(et_xm));
        NohttpUtils.getInstance().add(TlrkActivity.this, 0, request, callback, true, true, msg);
    }

    private void requestAll() {
        Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.TLRK);
        request.set("record", "all");
        NohttpUtils.getInstance().add(TlrkActivity.this, 3, request, callback, true, true, "加载中...");
    }


    private void matuReturn() {

        final WaitDialog dialog = new WaitDialog(context, "保存中...");

        dialog.show();

        KsoapUtils.callWebService(Contans.CK_BASE_IP + Contans.MATUSER_IP, "matuReturn", properties(), result -> {
            dialog.dismiss();
            if (result != null) {
                WebServiceResult webServiceResult = GsonUtils.GsonToBean(result.getProperty(0).toString(), WebServiceResult.class);
                if (webServiceResult.getStatus().equals("success")) {
                    showToast("保存成功");
                    if (lv.getVisibility() == View.VISIBLE) {
                        sendRequestNohttp(UiUtlis.getString(context, R.string.refresh));
                    } else {
                        requestAll();
                    }
                } else {
                    showToast("保存失败");
                }
            } else {
                showToast(UiUtlis.getString(context, R.string.data_error));
            }
        });
    }

    private void sendRequestNohttpPost() {
        Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.TLRKSC, RequestMethod.POST);
        request.setDefineRequestBodyForJson(toJson());
        Logger.v(toJson());
        NohttpUtils.getInstance().add(TlrkActivity.this, 1, request, callback, true, true, "上传中...");
    }

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

            switch (what) {
                case 0:
                    info = GsonUtils.GsonToBean(response.get(), WlffInfo.class);
                    if (info != null) {
                        if (info.getTotal() != 0) {
                            lists.clear();
                            lists.addAll(info.getRows());
                            initListData();
                        } else {
                            lists.clear();
                            seletcitem = -1;
                            if (adapter != null) {
                                if (lv.getHeaderViewsCount() == 1) {
                                    lv.removeHeaderView(headView);
                                }
                                adapter.notifyDataSetChanged();
                            }
                            showToast("没有查到符合条件记录");
                            ll.setVisibility(View.GONE);
                        }
                        clearView();
                    } else {
                        showToast(UiUtlis.getString(context, R.string.data_error));
                    }
                    break;

                case 1:
                    StatusInfo info = GsonUtils.GsonToBean(response.get(), StatusInfo.class);
                    if (info != null) {
                        String status = info.getRows().get(0).getStatus();
                        if (status.equals("1")) {
                            showToast(UiUtlis.getString(context, R.string.upload_success));
                            if (lv.getVisibility() == View.VISIBLE) {
                                sendRequestNohttp(UiUtlis.getString(context, R.string.refresh));
                            } else {
                                requestAll();
                            }
                        } else if (status.equals("0")) {
                            showToast(UiUtlis.getString(context, R.string.upload_error));
                        }
                    } else {
                        showToast(UiUtlis.getString(context, R.string.data_error));
                    }
                    break;

                //所有数据
                case 3:
                    WlffInfo wlffInfo = GsonUtils.GsonToBean(response.get(), WlffInfo.class);
                    if (wlffInfo != null) {
                        if (wlffInfo.getTotal() != 0) {
                            yths.clear();
                            yths.addAll(wlffInfo.getRows());
                            try {
                                setythList();
                            } catch (Exception e) {
                            }
                        } else {
                            yths.clear();
                            seletcitem = -1;
                            try {
                                setythList();
                            } catch (Exception e) {
                            }
                        }
                        clearView();
                    } else {
                        showToast("数据解析出错");
                    }
                    break;

                //退回记录
                case 4:
                    FfjlInfo ffjlInfo = GsonUtils.GsonToBean(response.get(), FfjlInfo.class);
                    if (ffjlInfo != null) {
                        if (!ffjlInfo.getTotal().equals("0")) {
                            thjls.clear();
                            thjls.addAll(ffjlInfo.getRows());
                            showThjl();
                        } else {
                            showToast("没有退回记录");
                            loading.dismiss();
                        }

                    } else {
                        showToast("数据出错");
                        loading.dismiss();
                    }
                    break;
            }
        }

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

        }
    };

    /**
     * 有数据，给listview填充数据
     */
    private void initListData() {
        ll.setVisibility(View.VISIBLE);
        lv.setVisibility(View.VISIBLE);
        yth.setVisibility(View.GONE);
        if (lv.getHeaderViewsCount() == 0) {
            lv.addHeaderView(headView, null, false);
        }
        if (adapter == null) {
            adapter = new TlrkAdapter(lists, this, this);
            lv.setAdapter(adapter);
//            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                @Override
//                public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) {
//                    if (position != 0) {
//                        if (lists.get(position - 1).isPrint() == true) {
//                            lists.get(position - 1).setPrint(false);
//                            Logger.v("执行到这里..item");
//                            clearView();
//                        } else {
//                            lists.get(position - 1).setPrint(true);
//                            for (int background = 0; background < lists.size(); background++) {
//                                if (background != position - 1) {
//                                    lists.get(background).setPrint(false);
//                                }
//                            }
//                            updateView(position - 1);
//                        }
//                    }
//                    adapter.notifyDataSetChanged();
//                }
//            });
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    //设置预留项目列表
    private void setythList() {
        ll.setVisibility(View.VISIBLE);
        lv.setVisibility(View.GONE);
        yth.setVisibility(View.VISIBLE);

        if (yth.getHeaderViewsCount() == 0) {
            yth.addHeaderView(ythHeadView, null, false);
        }
        if (commonAdapter == null) {
            commonAdapter = new CommonAdapter<WlffInfo.Wlff>(context, R.layout.yth_item, yths) {
                @Override
                protected void convert(ViewHolder viewHolder, final WlffInfo.Wlff item, final int position) {
                    viewHolder.setOnClickListener(R.id.cb, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            CheckBox cb = (CheckBox) v;
                            if (cb.isChecked() == false) {
                                yths.get(position).setPrint(false);
                                clearView();
                                seletcitem = -1;
                            } else {
                                seletcitem = position;
                                yths.get(position).setPrint(true);
                                for (int a = 0; a < yths.size(); a++) {
                                    if (a != position) {
                                        yths.get(a).setPrint(false);
                                    }
                                }
                                updateView(position);
//                                etlisetener.setNum(yths.get(position).getItemqty());
//                                etlisetener.setMsg("不能大于发放数量");
                            }
                            commonAdapter.notifyDataSetChanged();
                        }
                    });
                    viewHolder.setChecked(R.id.cb, item.isPrint());
                    viewHolder.setText(R.id.tv_xh, (position + 1) + "");
                    viewHolder.setText(R.id.tv_xm, item.getItemnum().equals(null) ? " " : item.getItemnum());//项目
                    viewHolder.setText(R.id.tv_hj, item.getBinnum().equals(null) ? " " : item.getBinnum());//货架
                    viewHolder.setText(R.id.tv_xmlx, "项目");//项目类型
                    viewHolder.setText(R.id.tv_ms, item.getDescription() == null ? " " : item.getDescription());//描述
                    viewHolder.setText(R.id.tv_count, item.getItemqty() == null ? " " : item.getItemqty());//数量
                    viewHolder.setText(R.id.tv_thsl, item.getCurbal() == null ? " " : item.getCurbal());//已退回数量
                    viewHolder.setText(R.id.tv_ffd, item.getIssueto() == null ? " " : item.getIssueto());//发放到
                    viewHolder.setText(R.id.tv_gd, item.getWonum() == null ? " " : item.getWonum());//工单
                    viewHolder.setText(R.id.tv_date, item.getTransdate() == null ? "" : item.getTransdate());//实际时间
                }
            };
            yth.setAdapter(commonAdapter);
        } else {
            commonAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View view) {
        CheckBox cb = (CheckBox) view;
        int index = (int) cb.getTag();
        //如果没有选中。
        if (cb.isChecked() == false) {
            lists.get(index).setPrint(false);
            Logger.v("执行到这里..click" + index);
            clearView();
            seletcitem = -1;
        } else {
            seletcitem = index;
            lists.get(index).setPrint(true);
            for (int a = 0; a < lists.size(); a++) {
                if (a != index) {
                    lists.get(a).setPrint(false);
                }
            }
            updateView(index);
        }
        adapter.notifyDataSetChanged();
    }

    private void clearView() {
        if (spAdapter != null) {
            hgs.clear();
            spAdapter.notifyDataSetChanged();
        }
        et_hg.setText("");
        et_tlsl.setText("");
        et_tlsl.setEnabled(false);
    }

    private void updateView(int i) {
        double num = 0;
        if (lv.getVisibility() == View.VISIBLE) {
            num = UiUtlis.getNum(lists.get(i).getItemqty()) - UiUtlis.getNum(lists.get(i).getCurbal());
        } else {
            num = UiUtlis.getNum(yths.get(i).getItemqty()) - UiUtlis.getNum(yths.get(i).getCurbal());
        }
        etlisetener.setNum(num + "");
        etlisetener.setMsg("退料数量不能大于余量");
        initSpinner(i);
        et_tlsl.setEnabled(true);
        et_tlsl.setText("");
    }

    private String toJson() {
        String json = "";

        if (lv.getVisibility() == View.VISIBLE) {
            for (int i = 0; i < lists.size(); i++) {
                if (lists.get(i).isPrint()) {
                    WlffInfo info = new WlffInfo();
                    List<WlffInfo.Wlff> list = new ArrayList<>();
                    info.setTotal(1);
                    WlffInfo.Wlff wlff = new WlffInfo.Wlff();
                    wlff = lists.get(i);
                    wlff.setTlsl(et_tlsl.getText().toString());
                    wlff.setEnterby((String) SPUtils.get(TlrkActivity.this, Contans.USERNAME, ""));
                    wlff.setInputhg(et_hg.getText().toString().trim().toUpperCase());
                    wlff.setSelecthg(sp_hg.getSelectedItem().toString());
                    wlff.setIssueto("");
                    list.add(wlff);
                    info.setRows(list);
                    json = GsonUtils.GsonString(info);
                }
            }
        } else {
            for (int i = 0; i < yths.size(); i++) {
                if (yths.get(i).isPrint()) {
                    WlffInfo info = new WlffInfo();
                    List<WlffInfo.Wlff> list = new ArrayList<>();
                    info.setTotal(1);
                    WlffInfo.Wlff wlff = new WlffInfo.Wlff();
                    wlff = yths.get(i);
                    wlff.setTlsl(et_tlsl.getText().toString());
                    wlff.setEnterby((String) SPUtils.get(TlrkActivity.this, Contans.USERNAME, ""));
                    wlff.setInputhg(et_hg.getText().toString().trim().toUpperCase());
                    wlff.setSelecthg(sp_hg.getSelectedItem().toString());
                    wlff.setIssueto("");
                    list.add(wlff);
                    info.setRows(list);
                    json = GsonUtils.GsonString(info);
                }
            }
        }
        return json;
    }

    private List<String> properties() {

        List<String> properties = new ArrayList<>();

        if (lv.getVisibility() == View.VISIBLE) {
            for (int i = 0; i < lists.size(); i++) {
                if (lists.get(i).isPrint()) {
                    WlffInfo info = new WlffInfo();
                    List<WlffInfo.Wlff> list = new ArrayList<>();
                    info.setTotal(1);
                    WlffInfo.Wlff wlff = new WlffInfo.Wlff();
                    wlff = lists.get(i);

                    HgInfo.Hg hg = (HgInfo.Hg) sp_hg.getSelectedItem();
                    properties.add(TextUtils.isEmpty(UiUtlis.getText(et_hg)) ? hg.getBinnum() : UiUtlis.getText(et_hg));//货柜号
                    properties.add(UiUtlis.getText(et_tlsl));//退回数量
                    properties.add(wlff.getTlid());//发放ID
                    properties.add((String) SPUtils.get(context, Contans.USERNAME, ""));//退回人
                }
            }
        } else {
            for (int i = 0; i < yths.size(); i++) {
                if (yths.get(i).isPrint()) {
                    WlffInfo info = new WlffInfo();
                    List<WlffInfo.Wlff> list = new ArrayList<>();
                    info.setTotal(1);
                    WlffInfo.Wlff wlff = new WlffInfo.Wlff();
                    wlff = yths.get(i);

                    HgInfo.Hg hg = (HgInfo.Hg) sp_hg.getSelectedItem();
                    properties.add(TextUtils.isEmpty(UiUtlis.getText(et_hg)) ? hg.getBinnum() : UiUtlis.getText(et_hg));//货柜号
//                    properties.add("批次号");//批次号
                    properties.add(UiUtlis.getText(et_tlsl));//退回数量
                    properties.add(wlff.getTlid());//发放ID
                    properties.add((String) SPUtils.get(context, Contans.USERNAME, ""));//退回人
                }
            }
        }
        return properties;
    }

    private void initSpinner(int i) {
        list.clear();
        hgs.clear();
        if (lv.getVisibility() == View.VISIBLE) {
//            list.add(lists.get(i).getBinnum());

            HgInfo.Hg hg = new HgInfo.Hg();
            hg.setBinnum(lists.get(i).getBinnum());
            hgs.add(hg);
        } else {
//            list.add(yths.get(i).getBinnum());
            HgInfo.Hg hg = new HgInfo.Hg();
            hg.setBinnum(yths.get(i).getBinnum());
            hgs.add(hg);
        }

        HgInfo.Hg hg = new HgInfo.Hg();
        hg.setBinnum("列表中没有可选货柜");
        hgs.add(hg);
        if (spAdapter == null) {
            spAdapter = new SpinnerAdapter<HgInfo.Hg>(hgs, context);
            sp_hg.setAdapter(spAdapter);
        } else {
            sp_hg.setSelection(0);
            spAdapter.notifyDataSetChanged();
        }
    }

    private void requesthjl(String itemnum) {
        loading.show();
        Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.WLFFJL);
        request.set("itemnum", itemnum);
        request.set("status", "2");
        NohttpUtils.getInstance().add(this, 4, request, callback, true, true, "加载中...");
    }

    private void showThjl() {

        loading.dismiss();

        final View view = View.inflate(context, R.layout.fljl_list, null);
        ListView lv = (ListView) view.findViewById(R.id.lv);

        CommonAdapter<FfjlInfo.Ffjl> ffjhadapter = new CommonAdapter<FfjlInfo.Ffjl>(context, R.layout.ffjl_item, thjls) {
            @Override
            protected void convert(ViewHolder viewHolder, FfjlInfo.Ffjl item, int position) {
                viewHolder.setText(R.id.tv_xh, (position + 1) + "");
                viewHolder.setText(R.id.tv_ms, item.getDescription());
                viewHolder.setText(R.id.tv_date, item.getTransdate());
                viewHolder.setText(R.id.tv_ffd, item.getIssueto());
                viewHolder.setText(R.id.tv_name, item.getDisplayname());
                viewHolder.setText(R.id.tv_gd, item.getRefwo());
                viewHolder.setText(R.id.tv_wz, item.getLocation());
                viewHolder.setText(R.id.tv_zc, item.getAssetnum());
                viewHolder.setText(R.id.tv_sl, item.getQuantity());
                viewHolder.setText(R.id.tv_dwcb, item.getUnitcost());
                viewHolder.setText(R.id.tv_hcb, item.getLinecost());
                viewHolder.setText(R.id.tv_jylx, item.getIssuetype());
                viewHolder.setText(R.id.tv_hg, item.getBinnum());
                viewHolder.setText(R.id.tv_srr, item.getEnterby());
                viewHolder.setText(R.id.tv_xm, item.getItemnum());
            }
        };
        View head = View.inflate(context, R.layout.ffjl_item, null);
        head.findViewById(R.id.head).setVisibility(View.VISIBLE);
        lv.addHeaderView(head, null, false);
        lv.setAdapter(ffjhadapter);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(view);
        builder.setCancelable(false);
        builder.setTitle("退回记录");
        builder.setPositiveButton("返回", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }
}