package yem.wm.bd.addsupplier.dynamic;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.field.BasedataEdit;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.basedata.BaseDataServiceHelper;
import yem.wm.bd.util.BothCardHelper;

import java.util.EventObject;

/**
 * @Description 添加供应商
 * 1、加载界面时，默认获取单据中的{抬头明细(隐藏)}中的供应商信息自动给界面赋值
 * 2、切换【供应商】，重新加载【供应商】的默认信息
 * 3、点击确定给父页面返回数据
 * @Author: zhangh
 * @CreateTime: 2025-02-08  16:32
 */
public class AddSupplierDynamicPlugin extends AbstractBillPlugIn {

    @Override
    public void afterCreateNewData(EventObject e) {
        BothCardHelper.init(this);//加载父页面的信息携带到动态表单上
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("yem_supplierbank", "yem_supplierlinkman");
        this.addClickListeners("btnok");
        this.initF7();
    }

    /**
     * @return void
     * @Description: 对供应商进行添加事件的注册以及过滤
     * @function 功能
     * @purposes 用途
     */
    private void initF7() {
        IFormView parentView = this.getView().getParentView();
        IDataModel parentModel = parentView.getModel();
        DynamicObject org = (DynamicObject) parentModel.getValue("org");//获取父页面的【组织】
        BasedataEdit baseDataCtr = this.getControl("yem_supplier");//获取【供应商】字段控件
        if (baseDataCtr == null) {
            return;
        }
        baseDataCtr.addBeforeF7SelectListener((beforeF7SelectEvent) -> {
            ListShowParameter showParameter = (ListShowParameter) beforeF7SelectEvent.getFormShowParameter();
            if (org != null) {
                QFilter filter = BaseDataServiceHelper.getBaseDataFilter(showParameter.getBillFormId(), org.getLong("id"));//根据【组织】对【供应商】字段进行过滤
                if (filter != null) {
                    filter.and(new QFilter("status", "=", "C"));
                } else {
                    filter = new QFilter("status", "=", "C");
                }
                showParameter.getListFilterParameter().getQFilters().add(filter);
            }
        });
    }

    /**
     * @param e
     * @return void
     * @Description: 切换字段发生只改变
     * @function 切换【供应商】发生改变
     * @purposes 用途
     */
    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String key = e.getProperty().getName();
        switch (key) {
            case "yem_supplier"://供应商
                BothCardHelper.setSupplierInfor(this);
                break;
        }
    }

    @Override
    public void click(EventObject evt) {
        Control c = (Control) evt.getSource();
        String key = c.getKey().toLowerCase();
        switch (key) {
            case "yem_supplierbank":// 开户行
                BothCardHelper.showChangePage(this, "yem_bd_supplierbankinfor");//跳转动态表单
                break;
            case "yem_supplierlinkman":// 点击联系人
                BothCardHelper.showChangePage(this, "yem_bd_suppliermaninfor");//跳转动态表单
                break;
            case "btnok"://确定按钮
                this.btnOk();//点击确定按钮返回数据
                break;
        }
    }

    /**
     * @return void
     * @Description: 点击确定给父页面返回信息
     * @function 功能
     * @purposes 用途
     */
    private void btnOk() {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();

        BothCardHelper.btnOkParent(this);//将动态表单的信息返回给付页面
        DynamicObject supplier = (DynamicObject) model.getValue("yem_supplier");//获取供应商
        String companyName = supplier != null ? supplier.getString("name") : "";
        parentModel.setValue("yem_suppliername", companyName);//供应商名称

        view.sendFormAction(parentView);
        view.close();
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent e) {
        String id = e.getActionId();
        Object returnData = e.getReturnData();
        switch (id) {
            case "yem_bd_supplierbankinfor":
                BothCardHelper.setSuppBankInfor(this, returnData);
                break;
            // 联系人信息回填
            case "yem_bd_suppliermaninfor":
                BothCardHelper.setCustManInfor(this, returnData);//点击《供应商联系人信息》之后，给父页面的信息赋值
                break;
        }
    }
}
