package com.ygtx.emcs.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ygtx.emcs.R;
import com.ygtx.emcs.activity.adapter.StockLineListViewAdapter;
import com.ygtx.emcs.activity.model.StockModel;
import com.ygtx.emcs.activity.model.StockLineModel;
import com.ygtx.emcs.comm.BaseActivity;
import com.ygtx.emcs.databinding.YgtxStockInBinding;
import com.ygtx.emcs.databinding.YgtxStockProductBinding;
import com.ygtx.emcs.util.Constant;
import com.ygtx.emcs.util.HttpsUtil;
import com.ygtx.emcs.util.MyProgress;
import com.ygtx.emcs.util.Permission;
import com.ygtx.emcs.util.ToastUtils;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


public class StockLineInfoActivity extends BaseActivity {
    public static Logger mylog =Logger.getLogger(StockLineInfoActivity.class);

    private YgtxStockProductBinding binding;
    private Context context;
    private StockLineListViewAdapter stockLineListViewAdapter;
    private ArrayList<StockLineModel> stockLineList = new ArrayList<>();
    private String stockId;
    private String from;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = YgtxStockProductBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        context = this;
        stockId = getParamString("stockId");
        from = getParamString("from");
        initCommonTop2();
        initView();
        searchTask();
        Permission.checkPermission(this);
    }

    private void initView(){
        stockLineList = new ArrayList<>();
        stockLineListViewAdapter = new StockLineListViewAdapter(stockLineList, context);
        binding.lvStock.setAdapter(stockLineListViewAdapter);

        ImageView back = findViewById(R.id.c_iv_common_back);
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(view -> {
            finish();
        });
    }

    @Override
    protected String getCommonTopTitle() {
        return "物料清单";
    }

    @Override
    protected int getCommonTopICO() {
        return 0;
    }

    @Override
    public void receiverTask(String barcode) {

    }


    private void searchTask() {
        MyProgress myProgress = MyProgress.getInstance();
        myProgress.show(context, "提示", "正在加载");
        if(Constant.user==null || Constant.user.getUserId() == null){
            return;
        }
        RequestParams params = httParams(Constant.STOCK_IN_LINE_LIST);
        if("1".equals(from)){
            params = httParams(Constant.STOCK_OUT_LINE_LIST);
        }
        params.addBodyParameter("instockId", stockId);
        params.addBodyParameter("outstockId", stockId);
        params.addBodyParameter("pageNum", "1");
        params.addBodyParameter("pageSize", "100");
        params.addBodyParameter("orderByColumn", "create_time");
        params.addBodyParameter("isAsc", "desc");
        x.http().get(params, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject rs) {
                mylog.info(rs.toString());
                try {
                    if (rs.getInt("code") != 200) {
                        ToastUtils.showToast(context, rs.getString("msg"));
                    } else {
                        Type rds = new TypeToken<List<StockLineModel>>() {}.getType();
                        int total = rs.getInt("total");
                        if(total == 0){
                            mylog.info("没有记录");
                            return;
                        }
                        stockLineList.clear();
                        List<StockLineModel> stockLineModels = new Gson().fromJson(rs.get("rows").toString(), rds);
                        stockLineList.addAll(stockLineModels);
                        stockLineListViewAdapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
                HttpsUtil.parserError(StockLineInfoActivity.this, ex);
                mylog.error("请求异常：", ex);
            }

            @Override
            public void onFinished() {
                myProgress.close();
            }

            @Override
            public void onCancelled(CancelledException arg0) {
                mylog.error("onRead:", arg0);
            }
        });
    }

}