<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, watch, ref, onMounted } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { cloneDeep } from 'lodash-es'
import { getRoleListApi } from '@/api/system/role'
import type { FormRules } from 'element-plus'
import { getUnitList } from '@/api/goods'
const { required, phone, lengthRange } = useValidator()

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

const formSchema = ref<FormSchema[]>([
  {
    field: 'account',
    label: '管理员账号',
    component: 'Input',
    componentProps: {
      placeholder: '请输入管理员账号'
    }
  },
  {
    field: 'real_name',
    label: '管理员姓名',
    component: 'Input',
    componentProps: {
      placeholder: '请输入管理员姓名'
    }
  },
  {
    field: 'pwd',
    label: '密码',
    component: 'InputPassword',
    componentProps: {
      strength: true,
      placeholder: '请输入登录密码'
    }
  },
  {
    field: 'conf_pwd',
    label: '确认密码',
    component: 'InputPassword',
    componentProps: {
      strength: true,
      placeholder: '请输入重复输入登录密码'
    }
  },

  {
    field: 'phone',
    label: '管理员电话',
    component: 'Input',
    componentProps: {
      type: 'number',
      placeholder: '请输入联系电话'
    }
  },
  {
    field: 'roles',
    label: '管理员身份',
    component: 'Select',
    componentProps: {
      multiple: true
    }
  },
  {
    field: 'configure_id',
    label: '管理区域',
    component: 'Select'
  },
  {
    field: 'status',
    label: '状态',
    component: 'Switch',
    value: true
  },
  {
    field: 'is_all',
    label: '全局',
    component: 'Switch',
    value: false
  },
  {
    field: 'id',
    label: 'id',
    hidden: true,
    component: 'Input'
  }
])
interface RuleForm {
  account: string
  pwd: string
  conf_pwd: string
  roles: string
  phone: string
  real_name: string
}
const rules = ref<FormRules<RuleForm>>({
  account: [
    {
      required: true,
      ...lengthRange({
        min: 4,
        max: 64,
        message: '管理员账号长度4-64位字符'
      })
    }
  ],
  pwd: [required('请填写登录密码')],
  conf_pwd: [
    {
      required: true,
      validator: (_, val, callback) => {
        getFormData().then((formData) => {
          if (!val) return callback()
          if (formData.pwd !== val) {
            callback(new Error('两次密码不一致'))
          } else {
            callback()
          }
        })
      }
    }
  ],
  roles: [required('请选择管理员身份')],
  phone: [
    {
      required: true,
      ...phone()
    }
  ],
  real_name: [required('请填写管理员姓名')]
})
interface op {
  label: string
  value: number
}
const areaList = ref<op[]>([])
onMounted(async () => {
  const re = await getRoleListApi({ page: 1, limit: 9999 })
  roleList.value = re.data.list.map((res) => {
    return {
      label: res.role_name,
      value: res.id
    }
  })
  const data1 = await getUnitList({ page: 1, limit: 100, type: 'area',pid: 0})

  data1.data.list.forEach(element => {
    if (element.children.length == 0) {
      areaList.value.push({
        value: element.id,
        label: element.name,
      })
    } else {
      areaList.value.push({
        value: element.id,
        label: element.name,
      })
      element.children.forEach(element1 => {
        areaList.value.push({
          value: element1.id,
          label: '|__' + element1.name,
        })
      });
    }
  });
  if (props.currentRow) {
    rules.value.pwd = [
      {
        required: false
      }
    ]
    rules.value.conf_pwd = [
      {
        required: false
      }
    ]
  }

})

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
  }
}


watch(
  () => props.currentRow,
  async (value) => {
    if (!value) return
    const currentRow = cloneDeep(value)
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)
watch(
  () => roleList,
  async (value) => {
    if (!value) return
    setSchema([
      {
        field: 'roles',
        path: 'componentProps.options',
        value: value
      }
    ])
  },
  {
    deep: true,
    immediate: true
  }
)
watch(
  () => areaList,
  async (value) => {
    if (!value) return
    setSchema([
      {
        field: 'configure_id',
        path: 'componentProps.options',
        value: value
      }
    ])
  },
  {
    deep: true,
    immediate: true
  }
)
defineExpose({
  submit
})
</script>

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