package com.yem.wm.syn.eas.form.fi;

import com.alibaba.fastjson.JSON;
import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.filter.FilterColumn;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.FilterContainerInitArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.IListView;
import kd.bos.list.events.ListRowClickEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;

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

/**
 * 付款申请单列表插件
 */
public class PayapplyList extends AbstractListPlugin {


    @Override
    public void listRowClick(ListRowClickEvent evt) {
        super.listRowClick(evt);
        ArrayList<Boolean> list = new ArrayList<>();
        ListSelectedRowCollection selectedRows = evt.getListSelectedRowCollection();
        for (ListSelectedRow selectedRow: selectedRows) {
            Object primaryKeyValue = selectedRow.getPrimaryKeyValue();
            String billFormId = ((IListView) this.getView()).getBillFormId();
            DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(primaryKeyValue, billFormId);
            boolean yemIseas = dynamicObject.getBoolean("yem_iseas");
            if (yemIseas){
                list.add(yemIseas);
            }
        }
        if (list.size()>0){
            this.getView().setEnable(false,new String[]{"tblcheck","tblunsubmit"});
        }else {
            this.getView().setEnable(true,new String[]{"tblcheck","tblunsubmit"});
        }
    }

    @Override
    public void filterContainerInit(FilterContainerInitArgs args)
    {
        super.filterContainerInit(args);
        List<FilterColumn> columns = args.getCommonFilterColumns();
        for (FilterColumn column : columns) {
            String fieldName = column.getFieldName();
            if ("yem_applyorg.name".equals(fieldName)) {
                long currentOrgId = YEM.getCurrentOrgId();
                column.setDefaultValue(String.valueOf(currentOrgId));
            }
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args)
    {
        super.beforeDoOperation(args);
        FormOperate operate = (FormOperate) args.getSource();
        String operateKey = operate.getOperateKey();
        if ("new".equals(operateKey)) {
            List<Object> selectedMainOrgIds = this.getControlFilters().getFilter("yem_applyorg.id");
            this.getPageCache().put("currSelectedOrg", JSON.toJSONString(selectedMainOrgIds));
        }
    }
}
