import u from "/@/utils/u";

/**
 * 字段信息处理工具类
 */
const fieldUtil = {
    fieldList: [
        {name: '单行文本', type: 'text', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '多行文本', type: 'textarea', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '数字框', type: 'number', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '下拉框', type: 'dict', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '搜索下拉框', type: 'select', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '单选框', type: 'radio', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '复选框', type: 'checkbox', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '富文本', type: 'rich', icon: 'ele-Aim', cluster: '基础组件', conf: {mode: 'simple'}},
        {name: '图片', type: 'image', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '附件', type: 'attach', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '日历', type: 'calendar', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '日期', type: 'datetime', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '日期区间', type: 'daterange', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '评分', type: 'rate', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '进度', type: 'progress', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '颜色', type: 'color', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '开关', type: 'switch', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '滑块', type: 'silde', icon: 'ele-Aim', cluster: '基础组件'},
        {name: '部门', type: 'dept', icon: 'ele-Aim', cluster: '高级组件'},
        {name: '成员', type: 'user', icon: 'ele-Aim', cluster: '高级组件'},
        {name: '搜索', type: 'search', icon: 'ele-Aim', cluster: '高级组件'},
        {name: '级联框', type: 'cascade', icon: 'ele-Aim', cluster: '高级组件'},
        {name: '表格', type: 'table', icon: 'ele-Aim', cluster: '高级组件'},
        {name: '定时器', type: 'cron', icon: 'ele-Aim', cluster: '高级组件'},
        {name: '关联字段', type: 'assofield', icon: 'ele-Aim', cluster: '高级组件'},
        {name: '关联列表', type: 'assolist', icon: 'ele-Aim', cluster: '高级组件'},
        {name: '应用成员', type: 'appletuser', icon: 'ele-Aim', cluster: '高级组件'},
        {name: '应用角色', type: 'appletrole', icon: 'ele-Aim', cluster: '高级组件'},
        {name: '分栏', type: 'divider', icon: 'ele-Aim', cluster: '其他组件'},
        {name: '提示语', type: 'tip', icon: 'ele-Aim', cluster: '其他组件'},
        {name: '标题栏', type: 'title', icon: 'ele-Aim', cluster: '其他组件'},
        {name: '渲染器', type: 'render', icon: 'ele-Aim', cluster: '其他组件'},
        {name: '当前时间', type: 'curdate', icon: 'ele-Aim', cluster: '静态组件'},
        {name: '当前部门', type: 'curdept', icon: 'ele-Aim', cluster: '静态组件'},
        {name: '当前用户', type: 'curuser', icon: 'ele-Aim', cluster: '静态组件'},
    ],

    toFormQueryField(fields: Array<IField>): Array<any> {
        let ret = [] as Array<any>;
        if (u.isEmptyOrNull(fields)) {
            return ret;
        }
        for (let i = 0; i < fields.length; i++) {
            let f = fields[i];
            if (!f.query) {
                continue;
            }
            //不支持查询的字段类型
            if (!f.type || ["rich", "image", "attach", "calendar", "table", "map", "color", "search", "title", "split", "button", "tip", 'divider'].includes(f.type)) {
                continue;
            }
            //查询框分类  文本、数字、下拉、部门、用户、搜索框
            let fconfig = {
                ...f,
                xs: 12,
                sm: 8,
                md: 6,
                lg: 6,
                xl: 3,
                rules: [],
                placeholder: '请输入' + f.label,
                readonly: false
            }
            if (fconfig.conf) {
                fconfig.conf.clearable = true;
            }
            //queryType高优先级覆盖
            if (f.queryType) {
                f.type = f.queryType;
            }
            //文本类
            if (['text', 'textarea'].includes(f.type)) {
                fconfig.type = 'text';
            } else if (['number', 'rate', 'progress', 'slide'].includes(f.type)) {
                fconfig.type = 'number';
            }
            if (['date', 'time', 'number', 'datetime', 'rate', 'progress', 'slide'].includes(f.type)) {
                if (f.conf && f.conf.op) {
                    let operation = this.convertOperate(f.conf.op);
                    if (operation) {
                        ret.push({
                            ...fconfig,
                            prop: `${f.prop}${operation.key}`,
                            label: `${f.label}${operation.remark}`,
                            placeholder: `${f.label}${operation.remark}`
                        });
                    } else {
                        console.error("不能识别的操作符:", f.conf.op)
                    }

                } else {
                    ret.push({
                        ...fconfig,
                        prop: `${f.prop}Start`,
                        label: `${f.label}大于`,
                        placeholder: `${f.label}大于`
                    });
                    ret.push({
                        ...fconfig,
                        prop: `${f.prop}End`,
                        label: `${f.label}小于`,
                        placeholder: `${f.label}小于`
                    });
                }
            } else {
                ret.push(fconfig);
            }

        }
        console.log("toFormQueryField>>>>", JSON.stringify(ret))
        return ret;

    },
    toFormCreateField(fields: Array<IField>): Array<any> {
        let ret = [] as Array<any>;
        if (u.isEmptyOrNull(fields)) {
            return ret;
        }
        console.log("toFormCreateField>>>>", JSON.stringify(fields))
        for (let i = 0; i < fields.length; i++) {
            let f = fields[i];
            //表单 分类  文本、数字、下拉、部门、用户、搜索框
            let fconfig = {
                xs: 12,
                sm: 12,
                md: 12,
                lg: 4,
                xl: 4,
                rules: [],
                ...f,
                placeholder: f.placeholder || ('请设置' + f.label),
                disabled: !!f.readonly
            }
            if (fconfig.conf) {
                fconfig.conf.clearable = true;
            }

            if (['textarea', 'divider'].includes(f.type)) {
                fconfig = {...fconfig, xs: 24, sm: 24, md: 24, lg: 24, xl: 24}
            }

            if (['dept', 'user', 'search'].includes(f.type)) {
                fconfig["displayList"] = f.prop.replaceAll("IdList", "List")
                    .replaceAll("Id", "Name");
            }

            ret.push(fconfig);

        }
        console.log("toFormCreateField>>>>", JSON.stringify(ret))
        return ret;
    },
    toTableShowField(fields: Array<IField>): Array<IField> {
        let ret = [] as Array<IField>;
        if (u.isEmptyOrNull(fields)) {
            return ret;
        }

        for (let i = 0; i < fields.length; i++) {
            let f = {...fields[i]};
            //不支持查询的字段类型
            if (!f.type || ["calendar", "table", "map", "color", "search", "title", "split", "button", "tip"].includes(f.type)) {
                continue;
            }
            if (f.hide) {
                continue
            }
            if (!f.width && f.autoWidth) {
                f.width = f.label.length * 25 + 20;
            }

            //date字段宽度
            if (this.isDateField(f) && !f.width) {
                f.width = 150;
            }
            ret.push(f);
        }
        console.log("toTableShowField>>>>", JSON.stringify(ret))
        return ret;
    },
    toWorkflowField(fields: Array<IField>): Array<IField> {
        let ret = [] as Array<IField>;
        return ret;
    },
    toTaskWriteField(fields: Array<any>): Array<IField> {
        let ret = [] as Array<any>;
        if (u.isEmptyOrNull(fields)) {
            return ret;
        }
        console.log("toTaskWriteField>>>>", JSON.stringify(fields))
        for (let i = 0; i < fields.length; i++) {
            let f = fields[i];
            let conf = f.conf||{};
            let ww = conf.ww || 3;  //6等分
            let widthStyle = {
                xs: ww * 4,
                sm: ww * 4,
                md: ww * 4,
                lg: ww * 2,
                xl: ww * 2
            }
            //表单 分类  文本、数字、下拉、部门、用户、搜索框
            let fconfig = {
                ...widthStyle,
                rules: [],
                ...f,
                placeholder: f.remark || ('请设置' + f.name),
            }

            if (['textarea', 'divider','rich'].includes(f.type)) {
                fconfig = {...fconfig, xs: 24, sm: 24, md: 24, lg: 24, xl: 24}
            }

            if (['dept', 'user', 'search'].includes(f.type)) {
                fconfig["displayList"] = f.prop.replaceAll("IdList", "List")
                    .replaceAll("Id", "Name");
            }

            ret.push(fconfig);

        }
        console.log("toTaskWriteField>>>>", JSON.stringify(ret))
        return ret;
    },
    convertOperate(op: string) {
        let operations = [
            {
                symbols: ["eq", "="],
                key: '',
                remark: '等于'
            },
            {
                symbols: ["neq", "!=", "<>"],
                key: 'Not',
                remark: '不等于'
            },
            {
                symbols: ["gt", ">"],
                key: 'Start',
                remark: '大于'
            },
            {
                symbols: ["gte", ">="],
                key: 'StartC',
                remark: '不小于'
            },
            {
                symbols: ["lt", "<"],
                key: 'End',
                remark: '小于'
            },
            {
                symbols: ["lte", "<="],
                key: 'EndC',
                remark: '不大于'
            },
            {
                symbols: ["in"],
                key: 'In',
                remark: '包含'
            },
            {
                symbols: ["nin"],
                key: 'Nin',
                remark: '不包含'
            },

        ]

        let sym = operations.find(k => k.symbols.includes(op));
        if (sym) {
            return sym;
        }
        return null;
    },
    isNormalField(field: IField) {
        return ['text', 'textarea'].includes(field.type)
    },
    isDateField(field: IField) {
        return ['date', 'time', 'datetime'].includes(field.type)
    },
    isDeptUserField(field: IField) {
        return ['dept', 'user'].includes(field.type)
    },
    value(field: IField, row: any) {
        let ret: any = row[field.prop];
        if (this.isNormalField(field)) {
            ret = row[field.prop];
        } else if (this.isDateField(field)) {
            if (field.conf && field.conf.format) {
                return field.conf.format(row[field.prop]);
            }
        } else if (this.isDeptUserField(field)) {
            let multiple = !!(field.conf?.multiple);
            let prop = field.prop;
            if (multiple) {
                //ownerIdList==> ownerList  col_100==>col_100val
                if (prop.endsWith("IdList")) {
                    let key = prop.substring(0, prop.indexOf("IdList")) + "List"
                    if (row[key]) {
                        //判断是否数组
                        if (Array.isArray(row[key])) {
                            ret = row[key].map((k: any) => k.name || k.title);
                        }
                    }
                } else if (prop.startsWith("col_")) {
                    let key = prop + "val";
                    if (row.formData[key]) {
                        if (Array.isArray(row.formData[key])) {
                            ret = row.formData[key].map((k: any) => k.name || k.title);
                        }
                    }
                }
                if (!ret) {
                    return [];
                }
                return ret;
            } else {
                //createBy==>createName  categroyId=>categoryName   col_100 =>col_100val
                if (prop.endsWith("By")) {
                    let key = prop.substring(0, prop.indexOf("By")) + "Name"
                    if (row[key]) {
                        ret = [row[key]]
                    } else {
                        if (row.formData[key]) {
                            ret = row.formData[key];
                        }
                    }
                } else if (prop.endsWith("Id")) {
                    let key = prop.substring(0, prop.indexOf("Id")) + "Name"
                    if (row[key]) {
                        ret = [row[key]]
                    }
                } else if (prop.startsWith("col_")) {
                    let key = prop + "val";
                    if (row[key]) {
                        ret = [row[key]]
                    }
                }
                if (!ret) {
                    return [];
                }
                return ret;
            }
        }
        if (!ret && field.conf && field.conf.deft) {
            ret = field.conf.deft;
        }
        return ret;
    },
    setFormSortQuery(order: any, formQuery: any) {
        console.log(order, formQuery)
        //
        let key = order?.column?.columnKey || order?.prop;
        if (!key) {
            return;
        }
        formQuery.sortFields = formQuery.sortFields || [];
        let keyIndex = formQuery.sortFields.indexOf(key);
        let ord = order.order;
        if (ord === "descending") {
            formQuery[`${key}Sort`] = 2;
            if (keyIndex < 0) {
                // formQuery.sortFields.push(key);
            }
        } else if (ord === "ascending") {
            formQuery[`${key}Sort`] = 1;
            if (keyIndex < 0) {
                // formQuery.sortFields.push(key);
            }
        } else {
            formQuery[`${key}Sort`] = null;
            if (keyIndex >= 0) {
                // formQuery.sortFields.splice(keyIndex,1);
            }
        }

        console.log(order, formQuery)
    }
}


export default fieldUtil;
