<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, watch, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { cloneDeep } from 'lodash-es'
import { getCity } from '@/api/system/admin'
import { DesignerButtom } from '@/components/DesignerList'
const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  }
})
const getCityList = async (node, resolve) => {
  const { value } = node
  const res = await getCity({ pid: value || 0, type: 2 })
  const data = res.data.map((ree) => {
    return {
      label: ree.label,
      value: ree.value,
      leaf: !ree.children
    }
  })
  resolve(data)
}
const cityChange = (value) => {
  console.log(value, 'value')
  setValues({
    province: value[0],
    city: value[1],
    area: value[2]
  })
}
const checked = (res) => {
  // console.log(res, 'res')
  setValues({
    group_leader_id: res.id
  })
}
const formSchema = ref<FormSchema[]>([
  {
    field: 'name',
    label: '组名',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      placeholder: '请输入组名称'
    }
  },
  {
    field: 'group_leader_id',
    label: '组长',
    component: 'CheckboxGroup',
    formItemProps: {
      slots: {
        default: (data) => {
          return (
            <>
              <DesignerButtom
                v-model={data.id}
                onChange={(res) => {
                  checked(res)
                }}
              ></DesignerButtom>
            </>
          )
        }
      }
    }
  },
  {
    field: 'address',
    label: '地址选择',
    component: 'Cascader',
    colProps: {
      span: 24
    },
    componentProps: {
      on: {
        change: cityChange
      },
      props: {
        lazy: true,
        lazyLoad: getCityList
      },
      placeholder: '请输入组名称'
    }
  },
  {
    field: 'province',
    label: '省份',
    component: 'Input',
    hidden: true
  },
  {
    field: 'city',
    label: '市',
    hidden: true,
    component: 'Input'
  },
  {
    field: 'area',
    label: '区',
    hidden: true,
    component: 'Input'
  },
  {
    field: 'id',
    label: 'id',
    hidden: true,
    component: 'Input'
  }
])

const rules = ref({
  name: [required('请填写组名称')],
  group_leader_id: [required('请选择设计师')]
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = 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
  }
}

watch(
  () => props.currentRow,
  async (value) => {
    if (!value) return
    const currentRow = cloneDeep(value)
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="formSchema" />
</template>
