import { h } from "vue"
import { ElMessageBox } from "element-plus"
import { mapPropertyToLabel, requiredProps } from "./columns-config"
import { mapLabelToSource, mapLabelToType, sourceOptions, typeOptions } from "../../config/shared"
import { cg_order_type_options, mapGoodTypeToLabel, mapLabelToInvtype, xs_inv_type_list } from "/@/utils/status"

function onDisplayErrorMessage(errors) {
    ElMessageBox({
        type: 'warning',
        title: '数据校验失败',
        message: h('div', { style: 'display: flex, flex-direction: column' }, {
            default: () => Object.keys(errors).reduce((prev, index) => {
                return [
                    ...prev,
                    h('p', {}, '第' + index  + '行,' + errors[index])
                ]
            }, [])
        })
    })
}


export function isValidRequired(results: any[]) {
    const errors = {}

    let isTypeError = false
    let isSourceError = false
    let isInvError = false
    let isGoodType = false

    for (const sourceIndex in results) {
        const item = results[sourceIndex];
        const index = Number(sourceIndex) + 1;

        for (const key in item) {
            const value = String(item[key]).trim()
            if (requiredProps.includes(key) && (!value || !String(value).trim())) {
                if (errors[index]) {
                    errors[index] = errors[index] += '、' + mapPropertyToLabel[key]
                } else {
                    errors[index] = mapPropertyToLabel[key]
                }
            } else if (key === 'type' && !mapLabelToType[value]){
                isTypeError = true
            } else if (key === 'source' && !mapLabelToSource[value]) {
                isSourceError = true
            } else if (key === 'inv_type' && !mapLabelToInvtype[value]){
                isInvError = true
            } else if (key === 'goodType' && !mapGoodTypeToLabel[value]) {
                isGoodType = true
            }
        }

        if (errors[index]) {
            errors[index] += '不能为空'
            if (isTypeError) errors[index] += ' , 类型必须为' + typeOptions.map(({ label }) => label).join('、')
            if (isSourceError) errors[index] += ' ,订单来源必须为' + sourceOptions.map(({ label }) => label).join('、')
            if (isInvError) errors[index] += ' ,发票类型必须为' + xs_inv_type_list.map(({ label }) => label).join('、')
            if (isGoodType) errors[index] += ' ,商品类型必须为' + cg_order_type_options.map(({ label }) => label).join('、')
        } else {
            if (isTypeError) errors[index] = '类型必须为' + typeOptions.map(({ label }) => label).join('、')
            if (isSourceError) errors[index] = ' 订单来源必须为' + sourceOptions.map(({ label }) => label).join('、')
            if (isInvError) errors[index] = ' 发票类型必须为' + xs_inv_type_list.map(({ label }) => label).join('、')
            if (isGoodType) errors[index] = '商品类型必须为' + cg_order_type_options.map(({ label }) => label).join('、')
        }
    }


    if (Object.keys(errors).length === 0) {
        return true
    } else {
        onDisplayErrorMessage(errors)
        return false
    }
}

export function isImportDataValid(results: any[]) {
    /* 校验必填字段是否填入 */
    if (!isValidRequired(results)) { return false }
    return true
}