package com.yem.wm.fm.lettercredit;

import com.yem.wm.bc.common.CommonUtils;
import com.yem.wm.utils.FunctionalCommon;
import com.yem.wm.utils.NumberChineseUtil;
import com.yem.wm.utils.YEM;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.RowDataEntity;
import kd.bos.entity.datamodel.events.AfterAddRowEventArgs;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.exception.KDBizException;
import kd.bos.form.IFormView;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

/**
 * @author xwudd
 * @date 10:48
 * @Description
 **/
public class LetterCreditEdit extends AbstractBillPlugIn {

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        IDataModel model = this.getModel();
        String name = e.getProperty().getName();
        ChangeData changeData = e.getChangeSet()[0];
        int rowIndex = changeData.getRowIndex();
        switch(name){
            case "yem_amount" :
                setAmtInWords();
                break;
            case "yem_salesmanids" :
                DynamicObject saleSmanIds = (DynamicObject)model.getValue("yem_salesmanids");
                if(YEM.isNotEmpty(saleSmanIds)){
                    long orgId = RequestContext.get().getOrgId();
                    List orgUserDeps = CommonUtils.getOrgUserDeps(orgId,saleSmanIds.getLong("id"));
                    if(YEM.isNotEmpty(orgUserDeps) && orgUserDeps.size() > 0){
                        model.setValue("yem_departmentids",orgUserDeps.get(0),rowIndex);
                    }
                }
                break;
        }
    }

    public void setAmtInWords(){
        IDataModel model = this.getModel();
        BigDecimal amount = (BigDecimal) model.getValue("yem_amount");
        String format = NumberChineseUtil.format(amount.doubleValue(), false, true);
        model.setValue("yem_amtinwords",format);
    }

    @Override
    public void afterAddRow(AfterAddRowEventArgs e) {
        super.afterAddRow(e);
        IDataModel model = this.getModel();
        RowDataEntity[] rowDataEntities = e.getRowDataEntities();
        String name = e.getEntryProp().getName();
        switch(name){
            case "yem_fm_materialinfo" :
                for(RowDataEntity rowDataEntitie : rowDataEntities) {
                    int rowIndex = rowDataEntitie.getRowIndex();
                    setDept(rowIndex);
                }
                break;
        }
    }

    public void setDept(int rowIndex){
        IDataModel model = this.getModel();
        long currUserId = RequestContext.get().getCurrUserId();
        long orgId = RequestContext.get().getOrgId();
        List orgUserDeps = CommonUtils.getOrgUserDeps(orgId,currUserId);
        if(YEM.isNotEmpty(orgUserDeps) && orgUserDeps.size() > 0){
            model.setValue("yem_departmentids",orgUserDeps.get(0),rowIndex);
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {
        super.beforeDoOperation(e);
        FormOperate formOperate = (FormOperate) e.getSource();
        String operateKey = formOperate.getOperateKey();
        IDataModel model = this.getModel();
        DynamicObject dataEntity = model.getDataEntity(true);
        IFormView view = this.getView();
        if ("draw".equals(operateKey)) {
            //公司+客户过滤
            //公司
            DynamicObject org = (DynamicObject) model.getValue("org");
            //客户
            DynamicObject customer = (DynamicObject) model.getValue("yem_customer");
            if (org != null && customer != null) {

                formOperate.getOption().setVariableValue("org", String.valueOf(org.getLong("id")));
                formOperate.getOption().setVariableValue("customer", String.valueOf(customer.getLong("id")));
            } else {
                throw new KDBizException("客户或外贸组织为空，不允许运行选单！");
            }
        }
    }
}
