import request from '@/utils/request'

import { UPCLOUD_RDC_TMS_MAINDATA_SERVICE } from '@/config/serviceSettings.js'

//动态加载字体
function load_style(state, cssObj) {


  let head = document.getElementsByTagName('head')[0]
  state.sign && head.removeChild(state.sign)
  let style = document.createElement('style')
  style.type = 'text/css'
  style.id = 'sign-css'

  cssObj = cssObj.join('')
  style.innerText = cssObj
  state.sign = style
  document.getElementsByTagName('head')[0].appendChild(state.sign)
}

const app = {
  state: {
    sign: null
  },
  mutations: {
    setSign: (state, type) => {
      const { data } = type
      if (data?.length > 0) {
        let cssObj = []
        data.forEach(item => {
          const { id, color, background } = item
          cssObj.push(`.sign_${id} td,.p-table .active.sign_${id}>td{  position:relative;border-color: ${background} !important; color:${color} !important;background:${background} !important;} .sign_${id} *{ color:${color} !important;} .active.sign_${id}>td:after{content:'';left:0;top:0;bottom:0;right:0;position:absolute;
          background:rgba(0,0,0,0.05);}`)
        })
        load_style(state, cssObj)
      }
    },
    removeColumnPosition: (state, type) => {
      const { menuId } = type
      delete state[menuId]
    }
  },
  actions: {
    getSign({ commit }) {
      return request.post(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+'/signconfig/query', { pageNum: 1, pageSize: 9999 }).then(response => {
        if (response.success) {
          commit('setSign', { data: response.data.list })
          return response
        }
      });
    },

  }
}
export default app
