<script lang="ts" setup>
import api from '@/servers/finyx/api'
import { ElMessage } from 'element-plus'
import { cloneDeep, isEmpty, pull, remove } from 'lodash-es'
import { ref } from 'vue'
import DialogBox from '@/components/dialog-box/index.vue'
// 定义组件属性，默认 confirmText 为 '确定'
const props = withDefaults(
  defineProps<{
    confirmText?: string
    title: string
    type?: 'category' | 'move'
    moveLabel?: '目标维度' | '目标类目'
    moveCurrentId?: string
  }>(),
  {
    confirmText: '确定',
    type: 'category'
  }
)

// 定义组件事件，允许触发 submitHandle 事件
const emit = defineEmits(['submitHandle'])

// 定义一个响应式变量 dialogVisible，用于控制对话框的显示和隐藏
const dialogVisible = ref<boolean>(false)
// 定义一个响应式变量 formData，用于存储表单数据
const formData = ref<any>({})
// 定义一个响应式变量 formDataRef，用于引用表单实例
const formDataRef = ref<any>(null)

// 定义一个响应式变量 loading，用于控制按钮的加载状态
const loading = ref<boolean>(false)

const optionData = ref<any>([])

const getCategoryList = async () => {
  try {
    loading.value = true
    optionData.value = await api.leimu.list({})
    if (props.moveCurrentId) {
      remove(optionData.value, (v: any) => v.id === props.moveCurrentId)
    }
  } catch (e: any) {
    ElMessage.error('获取类目列表失败')
  } finally {
    loading.value = false
  }
}

const getTagData = async () => {
  try {
    loading.value = true
    optionData.value = await api.weidu.query10({})

    if (props.moveCurrentId) {
      remove(optionData.value, (v: any) => v.id === props.moveCurrentId)
    }
  } catch (e: any) {
    ElMessage.error('获取维度列表失败')
  } finally {
    loading.value = false
  }
}

// 关闭对话框的方法
const close = () => {
  dialogVisible.value = false
  formData.value = {}
}
// 关闭对话框的方法
const open = (query?: any) => {
  dialogVisible.value = true
  if (!isEmpty(query)) {
    formData.value = cloneDeep(query)
  }
  if (props.type === 'move' && props.moveLabel === '目标类目') {
    getCategoryList()
  }
  if (props.type === 'move' && props.moveLabel === '目标维度') {
    getTagData()
  }
}

// 提交表单的方法
const submitHandle = async () => {
  try {
    await formDataRef.value.validate()
    // 触发 submitHandle 事件，传递表单数据
    emit('submitHandle', formData.value)
  } catch (e) {
    console.log(e)
  }
}
// 暴露 open 和 closeDialog 方法给外部使用
defineExpose({ open, close })
</script>

<template>
  <dialog-box :dialogVisible="dialogVisible" :title="title" width="500" @close="close">
    <el-form ref="formDataRef" :model="formData" label-width="auto">
      <template v-if="props.type === 'category'">
        <el-form-item label="类目名称" prop="name" :rules="[{ required: true, message: '请输入类目名称', trigger: 'blur' }]">
          <el-input
            v-model="formData.name"
            maxlength="30"
            placeholder="请输入类目名称"
            show-word-limit
          />
        </el-form-item>
      </template>
      <template v-else-if="props.type === 'move'">
        <el-form-item
          :label="props.moveLabel"
          :rules="[{ required: true, message: `请选择${props.moveLabel}`, trigger: 'blur' }]"
          prop="target"
        >
          <el-select v-model="formData.target" placeholder="请选择目标类目">
            <el-option
              v-for="item in optionData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </template>
    </el-form>
    <template #footer>
      <span class="dialog-footer res_el-dialog__footer">
        <el-button :loading="loading" @click.prevent="close"> 取消 </el-button>
        <el-button :loading="loading" type="primary" @click="submitHandle()">
          {{ confirmText }}
        </el-button>
      </span>
    </template>
  </dialog-box>
</template>

<style lang="scss" scoped></style>
