<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 } from "@/api/system/dic";
import { useDataStore } from "@/store/modules/data";
import {
  findAllNjBjTree,
  findAllXxXqNjTree,
  kmFindAllApi,
} from "@/api/base/server";

const { getFile, setFile } = useDataStore();

const [register, { getValue, setValue }] = useForm({
  schema: [
    {
      field: "dzzw",
      label: "党政职务:",
      component: "BasicCheckbox",
      itemProps: {
        labelPosition: "top",
      },
      componentProps: {
        api: dicApi,
        multiple: true,
        param: { pid: 1063530116 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    {
      field: "qtzw",
      label: "其他职务",
      component: "BasicCheckbox",
      itemProps: {
        labelPosition: "top",
      },
      componentProps: {
        api: dicApi,
        multiple: true,
        param: { pid: 246682695 },
        rangeKey: "dictionaryValue",
        savaKey: "dictionaryCode",
      },
    },
    // {
    //   field: "zrxk",
    //   label: "主任学科",
    //   component: "BasicPicker",
    //   componentProps: {
    //     api: kmFindAllApi,
    //     rangeKey: "kmmc",
    //     savaKey: "id",
    //   },
    // },
    // {
    //   field: "zrbj",
    //   label: "主任班级",
    //   component: "BasicPicker",
    //   componentProps: {
    //     api: findAllNjBjTree,
    //     rangeKey: "title",
    //     savaKey: "key",
    //   },
    // },
    // {
    //   field: "jrxk",
    //   label: "兼任学科",
    //   component: "BasicPicker",
    //   componentProps: {
    //     api: kmFindAllApi,
    //     rangeKey: "kmmc",
    //     savaKey: "id",
    //   },
    // },
    // {
    //   field: "jrbj",
    //   label: "兼任班级",
    //   component: "BasicPicker",
    //   componentProps: {
    //     api: findAllNjBjTree,
    //     rangeKey: "title",
    //     savaKey: "key",
    //   },
    // },
    {
      field: "njId",
      label: "所属年级",
      component: "BasicTree",
      componentProps: {
        api: findAllXxXqNjTree,
        rangeKey: "title",
        savaKey: "key",
      },
    },
    // {
    //   field: "bzrBjId",
    //   label: "班主任班级",
    //   component: "BasicTree",
    //   componentProps: {
    //     api: findAllNjBjTree,
    //     rangeKey: "title",
    //     savaKey: "key",
    //   },
    // },
  ],
});

if (getFile.dzzw && typeof getFile.dzzw == "string") {
  setValue({ 
    ...getFile, 
    dzzw: getFile.dzzw.split(","),
    qtzw: getFile.qtzw && typeof getFile.qtzw == "string" ? getFile.qtzw.split(",") : []
  });
} else {
  setValue({ 
    ...getFile, 
    dzzw: [], 
    qtzw: getFile.qtzw && typeof getFile.qtzw == "string" ? getFile.qtzw.split(",") : [] 
  });
}

async function submit() {
  try {
    const value = await getValue();
    // 处理 dzzw 和 qtzw 字段，将数组转换为字符串
    if (Array.isArray(value.dzzw)) {
      value.dzzw = value.dzzw.join(',');
    }
    if (Array.isArray(value.qtzw)) {
      value.qtzw = value.qtzw.join(',');
    }
    setFile({ ...getFile, ...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>
