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.PagePermissionType;
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.WorkUtils;

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

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 StoreOutsourcingActivity extends WorkToolBarActivity implements BatSaleContract.View {

    @BindView(R.id.tv_client)
    TextView tvClient;
    @BindView(R.id.rl_client)
    RelativeLayout rlClient;
    @BindView(R.id.tv_dep)
    TextView tvDep;
    @BindView(R.id.rl_dep)
    RelativeLayout rlDep;
    @BindView(R.id.tv_process)
    TextView tvProcess;
    @BindView(R.id.rl_process)
    RelativeLayout rlProcess;
    @BindView(R.id.tv_product)
    TextView tvProduct;
    @BindView(R.id.rl_product)
    RelativeLayout rlProduct;
    @BindView(R.id.tv_supplier)
    TextView tvSupplier;
    @BindView(R.id.rl_supplier)
    RelativeLayout rlSupplier;
    private BatSalePresenter batSalePresenter;
    private QMUIDialog.MenuDialogBuilder depDialog;
    private QMUIDialog.MenuDialogBuilder processDialog;
    private List<DepBean> depList;
    List<String> depNameList = new ArrayList<>();
    private DepBean chooseDep;
    private Integer clientId;
    private List<String> processNameList = new ArrayList<>();
    private List<ProcessBean> processList;
    private ProcessBean chooseProcess;
    private Integer supplierId;

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

    @Override
    protected void initData() {
        batSalePresenter.getDep(PermissionType.PURCHASE_SELL.getCode());
    }

    @Override
    protected void initEvent() {
    }

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


    @OnClick({R.id.rl_client, R.id.rl_dep, R.id.rl_process, R.id.rl_product, R.id.rl_supplier})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_client:
                startActivityForResult(StoreSaleClientActivity.class, 10001, "code", "CUSTOMER");
                break;
            case R.id.rl_dep:
                depDialog.show();
                break;
            case R.id.rl_supplier:
                if (clientId == null) {
                    toast("请先选择客户");
                    return;
                }
                if (chooseDep == null) {
                    toast("请先选择接单部门");
                    return;
                }
                startActivityForResult(StoreSaleClientActivity.class, 10002,
                        "depId", chooseDep.getId() + "", "flag", "1", "code", "SUPPLIER", "supplier", "选择供应商");
                break;
            case R.id.rl_process:
                if (!WorkUtils.hasPermission(PagePermissionType.SUB_PROCESS.getPermission())){
                    toast(R.string.permission);
                    return;
                }
                if (clientId == null) {
                    toast("请先选择客户");
                    return;
                }
                processDialog.show();
                break;
            case R.id.rl_product:
                if (clientId == null) {
                    toast("请选择客户");
                    return;
                }
                if (chooseDep == null) {
                    toast("请选择部门");
                    return;
                }
                if (supplierId == null) {
                    toast("请选择供应商");
                    return;
                }
                if (chooseProcess == null) {
                    toast("请选择子流程");
                    return;
                }
                startActivity(StoreSaleProductActivity.class, "depId", chooseDep.getId() + "",
                        "subProcess", chooseProcess.getName(),
                        "clientName", tvClient.getText().toString(),
                        "depName", chooseDep.getName(),
                        "customerId", clientId.toString(),
                        "transitPartId", supplierId.toString(),
                        "supplierId", supplierId.toString(),
                        "supplierName", tvSupplier.getText().toString(),
                        "subProcessId", chooseProcess.getId() + "",
                        "code", PermissionType.PROCESS_PURCHASE.getCode());
                break;
        }
    }

    @Override
    public void processList(List<ProcessBean> dataList) {
        processList = dataList;
        processNameList.clear();
        for (ProcessBean process : dataList) {
            processNameList.add(process.getName());
        }
        processDialog.addItems(processNameList.toArray(new String[processNameList.size()]), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                chooseProcess = processList.get(which);
                tvProcess.setText(processNameList.get(which));
                dialog.dismiss();
            }
        });
    }


    @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) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10001 && resultCode == 10001) {
            String clientName = data.getStringExtra("clientName");
            clientId = data.getIntExtra("id", 0);
            tvClient.setText(clientName);
            batSalePresenter.getProcess(PermissionType.PROCESS_SALE.getCode(), clientId);
        } else if (requestCode == 10002 && resultCode == 10002) {
            String clientName = data.getStringExtra("clientName");
            supplierId = data.getIntExtra("id", 0);
            tvSupplier.setText(clientName);
        }
    }

}
