<template>
  <div class="m-switch-wrap">
    <div
      @click="disabled ? (e: any) => e.preventDefault() : onSwitch()"
      :class="['m-switch', { 'switch-checked': checked, disabled: disabled }]"
    >
      <div
        :class="[
          'u-switch-inner',
          checked ? 'inner-checked' : 'inner-unchecked',
        ]"
      >
        {{ checked ? checkedInfo : uncheckedInfo }}
      </div>
      <div :class="['u-node', { 'node-checked': checked }]">
        <themeLight v-show="!checked" class="icon"></themeLight>
        <themeDark v-show="checked" class="icon"></themeDark>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from '@vue/reactivity';
import { computed, watch } from '@vue/runtime-core';
import { useStore } from 'vuex';
import themeLight from '@/assets/svg/user/theme-switch-light.svg?component';
import themeDark from '@/assets/svg/user/theme-switch-dark.svg?component';
const props = defineProps({
  defaultChecked: {
    // 初始是否选中
    type: Boolean,
    default: false,
  },
  checkedInfo: {
    // 选中时的内容
    type: [Number, String],
    default: null,
  },
  uncheckedInfo: {
    // 未选中时的内容
    type: [Number, String],
    default: null,
  },
  disabled: {
    // 是否禁用
    type: Boolean,
    default: false,
  },
});
const store = useStore();
const theme = computed(() => store.getters['theme/getTheme']);
const checked = ref(false);

watch(
  () => theme.value,
  (v) => {
    if (v) {
      let html = document.children[0];
      html.setAttribute('theme-mode', v);
      if (v === 'dark') {
        checked.value = true;
      } else {
        checked.value = false;
      }
    } else {
      checked.value = false;
    }
  },
  {
    immediate: true,
  }
);
const onSwitch = () => {
  if (!checked.value) {
    store.commit('theme/setTheme', 'dark');
  } else {
    store.commit('theme/setTheme', 'light');
  }
  checked.value = !checked.value;
};
</script>

<style lang="less" scoped>
@import '@/style/flex.less';
@themeColor: #1890ff;
.m-switch-wrap {
  height: 22px;
  min-width: 54px;
  display: inline-block;
  .m-switch {
    position: relative;
    height: 24px;
    color: rgba(0, 0, 0, 0.65);
    font-size: 14px;
    background: transparent;
    border-radius: 12px;
    border: 1px solid #dddddd;
    border-radius: 12px;
    transition: background 0.36s;
    .u-switch-inner {
      display: inline-block;
      color: #fff;
      font-size: 14px;
      line-height: 22px;
      padding: 0 8px;
      transition: all 0.36s;
    }
    .inner-checked {
      margin-right: 18px;
    }
    .inner-unchecked {
      margin-left: 18px;
    }
    .u-node {
      position: absolute;
      top: 2px;
      left: 2px;
      width: 18px;
      height: 18px;
      background: #fff;
      border-radius: 100%;
      transition: all 0.36s;
      .dja();
      .icon {
        width: 80%;
        height: 80%;
      }
    }
    .node-checked {
      // 结果等价于right: 2px; 为了滑动效果都以左边为基准进行偏移
      left: 100%;
      margin-left: -2px;
      transform: translateX(-100%);
    }
  }
  .switch-checked {
    background: transparent;
  }
  .disabled {
    cursor: not-allowed;
    opacity: 0.4;
  }
}
</style>
