<template>
  <el-dialog v-model="dialogVisible" title="分工" :width="500" center align-center :close-on-click-modal="false" @closed="closed">
    <div class="tips">待分配条文数：{{ formData.articleIds?.length }}条</div>
    <el-form style="max-width: 1000px" ref="formRef" label-width="80px" :model="formData" :rules="formRules">
      <el-form-item label="标注员：" prop="annotationBy">
        <el-scrollbar class="scroll-box" height="200px">
          <el-radio-group v-model="formData.annotationBy" style="flex-direction: column; align-items: flex-start">
            <el-radio v-for="item in userList" :key="item.userId" :label="item.userId">
              <el-tooltip placement="top" effect="dark" :content="`角色：${item.roleNameStr}`">
                {{ item.nickName }}（{{ item.userName }}）
              </el-tooltip>
            </el-radio>
          </el-radio-group>
        </el-scrollbar>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" class="ml30" v-throttle="handleSubmit">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, defineExpose, nextTick } from "vue";
import { FormInstance, FormRules } from "element-plus";
import { AnnoDivision, User } from "@/api/interface";
import { getAllUserList } from "@/api/modules/common";
import { divideArticle } from "@/api/modules/annotation/division";

const userList = ref<User.ResUserList[]>([]);
// 校验规则
const formRules: FormRules = {
  annotationBy: [{ required: true, message: "请选择标注员", trigger: "blur" }]
};

// interface
type FormData = AnnoDivision.ReqDivideArticleParams;

// ref
const dialogVisible = ref(false);
const formRef = ref<FormInstance>();
const formData = ref<FormData>({} as FormData);
const openPromise = ref<any>({});

// 加载用户列表（仅显示启用用户）
async function loadUserList() {
  const res = await getAllUserList();
  userList.value = res.data.filter(item => item.status === "0");
}

loadUserList();

// 弹窗关闭
function closed() {
  formData.value = {} as FormData;
  nextTick(formRef.value?.clearValidate);
}
// 取消
function handleCancel() {
  dialogVisible.value = false;
}
//提交
function handleSubmit() {
  formRef.value?.validate(async valid => {
    if (!valid) return;
    await divideArticle(formData.value);
    dialogVisible.value = false;
    openPromise.value.resolve();
  });
}

function open(params: FormData) {
  formData.value = params;
  dialogVisible.value = true;
  return new Promise(resolve => {
    openPromise.value.resolve = resolve;
  });
}
defineExpose({
  open
});
</script>

<style lang="scss" scoped>
.tips {
  margin-bottom: 20px;
  font-size: 18px;
  font-weight: bold;
  color: #333333;
  text-align: center;
}
.scroll-box {
  width: 100%;
  padding: 0 10px;
  border: 1px solid var(--el-aside-border-color);
  border-radius: 4px;
}
</style>
