<template>
  <BasicLayout>
    <view class="px-15 pb-15">
      <BasicForm @register="register"> </BasicForm>
    </view>
    <template #bottom>
      <view class="flex-row items-center pb-10 pt-5">
        <u-button
          text="上一步"
          class="ml-15 mr-7"
          :plain="true"
          @click="navigateBack"
        />
        <u-button
          text="下一步"
          class="mr-15 mr-7"
          type="primary"
          @click="submit"
        />
      </view>
    </template>
  </BasicLayout>
</template>

<script setup lang="ts">
import { navigateBack, navigateTo } from "@/utils/uniapp";
import { useForm } from "@/components/BasicForm/hooks/useForm";
import dayjs from "dayjs";
import { useUserStore } from "@/store/modules/user";
import { useDicStore } from "@/store/modules/dic";
const { findByPid } = useDicStore();
const { getJs, setJs } = useUserStore();

const [register, { getValue, setValue }] = useForm({
  schema: [
    {
      title: "教师资格",
    },
    {
      field: "zgxdId",
      label: "教师资格学段",
      component: "BasicPicker",
      componentProps: {
        api: findByPid,
        param: { pid: 1680570189 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      field: "zgxkId",
      label: "教师资格学科",
      component: "BasicPicker",
      componentProps: {
        api: findByPid,
        param: { pid: 1029593616 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      field: "zgzs",
      label: "教师资格证书号",
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "zgnytime",
      label: "教师资格取得年月",
      component: "BasicDateTime",
      componentProps: {
        mode: "year-month",
      },
    },
    {
      field: "zgzctime",
      label: "当前教师资格注册时间",
      component: "BasicDateTimes",
      componentProps: {
        change: (e: any) => {
          if (e) {
            // 计算5年后的日期
            const nextDate = dayjs(e).add(5, 'year').format('YYYY-MM-DD');
            setValue({ xczgzctime: nextDate });
          }
        },
      },
    },
    {
      field: "xczgzctime",
      label: "下次教师资格注册时间",
      component: "BasicDateTimes",
      componentProps: {},
    },
  ],
});

setValue(getJs);

async function submit() {
  try {
    const value = await getValue();
    setJs({ ...getJs, ...value });
    navigateTo("/pages/view/hr/teacherProfile/position");
  } catch (error) {}
}
</script>

<style lang="scss" scoped>
.delete-icon {
  position: absolute;
  right: 0;
  top: 0;
  z-index: 1;
}
</style>
