package com.yem.wm.im.debcrednote.form;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.IFormView;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;

/**
 * @author zhouc
 * @date 2024/4/15 22:09
 * @className DebCredNote_Adjust
 * @description 转换贷记单表单插件
 */
public class DebCredNote_Adjust extends AbstractBillPlugIn {
    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        String name = e.getProperty().getName();
        ChangeData changeData = e.getChangeSet()[0];
        int rowIndex = changeData.getRowIndex();
        Object newValue = changeData.getNewValue();
        switch (name) {
            case "yem_adjustdirection":
                String adjustdirection = newValue.toString();
                if (adjustdirection.length() > 0) {
                    String[] s = adjustdirection.split("_");
                    if (s.length > 1) {
                        QFilter qFilter = new QFilter("number", QCP.equals, s[0]);
                        DynamicObject yem_adjustfromcurr = BusinessDataServiceHelper.loadSingle("bd_currency", "id", qFilter.toArray());
                        QFilter qFilter1 = new QFilter("number", QCP.equals, s[1]);
                        DynamicObject yem_adjusttocurr = BusinessDataServiceHelper.loadSingle("bd_currency",
                                "id", qFilter1.toArray());
                        model.setValue("yem_adjustfromcurr", yem_adjustfromcurr);
                        model.setValue("yem_adjusttocurr", yem_adjusttocurr);
                        model.setValue("yem_currency", yem_adjusttocurr);
//                        model.setValue("yem_currencybase", yem_adjusttocurr);
                        model.deleteEntryData("yem_creditnotesource");
                        model.deleteEntryData("yem_creditnotedes");
                        //新增来源
                        int row = model.createNewEntryRow("yem_creditnotesource");
                        //新增去向
                        int row1 = model.createNewEntryRow("yem_creditnotedes");
                        if (adjustdirection.equals("CNY_USD")) {
                            model.setValue("yem_creditnotecont_s", "人民币转换为美元", row);
                            model.setValue("yem_creditnotecont_d", "人民币转换为美元", row1);
                        } else {
                            model.setValue("yem_creditnotecont_s", "美元转换为人民币", row);
                            model.setValue("yem_creditnotecont_d", "美元转换为人民币", row1);
                        }
                        model.setValue("yem_directionrmbamt", new BigDecimal(0));
                        model.setValue("yem_directionusdamt", new BigDecimal(0));
                        model.setValue("yem_adjustexrate", new BigDecimal(0));
                    }
                } else {
                    model.setValue("yem_directionrmbamt", new BigDecimal(0));
                    model.setValue("yem_directionusdamt", new BigDecimal(0));
                    model.setValue("yem_adjustexrate", new BigDecimal(0));
                    model.setValue("yem_adjustfromcurr", null);
                    model.setValue("yem_adjusttocurr", null);
                    model.deleteEntryData("yem_creditnotesource");
                    model.deleteEntryData("yem_creditnotedes");
                }
                break;
            case "yem_directionrmbamt":
                String yem_adjustdirection = (String) model.getValue("yem_adjustdirection");
                if (yem_adjustdirection.equals("CNY_USD")) {
                    model.setValue("yem_originalamt", newValue, 0);
                } else {
                    model.setValue("yem_goalamt", newValue, 0);
                }
                view.updateView("yem_creditnotesource");
                view.updateView("yem_creditnotedes");
                break;
            case "yem_directionusdamt":
                String yemadjustdirection = (String) model.getValue("yem_adjustdirection");
                if (yemadjustdirection.equals("CNY_USD")) {
                    model.setValue("yem_goalamt", newValue, 0);
                } else {
                    model.setValue("yem_originalamt", newValue, 0);
                }
                view.updateView("yem_creditnotesource");
                view.updateView("yem_creditnotedes");
//            case "yem_adjustexrate":
//                String yemdirection = (String) model.getValue("yem_adjustdirection");
//                if (yemdirection.equals("CNY_USD")) {
//                    model.setValue("yem_goalamt", newValue, 0);
//                } else {
//                    model.setValue("yem_originalamt", newValue, 0);
//                }
                break;
            default:
                break;
        }
    }


}
