<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 { dicApi, findDicTreeByPidApi } from "@/api/system/dic";
import { useDataStore } from "@/store/modules/data";
import { map } from "lodash";
import dayjs from "dayjs";

const { getFile, setFile } = useDataStore();
const jsjg = ref("");

// 计算日期与当前日期相差的年数
const calculateYearDiff = (startDate: string) => {
  if (!startDate) return "";
  const start = dayjs(startDate);
  const now = dayjs();
  console.log(now.diff(start, "year"));
  return now.diff(start, "year");
};

const [register, { getValue, setValue }] = useForm({
  schema: [
    {
      title: "基础信息",
    },
    {
      field: "jsdah",
      label: "档案号",
      component: "BasicInput",
      componentProps: {
        disabled: true,
      },
    },
    {
      field: "jsxm",
      label: "姓名",
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "jsxbId",
      label: "性别",
      component: "BasicPicker",
      componentProps: {
        api: dicApi,
        param: { pid: 13001 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      field: "jsjgId",
      label: "籍贯",
      component: "BasicDataPicker",
      componentProps: {
        api: findDicTreeByPidApi,
        param: { pid: 3701 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
        onChange: (e: any) => {
          jsjg.value = map(e.detail.value, (item) => {
            return item.text;
          }).join("");
        },
      },
    },
    {
      field: "mzId",
      label: "民族",
      component: "BasicPicker",
      componentProps: {
        api: dicApi,
        param: { pid: 19204295 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      field: "sfzh",
      label: "身份证号",
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "age",
      label: "年龄",
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "cstime",
      label: "出生日期",
      component: "BasicDateTimes",
      componentProps: {},
    },
    {
      field: "zzmmId",
      label: "政治面貌",
      component: "BasicPicker",
      componentProps: {
        api: dicApi,
        param: { pid: 385536488 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      field: "rdtime",
      label: "入党时间",
      component: "BasicDateTimes",
      componentProps: {},
    },
    {
      field: "lxdh",
      label: "联系电话",
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "dzyx",
      label: "电子邮箱",
      component: "BasicInput",
      componentProps: {},
    },

    {
      field: "sbkh",
      label: "社保卡号",
      component: "BasicInput",
      componentProps: {},
    },
    {
      title: "学历情况",
    },
    {
      field: "dyxlId",
      label: "初始学历",
      component: "BasicPicker",
      componentProps: {
        api: dicApi,
        param: { pid: 1914632204 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      field: "dybyyx",
      label: "全日制学历毕业院校",
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "dybyzy",
      label: "全日制学历专业",
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "zhxlId",
      label: "最后学历",
      component: "BasicPicker",
      componentProps: {
        api: dicApi,
        param: { pid: 1914632204 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      field: "zhbyyx",
      label: "最后学历毕业院校",
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "zhbyzy",
      label: "最后学历专业",
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "zhbynytime",
      label: "最后学历毕业年月",
      component: "BasicDateTimes",
      componentProps: {},
    },
    {
      field: "xwlbId",
      label: "学位类别",
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "xw",
      label: "学位",
      component: "BasicPicker",
      componentProps: {
        api: dicApi,
        param: { pid: 1513282965 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      field: "xwzy",
      label: "学位专业",
      component: "BasicInput",
      componentProps: {},
    },
    {
      title: "教师资格",
    },
    {
      field: "zgxdId",
      label: "教师资格学段",
      component: "BasicPicker",
      componentProps: {
        api: dicApi,
        param: { pid: 1680570189 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      field: "zgxkId",
      label: "教师资格学科",
      component: "BasicPicker",
      componentProps: {
        api: dicApi,
        param: { pid: 1029593616 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      field: "zgzs",
      label: "教师资格证书号",
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "zgnytime",
      label: "教师资格取得年月",
      component: "BasicDateTimes",
      componentProps: {},
    },
    {
      field: "zgzctime",
      label: "教师资格注册时间",
      component: "BasicDateTimes",
      componentProps: {},
    },
    {
      title: "任职情况",
    },
    {
      field: "cgtime",
      label: "参公时间",
      component: "BasicDateTimes",
      componentProps: {
        change: (e: any) => {
          const yearDiff = calculateYearDiff(e);
          setValue({ jsgl: yearDiff });
        },
      },
    },
    {
      field: "jsgl",
      label: "工龄",
      component: "BasicInput",
      componentProps: {
        disabled: true,
        placeholder: "请选择参公时间",
      },
    },
    {
      field: "jrdwtime",
      label: "进入本单位时间",
      component: "BasicDateTimes",
      componentProps: {
        change: (e: any) => {
          const yearDiff = calculateYearDiff(e);
          setValue({ jrdwnx: yearDiff });
        },
      },
    },
    {
      field: "jrdwnx",
      label: "进入本单位年限",
      component: "BasicInput",
      componentProps: {
        placeholder: "请选择进入本单位时间",
        disabled: true,
      },
    },
    {
      field: "jsTime",
      label: "教龄时间",
      component: "BasicDateTimes",
      componentProps: {
        change: (e: any) => {
          const yearDiff = calculateYearDiff(e);
          setValue({ jl: yearDiff });
        },
      },
    },
    {
      field: "jl",
      label: "教龄",
      component: "BasicInput",
      componentProps: {
        disabled: true,
        placeholder: "请选择教龄时间",
      },
    },

    {
      field: "jrly",
      label: "进入来源",
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "bzqkId",
      label: "编制情况",
      component: "BasicPicker",
      componentProps: {
        api: dicApi,
        param: { pid: 290357612 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      field: "zdqkId",
      label: "在岗情况",
      component: "BasicPicker",
      componentProps: {
        api: dicApi,
        param: { pid: 810984651 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
  ],
});
setValue(getFile);

async function submit() {
  try {
    const value = await getValue();
    setFile({ ...getFile, ...value, jsjg: jsjg.value });
    navigateTo("/pages/view/hr/teacherProfile/TitleInfo");
  } catch (error) {}
}
</script>

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