import defaultSettings from '@/settings'
import { useDynamicTitle } from '@/utils/dynamicTitle'
import { getConfigKey } from '@/api/system/config'

const { sideTheme, showSettings, topNav, tagsView, fixedHeader, sidebarLogo, dynamicTitle } = defaultSettings

const storageSetting = JSON.parse(localStorage.getItem('layout-setting')) || ''

const useSettingsStore = defineStore(
  'settings',
  {
    state: () => ({
      title: '',
      theme: storageSetting.theme || '#409EFF',
      sideTheme: storageSetting.sideTheme || sideTheme,
      showSettings: showSettings,
      topNav: storageSetting.topNav === undefined ? topNav : storageSetting.topNav,
      tagsView: storageSetting.tagsView === undefined ? tagsView : storageSetting.tagsView,
      fixedHeader: storageSetting.fixedHeader === undefined ? fixedHeader : storageSetting.fixedHeader,
      sidebarLogo: storageSetting.sidebarLogo === undefined ? sidebarLogo : storageSetting.sidebarLogo,
      dynamicTitle: storageSetting.dynamicTitle === undefined ? dynamicTitle : storageSetting.dynamicTitle,
      genLotBySub: false,
      dataSource:false,
      showMessage: false,
      showNotification: false,
      closeNotification: false
    }),
    actions: {
      // 修改布局设置
      changeSetting(data) {
        const { key, value } = data
        if (this.hasOwnProperty(key)) {
          this[key] = value
        }
      },
      // 设置网页标题
      setTitle(title) {
        this.title = title
        useDynamicTitle();
      },
      setCloseNotification(value) {
        this.closeNotification = value
      },
      initGenLotBySub(config) {
        getConfigKey('business.production.genLotBySub').then(res => {
          // console.log(res)
          this.genLotBySub = res.msg == 'true'
        })
      },
      initPullSource(config) {
        getConfigKey('switch_data_source').then(res => {
          // console.log(res)
          this.dataSource = res.msg == 'true'
        })
      },
      initShowMessage(config) {
        getConfigKey('business.sys.message').then(res => {
          // console.log(res)
          this.showMessage = res.msg == 'true'
          console.log('是否显示站内信', this.showMessage)
        })
      },
      initShowNotification(config) {
        getConfigKey('business.sys.showAffiche').then(res => {
          // console.log(res)
          this.showNotification = res.msg == 'true'
          console.log('是否显示公告', this.showNotification)
        })
      }
    }
  })

export default useSettingsStore
