<!-- 选择角色 -->
<script setup lang="ts" name="SelectRoleDialog">
import { computed, getCurrentInstance, onMounted, reactive, ref, watch } from 'vue'
import type { IroleInfo } from './dialog'
import { getRoleList } from '@/api/system/role'
import { toTreeList } from '@/utils/structure'
import SearchArea from '@/components/SearchArea/index.vue'
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  data: {
    type: Array,
    default: () => [],
  },
  // 是否允许多选
  isMulti: {
    type: Boolean,
    default: true,
  },
})

const emits = defineEmits(['update:visible', 'change'])
const table = ref()// 表格ref
// 数据权限类型字典
const dataScopeTypeDict = {
  1: '所有数据',
  2: '所属及下属部门',
  3: '本部门',
  4: '自定义',
}
// 表格表头
const columns = ref([
  { text: '角色名称', value: 'name', align: 'left' },
  { text: '所在组织机构', value: 'deptName' },
  // { text: '数据权限类型', value: 'dataScopeTypeName' },
  // { text: '别名', value: 'tips' },
  // { text: '排序', value: 'num', width: 80 },
])
const loading = ref(false)

// 默认查询条件
const defaultQuery = {
  keyword: '',
}
// 角色列表数据
const roleList = ref([]) as any

// 查询条件
const listQuery = reactive({ ...defaultQuery })
const checkedRoleList = ref([])
// 多选的数据
const multipleTable = ref([])
const closeDialog = () => {
  emits('update:visible', false)
}
const visibleDialog = computed({
  get() {
    return props.visible
  },
  set(val) {
    closeDialog()
  },
})
watch(() => props.visible, (val) => {
  if (!val) {
    table.value!.clearSelection()
  }
})
const saveDialog = () => {
  const checkedList = multipleTable.value.map((item: any) => ({
    ...item,
    type: 2, // type是啥
    // targetId: item.roleId,
    // name: item.roleName,
    targetId: item.id,
  }))
  emits('change', checkedList)
}

// 角色查询数据
function fetchRoleData() {
  // loading.value = true
  getRoleList(listQuery).then((res) => {
    const data = res.data.map((item: any) => {
      return {
        ...item,
        dataScopeTypeName: dataScopeTypeDict[parseInt(item.dataScopeType)],
      }
    })
    const treeData = toTreeList(data)
    roleList.value = treeData

    // loading.value = false
    console.log('角色树-----')
    console.log(roleList.value[0])
  })
}
// 多选
const handleSelectionChange = (val: any) => {
  console.log('多选角色')
  multipleTable.value = val
}
// 搜索
function search() {
  fetchRoleData()
}
// 多选框单选功能
const selectClick = (selection: any, row: any) => {
  if (!props.isMulti) {
    console.log('selectClick')
    if (selection.length > 1) {
      const del_row = selection.shift()
      table.value?.toggleRowSelection(del_row, false) // 用于多选表格，切换某一行的选中状态，如果使用了第二个参数，则是设置这一行选中与否（selected 为 true 则选中）
    }
  }
}
onMounted(() => {
  search()
})
</script>

<template>
  <div class="select-role-dialog">
    <el-dialog v-model="visibleDialog" title="选择角色" max-width="80%" append-to-body custom-class="promoter_person">
      <div class="person_body clear">
        <search-area @search="search">
          <search-item>
            <el-input v-model="listQuery.keyword" placeholder="角色名称" />
          </search-item>
        </search-area>
        <el-table
          ref="table"
          v-loading="loading"
          show-overflow-tooltip
          :data="roleList"
          row-key="id"
          :border="false"
          style="width: 100%;"
          :tree-props="{ children: 'children' }"
          @selection-change="handleSelectionChange"
          @select="selectClick"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column
            v-for="column of columns" :key="column.value" :label="column.text" :prop="column.value" :width="column.width" :align="column.align"
          />
        </el-table>
      </div>
      <template #footer>
        <el-button @click="closeDialog">
          取 消
        </el-button>
        <el-button type="primary" @click="saveDialog">
          确 定
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss">
@import "@/css/dialog.css";

.select-role-dialog {
  .el-table .cell {
    white-space: nowrap;
  }
}
</style>
