package com.yem.em.dynamic;

import com.yem.wm.utils.StringUtils;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.context.RequestContext;
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.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.OpenStyle;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.Search;
import kd.bos.form.control.events.SearchEnterEvent;
import kd.bos.form.control.events.SearchEnterListener;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;

import java.util.*;

/**
 * @author lipan
 * @date 2023/9/1 18:28
 * @className CusorderDynamicEdit
 * @description 订单管理动态表单插件
 */
public class CusorderDynamicEdit extends AbstractFormPlugin implements SearchEnterListener {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        Search search = getView().getControl("yem_searchap");//资源号搜索
        search.addEnterListener(this);
    }


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

    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);

        String name = e.getProperty().getName();
        ChangeData changeData = e.getChangeSet()[0];
        Object newValue = changeData.getNewValue();
        switch (name) {
            case "yem_daterangefield_startdate":
            case "yem_daterangefield_enddate":
                Showquery();
                break;
            default:
                break;
        }
    }

    private void Showquery() {
        IDataModel model = this.getModel();
        Search yemSearchap = this.getControl("yem_searchap");
        yemSearchap.setSearchKey("");
        this.getView().updateView("yem_searchap");
        this.getModel().deleteEntryData("yem_entryentity");
        Date yem_daterangefield_startdate = (Date) model.getValue("yem_daterangefield_startdate");
        Date yem_daterangefield_enddate = (Date) model.getValue("yem_daterangefield_enddate");
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        DynamicObjectCollection yem_entryentity = dataEntity.getDynamicObjectCollection("yem_entryentity");
        QFilter filter = new QFilter("yem_billtype.number", QCP.equals, "yem_es_salesorder_ZJ");
        filter.and(new QFilter("billstatus", QCP.equals, "C"));
        if (yem_daterangefield_startdate != null) {
            filter.and(new QFilter("yem_bizdate", QCP.large_equals, yem_daterangefield_startdate));
        }
        if (yem_daterangefield_enddate != null) {
            filter.and(new QFilter("yem_bizdate", QCP.less_equals, yem_daterangefield_enddate));
        }

        DynamicObjectCollection collection = QueryServiceHelper.query("yem_es_salesorder",
                "id,billno,yem_bizdate,yem_bd_products.name,yem_ordersumnumber,org.name,yem_operator.name", new QFilter[]{filter}, "yem_bizdate");
        if (collection.size() > 0) {
            if (collection.size() < 20) {
                for (DynamicObject obj : collection) {
                    int row = this.getModel().createNewEntryRow("yem_entryentity");
                    model.setValue("yem_textfield", obj.getString("billno"), row);
                    model.setValue("yem_textfield1", obj.getString("yem_bizdate"), row);
                    model.setValue("yem_textfield2", obj.getString("yem_bd_products.name"), row);
                    model.setValue("yem_textfield3", obj.getString("yem_ordersumnumber"), row);
                    model.setValue("yem_textfield4", obj.getString("org.name"), row);
                    model.setValue("yem_textfield5", obj.getString("yem_operator.name"), row);
                }
            } else {
                for (int i = 0; i < 20; i++) {
                    DynamicObject obj = collection.get(i);
                    int row = this.getModel().createNewEntryRow("yem_entryentity");
                    model.setValue("yem_textfield", obj.getString("billno"), row);
                    model.setValue("yem_textfield1", obj.getString("yem_bizdate"), row);
                    model.setValue("yem_textfield2", obj.getString("yem_bd_products.name"), row);
                    model.setValue("yem_textfield3", obj.getString("yem_ordersumnumber"), row);
                    model.setValue("yem_textfield4", obj.getString("org.name"), row);
                    model.setValue("yem_textfield5", obj.getString("yem_operator.name"), row);
                }
            }
            this.getView().updateView("yem_entryentity");
        }
    }


    /**
     * 输入回车进入方法
     *
     * @param e
     */
    @Override
    public void search(SearchEnterEvent e) {
        Search search = (Search) e.getSource();
        String key = search.getKey();
        String searchText = e.getText();
        if (key.equals("yem_searchap")) {
            if (StringUtils.isNotEmpty(searchText)) {
                searchapSearch(searchText);
            }
        }
    }

    @Override
    public List<Object> getComPlexSearchList(SearchEnterEvent evt) {
        List<String> fieldNames = evt.getFieldNames();

        return SearchEnterListener.super.getComPlexSearchList(evt);

    }

    /**
     * 资源号搜索
     *
     * @param key 用户输入内容
     */
    public void searchapSearch(String key) {
        IDataModel model = this.getModel();
        this.getModel().beginInit();
        this.getModel().setValue("yem_daterangefield_startdate", null);
        this.getModel().setValue("yem_daterangefield_enddate", null);
        this.getModel().endInit();
        this.getView().updateView("yem_daterangefield");
        this.getModel().deleteEntryData("yem_entryentity");
        Date yem_daterangefield_startdate = (Date) model.getValue("yem_daterangefield_startdate");
        Date yem_daterangefield_enddate = (Date) model.getValue("yem_daterangefield_enddate");
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        DynamicObjectCollection yem_entryentity = dataEntity.getDynamicObjectCollection("yem_entryentity");
        QFilter filter = new QFilter("yem_billtype.number", QCP.equals, "yem_es_salesorder_ZJ");
        filter.and(new QFilter("billstatus", QCP.equals, "C"));
        if (yem_daterangefield_startdate != null) {
            filter.and(new QFilter("yem_bizdate", QCP.large_equals, yem_daterangefield_startdate));
        }
        if (yem_daterangefield_enddate != null) {
            filter.and(new QFilter("yem_bizdate", QCP.less_equals, yem_daterangefield_enddate));
        }
        if (key != null) {
            filter.and(new QFilter("billno", QCP.like, "%" + key + "%"));

        }
        DynamicObjectCollection collection = QueryServiceHelper.query("yem_es_salesorder",
                "id,billno,yem_bizdate,yem_bd_products.name,yem_ordersumnumber,org.name,yem_operator.name", new QFilter[]{filter}, "yem_bizdate");
        if (collection.size() > 0) {
            if (collection.size() < 20) {
                for (DynamicObject obj : collection) {
                    int row = this.getModel().createNewEntryRow("yem_entryentity");
                    model.setValue("yem_textfield", obj.getString("billno"), row);
                    model.setValue("yem_textfield1", obj.getString("yem_bizdate"), row);
                    model.setValue("yem_textfield2", obj.getString("yem_bd_products.name"), row);
                    model.setValue("yem_textfield3", obj.getString("yem_ordersumnumber"), row);
                    model.setValue("yem_textfield4", obj.getString("org.name"), row);
                    model.setValue("yem_textfield5", obj.getString("yem_operator.name"), row);
                }
            } else {
                for (int i = 0; i < 20; i++) {
                    DynamicObject obj = collection.get(i);
                    int row = this.getModel().createNewEntryRow("yem_entryentity");
                    model.setValue("yem_textfield", obj.getString("billno"), row);
                    model.setValue("yem_textfield1", obj.getString("yem_bizdate"), row);
                    model.setValue("yem_textfield2", obj.getString("yem_bd_products.name"), row);
                    model.setValue("yem_textfield3", obj.getString("yem_ordersumnumber"), row);
                    model.setValue("yem_textfield4", obj.getString("org.name"), row);
                    model.setValue("yem_textfield5", obj.getString("yem_operator.name"), row);
                }
            }
            this.getView().updateView("yem_entryentity");
        }
    }

    /**
     * 自动补全风格 输入搜索条件后 进入该方法组织数据返回前端显示*。
     *
     * @param evt 上下文
     * @return
     */
    @Override
    public List<String> getSearchList(SearchEnterEvent evt) {
        Search search = (Search) evt.getSource();
        String key = search.getKey();
        String searchText = evt.getText();
        if (key.equals("yem_searchap")) {//资源号搜索
            if (StringUtils.isNotEmpty(searchText)) {
                return SearchList(searchText);
            }
        }
        return null;
    }

    /**
     * 资源号模糊构建下拉
     *
     * @param searchText 输入内容
     * @return 查询结果集合
     */
    public List<String> SearchList(String searchText) {
        Map<String, String> searchList = new HashMap<String, String>();
        QFilter filter = new QFilter("yem_billtype.number", QCP.equals, "yem_es_salesorder_ZJ");
        filter.and(new QFilter("billstatus", QCP.equals, "C"));
        filter.and(new QFilter("billno", QCP.like, "%" + searchText + "%"));
        DynamicObjectCollection collection = QueryServiceHelper.query("yem_es_salesorder",
                "id,billno", new QFilter[]{filter});
        for (DynamicObject obj : collection) {
            searchList.put(obj.get("id").toString(), obj.get("billno").toString());
        }
        return new ArrayList<>(searchList.values());
    }

    
    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {
        super.beforeDoOperation(e);
        FormOperate operate = (FormOperate) e.getSource();
        String key = operate.getOperateKey();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        if (key.equals("xq")) {//订单详情
            int row = model.getEntryCurrentRowIndex("yem_entryentity");
            DynamicObjectCollection yem_entryentity1 = dataEntity.getDynamicObjectCollection("yem_entryentity");
            if (yem_entryentity1.size() > 0) {
                FormShowParameter showParameter = new FormShowParameter();
                showParameter.setFormId("yem_cusorderedit");
                showParameter.setCloseCallBack(null);
                Map<String, Object> entryInfo = new HashMap<String, Object>();
                entryInfo.put("yem_textfield", yem_entryentity1.get(row).getString("yem_textfield"));
                entryInfo.put("yem_textfield1", yem_entryentity1.get(row).getString("yem_textfield1"));
                entryInfo.put("yem_textfield2", yem_entryentity1.get(row).getString("yem_textfield2"));
                entryInfo.put("yem_textfield3", yem_entryentity1.get(row).getString("yem_textfield3"));
                entryInfo.put("yem_textfield4", yem_entryentity1.get(row).getString("yem_textfield4"));
                entryInfo.put("yem_textfield5", yem_entryentity1.get(row).getString("yem_textfield5"));
                showParameter.setCustomParams(entryInfo);
                showParameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
//                showParameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                this.getView().showForm(showParameter);
            }
        }
    }

}
