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

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
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.FinancailDetailBean;
import com.wd.workoffice.bean.FinancialBean;
import com.wd.workoffice.bean.event.CheckFinancialEvent;
import com.wd.workoffice.bean.event.CheckLogisticEvent;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.FinancialDetailAdapter;
import com.wd.workoffice.ui.adapter.FinancialModifyAdapter;
import com.wd.workoffice.util.UserKeeper;
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 java.util.Set;

import androidx.appcompat.app.AlertDialog;
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.disposables.Disposable;

/**
 * 财税详情
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class StoreFinancialModifyActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_remark)
    EditText tvRemark;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private List<FinancailDetailBean> dataList;
    private FinancialModifyAdapter 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() {
        FinancialBean.RecordsBean info = JSON.parseObject(getIntent().getStringExtra("info"), FinancialBean.RecordsBean.class);
        getData(info.getId() + "");
        tvTime.setText(info.getPayDate() + "");
        tvRemark.setText(info.getApplyRemark());
        tvStatus.setText(WorkUtils.getFinancialStatus(Integer.valueOf(info.getStatus())));
        dataList = new ArrayList<>();
        dataAdapter = new FinancialModifyAdapter(R.layout.item_financial_modify, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        param = new HashMap<>();
        param.put("id", info.getId());
        param.put("payDate",info.getPayDate());
    }


    private void getData(String id) {
        RtfUtils.getRtf().storeFinancialDetail(id).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;
                }
                List<FinancailDetailBean> dataBean = JSON.parseArray(data.getData().toString(), FinancailDetailBean.class);
                dataList.addAll(dataBean);
                dataAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initEvent() {
    }

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


    private void submit() {
        for (FinancailDetailBean financailDetailBean : dataList) {
            if (financailDetailBean.getAmount() == 0d) {
                toast("请输入金额");
                return;
            }
        }
        param.put("financeTaxItems",dataList);
        param.put("applyRemark", tvRemark.getText().toString());
        param.put("payDept", UserKeeper.getInstance().getUserDepId());
        RtfUtils.getRtf().addFinancial(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;
                }
                finish();
                toast("提交成功");
                EventBus.getDefault().post(new CheckFinancialEvent());
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_report, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.report:
                submit();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void check(int status, String remark) {
        param.put("status", status);
        param.put("auditRemark", remark);
        RtfUtils.getRtf().storeFinancialCheck(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;
                }
                EventBus.getDefault().post(new CheckLogisticEvent());
                toast("操作成功");
                finish();

            }
        });
    }

//    private boolean checkAddParam(int status) {
//        Map<Integer, String> errorMap = dataAdapter.getErrorMap();
//        Set<Map.Entry<Integer, String>> entries = errorMap.entrySet();
//        if (entries.size() == 0 && status == 2) {
//            toast("请标记错误并填写错误原因");
//            return false;
//        }
//        List<JSONObject> financeTaxItems = new ArrayList<>();
//
//        for (int i = 0; i < dataList.size(); i++) {
//            JSONObject data = new JSONObject();
//            if (errorMap.containsKey(i)) {
//                data.put("auditRemark", dataList.get(i).getAuditRemark());
//                data.put("id", dataList.get(i).getId());
//                data.put("status", 2);
//                if (TextUtils.isEmpty(dataList.get(i).getAuditRemark())) {
//                    toast("请标记错误并填写错误原因");
//                    return false;
//                }
//            } else {
//                data.put("auditRemark", dataList.get(i).getAuditRemark());
//                data.put("id", dataList.get(i).getId());
//                data.put("status", 1);
//            }
//            financeTaxItems.add(data);
//        }
//        param.put("financeTaxItems", financeTaxItems);
//        return true;
//    }

}
