<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch, ref, nextTick } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { useI18n } from '@/hooks/web/useI18n'
import { cloneDeep } from 'lodash-es'
import { ElButton } from 'element-plus'
import addApiUrl from './addApiUrl.vue'
import { Icon } from '@/components/Icon'
const { t } = useI18n()

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  },
  menuList: {
    type: Array,
    default: () => []
  }
})

const showApiDrawer = ref(false)

const formSchema = reactive<FormSchema[]>([
  {
    field: 'type',
    label: '菜单类型',
    component: 'RadioButton',
    value: 1,
    colProps: {
      span: 24
    },
    componentProps: {
      options: [
        {
          label: '菜单',
          value: 1
        },
        {
          label: '接口',
          value: 2
        }
      ],
      on: {
        change: async (val: number) => {
          if (val === 1) {
            //显示隐藏部分
            changeType(false)
          } else if (val === 2) {
            //隐藏显示部分
            changeType(true)
          }
        }
      }
    }
  },
  {
    field: 'parentId',
    label: '父级菜单',
    component: 'TreeSelect',
    componentProps: {
      nodeKey: 'id',
      props: {
        label: 'title',
        value: 'id',
        children: 'children'
      },
      highlightCurrent: true,
      expandOnClickNode: false,
      checkStrictly: true,
      checkOnClickNode: true,
      clearable: true
    },
    optionApi: async () => {
      return props.menuList || []
    }
  },
  {
    field: 'meta.title',
    label: t('menu.menuName'),
    component: 'Input',
    componentProps: {
      placeholder: '请输入菜单名称'
    }
  },
  {
    field: 'unique_auth',
    label: '权限标识',
    component: 'Input',
    componentProps: {
      placeholder: '请输入权限标识'
    }
  },
  {
    field: 'apiUrl',
    label: '接口地址',
    remove: true,
    component: 'Input',
    componentProps: {
      placeholder: '请输入接口地址',
      slots: {
        append: () => (
          <ElButton onClick={() => (showApiDrawer.value = true)}>
            <Icon icon="vi-ep:expand"></Icon>
          </ElButton>
        )
      }
    }
  },
  {
    field: 'methods',
    label: '请求方式',
    component: 'Select',
    remove: true,
    componentProps: {
      options: [
        {
          label: 'POST',
          value: 'POST'
        },
        {
          label: 'GET',
          value: 'GET'
        },
        {
          label: 'PUT',
          value: 'PUT'
        },
        {
          label: 'DELETE',
          value: 'DELETE'
        }
      ]
    }
  },

  {
    field: 'meta.icon',
    label: t('menu.icon'),
    remove: false,
    component: 'IconPicker',
    value: ''
  },
  {
    field: 'path',
    label: t('menu.path'),
    component: 'Input',
    componentProps: {
      placeholder: '请输入路由地址'
    }
  },
  {
    field: 'meta.activeMenu',
    label: t('menu.activeMenu'),
    component: 'Input'
  },
  {
    field: 'sort',
    label: '排序',
    component: 'Input',
    componentProps: {
      type: 'number',
      placeholder: '请输入数字越大越靠前'
    }
  },
  {
    field: 'status',
    label: t('menu.status'),
    component: 'Select',
    value: 1,
    componentProps: {
      options: [
        {
          label: t('userDemo.disable'),
          value: 0
        },
        {
          label: t('userDemo.enable'),
          value: 1
        }
      ]
    }
  },
  {
    field: 'meta.hidden',
    label: t('menu.hidden'),
    component: 'Switch'
  },
  {
    field: 'meta.canTo',
    label: t('menu.canTo'),
    component: 'Switch'
  },
  {
    field: 'meta.alwaysShow',
    label: t('menu.alwaysShow'),
    component: 'Switch'
  },
  {
    field: 'meta.noCache',
    label: t('menu.noCache'),
    component: 'Switch'
  },
  {
    field: 'meta.breadcrumb',
    label: t('menu.breadcrumb'),
    value: true,
    component: 'Switch'
  },
  {
    field: 'meta.affix',
    label: '固定标签页',
    component: 'Switch'
  },
  {
    field: 'meta.noTagsView',
    label: t('menu.noTagsView'),
    component: 'Switch'
  },

  {
    field: 'id',
    label: 'ID',
    component: 'Input',
    hidden: true
  }
])

const rules = reactive({
  path: [required()],
  'meta.title': [required()]
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose, setSchema } = formMethods

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    return formData
  }
}

const cacheComponent = ref('')

watch(
  () => props.currentRow,
  async (value) => {
    if (!value) return
    const currentRow = cloneDeep(value)
    // console.log(currentRow, 'currentRow')
    cacheComponent.value = currentRow.type === 1 ? currentRow.component : ''
    setValues(currentRow)
    await nextTick()
    if (currentRow.type === 1) {
      //显示隐藏部分
      changeType(false)
    } else if (currentRow.type === 2) {
      //隐藏显示部分
      changeType(true)
    }
  },
  {
    deep: true,
    immediate: true
  }
)

const changeType = (bol: boolean) => {
  setSchema([
    {
      field: 'meta.icon',
      path: 'remove',
      value: bol
    },
    {
      field: 'path',
      path: 'remove',
      value: bol
    },
    {
      field: 'meta.activeMenu',
      path: 'remove',
      value: bol
    },
    {
      field: 'name',
      path: 'remove',
      value: bol
    },
    {
      field: 'meta.permissionList',
      path: 'remove',
      value: bol
    },
    {
      field: 'meta.hidden',
      path: 'remove',
      value: bol
    },
    {
      field: 'meta.alwaysShow',
      path: 'remove',
      value: bol
    },
    {
      field: 'meta.noCache',
      path: 'remove',
      value: bol
    },
    {
      field: 'meta.breadcrumb',
      path: 'remove',
      value: bol
    },
    {
      field: 'meta.affix',
      path: 'remove',
      value: bol
    },
    {
      field: 'meta.noTagsView',
      path: 'remove',
      value: bol
    },
    {
      field: 'meta.canTo',
      path: 'remove',
      value: bol
    },
    {
      field: 'permissionList',
      path: 'remove',
      value: bol
    },
    {
      field: 'apiUrl',
      path: 'remove',
      value: !bol
    },
    {
      field: 'methods',
      path: 'remove',
      value: !bol
    }
  ])
}

defineExpose({
  submit
})

const openApiSeach = (res: any) => {
  setValues({
    apiUrl: res.rule,
    methods: res.method,
    meta: {
      title: res.real_name
    }
  })
  showApiDrawer.value = false
}
</script>

<template>
  <Form labelWidth="120px" :rules="rules" @register="formRegister" :schema="formSchema" />
  <addApiUrl v-model="showApiDrawer" @confirm="openApiSeach" />
</template>
