package com.yem.wm.bc.companytitle.dynamic;

import com.yem.wm.bc.companytitle.util.TitleFuncUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;

import java.util.EventObject;

/**
 * @author xwudd
 * @date 16:51
 * @Description
 **/
public class AddCompanyTitleEdit extends AbstractFormPlugin implements BeforeF7SelectListener {

    @Override
    public void registerListener(EventObject e) {
        this.addClickListeners("yem_companybankacc");
    }

    @Override
    public void click(EventObject e) {
        Control control = (Control)e.getSource();
        String key = control.getKey();
        switch (key){
            case "yem_companybankacc" :
                selectInfo();
                break;
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String name = e.getProperty().getName();
        switch(name){
            case "yem_company" :
                setInfo();
                break;
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        String name = e.getProperty().getName();
        FormShowParameter fsp = this.getView().getFormShowParameter();
        switch (name){
            case "yem_company" :
                DynamicObject obj = fsp.getCustomParam("obj");
                if(obj != null){
                    e.addCustomQFilter(new QFilter("yem_erporg", QCP.equals,obj.getPkValue()));
                }
                break;
        }
    }


    /**
     * @description 选择银行信息
     * @author xwudd
     * return
     * @time 2023/7/26 16:54
     */
    public void selectInfo(){
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        DynamicObject company = (DynamicObject) model.getValue("yem_company");
        if(company != null){
            FormShowParameter fsp = new FormShowParameter();
            fsp.setFormId("yem_companybankinfor");
            fsp.getOpenStyle().setShowType(ShowType.Modal);
            fsp.setCustomParam("company",company);
            view.showForm(fsp);
        }else{
            view.showErrorNotification("请选择公司抬头！");
        }
    }

    public void setInfo(){
        IDataModel model = this.getModel();
        DynamicObject company = (DynamicObject)model.getValue("yem_company");
        TitleFuncUtil.setCompanyInfo(model,company);
    }



}
