package nckd.jimin.jyyy.fi.plugin.form;

import com.kingdee.util.StringUtils;
import kd.bos.bill.BillOperationStatus;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import nckd.base.helper.CommonHelperUtils;

import java.util.*;

/**
 * 插件说明：表单插件
 * 表单标识：支付大类入口(nckd_category)
 * @author wanghaiwu_kd
 * @date 2025/04/27
 */
public class PayBigCategoryFormPlugin extends AbstractFormPlugin {
    private static final Log logger = LogFactory.getLog(PayBigCategoryFormPlugin.class);
    private static Map<String, String> mapEntity = null;

    private static Map<String, String> mapFilter = null;

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

        setBtnImgValues();
    }

    private void setBtnImgValues(){
        mapEntity = CommonHelperUtils.getCommonParams("fi-paycategoryentity");
        mapFilter = CommonHelperUtils.getCommonParams("fi-paycategoryfilter");
    }

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

        if(mapEntity == null || mapFilter == null){
            setBtnImgValues();
        }

        if(mapEntity != null){
            List<String> btnImages = new ArrayList<>();

            mapEntity.forEach((key, value) -> btnImages.add(key));

            this.addClickListeners(btnImages.toArray(new String[]{}));
        }
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control source = (Control)evt.getSource();
        String imageKey = source.getKey();

        //图片按钮
        if(mapEntity != null && mapEntity.containsKey(imageKey)){
            String formId = mapEntity.get(imageKey);
            if(!StringUtils.isEmpty(formId)){
                BillShowParameter parameter = new BillShowParameter();

                parameter.setFormId(formId);
                parameter.setBillStatus(BillOperationStatus.ADDNEW);
                parameter.setStatus(OperationStatus.ADDNEW);


                if(mapFilter != null && mapFilter.containsKey(imageKey)) {
                    String viewType = mapFilter.get(imageKey);
                    parameter.setCustomParam("viewType", viewType);
                    parameter.setCustomParam("imageKey", imageKey);
                    parameter.setCustomParam("formId", formId);
                    QFilter viewTypeQFilter = getViewTypeQFilter(imageKey);

                    //费用大类过滤
                    if(viewTypeQFilter != null) {
                        parameter.setCustomParam("nckd_viewTypeFilter", viewTypeQFilter.toSerializedString());
                    }
                }

                parameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);

                this.getView().showForm(parameter);
            }
        }
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
    }

    public String getFormId(String openType){
        String formId = "";

        return formId;
    }

    public QFilter getViewTypeQFilter(String openType){
        if(mapFilter == null){
            return null;
        }

        String filterValue = mapFilter.get(openType);
        if(StringUtils.isEmpty(filterValue)) {
            return null;
        }
        String[] typeStrings = filterValue.split(",");

        QFilter filer = new QFilter("nckd_payviewtype.number", QCP.in, typeStrings);

        return filer;
    }
}
