<template>
  <PageWrapper
    title="基础表单"
    content="基础表单，用于向用户收集表单信息，并展示 Password 组件使用示例"
  >
    <el-card shadow="never" class="mt-3 proCard">
      <el-row :gutter="20" justify="center" class="py-4">
        <el-col :xs="24" :sm="20" :md="14" :lg="12" :xl="8">
          <BasicForm
            submitButtonText="提交预约"
            :gridProps="{ cols: 1 }"
            :schemas="schemas"
            @submit="handleSubmit"
            @reset="handleReset"
          >
            <template #statusSlot="{ model, field }">
              <el-input v-model="model[field]" placeholder="请输入您今天的状态" />
            </template>
          </BasicForm>
        </el-col>
      </el-row>
    </el-card>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { BasicForm, FormSchema } from '@/components/Form/index';
  import { ElMessage } from 'element-plus';
  import { getClassifyList } from '@/api/select/select';

  const params = {
    type: 1,
  };

  async function loadSelectData(res) {
    //这里可以进行数据转换处理
    return (await getClassifyList({ ...res, ...params })).map((item, index) => {
      return {
        ...item,
        index,
      };
    });
  }

  const schemas: FormSchema[] = [
    {
      field: 'id',
      defaultValue: 128,
      hidden: true,
    },
    {
      field: 'identity',
      defaultValue: '我是一个隐藏字段内容',
      hidden: true,
    },
    {
      field: 'name',
      component: 'Input',
      label: '姓名',
      labelMessage: '这是一个提示',
      componentProps: {
        placeholder: '请输入姓名',
        onInput: (e: any) => {
          console.log(e);
        },
      },
      rules: [{ required: true, message: '请输入姓名', trigger: ['blur'] }],
    },
    {
      field: 'user.basic.age',
      component: 'InputNumber',
      label: '年龄',
      labelMessage: '这是一个field对象演示',
      componentProps: {
        placeholder: '请输入年龄',
        controlsPosition: 'right',
      },
      rules: [{ required: true, type: 'number', message: '请输入年龄', trigger: ['blur'] }],
    },
    {
      field: 'mobile',
      component: 'Input',
      label: '手机',
      componentProps: {
        placeholder: '请输入手机号码',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'type',
      component: 'Select',
      label: '类型',
      labelMessage: '选择类型会出现预约时间表单',
      componentProps: {
        placeholder: '请选择类型',
        clearable: true,
        options: [
          {
            label: '舒适性',
            value: 1,
          },
          {
            label: '经济性',
            value: 2,
          },
        ],
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'classify',
      component: 'BasicSelect',
      label: '动态分类',
      componentProps: {
        placeholder: '请选择分类',
        request: loadSelectData,
        onChange: (e: any) => {
          console.log(e);
        },
      },
      rules: [{ required: true, message: '请选择分类', trigger: ['change'] }],
    },
    {
      field: 'makeDate',
      component: 'DatePicker',
      label: '预约时间',
      defaultValue: 1183135260000,
      componentProps: {
        type: 'date',
        onChange: (e: any) => {
          console.log(e);
        },
      },
      // 根据 上面选择的类型，获取页面其他逻辑字段 处理显示表单
      // 可用字段 schema, values, model, field
      hidden: ({ model }) => {
        return !model.type;
      },
      rules: [{ required: true, type: 'number', message: '请选择预约时间', trigger: ['change'] }],
    },
    {
      field: 'makeTime',
      component: 'TimePicker',
      label: '停留时间',
      componentProps: {
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'status',
      label: '状态',
      //插槽
      slot: 'statusSlot',
    },
    {
      field: 'makeProject',
      component: 'Checkbox',
      label: '预约项目',
      componentProps: {
        placeholder: '请选择预约项目',
        options: [
          {
            label: '种牙',
            value: 1,
          },
          {
            label: '补牙',
            value: 2,
          },
          {
            label: '根管',
            value: 3,
          },
        ],
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'makeSource',
      component: 'RadioGroup',
      label: '来源',
      componentProps: {
        options: [
          {
            label: '网上',
            value: 1,
          },
          {
            label: '门店',
            value: 2,
          },
        ],
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    // {
    //   field: 'password',
    //   label: '密码',
    //   slot: 'passwordSlot',
    // },
  ];

  function handleSubmit(values: Recordable) {
    if (!values) return;
    ElMessage.success(JSON.stringify(values));
  }

  function handleReset(values: Recordable) {
    ElMessage.success(JSON.stringify(values));
  }
</script>

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