<script setup>
import RPC from '@/utils/rpc'
const props = defineProps({
  row: [Object],
  roleList: {
    type: Array,
    default: [],
  },
})

const emit = defineEmits(['closed', 'onSuccess'])

const dialogVisible = ref(false)

const isAdd = computed(() => props.type === 'add')

onMounted(() => {
  dialogVisible.value = true
})

const formRef = ref(null)
const formField = ref({})
const formRules = reactive({})

const loading = ref(false)

function closed() {
  emit('closed')
}

const submitForm = async () => {
  loading.value = true
  RPC.post('/assign_roles', { user_id: props.row.id, role_ids: checkList.value })
    .then((res) => {
      ElMessage.success('操作成功')
      emit('onSuccess')
      emit('closed')
    })
    .finally(() => {
      loading.value = false
    })
}
const checkList = ref(props.row?.roles?.map((item) => item.id) || [])
</script>

<template>
  <el-dialog
    align-center
    title="分配权限"
    width="80%"
    v-model="dialogVisible"
    @closed="closed"
    :close-on-click-modal="false"
  >
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :label="item.name" :value="item.id" />
    </el-checkbox-group>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closed"> 取 消 </el-button>
        <el-button type="primary" :loading="loading" @click="submitForm"> 确 定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped></style>
