<script setup lang="ts">
import { ref, reactive } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
import { postAddUserGroup } from "@/api/userGroup";
import { postAssignmentUser } from "@/api/userSetting";
import { postPageRole } from "@/api/roles";
const dialogVisibleAdd = ref(false);
const formRef = ref();
const emit = defineEmits(["handleClick"]);
const dialogTitle = ref("");
const dialogForm = ref(false);
const form = reactive({
  groupName: "",
  hospitalCode: "",
  leaderCode: ""
});
const rolesList = reactive({
  list: [],
  rolesName: [],
  assignmentParams: {
    sourceCodes: [],
    targetCodes: [],
    linkAction: "link",
    correlatedModel: "groupRole"
  }
});
const postAddUserGroupApi = async () => {
  rolesList.assignmentParams.targetCodes = [
    ...new Set(rolesList.assignmentParams.targetCodes)
  ];
  const { code, msg } =
    dialogTitle.value == "新建用户组"
      ? await postAddUserGroup(form)
      : await postAssignmentUser(rolesList.assignmentParams);
  if (code === 200 && dialogTitle.value == "新建用户组") {
    emit("handleClick");
    dialogVisibleAdd.value = false;
    ElMessage({
      message: "添加成功",
      type: "success"
    });
  } else if (code == 200) {
    emit("handleClick");
    dialogVisibleAdd.value = false;
    ElMessage({
      message: "添加成功",
      type: "success"
    });
  } else {
    // ElMessage.error(msg);
  }
  rolesList.assignmentParams.targetCodes = [];
};
const handleClose = () => {
  dialogVisibleAdd.value = false;
  // formRef.value.clearValidate(); // 清除验证错误
  // formRef.value.resetFields(); // 重置表单字段
};
// 角色数据

const rolesData = async () => {
  const { code, data } = await postPageRole({
    pageNumber: 1,
    pageSize: 100
  });
  if (code === 200) {
    rolesList.list = data.records;
  }
};
const changeRoles = () => {
  // rolesList.assignmentParams.targetCodes = [];
  rolesList.assignmentParams.targetCodes.push(...rolesList.rolesName);
  // console.log("123123",rolesList.rolesName)
  // rolesList.assignmentParams.sourceCodes = [];
  // rolesList.assignmentParams.sourceCodes.push(rolesList.rolesName);
};

// 添加部门保存
const saveDepartment = () => {
  // formRef.value.validate(valid => {
  //   if (valid) {
  //   }
  // });
  postAddUserGroupApi();
};
const open = item => {
  if (item) {
    dialogTitle.value = "配置角色";
    rolesList.assignmentParams.sourceCodes = [];
    rolesList.assignmentParams.sourceCodes.push(item.groupCode);
    // rolesList.assignmentParams.targetCodes = [];
    // rolesList.assignmentParams.targetCodes.push(item.groupCode);
    form.groupName = item.groupName;
    dialogForm.value = false;
  } else {
    Object.assign(form, {
      groupName: "",
      hospitalCode: "",
      leaderCode: ""
    });
    rolesList.rolesName = [];
    dialogTitle.value = "新建用户组";
    form.groupName = "";
    dialogForm.value = true;
  }
  dialogVisibleAdd.value = true;
  rolesData();
};
defineExpose({
  open
});
</script>

<template>
  <div>
    <el-dialog
      v-model="dialogVisibleAdd"
      :title="dialogTitle"
      width="500"
      :before-close="handleClose"
    >
      <el-form
        v-if="dialogForm"
        ref="formRef"
        :model="form"
        label-width="auto"
        style="max-width: 600px"
      >
        <el-form-item
          prop="groupName"
          label="用户组名称"
          :rules="[
            {
              required: true,
              message: '请填写用户组名称',
              trigger: 'blur'
            }
          ]"
        >
          <el-input v-model="form.groupName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="用户组编号">
          <el-input v-model="form.hospitalCode" placeholder="请输入" />
        </el-form-item>
        <el-form-item v-if="dialogTitle !== '新建用户组'" label="用户组负责人">
          <el-select v-model="form.leaderCode" placeholder="请选择">
            <el-option
              v-for="(item, index) in rolesList.list"
              :key="index"
              :label="item.roleName"
              :value="item.roleCode"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <el-form v-else :model="form" label-width="auto" style="max-width: 600px">
        <el-form-item label="配置角色">
          <el-select
            v-model="rolesList.rolesName"
            placeholder="请选择"
            multiple
            @change="changeRoles"
          >
            <el-option
              v-for="(item, index) in rolesList.list"
              :key="index"
              :label="item.roleName"
              :value="item.roleCode"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="saveDepartment"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
