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 ("input".equals(fieldHtmlType)) {
            fieldHtmlType = "1";
        } else if ("textarea".equals(fieldHtmlType)) {
            fieldHtmlType = "2";
        } else if ("browser".equals(fieldHtmlType)) {
            fieldHtmlType = "3";
        } else if ("check".equals(fieldHtmlType)) {
            fieldHtmlType = "4";
        } else if ("select".equals(fieldHtmlType)) {
            fieldHtmlType = "5";
        } else if ("upload".equals(fieldHtmlType)) {
            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 ("1".equals(controlType)) {
            if ("text".equals(browserType)) {
                browserType = "1";
            } else if ("int".equals(browserType)) {
                browserType = "2";
            } else if ("float".equals(browserType)) {
                browserType = "3";
            } else if ("file".equals(browserType)) {
                browserType = "1";
            }
        }
        if ("2".equals(controlType)) {
            browserType = "1";
        } else if ("3".equals(controlType)) {
            browserType = Util.null2String(((JSONObject) fieldType.get(1)).get("value"));
        } else if ("4".equals(controlType)) {
            browserType = "1";
        } else if ("5".equals(controlType)) {
            browserType = "1";
        } else if ("6".equals(controlType)) {
            if ("file".equals(browserType)) {
                browserType = "1";
            }
        }
        return browserType;
    }

    public String getDbType(String controlType, String browserType) {
        FieldParam fp = new FieldParam();
        if ("1".equals(controlType)) {
            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 ("2".equals(controlType)) {
            fp.setText();
        } else if ("3".equals(controlType)) {
            fp.setBrowser(Util.getIntValue(browserType, -1));
        } else if ("4".equals(controlType)) {
            fp.setCheck();
        } else if ("5".equals(controlType)) {
            fp.setSelect();
        } else if ("6".equals(controlType)) {
            fp.setAttach();
        }
        return fp.getFielddbtype();
    }
}