package com.wd.workoffice.ui.activity.login;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
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.DepBean;
import com.wd.workoffice.bean.event.AddDepEvent;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.bat.order.SendApplyAddActivity;
import com.wd.workoffice.util.DialogUtils;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

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 UserRegisterDepAddActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_code)
    EditText etCode;
    private String[] type = new String[]{"生产部门", "销售部门", "业务部门"};
    private Map<String, Object> param;
    private QMUIDialog.MenuDialogBuilder typeDialog;
    private int chooseType;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        typeDialog = DialogUtils.listDialog(this);
        typeDialog.addItems(type, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                chooseType = which + 1;
                tvType.setText(type[which]);
                dialog.dismiss();
            }
        });
    }


    @Override
    protected void initData() {
        param = WorkUtils.simpleParam();

    }

    private void submit() {
        if (chooseType == 0) {
            toast("请选择类型");
            return;
        }
        String name = etName.getText().toString();
        String code = etCode.getText().toString();
        if (TextUtils.isEmpty(name)) {
            toast("请填写部门名称");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            toast("请填写code");
            return;
        }
        param.put("type", chooseType);
        param.put("name", name);
        param.put("code", code);
        DepBean depBean = new DepBean();
        depBean.setType(chooseType);
        depBean.setName(name);
        depBean.setCode(code);
        depBean.setOfRegisterNewDept(1);
        EventBus.getDefault().post(new AddDepEvent(depBean));
        finish();

//        RtfUtils.getRtf().addDep(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.getMsg());
//                    return;
//                }
//                EventBus.getDefault().post(new AddDepEvent());
//                toast("添加成功");
//                finish();
//            }
//        });
    }


    @Override
    protected void initEvent() {
    }

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


    @OnClick(R.id.tv_type)
    public void onViewClicked() {
        typeDialog.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_finish, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.finish:
                submit();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
