<template>
  <el-row>
    <el-dialog title="选择角色" v-model="visible" width="70%" top="5vh" append-to-body>
      <el-form :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item label="角色名称" prop="role_name">
          <el-input v-model="queryParams.role_name" placeholder="请输入用户名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="权限字符" prop="role_code">
          <el-input v-model="queryParams.role_code" placeholder="请输入权限字符" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row>
        <el-table v-loading="loading" @row-click="clickRow" ref="tableRef" :data="userList" @selection-change="handleSelectionChange" height="260px">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column label="角色名称" prop="role_name" :show-overflow-tooltip="true"  />
          <el-table-column label="权限字符" prop="role_code" :show-overflow-tooltip="true"  />
          <el-table-column label="描述" prop="description" :show-overflow-tooltip="true"/>  
          <el-table-column label="创建时间" align="center" prop="created_at" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.created_at) }}</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-if="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.page_size" @pagination="getList" />
      </el-row>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSelectUser">确 定</el-button>
          <el-button @click="visible = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </el-row>
</template>

<script setup name="SelectUser">
import { authUserSelectAll, unallocatedUserList } from "@/api/role.js";

const props = defineProps({
    userId: {
        type: [Number, String]
    }
})

const { proxy } = getCurrentInstance();
const sys_normal_disable = ref([
    {
      label: '启用',
      value: '1'
    },
    {
      label: '停用',
      value: '0'
    }
])

const userList = ref([]);
const visible = ref(false);
const total = ref(0);
const roleIds = ref([]);
const loading = ref(false);

const queryParams = reactive({
  page: 1,
  page_size: 10, 
  userId:'',
  role_name: undefined,
  role_code: undefined
})

const tableRef = ref();
const queryFormRef = ref();

const show = () => {
  queryParams.userId = props.userId;
  getList();
  visible.value = true;
}

/**
 * 选择行
 */
const clickRow = (row) => { 
  tableRef.value?.toggleRowSelection(row, false);
}
/** 多选框选中数据 */
const handleSelectionChange = (selection) => {
    roleIds.value = selection.map((item) => item.uid); 
}

/** 查询数据 */
const getList = async () => {
  loading.value = true;
  const res = await unallocatedUserList(queryParams);
  userList.value = res.data.roles;
  total.value = res.data.pagination.total;
  loading.value = false;
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.page = 1;
  getList();
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  getList();
}

const emit = defineEmits(["ok"]);
/**选择授权用户操作 */
const handleSelectUser = async () => {
  const userId = queryParams.userId;
  const ids = roleIds.value; 
  if (ids.length == 0) {
    ElMessage.error('请选择要分配的角色'); 
    return;
  }
  await authUserSelectAll({ userId, role_uids: ids });
  ElMessage.success('分配成功');
  emit('ok');
  visible.value = false;
}
// 暴露
defineExpose({
  show,
});
</script>

<style scoped></style>
