import { doFetch } from "./doFetch";

function formatFieds(data) {
    let obj = {};
    for (let i = 0; i < data.length; i++) {
        let item = data[i],
            fieldsKey = "field" + i;
        if (item.fieldChar == 1) {
            obj[fieldsKey] = {
                value: item?.fieldRealValue ?? "",
                title: item.fieldName,
                name: [fieldsKey],
                required: false,
                type: "input",
            };
        } else {
            obj[fieldsKey] = {
                value:
                    item?.fieldRealValue == 0 || item?.fieldRealValue
                        ? item?.fieldRealValue
                        : item.fieldChar == 4
                            ? []
                            : "",
                title: item.fieldName,
                name: [fieldsKey],
                required: false,
                type: item.fieldChar == 3 ? "radio" : "select",
                options: item.valueList,
                multiple: item.fieldChar == 4 ? true : false,
            };
        }
    }
    return obj
}

async function tempfields(url, params) {
    let datas = await doFetch({ url, params })
    let list = datas?.data?.dataList;
    return {
        fields: formatFieds(list),
        data: list
    }
}


export default tempfields