export const mapFields = {
    invoiceType:'invoiceType', //发票类型
    invoiceNumber:"invoiceNumber", //发票号码
    invoiceCode:'invoiceCode',  //发票代码
    invoiceName:'invoiceName', //发票名称
    openTime:'openTime', //开票日期
    beforeTax:'beforeTax', //税前金额
    afterTax:'afterTax' //税后金额
}

export const getTableProperty = (key) => mapTemplateToTable[key]

/***
 * @props
 * 发票申请编号 invoiceNo
 * 发票状态 invoiceStatus
 * 发票类型 invoiceType
 * 发票号码 invoiceNumber
 * 发票代码 invoiceCode
 * 发票名称 invoiceName
 * 开票日期 openTime
 * 税前金额 beforeTax
 * 税后金额 afterTax
 * 校验码 checkNumber
*/

export const manuallyUploadRules = {
   invoiceType:[
        {
            trigger: "blur",
            message: "请选择发票类型",
            required: true
        }
    ],
    invoiceNumber: [
        {
            trigger: "blur",
            message: "请输入发票号码",
            required: true
        }
    ],
    invoiceCode: [
        {
            trigger: "blur",
            message: "请选择发票代码",
            required: true
        }
    ],
    openTime: [
        {
            trigger: "blur",
            message: "请选择开票时间",
            required: true
        }
    ],
    beforeTax: [
        {
            trigger: "blur",
            message: "请输入金额",
            required: true
        }
    ],
    invoiceName: [
        {
            trigger: "blur",
            message: "请输入发票名称",
            required: true
        }
    ],
    checkNumber: [
        {
            trigger: "blur",
            message: "请输入校验码",
            required: true
        }
    ]
}

const mapTemplateToTable = {
    '发票申请编号': "invoiceNo",
    '发票类型': "invoiceType",
    '发票号码': "invoiceNumber",
    '开票日期': "openTime",
    '税后金额': "afterTax",
    '发票代码': "invoiceCode",
    '税前金额': "beforeTax",
    '校验码': "checkNumber"
}

export const columns = [
    {
        type: 'index',
        label: '序号',
        width: '70px'
    },
    {
        prop: "invoiceNo",
        label: '发票申请编号',
        width: '110px',
        required: true
    },
    {
        prop: "invoiceType",
        label: '发票类型',
        width: '120px',
        required: true
    },
    {
        prop: "invoiceNumber",
        label: '发票号码',
        width: '156px',
        required: true
    },
    {
        prop: "openTime",
        label: '开票日期',
        width: '180px',
        required: true
    },
    {
        prop: "afterTax",
        label: '税后金额',
        width: '156px',
        required: true
    },
    {
        prop: "invoiceCode",
        label: '发票代码',
        width: '156px',
        required: false
    },
    {
        prop: "beforeTax",
        label: '税前金额',
        width: '150px',
        required: false
    },
    {
        prop: "checkNumber",
        label: '校验码',
        minWidth: '150px',
        required: false
    },
]

export function createErrorMessage(messages) {
    const rows = Object.keys(messages)
    return rows.reduce((prev, row, index) => {
        const suffix = index === rows.length - 1 ? '</ul>' : ''
        const content = prev + ` <li><strong style="font-weight:700">第${row}行</strong>,${messages[row]} </li> `
        return content + suffix
    }, '<ul>')
}

export const createFieldVerification = (message) => ({
    notValidRows: [],
    isValid: true,
    message
})

export const template = Array(1).fill(1).map(() => Object.keys(mapTemplateToTable).reduce((prev, property) => {
    prev[property] = ''
    return prev
}, {}))

export const requsetFields = []

export const requiredFields = [
    "invoiceNo",
    "invoiceNumber",
    "invoiceType",
    "afterTax",
    "openTime"
]

export const numberFields = []

export const helper = {
    fields(sourceObject) {
        return Object.keys(sourceObject)
    },
    values(sourceObject, fields, index = 0) {
        return fields.map(field => {
            return sourceObject[field][index]
        })
    },
    write(sourceObject, fields) {
        return fields.reduce((prev, currentKey) => ({
            ...prev,
            [currentKey]: sourceObject[currentKey]
        }), {})
    }
}
