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

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
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.MoneyOutBean;
import com.wd.workoffice.bean.event.CheckMoneytEvent;
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 org.greenrobot.eventbus.EventBus;

import java.util.Map;

import androidx.appcompat.app.AlertDialog;
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 WorkFinancialDetailActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_person)
    TextView tvPerson;
    @BindView(R.id.tv_client)
    TextView tvClient;
    @BindView(R.id.tv_process)
    TextView tvProcess;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_review)
    TextView tvReview;
    @BindView(R.id.tv_review_time)
    TextView tvReviewTime;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    @BindView(R.id.tv_review_remark)
    TextView tvReviewRemark;
    @BindView(R.id.tv_refuse)
    TextView tvRefuse;
    @BindView(R.id.tv_agree)
    TextView tvAgree;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    private MoneyOutBean.RecordsBean dataBean;
    private Map<String, Object> param;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        changePermission();
        dataBean = JSON.parseObject(getIntent().getStringExtra("detail"), MoneyOutBean.RecordsBean.class);
        tvClient.setText(dataBean.getCreatedTime());
        tvCode.setText(dataBean.getSerialNumber());
//        tvDesc.setText();
        tvPerson.setText(dataBean.getApplierName());
        tvRemark.setText(dataBean.getAuditRemark());
        tvReviewRemark.setText(dataBean.getApplyRemark());
        tvPrice.setText(MathUtils.converData(String.valueOf(dataBean.getApplyAmount()), 3));
        tvProcess.setText(dataBean.getProcessName());
        tvReview.setText(dataBean.getUpdatedByName());
        tvReviewTime.setText(dataBean.getUpdatedTime());
        tvStatus.setText(WorkUtils.getFinancialStatus(Integer.valueOf(dataBean.getStatus())));
        tvTime.setText(dataBean.getCreatedTime());
        if (!TextUtils.equals(dataBean.getStatus(),"0")){
            llBottom.setVisibility(View.GONE);
        }
        param = WorkUtils.simpleParam();
        param.put("id", dataBean.getId());
        param.put("applyAmount", dataBean.getApplyAmount());
    }

    @Override
    protected void initData() {

    }


    @Override
    protected void initEvent() {
    }

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


    @OnClick({R.id.tv_refuse, R.id.tv_agree})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_refuse:
                checkDialog(2);
                break;
            case R.id.tv_agree:
                checkDialog(1);
                break;
        }
    }

    private void changePermission() {
        boolean hasPermission;
        if (TextUtils.equals("1", getIntent().getStringExtra("type"))) {
            hasPermission = WorkUtils.hasPermission(PagePermissionType.MONEY_BACK_EXAM.getPermission());
        } else {
            hasPermission = WorkUtils.hasPermission(PagePermissionType.MONEY_PAY_EXAM.getPermission());
        }
        if (!hasPermission) {
            llBottom.setVisibility(View.GONE);
            tvRefuse.setVisibility(View.GONE);
            tvAgree.setVisibility(View.GONE);
        }
    }

    private void checkDialog(int status) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.view_check, null);
        TextView tvOk = view.findViewById(R.id.tv_ok);
        TextView tvDesc = view.findViewById(R.id.tv_desc);
        tvDesc.setText(status == 1 ? "通过" : "拒绝");
        TextView tvNum = view.findViewById(R.id.tv_num);
        TextView tvCancel = view.findViewById(R.id.tv_cancel);
        EditText etRemark = view.findViewById(R.id.et_content);
        etRemark.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tvNum.setText(s.length() + "/140");
            }
        });
        builder.setView(view);
        AlertDialog addCartDialog = builder.create();
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(etRemark.getText().toString())) {
                    toast("请填写备注");
                    return;
                }
                check(status, etRemark.getText().toString());
                addCartDialog.dismiss();
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCartDialog.dismiss();
            }
        });
        addCartDialog.show();
    }


    private void check(int status, String remark) {
        param.put("status", status);
        param.put("auditRemark", remark);
        Observable<BaseBean> observable = RtfUtils.getRtf().checkMoneyBack(WorkUtils.convertMapToBody(param));
        if (TextUtils.equals("1", getIntent().getStringExtra("type"))) {
            observable = RtfUtils.getRtf().checkMoneyBack(WorkUtils.convertMapToBody(param));
        } else {
            observable = RtfUtils.getRtf().checkMoneyPay(WorkUtils.convertMapToBody(param));
        }
        observable.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 CheckMoneytEvent());
                finish();
            }
        });
    }
}
