<template>
  <el-dialog  title="提示" :model-value="modelValue" @closed="closed" width="22%">
    <div class="content-wrap">
      <p class="title">主题色更换</p>
      <el-color-picker v-model="mColor" :predefine="predefineColors"></el-color-picker>
    </div>
    <template #footer>
      <el-button type="primary" @click="clearTheme()">重置</el-button>
      <el-button @click="closed">取消</el-button>
      <el-button type="primary" @click="comfirm">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { useStore } from 'vuex'
import { MAIN_COLOR } from '@/constant';
import { defineProps, defineEmits, ref } from 'vue'
import { generateNewStyle, writeNewStyle } from '@/utils/theme';
import { removeItem } from '@/utils/storage';
import { useRouter } from 'vue-router';
const store = useStore();
defineProps({
  modelValue: {
    type: Boolean,
    required: true
  }
})
// 预定义色值
const predefineColors = [
  '#409EFF',
  '#ff4500',
  '#ff8c00',
  '#0800FF',
  '#1e90ff',
  '#c71585',
  'rgba(255, 69, 0, 0.68)',
  '#C0610D',
  'hsl(181, 100%, 37%)',
  '#216D6E',
  'hsla(209, 100%, 56%, 0.73)',
  '#c7158577'
]
//默认色值
const mColor = ref(store.getters.mainColor)
const emits = defineEmits(['update:modelValue']);

const closed = () => {
  emits('update:modelValue', false)
}

const comfirm = async () => {
  const newStyle = await generateNewStyle(mColor.value);
  writeNewStyle(newStyle)
  store.commit('theme/setMainColor', mColor.value)
  closed();
  router.go(0)
}
const router = useRouter();

const clearTheme = () => {
  removeItem(MAIN_COLOR);
  closed();
  router.go(0)
}

</script>

<style lang="scss" scoped>
.content-wrap {
  text-align: center;
  .title {
    margin-bottom: 12px;
  }
}
</style>
