let RESPONSE_MOCK = {
    code: 200,
    message: 'success',
    data: {
        id: 1, //模板表单ID
        businessCode: '20018', //业务编码
        compId: '0x01', //公司ID
        fields: [
            {
                id: '101',
                bindingField: `{
                    "list": "基本信息",
                    "type": "input",
                    "name": "name",
                    "title": "姓名",
                    "alias": ""
                }`,
                defaultRequired: true,
                showable: true,
                notNull: false,
                exportable: true,
            },
            {
                id: '102',
                bindingField: `{
                    "list": "基本信息",
                    "type": "input",
                    "name": "age",
                    "title": "年龄",
                    "alias": ""
                }`,
                defaultRequired: false,
                showable: false,
                notNull: false,
                exportable: true,
            },
            {
                id: '103',
                bindingField: `{
                    "list": "基本信息",
                    "type": "input",
                    "name": "tel",
                    "title": "电话",
                    "alias": "手机号"
                }`,
                defaultRequired: false,
                showable: true,
                notNull: true,
                exportable: false,
            },

            {
                id: '104',
                bindingField: `{
                    "table": "学历明细",
                    "type": "select",
                    "name": "xueli",
                    "title": "学历",
                    "alias": ""
                }`,
                defaultRequired: true,
                showable: true,
                notNull: true,
                exportable: false,
            },
            {
                id: '105',
                bindingField: `{
                    "table": "学历明细",
                    "type": "input",
                    "name": "zhuanye",
                    "title": "专业",
                    "alias": ""
                }`,
                defaultRequired: false,
                showable: true,
                notNull: true,
                exportable: false,
            },
            {
                id: '106',
                bindingField: `{
                    "table": "学历明细",
                    "type": "daterange",
                    "name": "date",
                    "title": "时间",
                    "alias": "起止时间"
                }`,
                defaultRequired: false,
                showable: true,
                notNull: true,
                exportable: false,
            },
            {
                id: '107',
                bindingField: `{
                    "table": "学历明细",
                    "type": "textarea",
                    "name": "school",
                    "title": "学校",
                    "alias": ""
                }`,
                defaultRequired: false,
                showable: true,
                notNull: true,
                exportable: false,
            },
            {
                id: '108',
                bindingField: `{
                    "table": "学历明细",
                    "type": "file",
                    "name": "school",
                    "title": "附件",
                    "alias": ""
                }`,
                defaultRequired: false,
                showable: true,
                notNull: true,
                exportable: false,
            },
        ]
    }
}


/**
 * 解析绑定字段
 * @returns 
    {
        list: '基本信息', // 所属分组 list和table只能选一个
        table: '详细信息', // 所属表格 list和table只能选一个

        type: 'input', // 字段类型
        name: 'age', // 表单提交名

        title: '',//显示名
        alias: '',//别名
    }
 */
function parseBindingField(bindingField){
    try {
        return JSON.parse(bindingField);       
    } catch (error) {
        return undefined;
    }
}

/**
 * 序列化绑定字段
 */
function toBindingField(parent, child){
    let obj = {};

    if(parent.type == 'table'){
        obj.table = parent.title;
    }else{
        obj.list = parent.title;
    }

    obj.type = child.type;
    obj.name = child.name;
    obj.title = child.title,
    obj.alias = child.alias;

    return JSON.stringify(obj);
}

/**
 * 前端数据与后台数据的桥接类
 */
export default {
    toClient(data){
        try {
            let temp = {};


            for(let i=0; i<data.fields.length; i++) {
                let item = data.fields[i];

                let id = item.id;//后台主键
                let bindingField = item.bindingField;//字段
                let defaultRequired = item.defaultRequired;//必选
                let showable = item.showable;//是否显示
                let notNull = item.notNull;//是否必填
                let exportable = item.exportable;//参与导出

                let field = parseBindingField(bindingField);
                if(!field){
                    continue;
                }

                let isTable = !!field.table; // 字段所属分组类型 普通分组还是表格
                let title = isTable ? field.table : field.list;
                if(!temp[title]){ // 是否需要构建分组
                    temp[title] = {
                        title: title,
                        type: isTable ? 'table' : 'list',
                        children: []
                    };
                }

                temp[title].children.push({
                    id: id,
                    name: field.name,
                    type: field.type,
                    title: field.title,
                    alias: field.alias,
                    notNull: notNull,
                    export: exportable,
                    disabled: defaultRequired,
                    selected: defaultRequired ? true : showable
                })
            }

            let list = [];
            for(let key in temp){
                list.push(temp[key]);
            }
            return list;
        } catch (error) {
            return [];       
        }
    },

    toServer(data){
        let list = [];

        for(let i=0; i<data.length; i++){
            let parent = data[i];
            let children = data[i].children;

            for(let j=0; j<children.length; j++){
                let child = children[j];

                list.push({
                    id: child.id,
                    bindingField: toBindingField(parent, child),
                    defaultRequired: child.disabled,
                    showable: child.selected,
                    notNull: child.notNull,
                    exportable: child.export
                });
            }
        }

        return list;
    },

    RESPONSE_MOCK: RESPONSE_MOCK
}
