package com.engine.organization.entity.extend.param;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.hrm.biz.FieldParam;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import weaver.general.Util;

/**
 * @author:dxfeng
 * @createTime: 2022/06/15
 * @version: 1.0
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ExtendInfoFieldParam {
    private Long id;
    // field_name
    private String fieldname;
    // field_name_desc
    private String fieldlabel;

    private Object fieldType;

    private String enable;

    private String required;

    private String isModify;

    private String key;

    public String getControlType() {
        JSONArray fieldType = (JSONArray) this.getFieldType();
        String fieldHtmlType = Util.null2String(fieldType.get(0));
        if (fieldHtmlType.equals("input")) {
            fieldHtmlType = "1";
        } else if (fieldHtmlType.equals("textarea")) {
            fieldHtmlType = "2";
        } else if (fieldHtmlType.equals("browser")) {
            fieldHtmlType = "3";
        } else if (fieldHtmlType.equals("check")) {
            fieldHtmlType = "4";
        } else if (fieldHtmlType.equals("select")) {
            fieldHtmlType = "5";
        } else if (fieldHtmlType.equals("upload")) {
            fieldHtmlType = "6";
        }
        return fieldHtmlType;
    }

    public String getBrowserType(String controlType) {
        String browserType = "1";
        JSONArray fieldType = (JSONArray) this.getFieldType();
        if (fieldType.size() > 1) {
            browserType = Util.null2String(fieldType.get(1));
        }
        if (controlType.equals("1")) {
            if (browserType.equals("text")) {
                browserType = "1";
            } else if (browserType.equals("int")) {
                browserType = "2";
            } else if (browserType.equals("float")) {
                browserType = "3";
            } else if (browserType.equals("file")) {
                browserType = "1";
            }
        }
        if (controlType.equals("2")) {
            browserType = "1";
        } else if (controlType.equals("3")) {
            browserType = Util.null2String(((JSONObject) fieldType.get(1)).get("value"));
        } else if (controlType.equals("4")) {
            browserType = "1";
        } else if (controlType.equals("5")) {
            browserType = "1";
        } else if (controlType.equals("6")) {
            if (browserType.equals("file")) {
                browserType = "1";
            }
        }
        return browserType;
    }

    public String getDbType(String controlType, String browserType) {
        FieldParam fp = new FieldParam();
        if (controlType.equals("1")) {
            JSONArray fieldType = (JSONArray) this.getFieldType();
            String dbLength = "100";
            if (fieldType.size() > 2) {
                dbLength = fieldType.get(2).toString();
                if (Integer.parseInt(dbLength) == 0) {
                    dbLength = "50";
                    fieldType.set(2,"50");
                }

            }
            fp.setSimpleText(Util.getIntValue(browserType, -1), dbLength);
        } else if (controlType.equals("2")) {
            fp.setText();
        } else if (controlType.equals("3")) {
            fp.setBrowser(Util.getIntValue(browserType, -1));
        } else if (controlType.equals("4")) {
            fp.setCheck();
        } else if (controlType.equals("5")) {
            fp.setSelect();
        } else if (controlType.equals("6")) {
            fp.setAttach();
        }
        return fp.getFielddbtype();
    }
}