package com.wd.workoffice.ui.adapter;


import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wd.workoffice.R;
import com.wd.workoffice.bean.TradingBean;
import com.wd.workoffice.bean.event.ModifyCarEvent;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.widget.AddAndReduceView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by flexible on 2018/8/13.
 */

public class TradingProCarAdapter extends BaseQuickAdapter<TradingBean.RecordsBean, BaseViewHolder> {

    public TradingProCarAdapter(int layoutResId, List data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, TradingBean.RecordsBean item) {
        helper.setText(R.id.tv_top_name, item.getProductName());
        helper.setText(R.id.tv_top_card, item.getProductSoleCode());
        helper.setText(R.id.tv_top_card_name, item.getProductSimpleCode());
        helper.setText(R.id.tv_apply_num, MathUtils.converData(item.getQuantity(),3));
        helper.setText(R.id.tv_dept, item.getStockName());
        AddAndReduceView userNum = helper.getView(R.id.ar_use_num);
        userNum.setNumber(item.getChooseNum());
        userNum.setMaxNum(item.getQuantity());
        userNum.setOnNumberChangedListener(new AddAndReduceView.OnNumberChangedListener() {
            @Override
            public void OnNumberChanged(String vs) {
                item.setChooseNum(vs);
                EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
            }
        });

        helper.setText(R.id.tv_name, item.getChangePro().getName());
        helper.setText(R.id.tv_card, item.getChangePro().getSoleCode());
        helper.setText(R.id.tv_card_name, item.getChangePro().getSimpleCode());
        helper.setText(R.id.tv_spec, item.getChangePro().getSpec());
        helper.setText(R.id.tv_price, MathUtils.converData(item.getChangePro().getInternalPrice(),3));
        helper.setText(R.id.tv_all_price, "小计:"+MathUtils.multiply(item.getChangePro().getCarNum(), MathUtils.converData(item.getChangePro().getInternalPrice(),3), 3));
        AddAndReduceView num = helper.getView(R.id.ar_num);
        num.setNumber(item.getChangePro().getCarNum());
        num.setOnNumberChangedListener(new AddAndReduceView.OnNumberChangedListener() {
            @Override
            public void OnNumberChanged(String vs) {
                item.getChangePro().setCarNum(vs);
                helper.setText(R.id.tv_all_price, "小计:"+MathUtils.multiply(vs , MathUtils.converData(item.getChangePro().getInternalPrice(),3), 3));
                EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
            }

        });
    }

}

