<script lang="ts" setup name="SystemEditResource">
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 { ResourceInfo, ResourceTypeInfo } from './resource-interface'
import { addResource, getResourceTreeList, updateResource } from '@/api/system/resource'
import { toTreeList } from '@/utils/structure'
// ----------------------- 以下是字段定义 emits props ---------------------
const props = defineProps({
  // 资源类型列表
  resourceTypeList: {
    type: Array<ResourceTypeInfo>,
    default: () => [],
  },
})

const emits = defineEmits(['closeRefresh'])

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

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

// ---------------表单提交--------------------------------
// 表单对象
const dataFormRef = ref<FormInstance>()
// 校验规则
const rules = reactive<FormRules>({
  name: [{ required: true, message: '资源名称不可为空', trigger: ['blur', 'change'] }],
  url: [{ required: true, message: '资源路径不可为空', trigger: ['blur', 'change'] }],
  code: [{ required: true, message: '资源编码不可为空', trigger: ['blur', 'change'] }],
  pid: [{ required: true, message: '父资源必选', trigger: ['blur', 'change'] }],
  resourceType: [{ 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
  addResource(formData.value).then((res) => {
    ElMessageBox.confirm(
      '新增成功，是否继续新增?',
      '提示',
      {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'info',
      },
    ).then(() => {
      resetForm()
    }).catch(() => {
      closeRefresh()
    })
    btnLoading.value = false
  }).catch((_) => {
    btnLoading.value = false
  })
}

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

// 重置表单
function resetForm() {
  nextTick(() => {
    if (!dataFormRef.value) { return }
    dataFormRef.value?.resetFields()
  })
}

// 树列表配置
const resourceTreeProps = {
  parent: 'pid',
  value: 'id',
  label: 'name',
  children: 'children',
}
const resourceTree: Ref<ResourceInfo[]> = ref([])
// 获取资源树列表
function fetchResourceTree() {
  getResourceTreeList().then((response) => {
    if (response.data) {
      resourceTree.value = toTreeList(response.data, '0', true)
    }
  })
}
//
const filterTree = (value: string, data: { name: string }) => data.name?.includes(value)

onBeforeMount(() => {
  fetchResourceTree()
})
// ----------初始化、关闭对话框相关-----------------
function initDialog(dialogstatus: string, row: ResourceInfo) {
  dialogStatus.value = dialogstatus
  dialogVisible.value = true
  btnLoading.value = false
  if (dialogstatus === 'create') {
    // resetForm()
  }
  else if (dialogstatus === 'update') {
    formData.value = {
      id: row.id,
      name: row.name,
      url: row.url,
      code: row.code,
      pid: row.pid,
      resourceType: row.resourceType,
      num: row.num,
      icon: row.icon,
    }
  }
}

// 关闭并刷新
function closeRefresh() {
  dialogVisible.value = false
  resetForm()
  emits('closeRefresh')
}
// 关闭弹窗
function dialogClose() {
  dialogVisible.value = false
}
// ----------------------- 以下是暴露的方法内容 ----------------------------
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="80px" 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="formData.name" type="text" placeholder="必填" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="grid-cell">
          <el-form-item label="资源路径" prop="url" class="required">
            <el-input v-model="formData.url" type="text" placeholder="必填" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="grid-cell">
          <el-form-item label="资源编码" prop="code" class="required">
            <el-input v-model="formData.code" 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="resourceTree"
              node-key="id"
              :filter-node-method="filterTree"
              :props="resourceTreeProps"
              :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="resourceType" class="required">
            <el-select v-model="formData.resourceType" class="full-width-input" clearable placeholder="请选择">
              <el-option v-for="(item, index) in props.resourceTypeList" :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-col :span="12" class="grid-cell">
          <el-form-item label="图标" prop="icon">
            <el-input v-model="formData.icon" type="text" placeholder="非必填" clearable />
          </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>
