<template>
    <!--关于操作列之后应该做的控制为:超过三个的情况下,第三个变成更多,通过下拉选择来进行触发,这时候需要考虑关于按钮隐藏的情况-->
    <div class="actions-column">
        <div class="action" v-for="(action, index) in actionList" :key="index">
            <Button
                v-if="action._visible"
                :disabled="action._disabled"
                size="small"
                :type="action.type ? action.type : 'text'"
                @click="onClick(action, index)"
            >{{ getActionTitle(action.title, index) }}</Button>
        </div>
    </div>
</template>
<script>
import queryUtil from "../queryUtil";
var currentRow = {};
export default {
    name: "tableActionColumn",
    props: {
        params: {
            type: Object,
            require: true
        },
        colItem: {
            type: Object,
            require: true
        },
        context: {},
        buttomParam: {},
        extraActionObject: {
            type: [Object, Array]
        },
        queryBus: {
            type: Object
        }
    },
    data() {
        for (let action of this.colItem.actions) {
            action._visible = true;
            action._disabled = false;
        }
        return {
            determineShowActions: [],
            actionList: this.colItem.actions ? this.colItem.actions : []
        };
    },
    mounted() {
        for (let action of this.colItem.actions) {
            this.setActionVisibleStatus(action);
            this.getActionEnableStatus(action);
        }
    },
    methods: {
        setActionVisibleStatus(v_action) {
            if (v_action && v_action.hasOwnProperty("visible")) {
                if (v_action.visible.type == "action") {
                    let functionName = v_action.visible.function;
                    let displayActions = [];
                    if (functionName) {
                        displayActions.push(v_action);
                        let actionPromises = this.getActionPromises(displayActions, this.params);
                        Promise.all(actionPromises).then((data) => {
                            v_action._visible = data[0].result;
                            this.$forceUpdate();
                            this.actionList = JSON.parse(JSON.stringify(this.colItem)).actions;
                        });
                    }
                }
                if (v_action.visible.type == "formula") {
                    v_action._visible = this.cacheFormula(v_action, "visible", this.params);
                    this.$forceUpdate();
                    this.actionList = JSON.parse(JSON.stringify(this.colItem)).actions;
                }
            }
        },
        getActionEnableStatus(d_action) {
            if (d_action && d_action.hasOwnProperty("disabled")) {
                if (d_action.disabled.type == "action") {
                    let functionName = d_action.disabled.function;
                    let disabledActions = [];
                    if (functionName) {
                        disabledActions.push(d_action);
                        let actionPromises = this.getActionEnables(disabledActions, this.params);
                        Promise.all(actionPromises).then((data) => {
                            d_action._disabled = data[0].enable;
                            this.$forceUpdate();
                            this.actionList = JSON.parse(JSON.stringify(this.colItem)).actions;
                        });
                    }
                }
                if (d_action.disabled.type == "formula") {
                    d_action._disabled = this.cacheFormula(d_action, "disabled", this.params);
                    this.$forceUpdate();
                    this.actionList = JSON.parse(JSON.stringify(this.colItem)).actions;
                }
            }
        },
        cacheFormula(action, key, param) {
            currentRow = param;
            if (this.context && this.context.queryBus && this.context.queryBus.datasource) {
                let formula_key = action[key]["formula-key"];
                if (this.context[this.context.queryBus.datasource]) {
                    if (!this.context[this.context.queryBus.datasource][action.name]) {
                        this.context[this.context.queryBus.datasource][action.name] = this.getActionResolveFormula(formula_key);
                    }
                } else {
                    this.context[this.context.queryBus.datasource] = {[action.name]: this.getActionResolveFormula(formula_key)};
                }
                return this.getFormulaResult(this.context[this.context.queryBus.datasource][action.name]);
            } else {
                //TODO
            }
        },
        getActionResolveFormula(formula_key) {
            let resolveFornulas = this.context.queryBus.config.formulas;
            let expression = "";
            if (resolveFornulas) {
                expression = this.resoleFormula(resolveFornulas[formula_key]);
            }
            return expression;
        },
        resoleFormula(formula) {
            let context = this,
                formulaCache = "",
                dynamicNodeFactory = "",
                expression;
            var QueryKeyNode = function (key) {
                this.assignData = function () {};
                this.getResult = function () {
                    return new AbstractData(currentRow.row[key], 10, false);
                };
                this.getResultType = function () {
                    return 10;
                };
                this.getShowString = function () {
                    return "test";
                };
                this.isPrevLinkChange = function () {
                    return false;
                };
            };
            var QueryKeyNode_T = function (fname) {
                expression = fname;
            };
            eval(formula);
            return expression;
        },
        getFormulaResult(expression) {
            if (expression) {
                let result = expression.getResult();
                if (result) {
                    return result.Value == "1" ? true : false;
                }
            }
            return false;
        },
        getActionTitle(title, index) {
            if (this.buttomParam) {
                return this.buttomParam[index];
            } else {
                return title;
            }
        },
        /**
         * 获取要做Promise数据
         * @actions 需要自定义判定是否展示的按钮
         * @param 指定行数据
         */
        getActionPromises(actions, param) {
            let result = [],
                currentNameObject = {
                    queryName: this.queryBus.config.name,
                    tabName: this.queryBus.currGroup.name
                };
            for (let i = 0, len = actions.length; i < len; i++) {
                param["actionParam"] = actions[i].param;
                param._key = actions[i].name;
                result.push(
                    queryUtil.actionDisplay(
                        this,
                        null,
                        actions[i].visible.function,
                        param,
                        null,
                        currentNameObject
                    )
                );
            }
            return result;
        },
        /**
         * 获取要做Enable判断的数据
         * @actions 需要自定义判定是否展示的按钮
         * @param 指定行数据
         */
        getActionEnables(actions, param) {
            let result = [];
            for (let i = 0, len = actions.length; i < len; i++) {
                if (!actions[i].hasOwnProperty("enable")) {
                    param._key = actions[i].name;
                    result.push(
                        queryUtil.actionEnable(
                            this,
                            null,
                            actions[i].disabled.function,
                            param
                        )
                    );
                }
            }
            return result;
        },
        onClick(action, index) {
            queryUtil.executeAction(
                this,
                [this.params.row],
                action.action,
                action.param,
                action.opporunity,
                this.extraActionObject,
                this.context,
                this.queryBus&&this.queryBus.currentDataQueryConditionSet
            );
        }
    }
};
</script>
<style lang="less">
.actions-column {
    // float: left;
    width: 100%;
    height: 100%;
    overflow: hidden;
    text-align: center;
    .action {
        display: inline-block;
        // float: left;
        margin-right: 5px;
    }
}
</style>