package com.wd.workoffice.ui.activity.bat.work;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.LogisticDetailBean;
import com.wd.workoffice.bean.TicketAddBean;
import com.wd.workoffice.bean.event.CheckLogisticEvent;
import com.wd.workoffice.bean.event.CheckTicketEvent;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.WorkAddLogisticProAdapter;
import com.wd.workoffice.ui.adapter.WorkAddTicketOrderAdapter;
import com.wd.workoffice.ui.adapter.WorkLogisticDetailAdapter;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

/**
 * 工作台 - 财务 -  物流- 提交
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkFinancialLogisticsSubmitActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_bottom)
    TextView tvBottom;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.tv_weight)
    TextView tvWeight;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.et_position)
    EditText etPosition;
    @BindView(R.id.et_company)
    EditText etCompany;
    @BindView(R.id.et_code)
    EditText etCode;
    private List<LogisticDetailBean.RecordsBean> dataList;
    private WorkLogisticDetailAdapter dataAdapter;
    private Map<String, Object> param;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        rvData.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
    }

    @Override
    protected void initData() {
        dataList = JSON.parseArray(getIntent().getStringExtra("info"), LogisticDetailBean.RecordsBean.class);
        dataAdapter = new WorkLogisticDetailAdapter(R.layout.item_logistics_detail, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        param = new HashMap<>();
        param.put("receiveDeptId", getIntent().getStringExtra("receiveDeptId"));
        param.put("logisticsId", getIntent().getStringExtra("logisticsId"));
        param.put("processCode", getIntent().getStringExtra("processCode"));
        changeNumAndPrice();
        coverProInfo(dataList);
    }

    @Override
    protected void initEvent() {
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_logistics_submit;
    }

    /**
     * 转换pro，生成上传参数
     *
     * @param proList
     * @return
     */
    private void coverProInfo(List<LogisticDetailBean.RecordsBean> proList) {
        List<JSONObject> orderItems = new ArrayList<>();
        for (LogisticDetailBean.RecordsBean recordsBean : proList) {
            JSONObject data = new JSONObject();
            data.put("id", recordsBean.getId());
            orderItems.add(data);
        }
        param.put("logisticsLogList", orderItems);
    }

    private void changeNumAndPrice() {
        String totalWeight = "";
        String applyAmount = "";
        for (LogisticDetailBean.RecordsBean recordsBean : dataList) {
            totalWeight = MathUtils.add(totalWeight, String.valueOf(recordsBean.getWeight()), 3);
            applyAmount = MathUtils.add(applyAmount, String.valueOf(recordsBean.getFareMoney()), 3);
        }
        param.put("totalWeight", totalWeight);
        param.put("applyAmount", applyAmount);
        tvWeight.setText(totalWeight);
        tvPrice.setText(applyAmount);
    }

    private void submit() {
        param.put("distributionSite", etPosition.getText().toString());
        param.put("issuingUnit", etCompany.getText().toString());
        param.put("invoiceNumber", etCode.getText().toString());
        RtfUtils.getRtf().logisticsApply(WorkUtils.convertMapToBody(param)).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                toast("操作成功");
                EventBus.getDefault().post(new CheckLogisticEvent());
                finish();
            }
        });
    }


    @OnClick(R.id.tv_bottom)
    public void onViewClicked() {
        if (TextUtils.isEmpty(etCompany.getText().toString())) {
            toast("请输入税费");
            return;
        }
        if (TextUtils.isEmpty(etPosition.getText().toString())) {
            toast("请输入税费");
            return;
        }
        submit();
    }
}
