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.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
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.ConsumRecordReportAdapter;
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.ui.activity.MainActivity;
import com.ifavine.pay.view.AmountView;
import com.ifavine.pay.view.MDialog;
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);
        dialog_msg.addTextChangedListener(textWatcher);

        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) {
                MainActivity.Current_Count_DialogInput = 0;
                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();
                MainActivity.Current_Count_DialogInput = 0;
            }

            @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 TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            MainActivity.Current_Count_DialogInput = 0;
        }
    };

    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);
        et1.addTextChangedListener(textWatcher);
        et2.addTextChangedListener(textWatcher);
        et3.addTextChangedListener(textWatcher);
        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) {
                MainActivity.Current_Count_DialogInput = 0;
                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 = new ArrayList<>();
        final List<WindowCostAmount> D = gson.fromJson(data, new TypeToken<List<WindowCostAmount>>() {
        }.getType());
        if (D == null) {
            for (int i = 0; i < 4; i++) {
                WindowCostAmount wca = new WindowCostAmount();
                wca.DinnerType = (i + 1) + "";
                DATA.add(wca);
            }
        } else {
            DATA.addAll(D);
        }

        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 TextView et1 = (TextView) view.findViewById(R.id.et_amt1);
        final TextView et2 = (TextView) view.findViewById(R.id.et_amt2);
        final TextView et3 = (TextView) view.findViewById(R.id.et_amt3);
        final TextView et4 = (TextView) 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);
        }
        view.findViewById(R.id.ll).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.e("love", "MDialog");
                MainActivity.Current_Count_DialogWindow = 0;
                return false;
            }
        });
        //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) {
                MainActivity.Current_Count_DialogWindow = 0;
                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) {
                MainActivity.Current_Count_DialogWindow = 0;
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date1, Calendar.getInstance());
            }
        });
        tv_date2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.Current_Count_DialogWindow = 0;
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date2, Calendar.getInstance());
            }
        });
        tv_date3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.Current_Count_DialogWindow = 0;
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date3, Calendar.getInstance());
            }
        });
        tv_date4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.Current_Count_DialogWindow = 0;
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date4, Calendar.getInstance());
            }
        });
        tv_date5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.Current_Count_DialogWindow = 0;
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date5, Calendar.getInstance());
            }
        });
        tv_date6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.Current_Count_DialogWindow = 0;
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date6, Calendar.getInstance());
            }
        });
        tv_date7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.Current_Count_DialogWindow = 0;
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date7, Calendar.getInstance());
            }
        });
        tv_date8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.Current_Count_DialogWindow = 0;
                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) + "");
                MainActivity.Current_Count_DialogWindow = 0;
            }
        });
        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) + "");
                MainActivity.Current_Count_DialogWindow = 0;
            }
        });
        //保存操作
        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) + "");
                MainActivity.Current_Count_DialogWindow = 0;
            }
        });
        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) + "");
                MainActivity.Current_Count_DialogWindow = 0;
            }
        });
        //保存操作
        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) + "");
                MainActivity.Current_Count_DialogWindow = 0;
            }
        });
        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) + "");
                MainActivity.Current_Count_DialogWindow = 0;
            }
        });      //保存操作
        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) + "");
                MainActivity.Current_Count_DialogWindow = 0;
            }
        });
        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) + "");
                MainActivity.Current_Count_DialogWindow = 0;
            }
        });

        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 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 TextView t4 = (TextView) view.findViewById(R.id.tv_BreakFastTotal);
        final TextView t5 = (TextView) view.findViewById(R.id.tv_LunchTotal);
        final TextView t6 = (TextView) view.findViewById(R.id.tv_SupperTotal);
        final TextView t7 = (TextView) view.findViewById(R.id.tv_NightTotal);
        final List<TextView> list = new ArrayList<>();
        list.add(t1);
        list.add(t2);
        list.add(t3);
        list.add(t4);
        list.add(t5);
        list.add(t6);
        list.add(t7);
        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);

        mLRecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.e("love", "MDialog");
                MainActivity.Current_Count_DialogWindow = 0;
                return false;
            }
        });
        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() {
                t1.setText(null);
                t2.setText(null);
                t3.setText(null);
                t4.setText(null);
                t5.setText(null);
                t6.setText(null);
                t7.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() {
                MainActivity.Current_Count_DialogWindow = 0;
                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);
        spinner2.setSelection(1);
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                MainActivity.Current_Count_DialogWindow = 0;
            }

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

            }
        });
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                MainActivity.Current_Count_DialogWindow = 0;
            }

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

            }
        });

        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }
        view.findViewById(R.id.ll).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.e("love", "MDialog");
                MainActivity.Current_Count_DialogWindow = 0;
                return false;
            }
        });

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

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

                @Override
                public void onClick(View v) {
                    mLRecyclerView.refresh();
                    MainActivity.Current_Count_DialogWindow = 0;
                }
            });
        }
        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) {
                MainActivity.Current_Count_DialogWindow = 0;
                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);
                }
                DialogUtil.tipDialog = 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);
                DialogUtil.tipDialog.show();
            }
        });

        tv_date2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.Current_Count_DialogWindow = 0;
                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);
                }
                DialogUtil.tipDialog = 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);
                DialogUtil.tipDialog.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().TotalDinnerAmount);
                        tvs.get(1).setText(response.body().TotalAllowanceAmount);
                        tvs.get(2).setText(response.body().TotalConsumption);
                        tvs.get(3).setText(response.body().BreakFastTotal);
                        tvs.get(4).setText(response.body().LunchTotal);
                        tvs.get(5).setText(response.body().SupperTotal);
                        tvs.get(6).setText(response.body().NightTotal);
                    }
                }

            }

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


    /**
     * 本机消费记录
     *
     * @param mContext
     * @param title
     * @param okListener
     * @param cancelable
     * @return
     */
    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 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 TextView t4 = (TextView) view.findViewById(R.id.tv_BreakFastTotal);
        final TextView t5 = (TextView) view.findViewById(R.id.tv_LunchTotal);
        final TextView t6 = (TextView) view.findViewById(R.id.tv_SupperTotal);
        final TextView t7 = (TextView) view.findViewById(R.id.tv_NightTotal);
        final List<TextView> list = new ArrayList<>();
        list.add(t1);
        list.add(t2);
        list.add(t3);
        list.add(t4);
        list.add(t5);
        list.add(t6);
        list.add(t7);

        view.findViewById(R.id.ll).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.e("love", "MDialog");
                MainActivity.Current_Count_DialogWindow = 0;
                return false;
            }
        });

        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);
        final Spinner spinner3 = (Spinner) view.findViewById(R.id.spinner3);

        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);

        mLRecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.e("love", "MDialog");
                MainActivity.Current_Count_DialogWindow = 0;
                return false;
            }
        });
        final ConsumRecordReportAdapter adapter = new ConsumRecordReportAdapter(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() {
                t1.setText(null);
                t2.setText(null);
                t3.setText(null);
                t4.setText(null);
                t5.setText(null);
                t6.setText(null);
                t7.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;
                }
                String windowName = spinner3.getSelectedItem().toString();
                GetConsumRecordReport(mLRecyclerView, adapter, windowName, sDate, eDate, dinnerType, record, String.valueOf(mCurrent), String.valueOf(Constants.PAGE_SIZE), list);

            }
        });
        mLRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                MainActivity.Current_Count_DialogWindow = 0;
                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;
                    }
                    String windowName = spinner3.getSelectedItem().toString();
                    GetConsumRecordReport(mLRecyclerView, adapter, windowName, 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);
        spinner2.setSelection(1);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                MainActivity.Current_Count_DialogWindow = 0;
            }

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

            }
        });
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                MainActivity.Current_Count_DialogWindow = 0;
            }

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

            }
        });
        //窗口设置
        String CanTeenCode = SPUtil.getInstance().readString("CanTeenCode");
        String data = SPUtil.getInstance().readString("CanteenWindows");
        final List<CanteenWindow> datas = gson.fromJson(data, new TypeToken<List<CanteenWindow>>() {
        }.getType());
        List<CanteenWindow.Window> windows = null;
        for (CanteenWindow w : datas) {
            if (CanTeenCode.equals(w.CanteenCode)) {
                windows = w.Windows;
                break;
            }
        }
        List<String> ws = new ArrayList<>();
        if (windows != null) {
            for (CanteenWindow.Window w : windows) {
                ws.add(w.WindowName);
            }
        }
        SpinnerAdapter adapter3 = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, ws.toArray());
        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) {
                    mLRecyclerView.refresh();
                    MainActivity.Current_Count_DialogWindow = 0;
                }
            });
        }
        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) {
                MainActivity.Current_Count_DialogWindow = 0;
                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);
                }
                DialogUtil.tipDialog = 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);
                DialogUtil.tipDialog.show();
            }
        });

        tv_date2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.Current_Count_DialogWindow = 0;
                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);
                }
                DialogUtil.tipDialog = 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);
                DialogUtil.tipDialog.show();
            }
        });

        dialog.show();
        return dialog;
    }


    private static void GetConsumRecordReport(final LRecyclerView mLRecyclerView, final ConsumRecordReportAdapter adapter, String WindowName, String sDate,
                                              String eDate, String dinnerType, String record,
                                              String PageIndex, String PageSize, final List<TextView> tvs) {
        String CanTeenCode = SPUtil.getInstance().readString("CanTeenCode");
        Call<CommonListResponse<LocalRecordReport>> call =
                APIWrapper.getInstance().GetConsumRecordReport(WindowName, CanTeenCode, 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().TotalDinnerAmount);
                        tvs.get(1).setText(response.body().TotalAllowanceAmount);
                        tvs.get(2).setText(response.body().TotalConsumption);
                        tvs.get(3).setText(response.body().BreakFastTotal);
                        tvs.get(4).setText(response.body().LunchTotal);
                        tvs.get(5).setText(response.body().SupperTotal);
                        tvs.get(6).setText(response.body().NightTotal);
                    }
                }

            }

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


    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);

        view.findViewById(R.id.ll).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.e("love", "MDialog");
                MainActivity.Current_Count_DialogWindow = 0;
                return false;
            }
        });

        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);

        mLRecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.e("love", "MDialog");
                MainActivity.Current_Count_DialogWindow = 0;
                return false;
            }
        });
        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() {
                MainActivity.Current_Count_DialogWindow = 0;
                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() {
                MainActivity.Current_Count_DialogWindow = 0;
                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);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                MainActivity.Current_Count_DialogWindow = 0;
            }

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

            }
        });
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                MainActivity.Current_Count_DialogWindow = 0;
            }

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

            }
        });

        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);
            }
        });
    }

}
