<template>
    <div class="kf-3dc-atom w-100 h-100" :class="'vivify '+ buttonClass" :style="buttonStyle" ref="buttonBox" v-show="props.show"
         @click="doClick" @dblclick="eventHandler.dbclick"
         @contextmenu.prevent="eventHandler.contextmenu">
        {{ buttonContent }}
    </div>
</template>

<script>
import {
    onMounted,
    ref,
    getCurrentInstance,
    computed,
    watch,
    nextTick,
} from "vue";
import styleManager from "../utils/style.vue";
import eventManager from "../utils/event";
import pageModel from "../model/page";
import dataModel from "@/components/ui/model/data";
import stateManager from "@/components/ui/utils/state";

export default {
    name: "kfButton",
    props: {
        props: {
            type: Object,
            default: () => {},
        },
        eventList: {
            type: Array,
            default: () => [],
        },
        eventScript: {
            type: String,
        },
    },
    widgetConfig: {
        parent: "base",
        rank: 1,
        label: {
            zh: "按钮",
        },
        category: "基础",
        component: "kfButton",
        display: true,
        version: 1,
        icon: "icon-anniu",
        defaultSize: {
            x: 80,
            y: 29,
        },
        imgName: "kfButton",
    },
    setup(props) {
        let bindDevice = null;
        let bindModel = null;
        let buttonBox = ref(null);
        const { proxy } = getCurrentInstance();

        let buttonStyle = computed(() => {
            return styleManager.computedStyle(proxy);
        });

        let buttonClass = computed(() => {
            return styleManager.computedClass(proxy);
        });

        let dataVersion = ref(0);
        const updateData = function () {
            dataVersion.value = new Date().getTime();
        };

        let buttonText = ref("");
        let originValue = ref(false);

        let defaultHandler = ref({});
        onMounted(() => {
            refreshEvents();
        });
        const refreshEvents = function (firstTime = false) {
            eventManager.registerEvent(proxy, () => {
                /*if (firstTime && props.props.defaultSelect) {
                    defaultHandler.value.selected();
                }*/
            });
        };
        // button 绑定数据更新
        const buttonContent = computed(() => {
            buttonText.value = props.props.label;
            if (dataVersion.value > 0) {
                const newData = dataModel.getData(props.props, false, true);
                if (newData !== undefined) {
                    originValue.value = newData;
                }
                if (
                    props.props.hasOwnProperty("contentShow") &&
                    props.props.contentShow
                ) {
                    if (newData !== undefined) {
                        buttonText.value = props.props.label;
                    }
                } else {
                    if (newData !== undefined) {
                        const changeValue = dataModel.getData(
                            props.props,
                            false,
                            false
                        );
                        buttonText.value = changeValue;
                    }
                }
            }
            return buttonText.value;
        });

        const doControl = () => {
            if (props.props.deviceId && props.props.deviceId != "") {
                if (!bindModel) {
                    updateDeviceAndModel(props.props.deviceId);
                }
                let enumLabel = dataModel.getEnumLabelForValue(
                    props,
                    originValue.value,
                    bindModel
                );
                if (enumLabel) {
                    dataModel.doControl(
                        props,
                        bindDevice,
                        bindModel,
                        enumLabel
                    );
                }
            }
        };

        const updateDeviceAndModel = function (deviceId) {
            if (!deviceId) return;
            bindDevice = dataModel.getDataSourceByDeviceId(deviceId);
            if (bindDevice) {
                bindModel = dataModel.getDeviceModelByDevice(bindDevice);
            }
        };

        watch(originValue, (newValue, oldValue) => {
            nextTick(() => {
                stateManager.onDataChange(props, newValue);
            });
        });

        pageModel.watchProps(props, watch, proxy, (changes) => {
            if (changes && changes["deviceId"]) {
                // 组件绑定的数据ID变化，需要请求新的设备数据和对应的物模型
                bindDevice = dataModel.getDataSourceByDeviceId(
                    props.props.deviceId
                );
                if (bindDevice) {
                    bindModel = dataModel.getDeviceModelByDevice(bindDevice);
                }
            }
        });

        const doClick = function (event) {
            try {
                eventHandler.value.click(event);
                doControl(event);
            } catch (e) {
                console.error(e);
            }
        };
        const eventHandler = computed(() => {
            return eventManager.loadEventHandler(
                defaultHandler,
                undefined,
                props.props.pageName
            );
        });

        return {
            buttonBox,
            buttonStyle,
            doClick,
            defaultHandler,
            eventHandler,
            buttonContent,
            buttonClass,
            updateData,
            buttonText,
        };
    },
};
</script>

<style lang="scss">
.kf-3dc-atom {
    border-width: 0;
    border-style: solid;
    transition: all 0.2s linear 0s;
    .el-button {
        width: 100%;
        height: 100%;
        background: inherit;
        border: none;
        display: flex;
        justify-content: inherit;
        align-items: center;
        color: inherit;
        font-size: inherit;
        border-radius: inherit;
        font-family: inherit;
        font-weight: inherit;
        padding: 0;
    }
}
</style>