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

import kd.bos.dataentity.entity.LocaleString;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.entity.botp.ConvertRuleCache;
import kd.bos.entity.botp.ConvertRuleElement;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.field.ComboEdit;
import kd.bos.form.field.ComboItem;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.util.StringUtils;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

public class ReimPushSettingEdit extends AbstractFormPlugin {


    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        IDataEntityProperty property = e.getProperty();
        String name = property.getName();
        if("nckd_sourcebilltype".equals(name) || "nckd_targetbilltype".equals(name)){
            fillRuleCombo();
            getModel().setValue("nckd_ruleid",null);
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        fillRuleCombo();
    }

    protected void fillRuleCombo(){
        String sourceBillType = (String)getModel().getValue("nckd_sourcebilltype");
        String targetBillType = (String)getModel().getValue("nckd_targetbilltype");

        if(StringUtils.isNotEmpty(sourceBillType) && StringUtils.isNotEmpty(targetBillType)){
            List<ComboItem> ruleItems = new ArrayList();
            List<ConvertRuleElement> convertRuleElements = ConvertRuleCache.loadRules(sourceBillType, targetBillType);
            for(ConvertRuleElement ruleElement : convertRuleElements ){
                boolean enabled = ruleElement.isEnabled();
                if(enabled){
                    ComboItem ruleItem = new ComboItem();
                    ruleItem.setValue(ruleElement.getId());
                    ruleItem.setCaption(ruleElement.getName());
                    ruleItems.add(ruleItem);
                }
            }
            ComboEdit ruleEditor = (ComboEdit)this.getView().getControl("nckd_ruleid");
            ruleEditor.setComboItems(ruleItems);
        }
    }
}
