<template>
    <EditPage title="编辑主页模块" :roles="roles" @handleSubmit="handleSubmit" @refresh-roles="init">
      <template v-slot:body="{ data }">
        <div class="pa-3">
          <feature-module ref="module" :is-edit="true" :roles="data" :default-list="list" @delete="handleDelete"/>
        </div>
      </template>
    </EditPage>
</template>

<script>
import EditPage from '@/components/Common/editPage'
import FeatureModule from './components/featureModule.vue'
import { getWorkbenchList, saveWorkbenchAll, deleteWorkbench } from '@/api/menu'
// import { mapGetters } from 'vuex'
export default {
  name: 'edit-page',
  components: { EditPage, FeatureModule },
  data () {
    return {
      roles: [],
      list: [
        // { name: 'message', enName: 'message', label: '消息列表' },
        // // { name: 'messageRead', enName: 'messageRead', label: '未读消息' },
        // { name: 'mapPackage', enName: 'Regional Statistics', label: '区域统计' },
        // { name: 'navigation', enName: 'Navigation', label: '导航' },
        // { name: 'intermediateBusiness', enName: 'Intermediate Business', label: '中间业务' },
        // { name: 'interbankSituation', enName: 'Interbank Situation', label: '金融同业情况' },
        // { name: 'salesCategory', enName: 'Sales Category', label: '销售类占比' },
        // { name: 'deviceTypeTraffic', enName: 'Device Type Service Volume', label: '设备类型业务量' },
        // { name: 'businessTypeTraffic', enName: 'Service Type Service Volume', label: '业务种类业务量' },
        // { name: 'isLabourTraffic', enName: 'Whether By Labor Service Volume', label: '是否人工业务量' }
      ]
    }
  },
  created () {
    this.init()
  },
  methods: {
    async init () {
      // 定义默认系统组件，公用模块
      // const list = this.list
      try {
        const { data } = await getWorkbenchList()
        console.log(data)
        // const content = data.map(e => {
        //   Object.assign(e.entity, { efficacy: e.efficacy })
        //   return e.entity
        // })
        // // 补充缺省值 默认模块
        // const defaultList = list.filter(e => !content.find(ele => e.name === ele.name)).map(e => {
        //   return {
        //     active: '0',
        //     enName: e.enName,
        //     label: e.label,
        //     meta: null,
        //     name: e.name
        //   }
        // })
        // if (defaultList.length) {
        //   const { data } = await saveWorkbenchAll({ userMenus: defaultList, sourceUserMenuIds: 0 })
        //   this.roles = [...data.userMenus, ...content]
        //   return
        // }
        // this.roles = content
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    async handleDelete (id) {
      try {
        await deleteWorkbench({ id })
        this.$snackbar.success('删除成功')
        this.init()
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    // 确定保存
    async handleSubmit (item) {
      // console.log(item)
      const val = this.$refs.module.getValue()
      const query = {
        userMenus: val,
        sourceUserMenuIds: val.map(e => e.id)
      }
      try {
        await saveWorkbenchAll(query)
        this.$snackbar.success('保存成功')
        this.$router.push('/home')
      } catch (error) {
        this.$snackbar.error(error)
      }
    }
  }
}
</script>

<style>

</style>
