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

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.event.CheckLogisticEvent;
import com.wd.workoffice.bean.event.CheckTicketEvent;
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 org.greenrobot.eventbus.Subscribe;

import java.util.Map;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 工作台 - 财务  - 物流新增
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkFinancialLogisticAddActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_next)
    TextView tvNext;
    @BindView(R.id.tv_dep)
    TextView tvDep;
    @BindView(R.id.rl_transport)
    LinearLayout rlTransport;
    @BindView(R.id.tv_process)
    TextView tvProcess;
    @BindView(R.id.rl_process)
    LinearLayout rlProcess;
    @BindView(R.id.rl_dep)
    LinearLayout rlDep;
    @BindView(R.id.tv_transport)
    TextView tvTransport;
    private Map<String, Object> param;
    private Integer clientId;
    private QMUIDialog.MenuDialogBuilder typeDialog;
    String[] typeList = new String[]{"销售订单", "外采购订单"};
    private String chooseType;
    private Integer depId;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        tvDep.setText(UserKeeper.getInstance().getUserDepName());
        param = WorkUtils.simpleParam();
        typeDialog = DialogUtils.listDialog(this);
        typeDialog.addItems(typeList, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                chooseType =typeList[which];
                tvProcess.setText(typeList[which]);
                dialog.dismiss();
            }
        });
    }

    @Override
    protected void initData() {
    }


    @Override
    protected void initEvent() {
    }

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


    @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);
            tvTransport.setText(clientName);
        } else if (requestCode == 10003 && resultCode == 10003) {
            String depName = data.getStringExtra("clientName");
            depId = data.getIntExtra("id", 0);
            tvDep.setText(depName);
        }
    }


    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void refresh(CheckLogisticEvent event) {
        finish();
    }


    @OnClick({R.id.tv_next, R.id.rl_transport, R.id.rl_process})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_transport:
                startActivityForResult(StoreSaleClientActivity.class, 10001,
                        "flag", "1", "supplier", "选择运输商", "code", "TRANSPORTERS");
                break;
            case R.id.rl_process:
                typeDialog.show();
                break;
            case R.id.tv_next:
//                if (depId == null) {
//                    toast("请先选择部门");
//                    return;
//                }
                if (clientId == null) {
                    toast("请先选择运输商");
                    return;
                }
                if (TextUtils.isEmpty(chooseType)) {
                    toast("请选择订单类型");
                    return;
                }
                startActivity(WorkFinancialLogisticsProActivity.class,
                        "logisticsId", clientId + "", "orderType", tvProcess.getText().toString(), "depId", UserKeeper.getInstance().getUserDepId());
                break;
        }
    }
}
