<template>
  <view class="page">
    <view class="body">
      <scroll-view scroll-y class="scroll">
        <view class="form-wrap">
          <form-template ref="formRef" :items="baseItems" :form="form" :rule="rules" @change="onChange" />
        </view>
      </scroll-view>


      <view class="footer">
        <button class="primary" @click="next">下一步</button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import FormTemplate from '@/components/form-template/index.vue';
import validate from '@/utils/validate';

const STORAGE_KEY = 'patient-create-base';

const formRef = ref(null);
const form = reactive({
  name: '',
  gender: '',
  age: '',
  mobile: '',
  birthday: '',
  idType: '',
  idNo: ''
});

const baseItems = [
  { title: 'name', name: '姓名', type: 'input', operateType: 'formCell', required: true, wordLimit: 20, inputType: 'text' },
  { title: 'gender', name: '性别', type: 'select', operateType: 'formCell', required: false, range: ['男', '女'] },
  { title: 'age', name: '年龄', type: 'input', operateType: 'formCell', required: false, wordLimit: 3, inputType: 'number' },
  { title: 'mobile', name: '手机号', type: 'input', operateType: 'formCell', required: false, wordLimit: 11, inputType: 'number' },
  { title: 'birthday', name: '出生日期', type: 'date', operateType: 'formCell', required: false },
  { title: 'idType', name: '证件类型', type: 'select', operateType: 'formCell', required: false, range: ['身份证', '护照', '港澳台通行证', '其他'] },
  { title: 'idNo', name: '证件号', type: 'input', operateType: 'formCell', required: false, wordLimit: 30, inputType: 'text' }
];

const rules = {
  idNo(value) {
    if (!value) return true;
    if (form.idType === '身份证') {
      const [ok, msg] = validate.isChinaId(value);
      if (!ok) return msg || '证件号格式不正确';
    }
    return true;
  }
};

onLoad(() => {
  const cached = uni.getStorageSync(STORAGE_KEY);
  if (cached && typeof cached === 'object') {
    Object.assign(form, cached);
  }
});

function onChange({ title, value }) {
  form[title] = value;
}

function next() {
  if (!formRef.value?.verify?.()) return;
  uni.setStorageSync(STORAGE_KEY, { ...form });
  uni.navigateTo({ url: '/pages/case/patient-inner-info' });
}
</script>

<style lang="scss" scoped>
.page {
  height: 100vh;
  background: #f6f6f6;
}

.body {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.scroll {
  flex: 1;
}

.form-wrap {
  background: #fff;
}

.step-note {
  position: fixed;
  left: 50%;
  bottom: 92px;
  transform: translateX(-50%);
  background: #f2df52;
  color: #000;
  font-size: 14px;
  padding: 10px 16px;
  border-radius: 4px;
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.18);
}

.footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.06);
}

.primary {
  width: 100%;
  height: 48px;
  line-height: 48px;
  background: #5d8aff;
  color: #fff;
  border-radius: 6px;
  font-size: 16px;
}

.primary::after {
  border: none;
}
</style>
