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

import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
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.event.CheckTicketEvent;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.ui.activity.bat.store.StoreSaleClientActivity;
import com.wd.workoffice.util.DialogUtils;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

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 WorkFinancialReceiveTicketAddActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_next)
    TextView tvNext;
    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.rl_order)
    LinearLayout rlOrder;
    @BindView(R.id.tv_dep)
    TextView tvDep;
    @BindView(R.id.rl_dep)
    LinearLayout rlDep;
    @BindView(R.id.tv_client)
    TextView tvClient;
    @BindView(R.id.rl_client)
    LinearLayout rlClient;
    private Map<String, Object> param;
    private Integer clientId;
    private QMUIDialog.MenuDialogBuilder typeDialog;
    String[] typeList = new String[]{"外采购订单", "供应订单"};
    private String chooseType;
    private Integer depId;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        param = WorkUtils.simpleParam();
        typeDialog = DialogUtils.listDialog(this);
        typeDialog.addItems(typeList, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                chooseType = WorkUtils.getOrderCode(typeList[which]);
                tvOrder.setText(typeList[which]);
                dialog.dismiss();
                if (!hasAddPermission()){
                    toast(R.string.permission);
                }
            }
        });
    }

    @Override
    protected void initData() {
    }


    @Override
    protected void initEvent() {
    }

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


    @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");
            clientId = data.getIntExtra("id", 0);
            tvClient.setText(clientName);
        } else if (requestCode == 10003 && resultCode == 10003) {
            String depName = data.getStringExtra("clientName");
            depId = data.getIntExtra("id", 0);
            tvDep.setText(depName);
        }
    }

    @OnClick({R.id.tv_next, R.id.rl_order, R.id.rl_dep, R.id.rl_client})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_next:
                if (!hasAddPermission()){
                    toast(R.string.permission);
                    return;
                }
                if (TextUtils.isEmpty(chooseType)) {
                    toast("请选择订单类别");
                    return;
                }
                if (depId == null) {
                    toast("请先选择部门");
                    return;
                }
                if (clientId == null) {
                    toast("请先选择供应商");
                    return;
                }
                startActivity(WorkChooseProductActivity.class,
                        "orderType", tvOrder.getText().toString(), "supplier", "选择供应商", "depId", depId + "", "customerId", clientId + "",
                        "customerName", tvClient.getText().toString());
                break;
            case R.id.rl_order:
                typeDialog.show();
                break;
            case R.id.rl_dep:
                startActivityForResult(WorkChooseDepActivity.class, 10003);
                break;
            case R.id.rl_client:
                startActivityForResult(StoreSaleClientActivity.class, 10002,
                        "depId", depId + "", "flag", "1", "code", "SUPPLIER", "supplier", "选择供应商");
                break;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void refresh(CheckTicketEvent event) {
        finish();
    }

    private boolean hasAddPermission() {
        boolean hasPermission = false;
        if (TextUtils.equals(chooseType, "供应订单")) {
            if (!WorkUtils.hasPermission(PagePermissionType.FO_RECEIPT_SAVE.getPermission())) {
                hasPermission = true;
            }
        } else if (TextUtils.equals(chooseType, "外采购订单")) {
            if (!WorkUtils.hasPermission(PagePermissionType.PO_RECEIPT_SAVE.getPermission())) {
                hasPermission = true;
            }
        }
        return hasPermission;
    }
}
