<template>
    <div class="kf-3dc-atom kf-circle w-100 h-100" :style="cardStyle" v-show="props.show" :class="'vivify '+ buttonClass" @click="doClick">
    </div>
</template>

<script>
import {
    ref,
    watch,
    onMounted,
    getCurrentInstance,
    computed,
    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: "kfCircle",
    props: {
        props: {
            type: Object,
            default: () => {},
        },
        eventScript: {
            type: String,
        },
    },
    widgetConfig: {
        rank: 1,
        label: {
            zh: "圆形",
        },
        category: "基础",
        component: "kfCircle",
        display: true,
        version: 1,
        icon: "icon-anniu",
        defaultSize: {
            x: 100,
            y: 100,
        },
        imgName: "kfCircle",
    },
    setup(props) {
        let bindDevice = null,
            bindModel = null;
        let cardStyle = ref({});
        const { proxy } = getCurrentInstance();
        let defaultHandler = ref({});
        let buttonClass = ref("");

        let originValue = ref(false);

        onMounted(() => {
            refreshEvents();
            cardStyle.value = styleManager.updateStyle(proxy);
            buttonClass.value = styleManager.updateClass(proxy);
        });
        const refreshEvents = function () {
            eventManager.registerEvent(proxy);
        };
        pageModel.watchProps(props, watch, proxy, (changes) => {
            refreshEvents();
            if (styleManager.styleChanged("kfCircle", changes)) {
                cardStyle.value = styleManager.updateStyle(proxy);
            }
            if (styleManager.classChanged("kfCircle", changes)) {
                buttonClass.value = styleManager.updateClass(proxy);
            }
            if (changes && changes["deviceId"]) {
                // 组件绑定的数据ID变化，需要请求新的设备数据和对应的物模型
                bindDevice = dataModel.getDataSourceByDeviceId(
                    props.props.deviceId
                );
                if (bindDevice) {
                    bindModel = dataModel.getDeviceModelByDevice(bindDevice);
                }
            }
        });

        let dataVersion = ref(0);

        const updateData = function () {
            dataVersion.value++;
        };

        const bindData = computed(() => {
            let value = "";
            if (dataVersion.value > 0) {
                let newData = dataModel.getData(props.props, false, true);
                if (newData !== undefined) {
                    originValue.value = newData;
                    value = newData;
                }
            }
            return value;
        });

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

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

        const doClick = function (event) {
            try {
                if (props.props.deviceId && props.props.deviceId != "") {
                    if (!bindModel) {
                        updateDeviceAndModel(props.props.deviceId);
                    }
                    let enumLabel = dataModel.getEnumLabelForValue(
                        props,
                        bindData.value,
                        bindModel
                    );
                    if (enumLabel) {
                        dataModel.doControl(
                            props,
                            bindDevice,
                            bindModel,
                            enumLabel
                        );
                    }
                }
                if (eventHandler.value?.click) {
                    eventHandler.value.click(event);
                }
            } catch (e) {
                console.error(e);
            }
        };
        /* watch(props,(newValue,oldValue)=>{
            refreshEvents();
            cardStyle.value = styleManager.updateStyle(proxy)
            buttonClass.value=styleManager.updateClass(proxy)
        });*/
        const eventHandler = computed(() => {
            return eventManager.loadEventHandler(
                defaultHandler,
                undefined,
                props.props.pageName
            );
        });
        return {
            cardStyle,
            eventHandler,
            value: 30,
            buttonClass,
            doClick,
            updateData,
        };
    },
};
</script>

<style lang="scss">
.kf-circle {
    border-radius: 50%;
}
/*.square{
    background: #669988;
}
.circle{
    background: #9a6e3a;
    border-radius: 50%;
}*/
</style>