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 StoreSaleActivity 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;
    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;

    @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.ES_SELL.getCode());
    }

    @Override
    protected void initEvent() {
    }

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


    @OnClick({R.id.rl_client, R.id.rl_dep, R.id.rl_process, R.id.rl_product})
    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_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 (chooseProcess == null) {
                    toast("请选择子流程");
                    return;
                }
                startActivity(StoreSaleProductActivity.class,
                        "depId", chooseDep.getId() + "",
                        "process", chooseProcess.getName(),
                        "subProcessId", chooseProcess.getId() + "",
                        "clientName", tvClient.getText().toString(),
                        "depName", chooseDep.getName(),
                        "transitPartId", clientId.toString(),
                        "customerId", clientId.toString(),
                        "code", PermissionType.PROCESS_SALE.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);
        }
    }
}
