package com.mortals.xhx.common.pdu.gen.component;

import com.mortals.xhx.common.code.ComponentEnum;
import com.mortals.xhx.common.pdu.gen.*;

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

/**
 * 下拉框组件
 *
 * @author: zxfei
 * @date: 2021/10/26 10:35
 * @description:
 **/
public class SelectComponent extends Component {

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

    @Override
    public FieldsItem buildDefaultComponent(ComponentCons cons) {
        FieldsItem fieldsItem = new FieldsItem();
        Config config = new Config();
        config.setLabel("下拉选项框");
        config.setLabelWidth("");
        config.setShowLabel(true);
        config.setChangeTag(true);
        config.setTag("el-select");
        config.setTagIcon("select");
        config.setLayout("colFormItem");
        config.setRequired(true);
        config.setDocument("https://element.eleme.cn/#/zh-CN/component/select");
        config.setRegList(new ArrayList<>());
        config.setFormId(1);
        config.setRenderKey("");
        config.setSpan(12);
        fieldsItem.setConfig(config);

        Slot slot = new Slot();
        List<OptionsItem> list = new ArrayList<>();
        OptionsItem optionsItem = new OptionsItem();
        optionsItem.setLabel("选项一");
        optionsItem.setValue("1");
        list.add(optionsItem);
        slot.setOptions(list);
        fieldsItem.setSlot(slot);

        Style style = new Style();
        style.setWidth("100%");
        fieldsItem.setStyle(style);
        fieldsItem.setClearable(true);
        fieldsItem.setMultiple(false);
        fieldsItem.setVModel("field");


        initDefaultSelectValue(cons, fieldsItem);
        return fieldsItem;
    }

    private void initDefaultSelectValue(ComponentCons cons, FieldsItem fieldsItem) {
        //判断如果包含年，月，日，选项框 基础化
        if (cons.getVal().indexOf("年") != -1) {
            //初始化10年
            List<OptionsItem> optionsItems = initYears();
            fieldsItem.getSlot().setOptions(optionsItems);
        } else if (cons.getVal().indexOf("月") != -1) {
            List<OptionsItem> optionsItems = initMonths();
            fieldsItem.getSlot().setOptions(optionsItems);
        } else if (cons.getVal().indexOf("日") != -1) {
            List<OptionsItem> optionsItems = initDays();
            fieldsItem.getSlot().setOptions(optionsItems);
        }

        if (cons.getLabel().indexOf("[") != -1) {
            //todo 初始选项框
        }
    }


    public List<OptionsItem> initYears() {
        List<OptionsItem> list = new ArrayList<>();
        for (int i = 2020; i < 2030; i++) {
            OptionsItem optionsItem = new OptionsItem();
            optionsItem.setLabel(i + "");
            optionsItem.setValue(i + "");
            list.add(optionsItem);
        }
        return list;
    }

    public List<OptionsItem> initMonths() {
        List<OptionsItem> list = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            OptionsItem optionsItem = new OptionsItem();
            optionsItem.setLabel(i + "");
            optionsItem.setValue(i + "");
            list.add(optionsItem);
        }
        return list;
    }

    public List<OptionsItem> initDays() {
        List<OptionsItem> list = new ArrayList<>();
        for (int i = 1; i <= 31; i++) {
            OptionsItem optionsItem = new OptionsItem();
            optionsItem.setLabel(i + "");
            optionsItem.setValue(i + "");
            list.add(optionsItem);
        }
        return list;
    }

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