package com.mortals.xhx.common.formdesign.component;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.mortals.xhx.common.code.ComponentEnum;
import com.mortals.xhx.common.formdesign.*;
import com.mortals.xhx.common.pdu.gen.component.ComponentCons;
import com.mortals.xhx.common.utils.ExportDocUtil;
import com.mortals.xhx.common.utils.StringUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 复选框组件
 *
 * @author: zxfei
 * @date: 2021/10/26 10:35
 * @description:
 **/
public class DesignCheckboxComponent extends DesignComponent {

    public DesignCheckboxComponent(String type) {
        super(type);
    }

    @Override
    public ListItem buildDefaultComponent(ComponentCons cons) {
        CheckboxListItem columnsItem = new CheckboxListItem();
        columnsItem.setId(cons.getVal());
        columnsItem.set_id(cons.getVal());
        columnsItem.setCompType("checkbox");
        columnsItem.setCompName(cons.getLabel());
        columnsItem.setCompIcon("checkbox");
        columnsItem.setEle("el-checkbox-group");
        columnsItem.setViewType("component");
        columnsItem.setConfig(true);
        columnsItem.setShowLabel(true);
        columnsItem.setLabel(cons.getLabel());
        columnsItem.setLabelWidth(120);
        columnsItem.setPlaceholder("请输入"+cons.getLabel());
        columnsItem.setMaxLength(50);
        columnsItem.setRequired(false);
        columnsItem.setMultiple(false);
        columnsItem.setCollapseTags(false);
        columnsItem.setDisabled(false);
        columnsItem.setBorder(false);
        columnsItem.setSize("medium");
        columnsItem.setGutter(15);
        columnsItem.setSpan(12);
        columnsItem.setOptionType("default");
        columnsItem.setDataType("static");
        columnsItem.setAction("https://www.fastmock.site/mock/51715c0157535b99010bde55f2df33c8/formDesigner/api/options");
        columnsItem.setClearable(false);
        columnsItem.setValue(Lists.newArrayList());
        columnsItem.setRules(Lists.newArrayList());
        columnsItem.setLayout("colItem");
        columnsItem.setRuleError("该字段不能为空");
        columnsItem.setRulesType("default");

        initDefaultCheckBoxValue(cons, columnsItem);
        return columnsItem;
    }

    /**
     *  值格式 *党员-非党员-群众*
     * @param cons
     * @param columnsItem
     */
    private void initDefaultCheckBoxValue(ComponentCons cons, ListItem columnsItem) {
        if (cons.getVal().indexOf("<") != -1) {
            String cks = StringUtils.substringBetween(cons.getVal(), "<", ">");
            if(ObjectUtils.isEmpty(cks))return;
            List<OptionsItem> optionsItemList = StrUtil.splitTrim(cks, "-", -1).stream().map(option -> {
                OptionsItem options = new OptionsItem();
                options.setLabel(option);
                options.setValue(option);
                return options;
            }).collect(Collectors.toList());
            columnsItem.setOptions(optionsItemList);
        }
    }

    @Override
    public String getType() {
        return ComponentEnum.CHECKBOX.getValue();
    }

}
