<!--
 * @Author: ChenChanghua
 * @Date: 2023-03-08 14:52:17
 * @Description: file content
-->
<script setup>
import { ref } from "vue";

const props = defineProps({
  data: Object,
  onAction: Object,
  query: Object,
  dataLength: Number,
});

const focused = ref(false);

const width = ref(props.data.width);
const duration = 200;
const transitionStr = `left ${duration / 1000}s, top ${
  duration / 1000
}s, width ${duration / 1000}s, height ${duration / 1000}s`;
const transition = ref(transitionStr);

const animInfo = {
  anchor: 0.5,
  duration: duration,
  onEnd: () => {
    console.log("resize end", props.query.index);
  },
};

const onFocus = () => {
  focused.value = true;
  if (width.value == props.data.width) {
    width.value = props.data.width + 200;
    props.query.updateItemSize(
      props.query.index,
      {
        width: width.value,
        height: props.data.height,
      },
      animInfo
    );
  }
};

const onBlur = () => {
  focused.value = false;
  if (width.value !== props.data.width) {
    width.value = props.data.width;
    props.query.updateItemSize(
      props.query.index,
      {
        width: width.value,
        height: props.data.height,
      },
      animInfo
    );
  }
};

const onClick = () => {
  console.log("onClick");
  if (width.value == props.data.width) {
    width.value = props.data.width + 200;
    props.query.updateItemSize(
      props.query.index,
      {
        width: width.value,
        height: props.data.height,
      },
      animInfo
    );
  } else {
    width.value = props.data.width;
    props.query.updateItemSize(
      props.query.index,
      {
        width: width.value,
        height: props.data.height,
      },
      animInfo
    );
  }
};

props.onAction.register("onFocus", onFocus);
props.onAction.register("onBlur", onBlur);
props.onAction.register("onClick", onClick);
</script>

<template>
  <div
    :style="{
      width: width,
      height: data.height,
      fontSize: 30,
      color: focused ? '#FF0000' : '#FFFFFF',
      backgroundColor: data.color,
      transition: transition,
      borderRadius: 10,
    }"
  >
    {{ data.content }}
  </div>
</template>
