package com.wd.workoffice.ui.adapter;


import android.widget.CheckBox;
import android.widget.CompoundButton;

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.AddStockChooseBean;
import com.wd.workoffice.bean.FareBean;
import com.wd.workoffice.bean.FareOrderBean;
import com.wd.workoffice.bean.event.ModifyCarEvent;
import com.wd.workoffice.util.MathUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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

public class FareOrderAdapter extends BaseQuickAdapter<FareOrderBean.RecordsBean, BaseViewHolder> {
    int type;// 0 新增选择  1 新增展示 2 详情

    Map<Integer, Boolean> isChoose = new LinkedHashMap<>();

    public FareOrderAdapter(int layoutResId, List data) {
        super(layoutResId, data);
        for (int i = 0; i < getData().size(); i++) {
            isChoose.put(i, false);
        }
    }

    public FareOrderAdapter(int layoutResId, List data, int type) {
        super(layoutResId, data);
        this.type = type;
        for (int i = 0; i < getData().size(); i++) {
            isChoose.put(i, false);
        }
    }


    @Override
    protected void convert(BaseViewHolder helper, FareOrderBean.RecordsBean item) {
        if (type == 1) {
            helper.setGone(R.id.cb_check, false);
        } else if (type == 2) {
            helper.setGone(R.id.cb_check, false);
        } else {
        }
        helper.setText(R.id.tv_name, String.format("订单号:%s", item.getOrderId()));
        helper.setText(R.id.tv_time, item.getUpdatedTime());
        helper.setText(R.id.tv_weight, MathUtils.converData(item.getTotalWeight(), 3));
        helper.setText(R.id.tv_company, item.getReceiveDeptName());
        CheckBox cbChoose = helper.getView(R.id.cb_check);
        cbChoose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isChoose.put(helper.getAdapterPosition(), cbChoose.isChecked());
                EventBus.getDefault().post(new ModifyCarEvent(JSON.toJSONString(getData())));
            }
        });
        cbChoose.setChecked(isChoose.get(helper.getAdapterPosition()) == null ? false : isChoose.get(helper.getAdapterPosition()));

    }

    public Map<Integer, Boolean> getIsChoose() {
        return isChoose;
    }

    public void setIsChoose(Map<Integer, Boolean> isChoose) {
        this.isChoose = isChoose;
    }

    public void initChoose() {
        for (int i = 0; i < getData().size(); i++) {
            isChoose.put(i, false);
        }
    }
}

