package yem.wmzh.csm.customeraccess.dynamic;

import com.alibaba.nacos.common.utils.StringUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.ILocaleString;
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.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import yem.base.common.utils.YEM;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 客商地址动态表单插件
 * @Author: LiuHB
 * @CreateTime: 2025-02-10  10:53
 */
public class CusdressFormPlugin extends AbstractFormPlugin {

    /**
     * @Description: 在此事件，可以侦听各个控件的插件事件
     * @function 1.侦听确认和取消按钮
     * @purposes 用户与界面上的控件进行交互时，触发此事件。
     */
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("btnok", "btncancel"); // 侦听确认和取消按钮
    }

    /**
     * @Description: 界面初始化
     * @function 加载客商准入单数据到动态表单
     * @purposes 对已创建好的模型数据包进一步加工
     */
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        init();
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        String name = e.getProperty().getName();
        switch (name) {
            case  "yem_addressnumber":
                DynamicObject bd_address = BusinessDataServiceHelper.loadSingle("bd_address", new QFilter[]{new QFilter("number", QCP.equals, (String) model.getValue("yem_addressnumber"))});
                if(null!= bd_address){
                    model.setValue("yem_addressnumber",null);
                    view.showErrorNotification("编码重复请重新填写");
                }
        }

    }

    /**
     * @Description: 插件可以在此相应点击事件，如弹出单独的编辑界面。
     * @function 回填客商地址基础资料数据到分录
     * @purposes 点击文本字段的按钮时，触发此事件
     */
    @Override
    public void click(EventObject evt) {
        super.click(evt);
        // 获取被点击的控件对象
        Control source = (Control) evt.getSource();
        String key = source.getKey();
        if (StringUtils.equals("btnok", key)) {
            //回传地址信息
            setaddressinformation();
            this.getView().close();
            return;
        } else {
            this.getView().close();
            return;
        }
    }

    /**
     * @Description: 初始化动态表单数据
     * @purposes 加载客商准入单数据到动态表单
     */

    private void init() {
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        IDataModel model = this.getModel();
        Map<String, Object> maps = showParameter.getCustomParams();
        Map<String, Object> parenrow = (Map) maps.get("row");//父页面选中行
        model.setValue("yem_addressnumber",parenrow.get("yem_addressnumber"));
        model.setValue("yem_addressname",parenrow.get("yem_addressname"));
        Map<String,Object> yemTradeterms = (Map<String,Object>)parenrow.get("yem_tradeterms");
        Long yemTradetermsid = null != yemTradeterms ? (Long)yemTradeterms.get("id") : 0L;
        model.setValue("yem_tradeterms",yemTradetermsid);
        model.setValue("yem_clearanceco",parenrow.get("yem_clearanceco"));
        model.setValue("yem_forwarderco",parenrow.get("yem_forwarderco"));
        model.setValue("yem_addressphone",parenrow.get("yem_addressphone"));
        model.setValue("yem_zipcode",parenrow.get("yem_zipcode"));
        model.setValue("yem_linkman",parenrow.get("yem_linkman"));
        Map<String,Object> yemTimezone = ( Map<String,Object>)parenrow.get("yem_timezone");
        long yemTimezoneid = null != yemTimezone ? (Long) yemTimezone.get("id") : 0L;
        model.setValue("yem_timezone",yemTimezoneid);
        model.setValue("yem_addressenable",parenrow.get("yem_addressenable"));
        model.setValue("yem_addressdefault",parenrow.get("yem_addressdefault"));
        String admindivision = (String) parenrow.get("yem_admindivision");
        Long yemadmindivisionid = 0L;
        if(!YEM.isEmpty(admindivision)){
             yemadmindivisionid = Long.valueOf(admindivision);
        }
        model.setValue("yem_admindivision",yemadmindivisionid);
        model.setValue("yem_detailaddress",parenrow.get("yem_detailaddress"));
        Map<String,Object> yemCustomeraddrsspur = (Map<String,Object>)parenrow.get("yem_customeraddrsspur");
        Long yemCustomeraddrsspurid = null != yemCustomeraddrsspur ? (Long)yemCustomeraddrsspur.get("id") : 0L;
        model.setValue("yem_customeraddrsspur",yemCustomeraddrsspurid);
    }

    /**
     * @Description: 回传地址信息到客商准入单
     */
    private void setaddressinformation() {
        IDataModel model = this.getModel();
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        Map<String, Object> maps = showParameter.getCustomParams();
        Map<String, Object> parenrow = (Map) maps.get("row");//父页面选中行
        int parenseq = (int) parenrow.get("seq");//父页面选中行号
        int indexof = parenseq - 1;
        Map<String, Object> hashMap = new HashMap<>();
        String yemAddressnumber = (String) model.getValue("yem_addressnumber");//编码
        ILocaleString yemAddressname = (ILocaleString) model.getValue("yem_addressname");//名称
        DynamicObject yemTradeterms = (DynamicObject) model.getValue("yem_tradeterms");//贸易术语
        String yemClearanceco = (String) model.getValue("yem_clearanceco");//清关公司
        String yemForwarderco = (String) model.getValue("yem_forwarderco");//货代公司
        ILocaleString yemAddressphone = (ILocaleString) model.getValue("yem_addressphone");//联系电话
        String yemZipcode = (String) model.getValue("yem_zipcode");//邮政编码
        String yemLinkman = (String) model.getValue("yem_linkman");//传真
        DynamicObject yemTimezone = (DynamicObject) model.getValue("yem_timezone");//时区
        String yemAddressenable = (String) model.getValue("yem_addressenable");//使用状态
        Boolean yemAddressdefault = (Boolean) model.getValue("yem_addressdefault");//默认
        String yemAdmindivision = (String) model.getValue("yem_admindivision");//行政区划
        ILocaleString yemDetailaddress = (ILocaleString) model.getValue("yem_detailaddress");//详细地址
        DynamicObject yemCustomeraddrsspur = (DynamicObject) model.getValue("yem_customeraddrsspur");//客户地址用途
        hashMap.put("yem_associatedaddress", yemAddressname);
        hashMap.put("yem_addressnumber", yemAddressnumber);
        hashMap.put("yem_addressname", yemAddressname);
        hashMap.put("yem_tradeterms", yemTradeterms);
        hashMap.put("yem_clearanceco", yemClearanceco);
        hashMap.put("yem_forwarderco", yemForwarderco);
        hashMap.put("yem_addressphone", yemAddressphone);
        hashMap.put("yem_zipcode", yemZipcode);
        hashMap.put("yem_linkman", yemLinkman);
        hashMap.put("yem_timezone", yemTimezone);
        hashMap.put("yem_addressenable", yemAddressenable);
        hashMap.put("yem_addressdefault", yemAddressdefault);
        hashMap.put("yem_admindivision", yemAdmindivision);
        hashMap.put("yem_detailaddress", yemDetailaddress);
        hashMap.put("yem_customeraddrsspur", yemCustomeraddrsspur);
        hashMap.put("indexof", indexof);
        this.getView().returnDataToParent(hashMap);
    }
}

