package com.wd.workoffice.ui.adapter;


import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;

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.ProductBean;
import com.wd.workoffice.bean.event.ModifyCarEvent;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.util.WorkUtils;
import com.wd.workoffice.widget.AddAndReduceView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import flexible.xd.android_base.utils.ToastUtil;

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

public class AddCrucibleCarAdapter extends BaseQuickAdapter<ProductBean.RecordsBean, BaseViewHolder> {

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


    @Override
    protected void convert(BaseViewHolder helper, ProductBean.RecordsBean item) {
        helper.setText(R.id.tv_name, item.getSimpleCode());
        helper.setText(R.id.tv_card, item.getSoleCode());
        helper.setText(R.id.tv_card_name, item.getName());
        helper.setText(R.id.tv_spec, item.getSpec());
        helper.setText(R.id.tv_price, "￥".concat(MathUtils.converData(item.getInternalPrice(), 3)));
        helper.setText(R.id.tv_unit, MathUtils.converData(item.getWeight(), 3).concat("吨"));
        helper.setText(R.id.tv_all_price, MathUtils.multiply(item.getCarNum(), MathUtils.converData(item.getInternalPrice(),3), 3));
        helper.addOnClickListener(R.id.tv_delete);
        AddAndReduceView num = helper.getView(R.id.ar_num);
        num.setMaxNum(item.getCanUseProductSum());
        num.setOnNumberChangedListener(new AddAndReduceView.OnNumberChangedListener() {
            @Override
            public void OnNumberChanged(String vs) {
                item.setCarNum(vs);
                helper.setText(R.id.tv_all_price, MathUtils.multiply(vs , item.getCarPrice(), 3));
                EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
            }
        });
        EditText etPrice = helper.getView(R.id.et_price);
        etPrice.setText(item.getCarPrice());
        WorkUtils.addDecimalsListener(etPrice);
        etPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    ToastUtil.showShort("请输入销售单价");
                    return;
                }
                item.setCarPrice(s.toString());
                helper.setText(R.id.tv_all_price, MathUtils.multiply(item.getCarNum(), item.getCarPrice(), 3));
                EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
            }
        });
        num.setNumber(item.getCarNum());

    }
}

