<template>
  <div class="white pa-3">
    <table-list
      :headers="headers"
      :items="items"
      :total="total"
      :loading="loading"
      :page-info="pageInfo"
      item-key="id"
      :is-tools="false"
      :show-select="false"
      @pageHandleChange="pageHandleChange"
      @sort="handleSort"
    >
      <template #navBtn>
        <v-btn
          color="primary"
          class="buttons"
          rounded
          elevation="5"
          v-permission="['systemManage:roleManage:create']"
          @click="handleAdd"
        >
          <v-icon left>mdi-plus</v-icon>
          新增
        </v-btn>
      </template>
      <template #actions="{ item }">
        <v-btn text color="primary" v-permission="['systemManage:roleManage:edit']" @click="handleEdit(item)">编辑</v-btn>
        <v-btn text color="primary" v-permission="['systemManage:roleManage:data']" @click="handleData(item)">数据权限</v-btn>
        <v-btn text color="error" v-permission="['systemManage:roleManage:delete']" @click="handleDelete(item.id)">删除</v-btn>
      </template>
      <template v-slot:createTime="{ item }">
        {{ dateFormat(item.createTime) }}
      </template>
    </table-list>
    <edit-dialog :title="isEdit ? '编辑角色' : '添加角色'" :visible.sync="show" @submit="handleSave">
      <user-edit v-if="show" ref="edit" :is-edit="isEdit" :item-data="itemData" :roles="roles" />
    </edit-dialog>
    <edit-dialog :title="`${itemDataPermission.roleName} 数据权限`" :visible.sync="showMenu" @submit="handleSaveMenu">
      <data-permission v-if="showMenu" ref="dataPermission" :item-data="itemDataPermission" />
    </edit-dialog>
  </div>
</template>

<script>
import TableList from '@/components/List/table.vue'
import EditDialog from '@/components/Dialog'
import {
  getRoleList,
  deleteRole,
  getRoleDetails,
  saveRole,
  getMenuTree,
  saveDataPermission
} from '@/api/menu'
import { dateFormat } from '@/utils/date'
import UserEdit from './components/userEdit'
import DataPermission from './components/DataPermission'
export default {
  name: 'role-manage-role',
  components: { TableList, UserEdit, EditDialog, DataPermission },
  data () {
    return {
      roles: [],
      isEdit: false,
      itemData: {},
      itemDataPermission: {},
      show: false,
      showMenu: false,
      headers: [
        { text: '角色', align: 'start', value: 'roleName' },
        { text: '创建时间', align: 'start', value: 'createTime', useSlot: true },
        { text: '操作', align: 'start', value: 'actions' }
      ],
      items: [],
      total: 0,
      pageInfo: {
        size: 10,
        current: 1
      },
      orders: [],
      loading: true
    }
  },
  created () {
    this.getMenu()
  },
  methods: {
    async getMenu () {
      try {
        const { data } = await getMenuTree({})
        this.roles = data.routes
        this.initPage()
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    async initPage () {
      this.loading = false
      try {
        const { data } = await getRoleList({ ...this.pageInfo, orders: this.orders })
        this.items = data.records
        this.total = data.total
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    async handleEdit (item) {
      try {
        const { data } = await getRoleDetails({ roleId: item.id })
        this.itemData = data
        this.isEdit = true
        this.show = true
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    async handleAdd () {
      this.isEdit = false
      this.show = true
    },
    handleData (item) {
      this.itemDataPermission = item
      this.showMenu = true
    },
    handleDelete (ids) {
      // console.log(ids)
      // if (Array.isArray(ids)) return
      if (Array.isArray(ids) && ids.length === 0) return this.$snackbar.warning('请选择删除的选项')
      // console.log(ids)
      // if (ids) return
      this.$confirm('提示', '是否确定删除该选项').then(async () => {
        try {
          await deleteRole({ id: ids })
          this.$snackbar.success('删除成功')
          this.initPage()
        } catch (error) {
          this.$snackbar.error(error)
        }
      })
    },
    async handleSave () {
      const obj = this.$refs.edit.getValue()
      if (this.isEdit) obj.id = this.itemData.id
      try {
        await saveRole(obj)
        this.$snackbar.success(this.isEdit ? '编辑成功' : '添加成功')
        this.show = false
        this.initPage()
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    async handleSaveMenu () {
      const obj = this.$refs.dataPermission.getValue()
      if (!obj) {
        return
      }
      try {
        await saveDataPermission(obj)
        this.$snackbar.success('保存成功')
        this.showMenu = false
        this.initPage()
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    pageHandleChange (page) {
      this.pageInfo.current = page
      this.initPage()
    },
    dateFormat (str) {
      const date = new Date(+str * 1000)
      return dateFormat('YYYY-mm-dd HH:MM:SS', date)
    },
    handleSort (val) {
      this.orders = val
      this.initPage()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
