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

import android.content.DialogInterface;
import android.view.View;
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.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.PermissionType;
import com.wd.workoffice.contract.BatSaleContract;
import com.wd.workoffice.presenter.BatSalePresenter;
import com.wd.workoffice.util.DialogUtils;
import com.wd.workoffice.util.UserKeeper;
import com.wd.workoffice.util.WorkUtils;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 商店-坩埚
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class StoreCrucibleActivity extends WorkToolBarActivity implements BatSaleContract.View {
    @BindView(R.id.tv_dep)
    TextView tvDep;
    @BindView(R.id.rl_dep)
    RelativeLayout rlDep;
    @BindView(R.id.tv_warehouse)
    TextView tvWarehouse;
    @BindView(R.id.rl_warehouse)
    RelativeLayout rlWarehouse;
    @BindView(R.id.tv_product)
    TextView tvProduct;
    @BindView(R.id.rl_product)
    RelativeLayout rlProduct;
    private BatSalePresenter batSalePresenter;
    private QMUIDialog.MenuDialogBuilder depDialog;
    private QMUIDialog.MenuDialogBuilder stockDialog;
    private List<DepBean> depList;
    List<String> depNameList = new ArrayList<>();
    private DepBean chooseDep;
    private Map<String, Object> stockParam;
    private List<StockBean> stockList;
    private List<String> stockNameList=new ArrayList<>();
    private StockBean chooseStock;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        batSalePresenter = new BatSalePresenter();
        batSalePresenter.onAttach(this);
        depDialog = DialogUtils.listDialog(this);
        stockDialog = DialogUtils.listDialog(this);
    }

    @Override
    protected void initData() {
        batSalePresenter.getDep(PermissionType.CRUCIBLE_SELL.getCode());
        stockParam = WorkUtils.simpleParam();
        stockParam.put("deptId", UserKeeper.getInstance().getUserDepId());
        batSalePresenter.getStock(stockParam);
//        stockParam.put("sign","send");
//        stockParam.put("customerId", UserKeeper.getInstance().getUserId());
    }

    @Override
    protected void initEvent() {
    }

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


    @OnClick({ R.id.rl_product, R.id.rl_warehouse, R.id.rl_dep})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_dep:
                depDialog.show();
                break;
            case R.id.rl_warehouse:
                stockDialog.show();
                break;
            case R.id.rl_product:
                if (chooseDep == null) {
                    toast("请先选择接单部门");
                    return;
                }
                if (chooseStock == null) {
                    toast("请选择入库仓库");
                    return;
                }
                startActivity(StoreSaleProductActivity.class, "depId", chooseDep.getId() + "",
                        "depName", chooseDep.getName(),  "code", PermissionType.PROCESS_CRUCIBLE.getCode());
                break;
        }
    }

    @Override
    public void processList(List<ProcessBean> dataList) {
    }


    @Override
    public void depList(List<DepBean> dataList) {
        depList = dataList;
        for (DepBean dep : dataList) {
            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
    public void getStock(List<StockBean> dataList) {
        stockList = dataList;
        for (StockBean dep : dataList) {
            stockNameList.add(dep.getName());
        }
        stockDialog.addItems(stockNameList.toArray(new String[stockNameList.size()]), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                chooseStock = stockList.get(which);
                tvWarehouse.setText(stockNameList.get(which));
                dialog.dismiss();
            }
        });
    }

}
