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

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
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.AddDeviceEvent;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.util.DialogUtils;
import com.wd.workoffice.util.PickTimeUtils;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import flexible.xd.android_base.utils.TimeUtils;
import io.reactivex.disposables.Disposable;

/**
 * 设备维修新增
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class DeviceBuyRepairAddActivity extends WorkToolBarActivity {

    @BindView(R.id.tv_dep)
    TextView tvDep;
    @BindView(R.id.rl_dep)
    RelativeLayout rlDep;
    @BindView(R.id.et_price)
    EditText etPrice;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.rl_time)
    RelativeLayout rlTime;
    @BindView(R.id.tv_product)
    TextView tvProduct;
    @BindView(R.id.rl_product)
    RelativeLayout rlProduct;
    private QMUIDialog.MenuDialogBuilder depDialog;
    private List<DepBean> depList;
    List<String> depNameList = new ArrayList<>();
    private DepBean chooseDep;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        depDialog = DialogUtils.listDialog(this);
        WorkUtils.addDecimalsListener(etPrice);
    }

    @Override
    protected void initData() {
//        batSalePresenter.getDep(PermissionType.REPAIR_BUY.getCode());
        Map<String, Object> param = WorkUtils.simpleParam();
        param.put("includeCO", "0");
        RtfUtils.getRtf().getDep(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.getMessage());
                    return;
                }
                List<DepBean> getList = JSON.parseArray(data.getData().toString(), DepBean.class);
                depList = getList;
                depNameList.clear();
                for (DepBean dep : getList) {
                    depNameList.add(dep.getName());
                }
                depDialog.addItems(depNameList.toArray(new String[depNameList.size()]), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        chooseDep = depList.get(which);
                        tvDep.setText(depNameList.get(which));
                        dialog.dismiss();
                    }
                });
            }
        });
    }

    @Override
    protected void initEvent() {
    }

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


    @OnClick({R.id.rl_dep, R.id.rl_time, R.id.rl_product})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_time:
                PickTimeUtils.showPickThree(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View view) {
                        tvTime.setText(TimeUtils.date2String(date, new SimpleDateFormat("yyyy-MM-dd")));
                    }
                }).show();
                break;
            case R.id.rl_dep:
                depDialog.show();
                break;
            case R.id.rl_product:
                if (chooseDep == null) {
                    toast("请选择接单部门");
                    return;
                }
                if (TextUtils.isEmpty(etPrice.getText().toString())) {
                    toast("请填写金额");
                    return;
                }
                if (TextUtils.isEmpty(tvTime.getText().toString())) {
                    toast("请填写时间");
                    return;
                }
                startActivity(DeviceBuyRepairProductActivity.class,
                        "depId", chooseDep.getId() + "",
                        "depName", chooseDep.getName(),
                        "amount", etPrice.getText().toString(),
                        "repairDateTime", tvTime.getText().toString());
                break;
        }
    }
    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

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

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(AddDeviceEvent event) {
        finish();
    }
}
