package yem.wmc.ydn.formplugin.dynamic;

import kd.bos.bill.OperationStatus;
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.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
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 kd.sdk.plugin.Plugin;
import yem.base.common.utils.DateUtil;
import yem.base.common.utils.YEM;
import yem.wmc.ydn.utils.DynamicObjectUtil;

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

/**
 * 动态表单插件
 */
public class LandCarriageFormPlugin extends AbstractFormPlugin implements Plugin {

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        init(this.getView(), this.getModel());
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        Map<String, Object> cusMapone = showParameter.getCustomParams();
        showForm("yem_ydn_viewmap", "yem_flexpanelap31",cusMapone);//地图
    }
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("yem_buttonap");
    }

    public void init(IFormView view, IDataModel model) {
        String name = model.getDataEntity(true).getDataEntityType().getName();
        stepsOption(name);
        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[]{}));
        for (DynamicObject dynamicObject : load) {
            DynamicObject yemEntryentityrow = yemEntryentity.addNew();
            yemEntryentityrow.set("yem_atd", dynamicObject.getString("yem_textfield"));//运单号/提单号
            yemEntryentityrow.set("yem_atd1", dynamicObject.getString("yem_textfield1"));//合同号
            yemEntryentityrow.set("yem_atd14", dynamicObject.getString("yem_textfield2"));//MMSI号
            yemEntryentityrow.set("yem_atd2", dynamicObject.getString("yem_textfield3"));//船名/航班号
            yemEntryentityrow.set("yem_atd11", dynamicObject.getString("yem_textfield4"));//客商
            yemEntryentityrow.set("yem_atd3", dynamicObject.getString("yem_textfield5"));//启运地/启运港
            yemEntryentityrow.set("yem_atd12", dynamicObject.getString("yem_textfield6"));//目的地/目的港
            yemEntryentityrow.set("yem_atd5", dynamicObject.getString("yem_textfield9"));//出运号
            yemEntryentityrow.set("yem_atd4", dynamicObject.getDate("yem_datetimefield"));//启运时间
            yemEntryentityrow.set("yem_atd13", dynamicObject.getDate("yem_datetimefield1"));//到港时间
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            yemEntryentityrow.set("yem_textfield6",sdf.format(date));//到港时间
        }
        view.updateView("yem_entryentity");
    }

    private void stepsOption(String name) {
        Wizard wizard = this.getControl("yem_wizardap");
        List<StepsOption> stepsOptionParent=new ArrayList<>();
        List<String> operationSteps = new ArrayList<>();  //海运
        List<String> preProcessSteps = new ArrayList<>(); // 陆运
        // 按顺序添加每个步骤
        operationSteps.add("提空箱"); // 提取空集装箱
        operationSteps.add("进场");   // 进入港口或场地
        operationSteps.add("放行");   // 放行手续
        operationSteps.add("装船");   // 装载到船上
        operationSteps.add("开航");   // 船只起航
        operationSteps.add("中转");   // 中转过程
        operationSteps.add("卸船");   // 从船上卸下货物
        operationSteps.add("提货");   // 提取货物
        operationSteps.add("还空");   // 归还空集装箱
        preProcessSteps.add("制单");   // 制作单据
        preProcessSteps.add("审核");   // 审核单据
        preProcessSteps.add("计划");   // 制定计划
        preProcessSteps.add("派车");   // 派送车辆
        preProcessSteps.add("预约");   // 预约提货或运输
        preProcessSteps.add("排队");   // 车辆排队
        preProcessSteps.add("入场");   // 车辆入场
        preProcessSteps.add("装车");   // 装载货物
        preProcessSteps.add("确认");   // 确认装载情况
        preProcessSteps.add("出场");   // 车辆出场
        preProcessSteps.add("在途");   // 运输中
        preProcessSteps.add("签收");   // 签收货物
        preProcessSteps.add("结算");   // 完成结算

        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime(); // 获取当前日期
        if("yem_ydn_landcarriage".equals(name)){
            for (String preProcessStep : preProcessSteps) {
                wizardap(stepsOptionParent,preProcessStep,date);
                // 增加一天
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                date = calendar.getTime(); // 更新 date
            }
        }else if ("yem_ydh_seafreight".equals(name)){
            for (String preProcessStep : operationSteps) {
                wizardap(stepsOptionParent,preProcessStep,date);
                // 增加一天
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                date = calendar.getTime(); // 更新 date
            }

        }
        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);
    }
    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":
                init(this.getView(), this.getModel());
                break;
        }
    }
    private void showForm(String formkey, String targetKey, Map<String, Object> cusMap) {
        IFormView view = this.getView();
        FormShowParameter showParameter = new FormShowParameter();
        showParameter.setStatus(OperationStatus.EDIT);
        showParameter.getOpenStyle().setShowType(ShowType.InContainer);
        showParameter.getOpenStyle().setTargetKey(targetKey);
        showParameter.setSendToClient(true);
        showParameter.setCustomParams(cusMap);
        showParameter.setFormId(formkey);
        view.showForm(showParameter);
    }
}