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

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.AllAccountBean;
import com.wd.workoffice.bean.event.FreezeEvent;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

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 WorkFinancialFreezeActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_dep_price)
    TextView tvDepPrice;
    @BindView(R.id.tv_dep_freeze_price)
    TextView tvDepFreezePrice;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.et_price)
    EditText etPrice;
    @BindView(R.id.tv_max)
    TextView tvMax;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.btn_ok)
    Button btnOk;
    @BindView(R.id.tv_price_desc)
    TextView tvPriceDesc;
    private AllAccountBean.RecordsBean info;
    private String type;
    private Map<String, Object> param;

    @Override
    protected void initView() {
//        action冻结(1)解冻(-1)动作判断
//                * amount操作金额（冻结解冻可用余额）
        ButterKnife.bind(this);
        param = new HashMap<>();
        info = JSON.parseObject(getIntent().getStringExtra("info"), AllAccountBean.RecordsBean.class);
        type = getIntent().getStringExtra("type");//冻结(1)解冻(-1)
        param.put("action", type);
        param.put("id", info.getId());
        tvDepPrice.setText(MathUtils.converData(info.getBalance(), 3));
        tvName.setText(info.getName());
        WorkUtils.addDecimalsListener(etPrice);
        changeView();
    }

    private void changeView() {//冻结(1)解冻(-1)
        if (TextUtils.equals("-1", type)) {
            tvDesc.setText("解冻金额");
            tvAll.setText("全部解冻");
            btnOk.setText("解冻");
            setTitle("解冻");
            tvPriceDesc.setText("已冻结余额");
            tvMax.setText("最多可解冻¥ " + MathUtils.converData(info.getFrozenAmount(), 3));
            tvDepFreezePrice.setText(MathUtils.converData(info.getFrozenAmount(), 3));
        } else {
            setTitle("冻结");
            tvPriceDesc.setText("可用余额");
            tvDesc.setText("冻结金额");
            tvDepFreezePrice.setText(MathUtils.converData(info.getBalance(), 3));
            tvAll.setText("全部冻结");
            btnOk.setText("冻结");
            tvMax.setText("最多可冻结¥ " +  MathUtils.converData(info.getBalance(), 3));
        }
    }

    @Override
    protected void initData() {
    }

    private void getData() {
        param.put("amount", etPrice.getText().toString());
        RtfUtils.getRtf().freezeOrUnfreezeBalanceShelf(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 FreezeEvent());
                finish();
            }
        });
    }

    @Override
    protected void initEvent() {
    }

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


    @OnClick({R.id.tv_all, R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_all:
                if (TextUtils.equals("-1", type)) {
                    etPrice.setText(MathUtils.converData(info.getFrozenAmount(), 3));
                } else {
                    etPrice.setText(MathUtils.converData(info.getBalance(), 3));
                }
                break;
            case R.id.btn_ok:
                String price = etPrice.getText().toString();
                if (TextUtils.isEmpty(price)) {
                    toast("请输入操作金额");
                    return;
                }
                if (Double.valueOf(price) == 0d) {
                    toast("操作金额不能为0");
                    return;
                }
                getData();
                break;
        }
    }

}
