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

import android.content.DialogInterface;
import android.content.Intent;
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 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 StoreSupplierActivity extends WorkToolBarActivity implements BatSaleContract.View {

    @BindView(R.id.tv_supplier)
    TextView tvSupplier;
    @BindView(R.id.rl_supplier)
    RelativeLayout rlSupplier;
    @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;
    @BindView(R.id.tv_dep)
    TextView tvDep;
    @BindView(R.id.rl_dep)
    RelativeLayout rlDep;
    private BatSalePresenter batSalePresenter;
    private QMUIDialog.MenuDialogBuilder depDialog;
    private QMUIDialog.MenuDialogBuilder stockDialog;
    private List<DepBean> depList;
    List<String> depNameList = new ArrayList<>();
    private DepBean chooseDep;
    private Integer supplierId;
    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.SUPPLY_SELL.getCode());
        stockParam = WorkUtils.simpleParam();
//        stockParam.put("sign", "SEND");
        stockParam.put("deptId", UserKeeper.getInstance().getUserDepId());
    }

    @Override
    protected void initEvent() {
    }

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


    @OnClick({R.id.rl_supplier, R.id.rl_product, R.id.rl_warehouse, R.id.rl_dep})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_supplier:
                if (chooseDep == null) {
                    toast("请先选择接单部门");
                    return;
                }
                startActivityForResult(StoreSaleClientActivity.class, 10002,
                        "flag","1","depId",chooseDep.getId()+"","code", "SUPPLIER", "supplier", "选择供应商");
                break;
            case R.id.rl_dep:
                depDialog.show();
                break;
            case R.id.rl_warehouse:
                if (supplierId == null) {
                    toast("请先选择供应商");
                    return;
                }
                stockDialog.show();
                break;
            case R.id.rl_product:
                if (chooseDep == null) {
                    toast("请先选择接单部门");
                    return;
                }
                if (supplierId == null) {
                    toast("请先选择供应商");
                    return;
                }
                if (chooseStock == null) {
                    toast("请选择入库仓库");
                    return;
                }
                startActivity(StoreSaleProductActivity.class,
                        "depId", chooseDep.getId() + "",
                        "depName", chooseDep.getName(),
                        "inStockId", chooseStock.getId()+"",
                        "customerId", "",
                        "clientName", "",
                        "subProcess", "",
                        "supplierId", supplierId.toString(),
                        "supplierName", tvSupplier.getText().toString(),
                        "transitPartId", supplierId.toString(),
                        "code", PermissionType.PROCESS_SUPPLY.getCode());
                break;
        }
    }

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


    @Override
    public void depList(List<DepBean> dataList) {
        depList = dataList;
        depNameList.clear();
        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;
        stockNameList.clear();
        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();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10002 && resultCode == 10002) {
            String clientName = data.getStringExtra("clientName");
            supplierId = data.getIntExtra("id", 0);
            tvSupplier.setText(clientName);
//            stockParam.put("customerId", supplierId);
            batSalePresenter.getStock(stockParam);
        }
    }

}
