package yem.wmc.ydn.formplugin.dynamic;


import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.IFormView;
import kd.bos.form.container.Wizard;
import kd.bos.form.control.Control;
import kd.bos.form.control.Steps;
import kd.bos.form.control.StepsOption;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import yem.base.common.utils.YEM;
import yem.wmc.ydn.utils.DynamicObjectUtil;

import java.text.SimpleDateFormat;
import java.util.*;


public class AirtransportationDynamic extends AbstractFormPlugin {
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        inia();//初始化数据
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("yem_buttonap");
    }

    private void inia() {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        DynamicObject dataEntity = model.getDataEntity(true);
        DynamicObjectCollection yemEntryentity = dataEntity.getDynamicObjectCollection("yem_entryentity");
        yemEntryentity.clear();
        String yem_contractnumber = dataEntity.getString("yem_contractnumber");
        String yem_custorsup = dataEntity.getString("yem_custorsup");
        String yem_shipmentarrival = dataEntity.getString("yem_shipmentarrival");
        String yem_blno = dataEntity.getString("yem_blno");
        String yem_mmsi = dataEntity.getString("yem_mmsi");
        List<QFilter> qFilters = new ArrayList<>();
        if (YEM.isNotEmpty(yem_contractnumber)){
            qFilters.add(new QFilter("yem_textfield1", QCP.like, "%" + yem_contractnumber + "%"));
        }

        if (YEM.isNotEmpty(yem_custorsup)){
            qFilters.add(new QFilter("yem_textfield4", QCP.like, "%" + yem_custorsup + "%"));
        }

        if (YEM.isNotEmpty(yem_shipmentarrival)){
            qFilters.add(new QFilter("yem_textfield9", QCP.like, "%" + yem_shipmentarrival + "%"));
        }

        if (YEM.isNotEmpty(yem_blno)){
            qFilters.add(new QFilter("yem_textfield", QCP.like, "%" + yem_blno + "%"));
        }

        if (YEM.isNotEmpty(yem_mmsi)){
            qFilters.add(new QFilter("yem_textfield2", QCP.like, "%" + yem_mmsi + "%"));
        }

        String selectfields = DynamicObjectUtil.getSelectfields("yem_ydn_transport", false);
        String entrySelectfields = DynamicObjectUtil.getEntrySelectfields(selectfields, "yem_ydn_transport", "entryentity", false);
        DynamicObject[] load = BusinessDataServiceHelper.load("yem_ydn_transport", entrySelectfields, qFilters.toArray(new QFilter[]{}));
        Wizard wizard = this.getControl("yem_wizardap");
        List<StepsOption> stepsOptionParent=new ArrayList<>();
        for (DynamicObject dynamicObject : load) {

            String textfield = dynamicObject.getString("yem_textfield");  //运单号/提单号
            String textfield1 = dynamicObject.getString("yem_textfield1");//合同号
            String textfield2 = dynamicObject.getString("yem_textfield2");//MMSI号
            String textfield3 = dynamicObject.getString("yem_textfield3");//船名/航班号
            String textfield4 = dynamicObject.getString("yem_textfield4");//客商
            String textfield5 = dynamicObject.getString("yem_textfield5");//启运地/启运港
            String textfield6 = dynamicObject.getString("yem_textfield6");//目的地/目的港
            String textfield9 = dynamicObject.getString("yem_textfield9");//出运号
            Date Datetimefield = dynamicObject.getDate("yem_datetimefield");  //启运时间
            Date Datetimefield1 = dynamicObject.getDate("yem_datetimefield1");//到港时间

            DynamicObject yemEntryentityrow = yemEntryentity.addNew();
            yemEntryentityrow.set("yem_atd",textfield);//运单号/提单号
            yemEntryentityrow.set("yem_atd1",textfield1);//合同号
            yemEntryentityrow.set("yem_atd14",textfield2);//MMSI号
            yemEntryentityrow.set("yem_atd2",textfield3);//船名/航班号
            yemEntryentityrow.set("yem_atd11",textfield4);//客商
            yemEntryentityrow.set("yem_atd3",textfield5);//启运地/启运港
            yemEntryentityrow.set("yem_atd12",textfield6);//目的地/目的港
            yemEntryentityrow.set("yem_atd5",textfield9);//出运号
            yemEntryentityrow.set("yem_atd4",Datetimefield);//启运时间
            yemEntryentityrow.set("yem_atd13",Datetimefield1);//到港时间
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            yemEntryentityrow.set("yem_textfield6",sdf.format(date));//到港时间
            DynamicObjectCollection entryentity = dynamicObject.getDynamicObjectCollection("entryentity");
            for (DynamicObject object : entryentity) {
                DynamicObjectCollection yemSubentryentity = yemEntryentityrow.getDynamicObjectCollection("yem_subentryentity");
                DynamicObject dynamicObject1 = yemSubentryentity.addNew();
                String textfield10 = object.getString("yem_textfield10");
                String textfield11 = object.getString("yem_textfield11");
                String textfield12 = object.getString("yem_textfield12");
                String textfield13 = object.getString("yem_textfield13");
                String textfield14 = object.getString("yem_textfield15");
                Date Datetimefield2 = object.getDate("yem_datetimefield2");
                dynamicObject1.set("yem_textfield",textfield10);
                dynamicObject1.set("yem_textfield1",textfield11);
                dynamicObject1.set("yem_textfield2",textfield12);
                dynamicObject1.set("yem_textfield3",textfield13);
                dynamicObject1.set("yem_textfield5",textfield14);
                dynamicObject1.set("yem_textfield4",Datetimefield2);
                wizardap(stepsOptionParent,textfield12,Datetimefield2);
            }
        }

        Trackinform trackinform=new Trackinform();
        trackinform.setContainer("20*40Dry");
        trackinform.setOrdernumber("218389651");
        trackinform.setPickupgood("06-22 23:08");
        trackinform.setShipname("SAN FERNANDO");
        trackinform.setShippcompanies("MSK[官网]");
        trackinform.setStepsOptionParent(stepsOptionParent);

        // 设置当前节点
        Map<String, Object> currentStepMap = new HashMap<>();
        currentStepMap.put("currentStep", 6);
        currentStepMap.put("currentStatus", Steps.FINISH);
        // 更新当前节点
        wizard.setWizardCurrentStep(currentStepMap);
        List<StepsOption> stepsOptions = wizard.getStepsOptions();
        stepsOptions.clear();
        stepsOptions.addAll(trackinform.getStepsOptionParent());
        // 更新步骤条设置
        wizard.setWizardStepsOptions(stepsOptions);
        view.updateView("yem_entryentity");
    }

    private void wizardap(List<StepsOption> stepsOptionParent,String textfield12, Date datetimefield2) {

        // 获取设计时的步骤条设置
        //父页面传参
        int size = stepsOptionParent.size();
        if(size<=9) {
            StepsOption stepsOption0 = new StepsOption();
            stepsOption0.setTitle(new LocaleString(textfield12));
            stepsOption0.setDescription(new LocaleString(datetimefield2.toString()));
            stepsOptionParent.add(stepsOption0);
        }
    }


    @Override
    public void click(EventObject evt) {
        super.click(evt);
        String key = ((Control) evt.getSource()).getKey();
        switch (key) {
            case "yem_buttonap":
                inia();
                break;
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String name = e.getProperty().getName();
        switch (name){
            case "yem_contractnumber":
            case "yem_custorsup":
            case "yem_shipmentarrival":
            case "yem_blno":
            case "yem_mmsi":

                break;
            default:
                break;
        }
    }
}
