import {validateNull} from "/@/utils/validate";

const PRINT_REPLACE = {
    value: "value",
    date: ["yyyy", "MM", "dd", "HH", "mm", "ss"]
}

// 自定义组件打印格式
const PRINT_FORMAT = [{props: [], propTypes: ["elImage", "SignInput"], tag: "<img src=\"value\" alt=\"\" />"}
    ,{props: [], propTypes: ["checkbox"], typeVals: [{value: "0", label: "□"},{value: "1", label: "☑"}]}
    ,{props: [], propTypes: ["radio"], typeVals: [{value: "0", label: "☑否 □是"},{value: "1", label: "□否 ☑是"}]}
    ,{props: [], propTypes: ["datePicker"], format: "yyyy_年_MM_月_dd_日_HH_时_mm_分_ss_秒"}]

export function handlePrintValue(optionItems, formDatum, value, label, prop?, propType?) {
    if (!validateNull(optionItems)) {
        if (Array.isArray(formDatum)) {
            let showKeys = ''
            for (let i = 0; i < formDatum.length; i++) {
                let item = optionItems.find(f => f[value] === formDatum[i]);
                if (item) {
                    if (i === formDatum.length -1) showKeys += handleFormatValue(item[value], item[label], prop, propType)
                    else showKeys += handleFormatValue(item[value], item[label], prop, propType) + ","
                }
            }
            return showKeys
        } else {
            let item = optionItems.find(f => f[value] === formDatum);
            if (item) return handleFormatValue(item[value], item[label], prop, propType)
        }
    }
}

export function handleFormatValue(value, label, prop?, propType?) {
    if (validateNull(PRINT_FORMAT) || (!prop && !propType)) return label
    let find = PRINT_FORMAT.find(f => f.props.includes(prop) || f.propTypes.includes(propType));
    if (validateNull(find) || !value) return label
    if (find.format) {
        if (value instanceof Array) return label
        let format = find.format
        for (let i = 0; i < PRINT_REPLACE.date.length; i++) {
            const eachFormat = PRINT_REPLACE.date[i];
            let eachValue = ''
            if (i <= 2) eachValue = value.split(" ")[0].split("-")[i]
            else eachValue = value.split(" ")[1].split(":")[i - 3]
            format = format.replace(eachFormat, eachValue)
        }
        return format;
    }
    if (find.typeVals) return find.typeVals.find(f => f.value === value).label
    if (find.tag) return find.tag.replace(PRINT_REPLACE.value, value)
}
