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

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
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.ProcessBean;
import com.wd.workoffice.bean.StockBean;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.bean.workEnum.PermissionType;
import com.wd.workoffice.contract.BatSaleContract;
import com.wd.workoffice.presenter.BatSalePresenter;
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.ui.activity.bat.store.StoreSaleProductActivity;
import com.wd.workoffice.util.DialogUtils;
import com.wd.workoffice.util.WorkUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
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 WorkDeviceBuyAddActivity extends WorkToolBarActivity  {

    @BindView(R.id.tv_dep)
    TextView tvDep;
    @BindView(R.id.rl_dep)
    RelativeLayout rlDep;
    @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);
    }

    @Override
    protected void initData() {
//        batSalePresenter.getDep(PermissionType.REPAIR_BUY.getCode());
        RtfUtils.getRtf().depList().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;
    }


    @OnClick({ R.id.rl_dep, R.id.rl_product})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_dep:
                depDialog.show();
                break;
            case R.id.rl_product:
                if (chooseDep == null) {
                    toast("请选择接单部门");
                    return;
                }
                startActivity(WorkDeviceBuyProductActivity.class,
                        "depId", chooseDep.getId() + "",
                        "depName", chooseDep.getName() + "");
                break;
        }
    }


}
