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.ColumnsItem;
import com.mortals.xhx.common.formdesign.DesignComponent;
import com.mortals.xhx.common.formdesign.ListItem;
import com.mortals.xhx.common.formdesign.OptionsItem;
import com.mortals.xhx.common.pdu.gen.component.ComponentCons;
import com.mortals.xhx.common.utils.StringUtils;

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

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

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

    @Override
    public ListItem buildDefaultComponent(ComponentCons cons) {
        ListItem columnsItem = new ListItem();
        columnsItem.setId(cons.getVal());
        columnsItem.set_id(cons.getVal());
        columnsItem.setCompType("radio");
        columnsItem.setCompName(cons.getLabel());
        columnsItem.setCompIcon("radio");
        columnsItem.setEle("el-radio-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.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("");
        columnsItem.setRules(Lists.newArrayList());
        columnsItem.setLayout("colItem");
        columnsItem.setRuleError("该字段不能为空");
        columnsItem.setRulesType("default");
        initDefaultCheckBoxValue(cons, columnsItem);
        return columnsItem;
    }

    private void initDefaultCheckBoxValue(ComponentCons cons, ListItem columnsItem) {
        if (cons.getLabel().indexOf("[") != -1) {
            String cks = StringUtils.substringBetween(cons.getLabel(), "[", "]");
            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);

            cons.setVal(cons.getVal().substring(0, cons.getVal().indexOf("[")));
            cons.setLabel(cons.getLabel().substring(0, cons.getLabel().indexOf("[")));

        }
    }

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

}
