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

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.AccountBean;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
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 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 WorkAccountActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_dep_name)
    TextView tvDpeName;
    @BindView(R.id.tv_dep_price)
    TextView tvDpePrice;
    @BindView(R.id.tv_dep_freeze_price)
    TextView tvDpeFreezePrice;
    @BindView(R.id.tv_see_flow)
    TextView tvSeeFlow;
    private AccountBean dataBean;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        if (!WorkUtils.hasPermission(PagePermissionType.BALANCE_DETAILS.getPermission())) {
            toast(R.string.permission);
        } else {
            getData();
        }
    }

    private void getData() {
        RtfUtils.getRtf().getAccount().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;
                }
                dataBean = JSON.parseObject(data.getData().toString(), AccountBean.class);
                tvName.setText(dataBean.getName());
                tvDpeName.setText(dataBean.getName());
                tvDpePrice.setText(MathUtils.converData(dataBean.getBalance(), 3));
                tvDpeFreezePrice.setText(MathUtils.converData(dataBean.getFrozenAmount(),3));
            }
        });
    }

    @Override
    protected void initEvent() {
    }

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


    @OnClick(R.id.tv_see_flow)
    public void onViewClicked() {
        if (!WorkUtils.hasPermission(PagePermissionType.FINANCE_SHELF_LIST.getPermission())) {
            toast(R.string.permission);
            return;
        }
        if (dataBean == null) {
            toast("正在加载中，请稍后");
            return;
        }
        startActivity(WorkAccountFlowActivity.class, "id", dataBean.getDeptId() + "");
    }
}
