const system = require("../../../system");
const ServiceBase = require("../../sve.base");
const settings = require("../../../../config/settings");

class FormitemService extends ServiceBase {
    constructor() {
        super("configmag", ServiceBase.getDaoName(FormitemService));
    }


    /**
     * 创建表单项
     * @param pobj
     * @returns {Promise<void>}
     */
    async createItem(pobj) {
        let config_params = await this.packageConfigParams(pobj);
        let code = await this.getBusUid('it');
        pobj.config_params = config_params;
        pobj.item_type_name = pobj.item_type_name;
        pobj.code = code;
        //保存表单项
        let result = await this.create(pobj);
        return system.getResult(result)
    }

    /**
     * 修改表表单项
     * @param pobj
     * @returns {Promise<void>}
     */
    async updateItem(pobj) {
        let upResult = await this.update(pobj);
        return system.getResult(upResult);
    }

    /**
     * 组装表单项参数
     * @param pobj
     * @returns {Promise<void>}
     */
    async packageConfigParams(pobj){
        let config_params = {};
        switch (pobj.item_type) {
            case "phone":
                config_params["mobile_input_length"]=pobj.mobile_input_length =="specific" ?1:2;
                config_params["verify_sms"]=pobj.verify_sms==true?1:0;
                break;
            case "singleBtn":
            case "multipleBtn":
            case "downOptions":
                config_params["options"] = pobj.options;
                break;
            case "singleText":
            case "multipleText":
                let length = []
                length.push(pobj.input_les);
                length.push(pobj.input_lar);
                config_params["input_length"] = length;
                break;
            case "dateTime":
                config_params["date_type"] = pobj.date_type == "specific" ? 1 : 2;
                config_params["date_accuracy"] = pobj.date_type == "day" ? 1 : 2;
                config_params["date_shield_type"] = pobj.date_shield_type == "default" ? 1 : 2;
                config_params["shield_date"] = pobj.shield_date;
                break;
            case "area":
                config_params["is_show_county"] = pobj.is_show_county;
                break;
            default:
                break;
        }
        return config_params;
    }
}

module.exports = FormitemService;