<template>
    <td :style="containerStyle" :class="containerClass" role="cell" :colspan="columnProp('colspan')" :rowspan="columnProp('rowspan')" v-bind="{ ...getColumnPT('root'), ...getColumnPT('footerCell') }" :data-p-frozen-column="columnProp('frozen')">
        <component v-if="column.children && column.children.footer" :is="column.children.footer" :column="column" />
        {{ columnProp('footer') }}
    </td>
</template>

<script>
import BaseComponent from 'primevue/basecomponent';
import { DomHandler, ObjectUtils } from 'primevue/utils';
import { mergeProps } from 'vue';
import { NumberFormat } from '../utils/NumberFormat';

export default {
    name: 'FooterCell',
    hostName: 'DataTable',
    extends: BaseComponent,
    props: {
        column: {
            type: Object,
            default: null
        },
        index: {
            type: Number,
            default: null
        }
    },
    data() {
        return {
            styleObject: {},
            d_value: null  /*保存计算公式返回的值 ，可以实现按计算结果设置样式 */
        };
    },
    mounted() {
        if (this.columnProp('frozen')) {
            this.updateStickyPosition();
        }
    },
    updated() {
        if (this.columnProp('frozen')) {
            this.updateStickyPosition();
        }
    },
    methods: {
        columnProp(prop) {
            if(prop==='footer' && this.$parentInstance ){
                 let dataTable=this.$parentInstance;
                 let footer = this.column.props['footer'] || '';

                 if(footer==='')
                    return '';

                 let field= this.column.props['field'] || ''
                 let value =footer;

                 if(typeof footer==='function' ){
                    this.d_value= footer( dataTable.primaryData,field );
                    value= NumberFormat(this.d_value, this.numberFormat ) ;
                 } 

                 else if( field && ( typeof footer==='string' ) &&  footer.startsWith("=") ){
                     const exp=footer.substring(1);

                     if(exp!==''){
                        this.d_value= dataTable.evaluate(exp,field)
                        value= NumberFormat(this.d_value, this.numberFormat) ;
                     }
                 }   
                 return value;
            }
            return ObjectUtils.getVNodeProp(this.column, prop);
        },
        getColumnPT(key) {
            const columnMetaData = {
                props: this.column.props,
                parent: {
                    instance: this,
                    props: this.$props,
                    state: this.$data
                },
                context: {
                    index: this.index,
                    size: this.$parentInstance?.$parentInstance?.size,
                    showGridlines: this.$parentInstance?.$parentInstance?.showGridlines || false
                }
            };

            return mergeProps(this.ptm(`column.${key}`, { column: columnMetaData }), this.ptm(`column.${key}`, columnMetaData), this.ptmo(this.getColumnProp(), key, columnMetaData));
        },
        getColumnProp() {
            return this.column.props && this.column.props.pt ? this.column.props.pt : undefined;
        },
        updateStickyPosition() {
            if (this.columnProp('frozen')) {
                let align = this.columnProp('alignFrozen');

                if (align === 'right') {
                    let right = 0;
                    let next = DomHandler.getNextElementSibling(this.$el, '[data-p-frozen-column="true"]');

                    if (next) {
                        right = DomHandler.getOuterWidth(next) + parseFloat(next.style.right || 0);
                    }

                    this.styleObject.right = right + 'px';
                } else {
                    let left = 0;
                    let prev = DomHandler.getPreviousElementSibling(this.$el, '[data-p-frozen-column="true"]');

                    if (prev) {
                        left = DomHandler.getOuterWidth(prev) + parseFloat(prev.style.left || 0);
                    }

                    this.styleObject.left = left + 'px';
                }
            }
        }
    },
    computed: {
        containerClass() {
             //增加列的对齐方式的设置
            const align= (this.columnProp('align') || '' ).toLowerCase();
            const alignClass= (align==='')? null : 'text-'+align;

            return [this.columnProp('footerClass'), this.columnProp('class'),  alignClass, this.cx('footerCell')];
        },
        containerStyle() {
             //增加列宽度的设置
             let width =  this.columnProp('width') || '';
            let widthStyle =(width==='') ? null : { width }  ;

            let bodyStyle = this.columnProp('footerStyle');
            let columnStyle = null ;//this.columnProp('style');

             //设置的格式是一个函数，则把当前脚本区的值传给函数计算，返回相应的格式
             if((typeof footerStyle)==='function')
            {
                if(this.d_value!==null){
                    footerStyle = footerStyle(this.d_value, this.columnProp('field'));
                }else{
                    footerStyle= null;
                }
            }

            return this.columnProp('frozen') ? [widthStyle, columnStyle, bodyStyle, this.styleObject] : [columnStyle, bodyStyle];
        },
        numberFormat(){
            let format = this.column.props["format"] || '';

            if(format==='' && this.column.props["editor"]==="InputNumber"){
                const editorProps=this.column.props["editorProps"] || {} ;
                const {minFractionDigits:minimumFractionDigits, maxFractionDigits:maximumFractionDigits} = editorProps;
                
                format =Object.assign( {}, editorProps, { style:'decimal', useGrouping:true , minimumFractionDigits,maximumFractionDigits } )
            }

            return format
        }
    }
};
</script>
