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.LocalRecordReportAdapter;
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.LocalRecordReport;
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() == null) {
                        AppContext.showToast("请求失败，请重试");
                        return;
                    }
                    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) {
                DialogUtil.dismissTipDialog();
                if (response.isSuccessful()) {
                    if (response.body() == null) {
                        AppContext.showToast("请求失败，请重试");
                        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 t0 = (TextView) view.findViewById(R.id.tv_Total);
        final TextView t1 = (TextView) view.findViewById(R.id.tv_TotalDinnerAmount);
        final TextView t2 = (TextView) view.findViewById(R.id.tv_TotalAllowanceAmount);
        final TextView t3 = (TextView) view.findViewById(R.id.tv_TotalConsumption);
        final List<TextView> list = new ArrayList<>();
        list.add(t0);
        list.add(t1);
        list.add(t2);
        list.add(t3);
        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);

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

        tv_date1.setText(DateUtil.formatDate(new Date().getTime(), "yyyy-MM-dd"));
        tv_date2.setText(DateUtil.formatDate(new Date().getTime(), "yyyy-MM-dd"));

        final LRecyclerView mLRecyclerView = (LRecyclerView) view.findViewById(R.id.mLRecyclerView);
        final LocalRecordReportAdapter adapter = new LocalRecordReportAdapter(mContext);
        LRecyclerViewAdapter mLRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        mLRecyclerView.setLayoutManager(new LinearLayoutManager(mContext) {
        });
        mLRecyclerView.setAdapter(mLRecyclerViewAdapter);

        mLRecyclerView.setPullRefreshEnabled(true);
        mLRecyclerView.setLoadMoreEnabled(true);
        mCurrent = 1;
        mLRecyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                t0.setText(null);
                t1.setText(null);
                t2.setText(null);
                t3.setText(null);
                mCurrent = 1;
                mLRecyclerView.setNoMore(false);
                mLRecyclerView.setLoadMoreEnabled(true);
                adapter.clear();
                String sDate = tv_date1.getText().toString() + " 00:00:00.001";
                String eDate = tv_date2.getText().toString() + " 23:59:59.999";
                String dinnerType = "";
                String record = "";
                int p1 = spinner1.getSelectedItemPosition();
                int p2 = spinner2.getSelectedItemPosition();
                if (p1 == 0) {
                    dinnerType = "";
                } else {
                    dinnerType = "" + p1;
                }
                if (p2 == 0) {
                    record = "";
                } else {
                    record = "" + p2;
                }
                GetLocalRecordReport(mLRecyclerView, adapter, sDate, eDate, dinnerType, record, String.valueOf(mCurrent), String.valueOf(Constants.PAGE_SIZE), list);

            }
        });
        mLRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (mCurrent * Constants.PAGE_SIZE < mCount) {
                    mCurrent++;
                    isLoadMore = true;
                    String sDate = tv_date1.getText().toString() + " 00:00:00.001";
                    String eDate = tv_date2.getText().toString() + " 23:59:59.999";
                    String dinnerType = "";
                    String record = "";
                    int p1 = spinner1.getSelectedItemPosition();
                    int p2 = spinner2.getSelectedItemPosition();
                    if (p1 == 0) {
                        dinnerType = "";
                    } else {
                        dinnerType = "" + p1;
                    }
                    if (p2 == 0) {
                        record = "";
                    } else {
                        record = "" + p2;
                    }
                    GetLocalRecordReport(mLRecyclerView, adapter, sDate, eDate, dinnerType, record, String.valueOf(mCurrent), String.valueOf(Constants.PAGE_SIZE), list);
                } else {
                    mLRecyclerView.setNoMore(true);
                    isLoadMore = false;
                }
            }
        });


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

        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) {
                    mLRecyclerView.refresh();
                }
            });
        }
        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;
    }


    private static void GetLocalRecordReport(final LRecyclerView mLRecyclerView, final LocalRecordReportAdapter adapter, String sDate,
                                             String eDate, String dinnerType, String record,
                                             String PageIndex, String PageSize, final List<TextView> tvs) {

        String WindowName = SPUtil.getInstance().readString("WindowName");
        String CanTeenName = SPUtil.getInstance().readString("CanTeenName");
        Call<CommonListResponse<LocalRecordReport>> call =
                APIWrapper.getInstance().GetLocalRecordReport(CanTeenName, WindowName, sDate, eDate, dinnerType, record, PageIndex, PageSize);
        call.enqueue(new Callback<CommonListResponse<LocalRecordReport>>() {
            @Override
            public void onResponse(Call<CommonListResponse<LocalRecordReport>> call,
                                   Response<CommonListResponse<LocalRecordReport>> response) {
                DialogUtil.dismissTipDialog();
                mLRecyclerView.refreshComplete(Constants.PAGE_SIZE);
                if (response.isSuccessful()) {
                    if (response.body() == null) {
                        AppContext.showToast("请求失败，请重试");
                        return;
                    }
                    if (response.body().Status != 200) {
                        AppContext.showToast(response.body().Message);
                        return;
                    }
                    List<LocalRecordReport> data = response.body().ModelJson;
                    adapter.addAll(data);
                    mCount = response.body().Total;
                    if (mCurrent * Constants.PAGE_SIZE < response.body().Total) {
                        mLRecyclerView.setNoMore(false);
                    } else {
                        mLRecyclerView.setNoMore(true);
                        isLoadMore = false;
                    }
                    if (tvs != null) {
                        tvs.get(0).setText(response.body().Total + "");
                        tvs.get(1).setText(response.body().TotalDinnerAmount);
                        tvs.get(2).setText(response.body().TotalAllowanceAmount);
                        tvs.get(3).setText(response.body().TotalConsumption);
                    }
                }

            }

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


    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 = 1;
    static int mCount = 20;
    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);

        final TextView t1 = (TextView) view.findViewById(R.id.tv_TotalDinnerAmount);
        final TextView t2 = (TextView) view.findViewById(R.id.tv_TotalAllowanceAmount);
        final TextView t3 = (TextView) view.findViewById(R.id.tv_TotalConsumption);


        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);
        final Spinner spinner1 = (Spinner) view.findViewById(R.id.sp_year);
        final 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() {
                t1.setText(null);
                t2.setText(null);
                t3.setText(null);
                adapter.clear();
                mCurrent = 1;
                String year = spinner1.getSelectedItem().toString();
                String month = spinner2.getSelectedItem().toString();
                GetMonthRecordReport(mLRecyclerView, adapter, year.substring(0, year.length() - 1), month.substring(0, month.length() - 1), t1, t2, t3);

            }
        });
        mLRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (mCurrent * Constants.PAGE_SIZE < mCount) {
                    adapter.clear();
                    mCurrent++;
                    isLoadMore = true;
                    String year = spinner1.getSelectedItem().toString();
                    String month = spinner2.getSelectedItem().toString();
                    GetMonthRecordReport(mLRecyclerView, adapter, year.substring(0, year.length() - 1), month.substring(0, month.length() - 1), t1, t2, t3);
                } else {
                    mLRecyclerView.setNoMore(true);
                    isLoadMore = false;
                }
            }
        });


        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) {
                    adapter.clear();
                    mLRecyclerView.refresh();
                }
            });
        }
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return dialog;
    }


    private static void GetMonthRecordReport(final LRecyclerView mLRecyclerView, final MonthRecordReportAdapter adapter, String year, String month, final TextView t1, final TextView t2, final TextView t3) {
        Call<CommonListResponse<MonthRecordReport>> call =
                APIWrapper.getInstance().GetMonthRecordReport(year, month);
        call.enqueue(new Callback<CommonListResponse<MonthRecordReport>>() {
            @Override
            public void onResponse(Call<CommonListResponse<MonthRecordReport>> call,
                                   Response<CommonListResponse<MonthRecordReport>> response) {
                DialogUtil.dismissTipDialog();
                mLRecyclerView.refreshComplete(Constants.PAGE_SIZE);
                if (response.isSuccessful()) {
                    if (response.body() == null) {
                        AppContext.showToast("请求失败，请重试");
                        return;
                    }
                    if (response.body().Status != 200) {
                        AppContext.showToast(response.body().Message);
                        return;
                    }
                    List<MonthRecordReport> data = response.body().ModelJson;
                    adapter.setDataList(data);
                    t1.setText(response.body().TotalDinnerAmount);
                    t2.setText(response.body().TotalAllowanceAmount);
                    t3.setText(response.body().TotalConsumption);
                }

            }

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

}
