<script lang="ts" setup name="SystemEditRole">
import type { Ref } from 'vue'
import { nextTick, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import type { DataScopeTypeInfo, RoleInfo } from './role-interface'
import { addRole, getDataScopeTypeList, getRoleTreeList, updateRole } from '@/api/system/role'
import { toTreeList } from '@/utils/structure'

// ----------------------- 以下是字段定义 emits props ---------------------
const emits = defineEmits(['closeRefresh'])

// 对话框类型：create,update
const dialogStatus = ref('create')
const dialogVisible = ref(false)
// 显示标题
const textMap: { [key: string]: string } = {
  update: '编辑',
  create: '新增',
}
// 表单数据对象
const formData: Ref<RoleInfo> = ref({
  id: '',
  name: '',
  tips: '',
  dataScopeType: '',
  pid: '',
  deptId: '',
  num: 0,
  version: '',
})

// 保存按钮加载状态
const btnLoading = ref(false)

// ---------------表单提交--------------------------------
// 表单对象
const dataFormRef = ref<FormInstance>()
// 校验规则
const rules = reactive<FormRules>({
  name: [{ required: true, message: '角色名称不可为空', trigger: ['blur', 'change'] }],
  tips: [{ required: true, message: '别名不可为空', trigger: ['blur', 'change'] }],
  deptId: [{ required: true, message: '所属组织不能为空', trigger: ['blur', 'change'] }],
  pid: [{ required: true, message: '父角色必选', trigger: ['blur', 'change'] }],
  dataScopeType: [{ required: true, message: '数据权限类型必选', trigger: ['blur', 'change'] }],
  num: [{ required: true, message: '排序不可为空', trigger: 'blur' }, { pattern: /^\d+(\.\d+)?$/, trigger: ['blur', 'change'], message: '' }],
})

function submitForm() {
  if (dataFormRef) {
    dataFormRef.value?.validate((valid: boolean) => {
      if (valid) {
        if (dialogStatus.value === 'create') {
          createData()
        }
        else if (dialogStatus.value === 'update') {
          updateData()
        }
      }
    })
  }
}
// 新增数据
function createData() {
  btnLoading.value = true
  addRole(formData.value).then((res) => {
    ElMessageBox.confirm(
      '新增成功，是否继续新增?',
      '提示',
      {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'info',
      },
    ).then(() => {
      resetForm()
    }).catch(() => {
      closeRefresh()
    })
    btnLoading.value = false
  }).catch((_) => {
    btnLoading.value = false
  })
}

// 更新数据
function updateData() {
  updateRole(formData.value).then((res) => {
    ElMessage.success('修改成功')
    btnLoading.value = false
    closeRefresh()
  }).catch((_) => { // 异常情况，loading置为false
    btnLoading.value = false
  })
}

// 重置表单
function resetForm() {
  formData.value = {
    id: '',
    name: '',
    tips: '',
    dataScopeType: '',
    pid: '',
    deptId: '',
    num: 0,
    version: '',
  }
}

// 树列表配置
const roleTreeProps = {
  parent: 'pid',
  value: 'id',
  label: 'name',
  children: 'children',
}
// 数据类型列表
const dataScopeTypeList: Ref<DataScopeTypeInfo[]> = ref([])
function fetchDataScopeType() {
  getDataScopeTypeList().then((response) => {
    dataScopeTypeList.value = response.data
  })
}
// 获取角色树列表
const roleTree: Ref<RoleInfo[]> = ref([])
function fetchRoleTree() {
  getRoleTreeList().then((response) => {
    if (response.data) {
      roleTree.value = toTreeList(response.data.list, '0', true)
    }
  })
}
//
const filterTree = (value: string, data: { name: string }) => data.name?.includes(value)

onBeforeMount(() => {
  fetchDataScopeType()
  fetchRoleTree()
})
// ----------初始化、关闭对话框相关-----------------
function initDialog(dialogstatus: string, row: RoleInfo) {
  dialogStatus.value = dialogstatus
  dialogVisible.value = true
  btnLoading.value = false
  if (dialogstatus === 'create') {
    resetForm()
    nextTick(() => {
      dataFormRef.value?.clearValidate()
    })
  }
  else if (dialogstatus === 'update') {
    formData.value = {
      id: row.id,
      name: row.name,
      tips: row.tips,
      dataScopeType: row.dataScopeType,
      pid: row.pid,
      deptId: row.deptId,
      num: row.num,
      version: row.version,
    }
  }
}

// 关闭并刷新
function closeRefresh() {
  dialogVisible.value = false
  resetForm()
  emits('closeRefresh')
}
// 关闭弹窗
function dialogClose() {
  dialogVisible.value = false
  resetForm()
}
// ----------------------- 以下是暴露的方法内容 ----------------------------
defineExpose({ initDialog })
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    :title="textMap[dialogStatus]"
    width="50%"
    :before-close="dialogClose"
    append-to-body
    :open-delay="0"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFormRef" :model="formData" :rules="rules" label-position="left" label-width="110px" class="form-container"
      size="default" @submit.prevent
    >
      <el-row :gutter="10">
        <el-col :span="12" class="grid-cell">
          <el-form-item label="角色名称" prop="name" class="required">
            <el-input v-model.trim="formData.name" type="text" placeholder="必填" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="grid-cell">
          <el-form-item label="别名" prop="tips" class="required">
            <el-input v-model.trim="formData.tips" type="text" placeholder="必填" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="grid-cell">
          <el-form-item label="父角色" prop="pid" class="required">
            <el-tree-select
              v-model="formData.pid"
              :data="roleTree"
              node-key="id"
              :filter-node-method="filterTree"
              :props="roleTreeProps"
              :expand-on-click-node="false"
              :render-after-expand="false"
              check-strictly
              check-on-click-node
              filterable
              class="full-width-input"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="grid-cell">
          <el-form-item label="所属组织" prop="deptId" class="required">
            <dept-select v-model="formData.deptId" :dept-show="true" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="grid-cell">
          <el-form-item label="数据权限类型" prop="dataScopeType" class="required">
            <el-select v-model="formData.dataScopeType" class="full-width-input" clearable placeholder="请选择">
              <el-option v-for="(item, index) in dataScopeTypeList" :key="index" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="grid-cell">
          <el-form-item label="排序" prop="num" class="required">
            <el-input-number
              v-model="formData.num" class="full-width-input" controls-position="right"
              :min="0" :max="999" :precision="0" :step="1"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button :loading="btnLoading" type="primary" @click="submitForm">
          保存
        </el-button>
        <el-button @click="dialogClose">
          取消
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.form-container {
  width: 100%;

  .full-width-input {
    width: 100%;
  }
}
</style>
