package nckd.jimin.jyyy.hr.swc.hcss.formplugin.web.income.deal;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.control.AttachmentPanel;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.hr.hbp.common.util.DomainFactory;
import kd.sdk.plugin.Plugin;
import kd.swc.hcss.business.service.income.IncomeProofBillService;

import java.util.EventObject;

/**
 * 收入证明办理 开具值处理成可转换成大写的格式 ： ####.##
 * Tyx
 * 2025-06-18
 */
public class IncomeProofBillDealPluginEx extends AbstractFormPlugin implements Plugin {

    private IncomeProofBillService incomeProofBillService = (IncomeProofBillService) DomainFactory.getInstance(IncomeProofBillService.class);


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

    private void setAttachment() {
        DynamicObject billData = this.getModel().getDataEntity(true);
        String name = this.incomeProofBillService.getBillNameByDataSource(billData.getString("datasource"));
        AttachmentPanel attachment = (AttachmentPanel)this.getView().getControl("nckd_attachmentpanelap");
        attachment.bindData(AttachmentServiceHelper.getAttachments(name, billData.getLong("id"), "nckd_attachmentpanelap"));
    }


    private void dealIncomeInfo() {
        IDataModel model = this.getModel();
        int rowCount = model.getEntryRowCount("entryentity");
        String regex = "[^0-9.]";
        for(int i = 0; i < rowCount; i++) {
            String checkValueOld = model.getValue("checkvalue", i).toString();
            //只保留数字和.
            String checkValueNew = checkValueOld.replaceAll(regex, "");
            model.setValue("checkvalue", checkValueNew, i);
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        super.afterDoOperation(afterDoOperationEventArgs);
        String operateKey = afterDoOperationEventArgs.getOperateKey();
        if("checkapprove".equals(operateKey)) {
            this.dealIncomeInfo();
        }
    }
}