/**
 * 渲染
 */
import { objectProperty } from 'mars-pro/src/pro/util'
import omit from 'mars-pro/src/pro/omit';
import { getAllProps, cloneElement } from 'mars-pro/src/pro/vue-util';
import Vue from 'vue'

export function renderVnodeTemplate(scope, c, rowIndex, index, currentRowComponentInstances, isTable, onlyUseDef, isProDetail) {
    const that = scope
    const h = that.$createElement
    const childAttrs = getAllProps(c)
    let currentValue = objectProperty(that.model, `${that.prop}[${rowIndex}].${childAttrs.prop}`)
    const currentProp = `${that.prop}[${rowIndex}].${childAttrs.prop}`

    const useDef = objectProperty(c, 'data.attrs.use-def')
    if (useDef && that.$scopedSlots[useDef]) {
        const useVnode = that.$scopedSlots[useDef]({
            scope: scope,
            // 是否为明细模式: 仅对明细表格有效
            isDetail: isProDetail,
            // 行索引
            rowIndex: rowIndex,
            // model对象
            model: that.model,
            //当前行model
            propModel: objectProperty(that.model, `${that.prop}[${rowIndex}]`),
            //集合model
            arrsModel: objectProperty(that.model, `${that.prop}`),
            //当前行的prop属性
            modelProp: `${that.prop}[${rowIndex}]`,
            
            itemChange: (v) => {
                that.$parent.$emit('itemChange', {
                    // 最新值
                    value: v,
                    // 序号
                    index: rowIndex,
                    // prop属性
                    prop: childAttrs.prop,
                    // model对象
                    model: that.model,
                    //当前行model
                    propModel: objectProperty(that.model, `${that.prop}[${rowIndex}]`),
                    //集合model
                    arrsModel: objectProperty(that.model, `${that.prop}`),
                    //vnode: 主要是为了获取vnode.componentInstance
                    getComponentInstance: (prop) => (!prop ? currentVnode.componentInstance : currentRowComponentInstances[prop].componentInstance)
                })
            }
        })
        if (isProDetail === true) {
            return (
                h('pro-detail', {
                    props: {
                        ...childAttrs
                    },
                    attrs: {
                        ...childAttrs
                    }
                }, [useVnode])
            )
        } else {
            return useVnode
        }
    }

    if (onlyUseDef === true) {
        return null
    }

    const userRender = that.renderItem({
        // 最新值
        value: currentValue,
        // 行索引
        index: rowIndex,
        // prop属性
        prop: childAttrs.prop,
        // model对象
        model: that.model,
        //当前行model
        propModel: objectProperty(that.model, `${that.prop}[${rowIndex}]`),
        //集合model
        arrsModel: objectProperty(that.model, `${that.prop}`)
    }) || {}
    currentValue = objectProperty(that.model, `${that.prop}[${rowIndex}].${childAttrs.prop}`)
    let userVnodeDatas = { ...omit(userRender || {}, ['vif', 'vshow']) }
    if (userRender.vIf === false) {
        return null
    }
    let directives = userVnodeDatas.directives || (userVnodeDatas.directives = [])
    if (typeof userRender.vShow === 'boolean') {
        directives.push({
            name: 'show',
            value: userRender.vShow
        })
    }

    const currentVnode = cloneElement(c, {
        // 使用prop作为key
        key: childAttrs.prop || index,
        props: {
            ...(userRender.attrs || {}),
            ...(userRender.props || {}),
            prop: currentProp,
            value: currentValue,
            ...(isTable ? { label: undefined, labelWidth: undefined } : {})

        },
        attrs: {
            ...(userRender.attrs || {}),
            ...(userRender.props || {}),
            prop: currentProp,
            ...(isTable ? { label: undefined, labelWidth: undefined } : {})
        },
        on: {
            ...(userRender.on || {}),
            input: (v) => {
                Vue.set(objectProperty(that.model, `${that.prop}[${rowIndex}]`), childAttrs.prop, v)
            },
            change: function (v) {
                that.$parent.$emit('itemChange', {
                    // 最新值
                    value: v,
                    // 序号
                    index: rowIndex,
                    // prop属性
                    prop: childAttrs.prop,
                    // model对象
                    model: that.model,
                    //当前行model
                    propModel: objectProperty(that.model, `${that.prop}[${rowIndex}]`),
                    //集合model
                    arrsModel: objectProperty(that.model, `${that.prop}`),
                    //vnode: 主要是为了获取vnode.componentInstance
                    getComponentInstance: (prop) => (!prop ? currentVnode.componentInstance : currentRowComponentInstances[prop].componentInstance)
                })
            }
        },
        directives
    })
    currentRowComponentInstances[childAttrs.prop] = currentVnode

    if (isProDetail === true) {
        return (
            h('pro-detail', {
                props: {
                    ...childAttrs
                },
                attrs: {
                    ...childAttrs
                }
            }, [currentVnode])
        )
    }
    return currentVnode
}