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.AfterServiceActivity;
import com.danding.yang800.ChangeShopNameActivity;
import com.danding.yang800.Entity.AfterServiceInfo;
import com.danding.yang800.General.HttpCallBack;
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.ArrayList;
import java.util.HashMap;

public class AfterServiceActivityPresenter {
    private AfterServiceActivity context;
    SharedPreferences sp;
    AfterServiceAdapter adapter;
    public AfterServiceActivityPresenter(AfterServiceActivity context,AfterServiceAdapter adapter) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
        this.adapter=adapter;
    }


    public void getInfo(final String packageSn) {

        HashMap<String, String> map = new HashMap<>();
        map.put("packageSn", packageSn);
        new VolleyUtil(context).request(context, "xhr.SafterSale.pGet", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONObject result = json.getJSONObject("result");
                    JSONObject safaterSale = result.getJSONObject("safaterSale");
                    String sn = StringUtil.toString(safaterSale.getString("sn"));
                    String id = StringUtil.toString(safaterSale.getString("id"));
                    String statusDesc = StringUtil.toString(safaterSale.getString("statusDesc"));
                    String typeDesc = StringUtil.toString(safaterSale.getString("typeDesc"));
                    String statusValue = StringUtil.toString(safaterSale.getString("statusValue"));
                    String totalAmount = StringUtil.doubleToString(safaterSale, "totalAmount");
                    String receiver = StringUtil.toString(safaterSale.getString("receiver"));
                    String applyRemark = StringUtil.toString(safaterSale.getString("applyRemark"));
                    String receiverAddress = StringUtil.toString(safaterSale.getString("receiverAddress"));
                    String receiverPhone = StringUtil.toString(safaterSale.getString("receiverPhone"));
                    JSONArray itemList = result.getJSONArray("itemList");
                    for (int i = 0; i <itemList.length() ; i++) {
                        JSONObject shopInfo=itemList.getJSONObject(i);
                        String skuName = StringUtil.toString(shopInfo.getString("skuName"));
                        String imageUrl = StringUtil.toString(shopInfo.getString("imageUrl"));
                        String quantity = StringUtil.toString(shopInfo.getString("quantity"));
                        adapter.addData(new AfterServiceInfo(skuName,imageUrl,quantity));
                    }

                    context.initData(sn,statusDesc,typeDesc,statusValue,totalAmount,receiver,
                            receiverAddress,receiverPhone,applyRemark,id);

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


}
