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

class ForminfoService extends ServiceBase {
    constructor() {
        super("configmag", ServiceBase.getDaoName(ForminfoService));
        this.formitemSve = system.getObject("service.configmag.formitemSve");
    }

    /**
     * 创建表单
     * @param pobj
     * @returns {Promise<void>}
     */
    async createForm(pobj){
        if(!pobj.name){
            return system.getResult(null,'name can not be empty');
        }
        if(!pobj.form_describe){
            return system.getResult(null,'form_describe can not be empty');
        }

        let code = await this.getBusUid("fm")
        pobj.code = code;
        pobj.user_id = pobj.userid;
        pobj.user_name = pobj.username;
        let result = await this.create(pobj)
        return system.getResult(result);
    }
    /**
     * 修改方法
     * @param pobj
     * @returns {Promise<void>}
     */
    async updateForm(pobj) {
        let upData = {
            name:pobj.name,
            form_describe: pobj.form_describe
        }
        //获取相关表单项
        let itemData = await this.formitemSve.findAll({form_id:pobj.id},[]);
        let form_items = '';
        if(itemData.length>0){
            itemData.forEach(v=>{
                form_items += v.item_type_name + ",";
            })
            form_items = form_items.substring(0,form_items.length-1)
        }
        upData.form_items = form_items;
        //组装form表单
        let form = await this.packageForm(itemData);
        upData.form_table = form;
        let result = await this.updateByWhere(upData,{id:pobj.id})
        return system.getResult(result);
    }

    //组装form
    async packageForm(items) {
        let data = {
            "phone": "input",
            "singleBtn": "radiogroup",
            "multipleBtn": "checkgroup",
            "downOptions": "dic-select",
            "singleText": "input",
            "multipleText": "textarea",
            "dateTime": "datetime",
            "area": "tree-sel"//忽略 4
        }

        let form = {};
        let ctls = [];
        if(items.length>0){
            items.forEach(item=>{
                let ctl = {}
                ctl['type'] = data[item.item_type];
                ctl['label'] = item.name;
                ctl['prop'] = item.name;
                //单选框 多选框 下拉选项 添加options属性 结构为  a,b,c
                if(['singleBtn','multipleBtn','downOptions'].includes(item.item_type) && item.config_params){
                    ctl['options'] = item.config_params.options
                }
                //校验
                let rules = [{ "required": true, "message": '  ', "trigger": 'blur' }];
                //单行文本 多行文本 增加校验项 有最小和最大值
                if(['singleText','multipleText'].includes(item.item_type)){
                    let rule =  { "validator": "validatex", "trigger": "blur","minchars":item.config_params.input_length[0],"maxchars":item.config_params.input_length[1]}
                    rules.push(rule)
                }
                //手机号选项 增加校验项 有最大最小值
                if('phone'== item.item_type){
                    let param = item.config_params;
                    let minchars,maxchars = 11;
                    if(param.mobile_input_length == 2){
                        minchars = 7;
                        maxchars = 11;
                    }
                    let rule = { "validator": "validatex", "trigger": "blur","minchars":minchars,"maxchars":maxchars}
                    rules.push(rule);
                }
                //省市选项 增加 ignorelevel属性 忽略区县为4，不忽略为5
                if('area' == item.item_type){
                    let param = item.config_params;
                    if(param.is_show_county == 0){
                        ctl['ignorelevel'] = 4;
                    }else{
                        ctl['ignorelevel'] = 5;
                    }
                }
                ctl['rules'] = rules;
                ctls.push(ctl);
            })
            //组装form 格式表单
            form = {
                name: "xxx",
                main: [
                    {
                        "title": "表单信息",
                        cols:1,
                        ctls: ctls
                    }
                ]
            }
        }
        return form;
    }

    async getFormList (pobj) {
        let res = await this.dao.findAndCountAll(pobj);
        return system.getResult(res);
    }
}

module.exports = ForminfoService;