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

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

import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.event.CheckMoneytEvent;
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.util.DialogUtils;
import com.wd.workoffice.util.UserKeeper;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import androidx.annotation.Nullable;
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 WorkFinancialMoneyAddActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_client)
    TextView tvClient;
    @BindView(R.id.rl_client)
    RelativeLayout rlClient;
    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.rl_order)
    RelativeLayout rlOrder;
    @BindView(R.id.rl_price)
    LinearLayout rlPrice;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.tv_remark_num)
    TextView tvRemarkNum;
    @BindView(R.id.btn_ok)
    Button btnOk;
    @BindView(R.id.et_price)
    EditText etPrice;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    private Map<String, Object> param;
    private Integer clientId;
    private QMUIDialog.MenuDialogBuilder typeDialog;
    String[] typeList = new String[]{"销售订单", "外采购订单", "组装订单"};
    String[] supplierTypeList = new String[]{"外采购订单", "供应订单"};
    private String chooseType;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        param = WorkUtils.simpleParam();
        typeDialog = DialogUtils.listDialog(this);

        if (!TextUtils.isEmpty(getIntent().getStringExtra("supplier"))) {
            tvDesc.setText("所属供应商");
            tvClient.setHint("选择供应商");
            typeDialog.addItems(supplierTypeList, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    chooseType = WorkUtils.getOrderCode(supplierTypeList[which]);
                    tvOrder.setText(supplierTypeList[which]);
                    dialog.dismiss();
                }
            });
        } else {
            typeDialog.addItems(typeList, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    chooseType = WorkUtils.getOrderCode(typeList[which]);
                    tvOrder.setText(typeList[which]);
                    dialog.dismiss();
                }
            });
        }
        WorkUtils.addNumListener(etRemark,tvRemarkNum);
    }

    @Override
    protected void initData() {
    }

    private void getData() {
        Observable<BaseBean> observable;
        if (!TextUtils.isEmpty(getIntent().getStringExtra("supplier"))) {
            param.put("supplierId", clientId);
            observable = RtfUtils.getRtf().moneyPayApply((WorkUtils.convertMapToBody(param)));
        } else {
            param.put("customerId", clientId);
            observable = RtfUtils.getRtf().moneyBackApply((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;
                }
                finish();
                toast("操作成功");
                EventBus.getDefault().post(new CheckMoneytEvent());
            }
        });
    }

    @Override
    protected void initEvent() {
    }

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


    @OnClick({R.id.rl_client, R.id.rl_order, R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_client:
                if (!TextUtils.isEmpty(getIntent().getStringExtra("supplier"))) {
                    startActivityForResult(StoreSaleClientActivity.class, 10002,
                            "code", "SUPPLIER","supplier","选择供应商","depId",UserKeeper.getInstance().getUserDepId(), "flag", "1");
                } else {
                    startActivityForResult(MoneyAddClientActivity.class, 10001);
                }
                break;
            case R.id.rl_order:
                typeDialog.show();
                break;
            case R.id.btn_ok:
                if (clientId == null) {
                    toast("请先选择客户");
                    return;
                }
                if (TextUtils.isEmpty(chooseType)) {
                    toast("请选择订单类别");
                    return;
                }
                if (TextUtils.isEmpty(etPrice.getText().toString())) {
                    toast("请输入金额");
                    return;
                }
                param.put("applyRemark", etRemark.getText().toString());
                param.put("applyDept", UserKeeper.getInstance().getUserDepId());
                param.put("orderType", chooseType);
                param.put("applyAmount", etPrice.getText().toString());
                getData();
                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);
            tvClient.setText(clientName);
        } else if (requestCode == 10002 && resultCode == 10002) {
            String depName = data.getStringExtra("clientName");
            clientId = data.getIntExtra("id", 0);
            tvClient.setText(depName);
        }
    }

}
