<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="mx-15 "
          type="primary"
          @click="submit"
        />
      </view>
    </template>
  </BasicLayout>
</template>

<script setup lang="ts">
import {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, setSchema }] = 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: "BasicDateTime",
      componentProps: {
        mode: "year-month",
      },
    },
    {
      field: "lxdh",
      label: "联系电话",
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "dzyx",
      label: "电子邮箱",
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "cgtime",
      label: "参加工作时间",
      component: "BasicDateTime",
      componentProps: {
        mode: "year-month",
        ok: (e: any) => {
          const yearDiff = calculateYearDiff(e.value);
          setValue({ jsgl: yearDiff });
        },
      },
    },
    {
      field: "jsgl",
      label: "工龄",
      component: "BasicInput",
      componentProps: {
        disabled: true,
      },
    },
    {
      field: "jsTime",
      label: "任教开始时间",
      component: "BasicDateTime",
      componentProps: {
        mode: "year-month",
        ok: (e: any) => {
          const yearDiff = calculateYearDiff(e);
          setValue({ jl: yearDiff });
        },
      },
    },
    {
      field: "jl",
      label: "教龄",
      component: "BasicInput",
      componentProps: {
        disabled: true,
        placeholder: "请选择教龄时间",
      },
    },
    {
      field: "jrdwtime",
      label: "进入本单位时间",
      component: "BasicDateTime",
      componentProps: {
        mode: "year-month",
        ok: (e: any) => {
          const yearDiff = calculateYearDiff(e.value);
          setValue({ jrdwnx: yearDiff });
        },
      },
    },
    {
      field: "jrdwnx",
      label: "进入本单位年限",
      component: "BasicInput",
      componentProps: {
        placeholder: "请选择进入本单位时间",
        disabled: true,
      },
    },

    {
      field: "jrly",
      label: "进入来源",
      component: "BasicPicker",
      componentProps: {
        api: dicApi,
        param: { pid: 2026977361 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      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",
        ok: (e: any, form: any, attrs: any) => {
          if (attrs[e].dictionaryValue == "调出") {
            setSchema([
              {
                field: "dcyy",
                ifShow: false,
              },
            ]);
          }
        },
      },
    },
    {
      field: "dcyy",
      label: "调出原因",
      ifShow: true,
      component: "BasicInput",
      componentProps: {},
    },
    {
      field: "gwjbId",
      label: "岗位级别",
      component: "BasicPicker",
      componentProps: {
        api: dicApi,
        param: { pid: 260783972 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      field: "xqxjId",
      label: "薪级",
      component: "BasicPicker",
      componentProps: {
        api: dicApi,
        param: { pid: 434953981 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      field: "sbkh",
      label: "社保卡号",
      component: "BasicInput",
      componentProps: {},
    },
  ],
});
setValue(getFile);

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

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