package com.yem.wm.bc.bill.from;

import com.yem.wm.bc.common.CommonUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;

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

/**
 * @author 夜子
 * @ClassName yemBillPlugInbasefrom
 * @description 云易贸出口基类表单插件---业务员业务分组谈业务部门处理；核心单据处理
 * @date 2023年06月15日
 * @version: 1.0
 */
public class yemBillPlugInEsBaseFrom extends AbstractBillPlugIn implements BeforeF7SelectListener{

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        IDataModel model = this.getModel();
        DynamicObject org = (DynamicObject) model.getValue("org");
        long userID =  CommonUtils.getUserID();
        DynamicObject operator =  BusinessDataServiceHelper.loadSingle(userID,"bos_user");
        this.getModel().setValue("yem_operator",operator);
        DynamicObject Userdep =  CommonUtils.getOrgUserDep(org.getLong("id"),userID);
        this.getModel().setValue("yem_department",Userdep);
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);

        BasedataEdit yem_operator = this.getView().getControl("yem_operator");
        if (yem_operator != null) {
            yem_operator.addBeforeF7SelectListener(this);
        }

        BasedataEdit yem_department = this.getView().getControl("yem_department");
        if (yem_department != null) {
            yem_department.addBeforeF7SelectListener(this);
        }

    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        IDataModel model = this.getModel();
        String name = e.getProperty().getName();
        ChangeData changeData = e.getChangeSet()[0];
        Object newValue = changeData.getNewValue();
        Object oldValue = changeData.getOldValue();
        int rowIndex = changeData.getRowIndex();
        DynamicObject bill=  model.getDataEntity(true);

        switch (name) {
            case "yem_operator":
                DynamicObject yem_operator = (DynamicObject) newValue;
                if(null != yem_operator){
                    DynamicObject org = (DynamicObject) model.getValue("org");
                    DynamicObject Userdep =  CommonUtils.getOrgUserDep(org.getLong("id"),yem_operator.getLong("id"));
                    this.getModel().setValue("yem_department",Userdep);
                }else{
                    this.getModel().setValue("yem_department",null);
                }
                break;

            default:
                break;
        }

    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        IDataModel model = this.getModel();
        DynamicObject bill=  model.getDataEntity(true);
        String key = e.getProperty().getName();
        switch (key) {
//            case "yem_department":
//                List list = new ArrayList();
//                DynamicObject yem_operator = (DynamicObject) model.getValue("yem_operator");
//                if(null != yem_operator){
//                    DynamicObject org = (DynamicObject) model.getValue("org");
//                    list = CommonUtils.getOrgUserDeps(org.getLong("id"),yem_operator.getLong("id"));
//                }else{
//                    list.add(0L);
//                }
//                // 生成过滤条件对象QFilter
//                QFilter mqFilter = new QFilter("id", QCP.in, list);
//                // 设置列表过滤条件
//                ListShowParameter mshowParameter = (ListShowParameter) e.getFormShowParameter();
//                mshowParameter.getListFilterParameter().setFilter(mqFilter);
//                break;
//            case "yem_operator":
//                DynamicObject yem_department = (DynamicObject) model.getValue("yem_department");
//                if(yem_department != null) {
//                    List<Long> allUsersOfOrg = UserServiceHelper.getAllUsersOfOrg(yem_department.getLong("id"));
//                    // 生成过滤条件对象QFilter
//                    QFilter mqFilter2 = new QFilter("id", QCP.in, allUsersOfOrg);
//                    // 设置列表过滤条件
//                    ListShowParameter mshowParameter2 = (ListShowParameter) e.getFormShowParameter();
//                    mshowParameter2.getListFilterParameter().setFilter(mqFilter2);
//                }
//                break;
            default:
                break;
        }
    }

}
