package com.bohuikeji.frame.module.admin.entity.web;

import com.bohuikeji.frame.module.admin.base.HtmlBlockFormBase;
import com.bohuikeji.frame.module.admin.base.SuperBase;
import com.bohuikeji.frame.module.admin.entity.web.block.ButtonBlock;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 表格弹出层model用于新增，修改、展示详情等
 * @author 10552
 */
@Data
@Slf4j
public class TableModel extends SuperBase {
    /**
     * 标题
     */
    private String title="新增/编辑";
    /**
     * 表单提交地址
     */
    private String url="/table/model/form";
    /**
     * 输入组件
     */
    private List<HtmlBlockFormBase> inputBlocks;

    /**
     * 新增组件
     */
    private List<HtmlBlockFormBase> addBlocks;

    /**
     * 编辑提交js
     */
    private String js;
    /**
     * 新增js
     */
    private String newJs;
    /**
     * 操作组件
     */
    private List<ButtonBlock> footButtons;


    public String getTitle() {
        return title;
    }

    public TableModel setTitle(String title) {
        this.title = title;
        return this;
    }

    public String getUrl() {
        return url;
    }

    public TableModel setUrl(String url) {
        this.url = url;
        return this;
    }

    public List<HtmlBlockFormBase> getInputBlocks() {
        return inputBlocks;
    }

    public TableModel setInputBlocks(List<HtmlBlockFormBase> inputBlocks) {
        this.inputBlocks = inputBlocks;
        StringBuilder str=new StringBuilder();;
        for ( HtmlBlockFormBase form:this.inputBlocks) {
            if (form==null){
                continue;
            }
            //针对已禁用的组件不生成提交js代码
            if ("disabled".equals(form.getDisabled())){
                continue;
            }
            String id=form.getId();
            str.append(form.getName()+":$('#"+id+"').val(),");
        }
        this.js=str.length()>0?str.deleteCharAt(str.length()-1).toString():"";
        return this;
    }

    public List<ButtonBlock> getFootButtons() {
        return footButtons;
    }

    public TableModel setFootButtons(List<ButtonBlock> footButtons) {
        this.footButtons = footButtons;
        return this;
    }

    public String getJs() {
        return js;
    }

    public TableModel setJs(String js) {
        this.js=js;
        return this;
    }

    public List<HtmlBlockFormBase> getAddBlocks() {
        return addBlocks;
    }

    public TableModel setAddBlocks(List<HtmlBlockFormBase> addBlocks) {
        this.addBlocks = addBlocks;
        if (addBlocks==null||addBlocks.size()<1){
            return this;
        }
        StringBuilder newStr=new StringBuilder();
        for ( HtmlBlockFormBase form:addBlocks) {
            if (form==null){continue;}
            String id=form.getId();
            newStr.append(form.getName()+":$('#"+id+"_new').val(),");
        }
        if(StringUtils.isBlank(newStr)){
            this.newJs="";
            return this;
        }
        this.newJs=newStr.deleteCharAt(newStr.length()-1).toString();
        return this;
    }
}
