package com.danding.yang800.Presenter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.danding.yang800.Adapter.AfterServiceAdapter;
import com.danding.yang800.Adapter.LogisticsAdapter;
import com.danding.yang800.AfterServiceActivity;
import com.danding.yang800.Entity.AfterServiceInfo;
import com.danding.yang800.Entity.LogisticInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.LogisticsActivity;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.Util.VolleyUtil;

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

import java.util.HashMap;

public class LogisticsActivityPresenter {
    private LogisticsActivity context;
    SharedPreferences sp;

    public LogisticsActivityPresenter(LogisticsActivity context) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;

    }
    public void getInfo(final String packageSn) {
        HashMap<String, String> map = new HashMap<>();
        map.put("packageSn", packageSn);
        new VolleyUtil(context).request(context, "order.getExpressDetail", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONObject result = json.getJSONObject("result");
                    String comName=StringUtil.toString(result.getString("comName"));
                    String nu=StringUtil.toString(result.getString("nu"));
                    String comcontact=StringUtil.toString(result.getString("comcontact"));
                    String goodsImg=StringUtil.toString(result.getString("goodsImg"));
                    String goodsNum=StringUtil.toString(result.getString("goodsNum"));
                    String expressUrl=StringUtil.toString(result.getString("expressUrl"));
//                    Object object = result.getString("data");
//                    LogUtil.d(object.toString());
//                    if (!object.toString().equals("null")){
//                        JSONArray dataArr=result.getJSONArray("data");
//                        for (int i = 0; i < dataArr.length(); i++) {
//                            JSONObject jsonObj=dataArr.getJSONObject(i);
//                            String time=StringUtil.toString(jsonObj.getString("time"));
//                            String context=StringUtil.toString(jsonObj.getString("context"));
//                            adapter.addData(new LogisticInfo(context,time));
//                        }
//                    }

                    context.initData(comName,nu,comcontact,goodsImg,goodsNum,expressUrl);
                } catch (Exception e) {

                }


            }

            @Override
            public void onerrSuccess() {
            }

            @Override
            public void onCatch() {
            }

            @Override
            public void onFail() {
            }
        });
    }
    public void cacel(final String id) {

        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);

        new VolleyUtil(context).request(context, "xhr.SafterSale.remove", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {

                try {
                    ToastUtil.show(context,"撤销成功");
                    Intent intent = new Intent();
                    context.setResult(1, intent);
                    context.finish();
                } catch (Exception e) {

                }


            }

            @Override
            public void onerrSuccess() {
            }

            @Override
            public void onCatch() {
            }

            @Override
            public void onFail() {
            }
        });
    }


}
