package com.danding.yang800.Presenter;

import android.content.Context;
import android.content.SharedPreferences;
import com.danding.yang800.Adapter.TakeNotesAdapter;
import com.danding.yang800.BuildConfig;
import com.danding.yang800.Entity.TakeNotesInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.General.Yang800App;
import com.danding.yang800.TakeNotesActivity;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.Util.VolleyUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/1/12.
 */
public class TakeNotesActivityPresenter {
    private TakeNotesActivity context;
    SharedPreferences sp;
    TakeNotesAdapter adapter;

    public TakeNotesActivityPresenter(TakeNotesActivity context, TakeNotesAdapter adapter) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
        this.adapter = adapter;
    }

    String state;

    public void getInfo(final String storeId, final String pageIndex) {
        context.initDialog();
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("storeId", storeId);
        map.put("pageIndex", pageIndex);
        map.put("limit", "10");
//        new VolleyUtil(context).request(context,"http://" + BuildConfig.HOSTURL + "/xhr/finance/storeIncome/cashWithdrawLog",true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context,"finance.storeIncome.cashWithdrawLog",true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                        JSONObject jsonOData = json.getJSONObject("result");
                        int totalCount = jsonOData.getInt("pageTotal");
                        JSONArray jjArray = jsonOData
                                .getJSONArray("data");
                        for (int i = 0; i < jjArray.length(); i++) {
                            JSONObject jsondata = jjArray.getJSONObject(i);
                            String status = jsondata.getString("status");

                            if (status.equals("1") || status.equals("2")) {
                                state = "提现审核中";
                            } else if (status.equals("3")) {
                                state = "提现失败";
                            } else if (status.equals("4")) {
                                state = "提现成功";
                            }
                            String createTime = jsondata.getString("createTime");
                            double cashWithdrawDouble = jsondata.getDouble("cashWithdraw");
                            String cashWithdraw = new DecimalFormat("0.00").format(cashWithdrawDouble);
                            adapter.addData(new TakeNotesInfo(createTime, cashWithdraw, state));
                        }
                        context.upadateAdapter(totalCount);
                        context.dismissDialog();

                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();

                }

            }


            @Override
            public void onerrSuccess() {
                context.dismissDialog();
            }

            @Override
            public void onCatch() {
                context.dismissDialog();
            }

            @Override
            public void onFail() {
                context.dismissDialog();
            }
        });
    }


}
