package nckd.jimin.jyyy.fi.plugin.form;

import kd.bos.algo.DataSet;
import kd.bos.bill.OperationStatus;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IClientViewProxy;
import kd.bos.form.ShowType;
import kd.bos.form.container.Tab;
import kd.bos.form.control.Control;
import kd.bos.form.control.Search;
import kd.bos.form.control.events.SearchEnterEvent;
import kd.bos.form.control.events.SearchEnterListener;
import kd.bos.form.control.events.TabSelectEvent;
import kd.bos.form.control.events.TabSelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import nckd.jimin.jyyy.fi.common.ReimDetailTypeEnum;

import java.util.EventObject;

public class ReimWorkBenchesFormPlugin extends AbstractFormPlugin implements TabSelectListener, SearchEnterListener {

    // 切换报销台
    private static final String BTN_QUICKSEARCH = "btn_quicksearch";

    private static final String NCKD_QUICKLYSEARCH = "nckd_quicklysearch";

    // 页签
    private static final String NCKD_TABAP = "nckd_tabap";

    private static final String NCKD_TAB_FLAG = "nckd_tab_";

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        Tab tab = this.getControl(NCKD_TABAP);
        tab.addTabSelectListener(this);

        this.addClickListeners(BTN_QUICKSEARCH);
    }

    @Override
    public void beforeBindData(EventObject e) {
        super.beforeBindData(e);
        Tab tab = this.getControl(NCKD_TABAP);
        tab.activeTab(NCKD_TAB_FLAG + ReimDetailTypeEnum.REIMING.getKey());
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control control = (Control)evt.getSource();
        switch (control.getKey()){
            case BTN_QUICKSEARCH:
                quicklySearch();
                break;
        }
    }

    protected  void quicklySearch(){
        // 查询数据，哪个有值就激活哪个面板
        Tab tab = this.getControl(NCKD_TABAP);

        ReimDetailTypeEnum detailType = getQucickFilterData();
        if(detailType == null){
            getView().showTipNotification("没有匹配的数据。");
            return;
        }

        String currentTab = tab.getCurrentTab();
        if(currentTab.equals(NCKD_TAB_FLAG + detailType.getKey())){
            // 搜索控件搜索的关键字
            FormShowParameter params = new FormShowParameter();
            params.setFormId("nckd_reimbenchesdetail");
            params.getOpenStyle().setShowType(ShowType.InContainer);
            params.getOpenStyle().setTargetKey(currentTab);
            params.setCustomParam("reimtype",currentTab.replace(NCKD_TAB_FLAG,""));
            params.setStatus(OperationStatus.VIEW);
            getView().showForm(params);
        }else{
            tab.activeTab(NCKD_TAB_FLAG + detailType.getKey());
        }
    }

    /**
     * 通过快速查询查询第一条匹配的数据
     * @return
     */
    protected ReimDetailTypeEnum getQucickFilterData(){
        ReimWorkBenchesHelper service = new ReimWorkBenchesHelper();
        ReimDetailTypeEnum[] detailTypeArray = ReimDetailTypeEnum.values();
        String quickFilter = (String)this.getModel().getValue(NCKD_QUICKLYSEARCH);
        for(ReimDetailTypeEnum detailType : detailTypeArray){

            if(detailType.equals(ReimDetailTypeEnum.APPLY)){
                DataSet rows = service.queryDataApply(null, quickFilter);
                if(rows.hasNext()){
                    return detailType;
                }
            }

            if(detailType.equals(ReimDetailTypeEnum.WAIREIM)){
                DataSet rows = service.queryDataWaitReim(null, quickFilter);
                if(rows.hasNext()){
                    return detailType;
                }
            }

            if(detailType.equals(ReimDetailTypeEnum.REIMING)){
                DataSet rows = service.queryDataReiming(null, quickFilter);
                if(rows.hasNext()){
                    return detailType;
                }
            }


            if(detailType.equals(ReimDetailTypeEnum.COMPLETE)){
                DataSet rows = service.queryDataComplete(null, quickFilter);
                if(rows.hasNext()){
                    return detailType;
                }
            }

            if(detailType.equals(ReimDetailTypeEnum.REPAY)){
                DataSet rows = service.queryDataRepay(null, quickFilter);
                if(rows.hasNext()){
                    return detailType;
                }
            }
        }
        return null;
    }

    @Override
    public void tabSelected(TabSelectEvent event) {
        String tabKey = event.getTabKey();
        // 搜索控件搜索的关键字
        FormShowParameter params = new FormShowParameter();
        params.setFormId("nckd_reimbenchesdetail");
        params.getOpenStyle().setShowType(ShowType.InContainer);
        params.getOpenStyle().setTargetKey(tabKey);
        params.setCustomParam("reimtype",tabKey.replace(NCKD_TAB_FLAG,""));
        params.setStatus(OperationStatus.VIEW);
        getView().showForm(params);

    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String name = e.getProperty().getName();
        if(NCKD_QUICKLYSEARCH.equals(name)){
            quicklySearch();
        }
    }

    @Override
    public void search(SearchEnterEvent event) {
//        Search search = (Search)event.getSource();
//        String key = search.getKey();
//        String text = event.getText();
//        // 查询单据，判断单据属于哪个页签，然后打开响应页签
//        if(NCKD_TABAP.equals(key)){
//            quicklySearch();
//        }
    }
}
