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);

        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": "textarea",
            "multipleText": "textarea",
            "dateTime": "datetime",
            "area": "省市"
        }

        let form = {};
        let ctls = [];
        if(items.length>0){
            items.forEach(item=>{
                console.log(item.config_params)
                let ctl = { "type": `${data[item.item_type_name]}`, "label": "名称",options:'ddd,,dd,,dd' ,"prop": "name",rules: [{ "required": true, "message": '  ', "trigger": 'blur' },] }
                ctl['type'] = data[item.item_type_name];
                ctl['label'] = item.name;
                ctl['prop'] = item.item_type;
                ctl['rules'] =
                ctls.push();
            })
            form = {
                name: "xxx",
                main: [
                    {
                        "title": "表单信息",
                        cols:1,
                        ctls: [
                            { "type": "input", "label": "名称",options:'ddd,,dd,,dd' ,"prop": "name","style": "padding-left:6px",rules: [{ "required": true, "message": '  ', "trigger": 'blur' },] },
                            { "type": "dic-select", "label": "类型", options:"","prop": "item_type", "placeHolder": "选择类型",dicName:"control_type","style": "width:100%",isMulti:false,rules: [{ "required": true, "message": '  ', "trigger": 'blur' },] },
                            { "type": "input", "label": "排序", "prop": "sequence","style": "padding-left:6px",rules: [{ "required": true, "message": '  ', "trigger": 'blur' },] },
                        ]
                    }
                ]
            }
        }
        return form;
    }

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

module.exports = ForminfoService;