<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="角色名称" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入角色名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button :icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-show="checkType === 'multiple'" ref="dataTable"  v-loading="loading" :data="roleList.value" @selection-change="handleMultipleRoleSelect">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="角色编号" prop="roleId" width="120" />
      <el-table-column label="角色名称" prop="roleName" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="权限字符" prop="roleKey" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="显示顺序" prop="roleSort" width="100" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-table v-show="checkType === 'single'" v-loading="loading" :data="roleList.value" @current-change="handleSingleRoleSelect">
      <el-table-column  width="55" align="center" >
        <template #default="scope">
          <!-- 可以手动的修改label的值，从而控制选择哪一项 -->
          <el-radio v-model="radioSelected" :label="scope.row.roleId">{{''}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="角色编号" prop="roleId" width="120" />
      <el-table-column label="角色名称" prop="roleName" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="权限字符" prop="roleKey" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="显示顺序" prop="roleSort" width="100" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page-sizes="[5,10]"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script lang="ts" name="FlowRole" setup>
import { listRole, getRole, delRole, addRole, updateRole, dataScope, changeRoleStatus, deptTreeSelect } from "@/api/system/role";
import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
import {StrUtil} from "@/utils/StrUtil";
import { Search, Refresh, Plus, Edit, Delete, Download } from '@element-plus/icons-vue'
import type { FormInstance, FormRules, ElForm } from "element-plus";
import { ref, reactive, getCurrentInstance } from 'vue';

// 引入全局方法
const { proxy } = getCurrentInstance() as any;
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
// 接受父组件的值
const props = defineProps<{
  // 回显数据传值
  selectValues: {
    type: Number | String | Array,
    default: null,
    required: false
  },
  checkType: {
    type: String,
    default: 'multiple',
    required: false
  },
}>()

// 遮罩层
const loading = ref<boolean>(true);
// 选中数组
const ids = reactive({
	value: [],
});
// 非单个禁用
const single = ref<boolean>(true);
// 非多个禁用
const multiple = ref<boolean>(true);
// 显示搜索条件
const showSearch = ref<boolean>(true);
// 总条数
const total = ref<number>(0);
// 角色表格数据
const roleList = reactive({
	value: [],
});
// 弹出层标题
const title = ref<string>("");
// 是否显示弹出层
const open = ref<boolean>(false);
// 查询参数
const queryParams = reactive<any>({
  pageNum: 1,
  pageSize: 5,
  roleName: undefined,
  roleKey: undefined,
  status: undefined
});
// 表单参数
const form = reactive<any>({});
 // 单选框传值
const radioSelected = ref<null>(null);
// 回显数据传值
const selectRoleList = reactive({
  value: []
});

// watch: {
//   selectValues: {
//     handler(newVal) {
//       if (StrUtil.isNotBlank(newVal)) {
//         if (newVal instanceof Number || newVal instanceof String) {
//           this.radioSelected = newVal
//         } else {
//           this.selectRoleList = newVal;
//         }
//       }
//     },
//     immediate: true
//   },
//   roleList: {
//     handler(newVal) {
//       if (StrUtil.isNotBlank(newVal) && this.selectRoleList.length > 0) {
//         this.$nextTick(() => {
//           this.$refs.dataTable.clearSelection();
//           this.selectRoleList?.split(',').forEach(key => {
//             this.$refs.dataTable.toggleRowSelection(newVal.find(
//               item => key == item.roleId
//             ), true)
//           });
//         });
//       }
//     },
//     immediate: true, // 立即生效
//     deep: true  //监听对象或数组的时候，要用到深度监听
//   }
// }

/** 查询角色列表 */
const getList = () => {
  loading.value = true;
  listRole(queryParams).then((response: any) => {
      roleList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}
getList()

const emit = defineEmits(["handleRoleSelect", "handleRoleSelect"]);
// 多选框选中数据
const handleMultipleRoleSelect = (selection: any) => {
  const idList = selection.map((item: any) => item.roleId);
  const nameList = selection.map((item: any) => item.roleName);
  emit('handleRoleSelect', idList.join(','), nameList.join(','));
}

// 单选框选中数据
const handleSingleRoleSelect = (selection: any) => {
  radioSelected.value = selection.roleId;
  const roleName = selection.roleName;
  emit('handleRoleSelect', radioSelected.value.toString(), roleName);
}

/** 搜索按钮操作 */
const handleQuery = () => {
	queryParams.pageNum = 1;
	getList();
};
    /** 重置按钮操作 */
const resetQuery = () => {
  handleQuery();
}
</script>
<style>
/*隐藏radio展示的label及本身自带的样式*/
/*.el-radio__label{*/
/*  display:none;*/
/*}*/
</style>
