<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 { UpImgButtom } from '@/components/UpFile'
import addLevelTesk from './addLevelTesk.vue'
import { levelTaskBase } from '@/api/user'

const { required } = useValidator()
onMounted(() => {
  getTaskList()
})
const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  }
})

const formSchema = ref<FormSchema[]>([
  {
    field: 'name',
    label: '等级名称',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      placeholder: '请输入等级名称'
    }
  },
  {
    field: 'grade',
    label: '等级',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      type: 'number',
      placeholder: '请输入等级'
    }
  },
{
    field: 'price_value',
    label: '价格取值',
    component: 'Select',
    value: 1,
    componentProps: {
      options: [
        {
          label: '普通会员',
          value: 0
        },
        {
          label: 'VIP价格',
          value: 1
        },
        {
          label: '金卡',
          value: 2
        },
{
          label: '钻卡',
          value: 3
        }
      ]
    }
  },
{
    field: 'price',
    label: '价格',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      type: 'number',
      placeholder: '请输入价格'
    }
  },
{
    field: 'gift_amount',
    label: '赠送余额',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      type: 'number',
      placeholder: '请输入赠送余额'
    }
  },
  {
    field: 'is_forever',
    label: '是否永久',
    component: 'Switch',
    value: true
  },
  {
    field: 'valid_date',
    label: '有效期',
    value: 0,
    component: 'Input',
    componentProps: {
      type: 'number',
      placeholder: '有效时间(天) 0为永久'
    }
  },

  {
    field: 'is_show',
    label: '是否启用',
    component: 'Switch',
    value: true
  },
  {
    field: 'icon',
    label: '图标',
    value: [],
    component: 'CheckboxGroup',
    colProps: {
      span: 24
    },
    componentProps: {
      placeholder: '请输入图标'
    },
    formItemProps: {
      slots: {
        default: (data: any) => {
          return (
            <>
              <UpImgButtom v-model={data.icon} />
            </>
          )
        }
      }
    }
  },
  {
    field: 'image',
    label: '背景',
    component: 'Input',
    value: [],
    colProps: {
      span: 24
    },
    componentProps: {
      placeholder: '请输入背景'
    },
    formItemProps: {
      slots: {
        default: (data: any) => {
          return (
            <>
              <UpImgButtom v-model={data.image} />
            </>
          )
        }
      }
    }
  },
  {
    field: 'explain',
    label: '等级描述',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      type: 'textarea',
      placeholder: '请输入等级描述',
      row: 4
    }
  },
  {
    field: 'id',
    label: '',
    hidden: true,
    component: 'Input'
  }
])

const rules = ref({
  name: [required('请填写等级名称')],
  grade: [required('请输入等级')],
  explain: [required('请填写等级描述')],
  price: [required('请填写价格')],
  gift_amount: [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
})
const showDrawer = ref(false)
const checkedLevelTesk = async (res: any) => {
  // console.log(res)
  const formData = await getFormData()
  res.units = res.units.map((item: any) => {
    return {
      key: item.key,
      value: 0,
      label: item.label,
      replace: item.replace
    }
  })
  //默认是否选中
  res.must = true
  setValues({
    task: [...(formData?.task || []), res]
  })
  showDrawer.value = false
}
const taskList = ref({})
const getTaskList = async () => {
  const { data } = await levelTaskBase()
  taskList.value = data
}
</script>

<template>
  <div>
    <Form :rules="rules" @register="formRegister" :schema="formSchema" />
    <addLevelTesk :list="taskList" v-model="showDrawer" @confirm="checkedLevelTesk" />
  </div>
</template>
<style lang="less" scoped>
:deep(.el-table__row) {
  .list {
    width: 50px;
    text-align: center;
  }
}
</style>
