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

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.FareOrderBean;
import com.wd.workoffice.bean.OrderDetail;
import com.wd.workoffice.bean.event.CheckSendEvent;
import com.wd.workoffice.bean.event.FareConfirmEvent;
import com.wd.workoffice.bean.event.SendWeightEvent;
import com.wd.workoffice.bean.workEnum.OrderType;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.bat.store.StoreSaleClientActivity;
import com.wd.workoffice.ui.adapter.FareOrderAdapter;
import com.wd.workoffice.ui.adapter.WorkSendApplyAddAdapter;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.util.PickTimeUtils;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.Nullable;
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 flexible.xd.android_base.utils.TimeUtils;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

/**
 * 物流单确认
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class FareAddConfirmActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_ok)
    TextView tvOk;
    @BindView(R.id.ll_bottom)
    RelativeLayout llBottom;
    @BindView(R.id.rv_from)
    RecyclerView rvData;
    @BindView(R.id.et_logistics_price)
    EditText etLogisticsPrice;
    @BindView(R.id.ll_logistics_price)
    LinearLayout llLogisticsPrice;
    @BindView(R.id.et_tax)
    EditText etTax;
    @BindView(R.id.ll_tax)
    LinearLayout llTax;
    @BindView(R.id.et_logistics_code)
    EditText etLogisticsCode;
    @BindView(R.id.ll_logistics_code)
    LinearLayout llLogisticsCode;
    @BindView(R.id.tv_logistics_company)
    TextView tvLogisticsCompany;
    @BindView(R.id.rl_set)
    RelativeLayout rlSet;
    private List<FareOrderBean.RecordsBean> dataList;
    private FareOrderAdapter dataAdapter;
    private Map<String, Object> param;
    private int clientId;

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

    @Override
    protected void initData() {
        dataList = JSON.parseArray(getIntent().getStringExtra("data"), FareOrderBean.RecordsBean.class);
        dataAdapter = new FareOrderAdapter(R.layout.item_fare_order, dataList, 1);
        dataAdapter.bindToRecyclerView(rvData);
        param = new HashMap<>();
        param.put("customerId", getIntent().getStringExtra("customerId"));
        tvPrice.setText(String.format("共选中 %s 条", dataList.size()));
    }

    @Override
    protected void initEvent() {
    }


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


    private boolean coverProInfo() {
        List<JSONObject> fareItemList = new ArrayList<>();
        for (FareOrderBean.RecordsBean recordsBean : dataList) {
            JSONObject data = new JSONObject();
            data.put("sendOutStoreApplyId", recordsBean.getId());
            fareItemList.add(data);
        }
        param.put("fareItemList", fareItemList);
        return true;
    }

    private void submit() {
        String price = etLogisticsPrice.getText().toString();
        String code = etLogisticsCode.getText().toString();
        String tax = etTax.getText().toString();
        if (TextUtils.isEmpty(price)) {
            toast("请输入运费");
            return;
        }
        if (TextUtils.isEmpty(tax)) {
            toast("请输入税率");
            return;
        }
        if (clientId == 0) {
            toast("请选择物流公司");
            return;
        }
        param.put("trackingNumber", code);
        param.put("fareMoney", price);
        param.put("tax", tax);
        param.put("transitPartId", clientId);
        RtfUtils.getRtf().fareAdd(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 FareConfirmEvent());
                finish();
            }

        });
    }


    @OnClick({R.id.tv_ok, R.id.rl_set})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_ok:
                if (coverProInfo()) {
                    submit();
                }
                break;
            case R.id.rl_set:
                startActivityForResult(StoreSaleClientActivity.class, 10001,
                        "flag", "1", "supplier", "选择运输商", "code", "TRANSPORTERS");
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10001 && resultCode == 10001) {
            String clientName = data.getStringExtra("clientName");
            clientId = data.getIntExtra("id", 0);
            tvLogisticsCompany.setText(clientName);
        }
    }

}
