<template>
  <Dialog title="权限管理" v-model="dialogVisible" width="70%">
    <el-form
      ref="formRef"
      :model="formData"
      label-width="100px"
      v-loading="formLoading"
    >
<!--      <el-form-item label="角色">-->
<!--        <el-select v-model="formData.roleIds" multiple placeholder="请选择角色">-->
<!--          <el-option v-for="item in roleList" :key="item.id" :label="item.name" :value="item.id" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->

<!--      <el-form-item label="用户">-->
<!--        <el-select v-model="formData.userIds" multiple placeholder="请选择用户">-->
<!--          <el-option v-for="item in userOptions" :key="item.id" :label="item.nickname" :value="item.id" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->

<!--      <el-form-item label="只读角色">-->
<!--        <el-select v-model="formData.readRoleIds" multiple placeholder="请选择角色">-->
<!--          <el-option v-for="item in roleList" :key="item.id" :label="item.name" :value="item.id" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->

<!--      <el-form-item label="只读用户">-->
<!--        <el-select v-model="formData.readUserIds" multiple placeholder="请选择用户">-->
<!--          <el-option v-for="item in userOptions" :key="item.id" :label="item.nickname" :value="item.id" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->

    </el-form>
    <ContentWrap>
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="100px"
        >
          <el-form-item label="昵称" prop="username">
            <el-input
              v-model="queryParams.nickname"
              placeholder="请输入用户昵称"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="mobile">
            <el-input
              v-model="queryParams.mobile"
              placeholder="请输入手机号码"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>

          <el-form-item>
            <el-button @click="handleQuery"><Icon icon="ep:search" />搜索</el-button>
            <el-button @click="resetQuery"><Icon icon="ep:refresh" />重置</el-button>

          </el-form-item>
        </el-form>
      </ContentWrap>
    <ContentWrap>
        <el-table v-loading="loading" :data="list">
          <el-table-column label="用户编号" align="center" key="id" prop="id" />
          <el-table-column
            label="用户昵称"
            align="center"
            prop="nickname"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="部门"
            align="center"
            key="deptName"
            prop="deptName"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="手机号码" align="center" prop="mobile" width="120" />
          <el-table-column label="成员权限" key="status" min-width="200px">
            <template #default="scope">
              <el-select v-model="scope.row.onlyRead" laceholder="请选择成员权限"
                @change="handlePermissionChange(scope.row)"
                         :disabled="scope.row.isSelf"
              >
                <el-option label="管理权限" :value="1" />
                <el-option label="操作权限" :value="2" />
                <el-option label="只读权限" :value="3" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="160">
            <template #default="scope">
              <div class="flex items-center justify-center">
                <el-button
                  type="primary"
                  link
                  @click="deleteQuery(scope.row.id)"
                  :disabled="scope.row.isSelf"
                >
                  <Icon icon="ep:edit" />删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
        <el-form :inline="true" label-width="100px">
          <el-form-item label="添加用户">
            <el-select v-model="queryParams.addUserId" placeholder="请选择用户" style="width: 200px;" >
              <el-option v-for="item in userOptions" :key="item.id" :label="item.nickname" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="addQuery"><Icon icon="ep:plus" />添加</el-button>
          </el-form-item>
        </el-form>

    </ContentWrap>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DisbursementApi } from '@/api/bpm/disbursement'
import * as RoleApi from "@/api/system/role";
import * as UserApi from "@/api/system/user";
import  { ref }  from 'vue';

/** 资金拨付流程表	 表单 */
defineOptions({ name: 'AuthorityForm' })
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formRef = ref() // 表单 Ref
const formData = ref({
  addUserId : undefined,
  projectId : undefined,
  pageNo: 1,
  pageSize: 10,
  nickname: undefined,
  mobile: undefined,
  status: undefined,
  deptId: undefined,
  createTime: []
})
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数
const queryParams = reactive({
  addUserId : undefined,
  projectId : -1,
  pageNo: 1,
  pageSize: 10,
  nickname: undefined,
  mobile: undefined,
  status: undefined,
  deptId: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

const getList = async () => {
  loading.value = true
  try {
      const data = await DisbursementApi.getTeamMember(queryParams)
      console.log(data)
      list.value = data.list
      total.value = data.total
  } finally {
    loading.value = false
  }
}

const roleList = ref([] as RoleApi.RoleVO[]) // 角色的列表
const userOptions = ref<UserApi.UserVO[]>([]) // 用户列表
/** 打开弹窗 */
const open = async ( id : number) => {
    queryParams.projectId = id
    getList();
    dialogVisible.value = true
    resetForm()
    // 修改时，设置数据
    roleList.value = await RoleApi.getSimpleRoleList()
    userOptions.value = await UserApi.getSimpleUserList()
}

defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
    formLoading.value = true
    try {
        console.log(formData.value)

        await DisbursementApi.setAuthority(formData.value);

        message.success(t('common.createSuccess'))
        dialogVisible.value = false
        // 发送操作成功的事件
        emit('success')
    } finally {
        formLoading.value = false
    }
}

/** 重置按钮操作 */
const addQuery = async () => {
  await  DisbursementApi.addTeamMember(queryParams)
  message.success(t('common.createSuccess'))
  queryFormRef.value?.resetFields()
  handleQuery()
}


/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 删除人员按钮操作 */
const deleteQuery = async (id : any) => {
  await DisbursementApi.deleteTeamMember(queryParams.projectId,id)
  message.success("删除成功！")
  queryFormRef.value?.resetFields()
  handleQuery()
}

const handlePermissionChange =async (row) =>{
  loading.value = true
  await DisbursementApi.updateTeamMember(row)
  message.success("更新成功！")
  loading.value = false
}
/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: -1 ,
    roleIds: [],
    userIds :[],
    userIds :[],
    readRoleIds: [],
    readUserIds :[]
  }
  formRef.value?.resetFields()
}
</script>
