package com.ifavine.pay.utils;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ifavine.pay.R;
import com.ifavine.pay.adapter.MonthRecordReportAdapter;
import com.ifavine.pay.adapter.ReportMonthsAdapter;
import com.ifavine.pay.adapter.SpinnerAdapter;
import com.ifavine.pay.api.APIWrapper;
import com.ifavine.pay.common.AppContext;
import com.ifavine.pay.common.Constants;
import com.ifavine.pay.view.AmountView;
import com.ifavine.pay.view.datepicker.DateUtil;
import com.ifavine.pay.vo.CanteenWindow;
import com.ifavine.pay.vo.CommonListResponse;
import com.ifavine.pay.vo.CommonResponse;
import com.ifavine.pay.vo.MonthRecordReport;
import com.ifavine.pay.vo.VersionBean;
import com.ifavine.pay.vo.WindowCostAmount;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DialogMainSetUtils {
    private static Dialog tipDialog;
    private static TextView tipTextView;

    public static final int OK = 0xff888801;
    public static final int CANCEL = 0xff888802;

    public static void dismissTipDialog() {
        if (tipDialog != null) {
            tipDialog.dismiss();
            tipDialog = null;
        }
    }

    public static boolean isShowing() {
        if (tipDialog == null) {
            return false;
        }
        return tipDialog.isShowing();
    }

    private static Gson gson = new Gson();

    /**
     * showConfirmDialog
     */
    public static Dialog showRSTSetDialog(final Context mContext, String title, final View.OnClickListener okListener,
                                          View.OnClickListener cancelListener, boolean cancelable) {
        View view = View.inflate(mContext, R.layout.dialog_rst_num_set, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_common);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view,
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        Button btn_ok = (Button) view.findViewById(R.id.dialog_ok);
        Button btn_cancel = (Button) view.findViewById(R.id.dialog_cancel);
        Button btn_clear = (Button) view.findViewById(R.id.dialog_clear);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);
        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        final EditText dialog_msg = (EditText) view.findViewById(R.id.dialog_msg);

        final Spinner spinner1 = (Spinner) view.findViewById(R.id.dialog_sp1);
        final Spinner spinner2 = (Spinner) view.findViewById(R.id.dialog_sp2);
        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }
        //set cancel listener
        if (cancelListener != null) {
            btn_cancel.setOnClickListener(cancelListener);
        } else {
            btn_cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        //set confirm listener
        //spinner 数据来源
        String data = SPUtil.getInstance().readString("CanteenWindows");
        final List<CanteenWindow> datas = gson.fromJson(data, new TypeToken<List<CanteenWindow>>() {
        }.getType());
        btn_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!SPUtil.getInstance().readString("pwd").equals(MD5Util.MD5(dialog_msg.getText().toString().trim()))) {
                    AppContext.showToast("管理员密码不正确");
                    return;
                }
                DialogUtil.showLoadingDialog(mContext, true, "处理中");
                String CanTeenName = datas.get(spinner1.getSelectedItemPosition()).CanteenName;
                String CanTeenCode = datas.get(spinner1.getSelectedItemPosition()).CanteenCode;
                String WindowName = spinner2.getSelectedItem().toString();
                SaveCanteenWindows(dialog, CanTeenCode, WindowName, CanTeenName, okListener);
            }
        });

        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog_msg.setText(null);
            }
        });


        final List<String> first = new ArrayList<>();
        final List<String> second = new ArrayList<>();
        for (CanteenWindow d : datas) {
            first.add(d.CanteenName);
        }
        List<CanteenWindow.Window> windows = datas.get(0).Windows;
        for (CanteenWindow.Window w : windows) {
            second.add(w.WindowName);
        }
        Object options[] = first.toArray();
        SpinnerAdapter adapter = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options);
        spinner1.setAdapter(adapter);

        Object options2[] = second.toArray();
        final SpinnerAdapter adapter2 = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options2);
        spinner2.setAdapter(adapter2);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                List<CanteenWindow.Window> windows = datas.get(i).Windows;
                second.clear();
                for (CanteenWindow.Window w : windows) {
                    second.add(w.WindowName);
                }
                adapter2.setData(second.toArray());
                adapter2.notifyDataSetChanged();
            }

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

            }
        });

        dialog.show();

        return dialog;
    }


    //线路处理
    private static void SaveCanteenWindows(final Dialog dialog, final String CanTeenCode, final String WindowName, final String CanTeenName, final View.OnClickListener l) {
        String Mac = SPUtil.getInstance().readString("Mac");
        Call<CommonResponse> call =
                APIWrapper.getInstance().SaveCanteenWindows(Mac, CanTeenCode, WindowName);
        call.enqueue(new Callback<CommonResponse>() {
            @Override
            public void onResponse(Call<CommonResponse> call,
                                   Response<CommonResponse> response) {
                DialogUtil.dismissTipDialog();
                if (response.isSuccessful()) {
                    if (response.body().Status != 200) {
                        AppContext.showToast(response.body().Message);
                        return;
                    }
                    SPUtil.getInstance().writeString("CanTeenCode", CanTeenCode);
                    SPUtil.getInstance().writeString("WindowName", WindowName);
                    SPUtil.getInstance().writeString("CanTeenName", CanTeenName);
                    AppContext.showToast(response.body().Message);
                    l.onClick(null);
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            dialog.dismiss();
                        }
                    }, 2000);
                } else {
                    AppContext.showToast("请求失败，请重试");
                }
            }

            @Override
            public void onFailure(Call<CommonResponse> call, Throwable t) {
                AppContext.showToast("请求失败，请重试");
                DialogUtil.dismissTipDialog();
            }
        });
    }


    /**
     * showConfirmDialog
     *
     * @param imageRes   imageID
     * @param okName     set button name
     * @param cancelName set button name
     */
    public static Dialog showInputDialog(Context mContext, Integer imageRes, String title,
                                         final String msg, String okName, String cancelName, final View.OnClickListener okListener,
                                         View.OnClickListener cancelListener, boolean cancelable) {
        View view = View.inflate(mContext, R.layout.dialog_common_input_confirm, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_style);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view,
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        Button btn_ok = (Button) view.findViewById(R.id.dialog_ok);
        Button btn_cancel = (Button) view.findViewById(R.id.dialog_cancel);
        final EditText tv_msg = (EditText) view.findViewById(R.id.dialog_msg);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);
        ImageView iv = (ImageView) view.findViewById(R.id.dialog_iv);
        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        if (msg != null) {
            tv_msg.setHint(msg);
            tv_msg.setVisibility(View.VISIBLE);
        }
        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }
        btn_ok.setText(okName);
        btn_cancel.setText(cancelName);
        //set image
        if (imageRes != null) {
            iv.setImageResource(imageRes);
        } else {
            iv.setVisibility(View.GONE);
        }
        //set cancel listener
        if (cancelListener != null) {
            btn_cancel.setOnClickListener(cancelListener);
        } else {
            btn_cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        //set confirm listener
        if (okListener != null) {
            btn_ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if ("".equals(tv_msg.getText().toString().trim())) {
                        AppContext.showToast("请输入" + msg);
                        return;
                    }
                    okListener.onClick(tv_msg);
                }
            });
        } else {
            btn_ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return dialog;
    }


    public static Dialog showChangePwdDialog(final Context mContext, final String title, final View.OnClickListener okListener,
                                             View.OnClickListener cancelListener, boolean cancelable) {
        View view = View.inflate(mContext, R.layout.dialog_pwd_set, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_style);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view,
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        Button btn_ok = (Button) view.findViewById(R.id.dialog_ok);
        Button btn_cancel = (Button) view.findViewById(R.id.dialog_cancel);
        final EditText et1 = (EditText) view.findViewById(R.id.dialog_et1);
        final EditText et2 = (EditText) view.findViewById(R.id.dialog_et2);
        final EditText et3 = (EditText) view.findViewById(R.id.dialog_et3);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);
        final TextView tips = (TextView) view.findViewById(R.id.tips);
        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);

        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }

        //set cancel listener
        if (cancelListener != null) {
            btn_cancel.setOnClickListener(cancelListener);
        } else {
            btn_cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        //set confirm listener
        btn_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String old = et1.getText().toString().trim();
                String newP = et2.getText().toString().trim();
                String comP = et3.getText().toString().trim();
                if (TextUtils.isEmpty(old) || TextUtils.isEmpty(newP) || TextUtils.isEmpty(comP)) {
                    tips.setText("密码都不能为空");
                    return;
                }
                if (!SPUtil.getInstance().readString("pwd").equals(MD5Util.MD5(old))) {
                    tips.setText("管理员密码不正确");
                    return;
                }
                if (!newP.equals(comP) && !newP.equals("")) {
                    tips.setText("确认密码不一致");
                    return;
                }
                SPUtil.getInstance().writeString("pwd", MD5Util.MD5(comP));
                tips.setTextColor(mContext.getResources().getColorStateList(R.color.green));
                tips.setText("密码修改成功!");
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dialog.dismiss();
                    }
                }, 2000);
            }
        });
        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return dialog;
    }

    static Handler handler = new Handler();

    public static Dialog showSetAmtDialog(final Context mContext, final String title, final View.OnClickListener okListener,
                                          View.OnClickListener cancelListener, boolean cancelable) {
        //  数据来源
        String data = SPUtil.getInstance().readString("WindowCostAmounts");
        final List<WindowCostAmount> DATA = gson.fromJson(data, new TypeToken<List<WindowCostAmount>>() {
        }.getType());

        View view = View.inflate(mContext, R.layout.dialog_amt_set, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_common);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view);
        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = 1800;
        params.height = 920;
        dialog.getWindow().setAttributes(params);
        Button btn_ok = (Button) view.findViewById(R.id.dialog_ok);
        Button btn_cancel = (Button) view.findViewById(R.id.dialog_cancel);
        final TextView tv_CanTeenName = (TextView) view.findViewById(R.id.tv_CanTeenName);
        final TextView tv_WindowName = (TextView) view.findViewById(R.id.tv_WindowName);
        tv_CanTeenName.setText(SPUtil.getInstance().readString("CanTeenName"));
        tv_WindowName.setText(SPUtil.getInstance().readString("WindowName"));
        final TextView tv_date1 = (TextView) view.findViewById(R.id.tv_date1);
        final TextView tv_date2 = (TextView) view.findViewById(R.id.tv_date2);
        final TextView tv_date3 = (TextView) view.findViewById(R.id.tv_date3);
        final TextView tv_date4 = (TextView) view.findViewById(R.id.tv_date4);
        final TextView tv_date5 = (TextView) view.findViewById(R.id.tv_date5);
        final TextView tv_date6 = (TextView) view.findViewById(R.id.tv_date6);
        final TextView tv_date7 = (TextView) view.findViewById(R.id.tv_date7);
        final TextView tv_date8 = (TextView) view.findViewById(R.id.tv_date8);
        AmountView amt1 = (AmountView) view.findViewById(R.id.amt1);
        AmountView amt2 = (AmountView) view.findViewById(R.id.amt2);
        AmountView amt3 = (AmountView) view.findViewById(R.id.amt3);
        AmountView amt4 = (AmountView) view.findViewById(R.id.amt4);
        AmountView amt5 = (AmountView) view.findViewById(R.id.amt5);
        AmountView amt6 = (AmountView) view.findViewById(R.id.amt6);
        AmountView amt7 = (AmountView) view.findViewById(R.id.amt7);
        AmountView amt8 = (AmountView) view.findViewById(R.id.amt8);
        final EditText et1 = (EditText) view.findViewById(R.id.et_amt1);
        final EditText et2 = (EditText) view.findViewById(R.id.et_amt2);
        final EditText et3 = (EditText) view.findViewById(R.id.et_amt3);
        final EditText et4 = (EditText) view.findViewById(R.id.et_amt4);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);

        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);

        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }


        //set cancel listener
        if (cancelListener != null) {
            btn_cancel.setOnClickListener(cancelListener);
        } else {
            btn_cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        //set confirm listener

        btn_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                DATA.get(0).StartTime = tv_date1.getText().toString();
                DATA.get(0).EndTime = tv_date2.getText().toString();
                DATA.get(1).StartTime = tv_date3.getText().toString();
                DATA.get(1).EndTime = tv_date4.getText().toString();
                DATA.get(2).StartTime = tv_date5.getText().toString();
                DATA.get(2).EndTime = tv_date6.getText().toString();
                DATA.get(3).StartTime = tv_date7.getText().toString();
                DATA.get(3).EndTime = tv_date8.getText().toString();
                DialogUtil.showInputDialog(mContext, null, "请输入管理员密码", "请输入管理员密码", "确定", "取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String DinnerCost = gson.toJson(DATA);
                        DialogUtil.showLoadingDialog(mContext, true, "处理中");
                        GetWindowCostAmount(dialog, DinnerCost, DATA, okListener);
                    }
                }, null, true);
            }
        });

        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        tv_date1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date1, Calendar.getInstance());
            }
        });
        tv_date2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date2, Calendar.getInstance());
            }
        });
        tv_date3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date3, Calendar.getInstance());
            }
        });
        tv_date4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date4, Calendar.getInstance());
            }
        });
        tv_date5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date5, Calendar.getInstance());
            }
        });
        tv_date6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date6, Calendar.getInstance());
            }
        });
        tv_date7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date7, Calendar.getInstance());
            }
        });
        tv_date8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date8, Calendar.getInstance());
            }
        });

        for (int i = 0; i < 4; i++) {
            WindowCostAmount w = DATA.get(i);
            if (i == 0) {
                amt1.setAmount(w.DinnerAmount);
                amt2.setAmount(w.AllowanceAmount);
                et1.setText((w.DinnerAmount - w.AllowanceAmount) + "");
                tv_date1.setText(w.StartTime);
                tv_date2.setText(w.EndTime);
            } else if (i == 1) {
                amt3.setAmount(w.DinnerAmount);
                amt4.setAmount(w.AllowanceAmount);
                et2.setText((w.DinnerAmount - w.AllowanceAmount) + "");
                tv_date3.setText(w.StartTime);
                tv_date4.setText(w.EndTime);
            } else if (i == 2) {
                amt5.setAmount(w.DinnerAmount);
                amt6.setAmount(w.AllowanceAmount);
                et3.setText((w.DinnerAmount - w.AllowanceAmount) + "");
                tv_date5.setText(w.StartTime);
                tv_date6.setText(w.EndTime);
            } else if (i == 3) {
                amt7.setAmount(w.DinnerAmount);
                amt8.setAmount(w.AllowanceAmount);
                et4.setText((w.DinnerAmount - w.AllowanceAmount) + "");
                tv_date7.setText(w.StartTime);
                tv_date8.setText(w.EndTime);
            }
        }
        //保存操作
        amt1.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, double amount) {
                DATA.get(0).DinnerAmount = amount;
                et1.setText((DATA.get(0).DinnerAmount - DATA.get(0).AllowanceAmount) + "");
            }
        });
        amt2.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, double amount) {
                DATA.get(0).AllowanceAmount = amount;
                et1.setText((DATA.get(0).DinnerAmount - DATA.get(0).AllowanceAmount) + "");
            }
        });
        //保存操作
        amt3.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, double amount) {
                DATA.get(1).DinnerAmount = amount;
                et2.setText((DATA.get(1).DinnerAmount - DATA.get(1).AllowanceAmount) + "");
            }
        });
        amt4.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, double amount) {
                DATA.get(1).AllowanceAmount = amount;
                et2.setText((DATA.get(1).DinnerAmount - DATA.get(1).AllowanceAmount) + "");
            }
        });
        //保存操作
        amt5.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, double amount) {
                DATA.get(2).DinnerAmount = amount;
                et3.setText((DATA.get(2).DinnerAmount - DATA.get(2).AllowanceAmount) + "");
            }
        });
        amt6.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, double amount) {
                DATA.get(2).AllowanceAmount = amount;
                et3.setText((DATA.get(2).DinnerAmount - DATA.get(2).AllowanceAmount) + "");
            }
        });      //保存操作
        amt7.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, double amount) {
                DATA.get(3).DinnerAmount = amount;
                et4.setText((DATA.get(3).DinnerAmount - DATA.get(3).AllowanceAmount) + "");
            }
        });
        amt8.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, double amount) {
                DATA.get(3).AllowanceAmount = amount;
                et4.setText((DATA.get(3).DinnerAmount - DATA.get(3).AllowanceAmount) + "");
            }
        });

        dialog.show();
        return dialog;
    }

    private static void GetWindowCostAmount(final Dialog dialog, String DinnerCost, final List<WindowCostAmount> DATA, final View.OnClickListener l) {

        String Mac = SPUtil.getInstance().readString("Mac");
        Call<CommonResponse> call =
                APIWrapper.getInstance().SaveWindowCostAmount(Mac, DinnerCost);
        call.enqueue(new Callback<CommonResponse>() {
            @Override
            public void onResponse(Call<CommonResponse> call,
                                   Response<CommonResponse> response) {
                if (response.isSuccessful()) {
                    if (response.body() == null) {
                        return;
                    }
                    if (response.body().Status != 200) {
                        return;
                    }
                    String data = gson.toJson(DATA);
                    SPUtil.getInstance().writeString("WindowCostAmounts", data);
                    DialogUtil.dismissTipDialog();
                    AppContext.showToast(response.body().Message);
                    l.onClick(null);
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            dialog.dismiss();
                        }
                    }, 2000);
                }
            }

            @Override
            public void onFailure(Call<CommonResponse> call, Throwable t) {
                DialogUtil.dismissTipDialog();
                AppContext.showToast("请求失败，请重试");
            }
        });
    }

    static int mYear;
    static int mMonth;
    static int mDay;

    /**
     * 本机消费记录
     *
     * @param mContext
     * @param title
     * @param okListener
     * @param cancelable
     * @return
     */
    public static Dialog showReportLocalDialog(final Context mContext, final String title, final View.OnClickListener okListener,
                                               boolean cancelable) {

        View view = View.inflate(mContext, R.layout.dialog_report_local, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_common);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view);
        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = 1800;
        params.height = 1100;
        dialog.getWindow().setAttributes(params);
        Button btn_search = (Button) view.findViewById(R.id.dialog_search);
        Button btn_close = (Button) view.findViewById(R.id.dialog_close);


        final TextView tv_date1 = (TextView) view.findViewById(R.id.tv_date1);
        final TextView tv_date2 = (TextView) view.findViewById(R.id.tv_date2);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);

        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        Spinner spinner1 = (Spinner) view.findViewById(R.id.spinner1);
        Spinner spinner2 = (Spinner) view.findViewById(R.id.spinner2);

        String options[] = mContext.getResources().getStringArray(R.array.main_report_type);
        SpinnerAdapter adapter = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options);
        spinner1.setAdapter(adapter);

        String options2[] = mContext.getResources().getStringArray(R.array.main_report_record_type);
        SpinnerAdapter adapter2 = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options2);
        spinner2.setAdapter(adapter2);

        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }

        //set confirm listener
        if (okListener != null) {

        } else {
            btn_search.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        final Calendar startcal = Calendar.getInstance();
        final Calendar c = Calendar.getInstance();
        tv_date1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String date = tv_date1.getText().toString();
                if (!TextUtils.isEmpty(date)) {
                    c.setTimeInMillis(DateUtil.formatDateStr(date, "yyyy-MM-dd").getTime());
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                } else {
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                }
                DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        startcal.set(Calendar.YEAR, year);
                        startcal.set(Calendar.MONTH, month);
                        startcal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        long mis = startcal.getTimeInMillis();
                        tv_date1.setText(DateUtil.formatDate(mis, "yyyy-MM-dd"));
                    }
                }, mYear, mMonth, mDay);
                datePickerDialog.show();
            }
        });

        tv_date2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String date = tv_date2.getText().toString();
                if (!TextUtils.isEmpty(date)) {
                    c.setTimeInMillis(DateUtil.formatDateStr(date, "yyyy-MM-dd").getTime());
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                } else {
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                }
                DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        startcal.set(Calendar.YEAR, year);
                        startcal.set(Calendar.MONTH, month);
                        startcal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        long mis = startcal.getTimeInMillis();
                        tv_date2.setText(DateUtil.formatDate(mis, "yyyy-MM-dd"));
                    }
                }, mYear, mMonth, mDay);
                datePickerDialog.show();
            }
        });

        dialog.show();
        return dialog;
    }


    public static Dialog showReportRecordDialog(final Context mContext, final String title, final View.OnClickListener okListener,
                                                boolean cancelable) {

        View view = View.inflate(mContext, R.layout.dialog_report_record, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_common);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view);
        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = 1800;
        params.height = 1100;
        dialog.getWindow().setAttributes(params);
        Button btn_search = (Button) view.findViewById(R.id.dialog_search);
        Button btn_close = (Button) view.findViewById(R.id.dialog_close);


        final TextView tv_date1 = (TextView) view.findViewById(R.id.tv_date1);
        final TextView tv_date2 = (TextView) view.findViewById(R.id.tv_date2);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);

        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        Spinner spinner1 = (Spinner) view.findViewById(R.id.spinner1);
        Spinner spinner2 = (Spinner) view.findViewById(R.id.spinner2);
        Spinner spinner3 = (Spinner) view.findViewById(R.id.spinner3);

        String options[] = mContext.getResources().getStringArray(R.array.main_report_type);
        SpinnerAdapter adapter = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options);
        spinner1.setAdapter(adapter);

        String options2[] = mContext.getResources().getStringArray(R.array.main_report_record_type);
        SpinnerAdapter adapter2 = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options2);
        spinner2.setAdapter(adapter2);

        String options3[] = mContext.getResources().getStringArray(R.array.main_rst_num_options);
        SpinnerAdapter adapter3 = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options3);
        spinner3.setAdapter(adapter3);

        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }

        //set confirm listener
        if (okListener != null) {

        } else {
            btn_search.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        final Calendar startcal = Calendar.getInstance();
        final Calendar c = Calendar.getInstance();
        tv_date1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String date = tv_date1.getText().toString();
                if (!TextUtils.isEmpty(date)) {
                    c.setTimeInMillis(DateUtil.formatDateStr(date, "yyyy-MM-dd").getTime());
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                } else {
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                }
                DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        startcal.set(Calendar.YEAR, year);
                        startcal.set(Calendar.MONTH, month);
                        startcal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        long mis = startcal.getTimeInMillis();
                        tv_date1.setText(DateUtil.formatDate(mis, "yyyy-MM-dd"));
                    }
                }, mYear, mMonth, mDay);
                datePickerDialog.show();
            }
        });

        tv_date2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String date = tv_date2.getText().toString();
                if (!TextUtils.isEmpty(date)) {
                    c.setTimeInMillis(DateUtil.formatDateStr(date, "yyyy-MM-dd").getTime());
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                } else {
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                }
                DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        startcal.set(Calendar.YEAR, year);
                        startcal.set(Calendar.MONTH, month);
                        startcal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        long mis = startcal.getTimeInMillis();
                        tv_date2.setText(DateUtil.formatDate(mis, "yyyy-MM-dd"));
                    }
                }, mYear, mMonth, mDay);
                datePickerDialog.show();
            }
        });

        dialog.show();
        return dialog;
    }

    static int mCurrent = 0;
    static int mCount = 10;
    static boolean isLoadMore = false;

    public static Dialog showReportSumDialog(final Context mContext, final String title, final View.OnClickListener okListener,
                                             boolean cancelable) {

        View view = View.inflate(mContext, R.layout.dialog_report_sum, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_common);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view);
        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = 1800;
        params.height = 1100;
        dialog.getWindow().setAttributes(params);
        Button btn_search = (Button) view.findViewById(R.id.dialog_search);
        Button btn_close = (Button) view.findViewById(R.id.dialog_close);


        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);
        Spinner spinner1 = (Spinner) view.findViewById(R.id.sp_year);
        Spinner spinner2 = (Spinner) view.findViewById(R.id.spinner2);
        final LRecyclerView mLRecyclerView = (LRecyclerView) view.findViewById(R.id.mLRecyclerView);
        final MonthRecordReportAdapter adapter = new MonthRecordReportAdapter(mContext);
        LRecyclerViewAdapter mLRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        mLRecyclerView.setLayoutManager(new LinearLayoutManager(mContext) {
        });
        mLRecyclerView.setAdapter(mLRecyclerViewAdapter);

        mLRecyclerView.setPullRefreshEnabled(true);

        mLRecyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrent = 1;
                //   getMessageList();
            }
        });
        mLRecyclerView.refresh();

        mLRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (mCurrent * Constants.PAGE_SIZE < mCount) {
                    mCurrent++;
                    isLoadMore = true;
                    //getMessageList();
                } else {
                    mLRecyclerView.setNoMore(true);
                    isLoadMore = false;
                }
            }
        });


        final List<MonthRecordReport> beanList = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            MonthRecordReport bean = new MonthRecordReport();
            bean.AllowanceAmount = "链接" + i;
            bean.asConsumption = "类型" + i;
            bean.CanteenName = "version_description" + i;
            bean.WindowName = "version_id" + i;
            beanList.add(bean);
        }
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mLRecyclerView.refreshComplete(1);
                adapter.setDataList(beanList);
            }
        }, 2000);

        String options[] = mContext.getResources().getStringArray(R.array.main_year);
        SpinnerAdapter adapter1 = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options);
        spinner1.setAdapter(adapter1);

        String options2[] = mContext.getResources().getStringArray(R.array.main_months);
        SpinnerAdapter adapter2 = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options2);
        spinner2.setAdapter(adapter2);

        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }

        //set confirm listener
        if (okListener != null) {

        } else {
            btn_search.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });


        dialog.show();
        return dialog;
    }

}
