<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 { useUserStore } from "@/store/modules/user";
import { showLoading, hideLoading } from "@/utils/uniapp";
const { getJs, setJs } = useUserStore();
import { zwGetListByLxApi, findAllXxXqNjTree } from "@/api/base/server";

let dzZwList: any = [];
let qtZwList: any = [];
const formSchema: any = [
  {
    title: "任职情况",
  },
  {
    field: "dzzw",
    label: "党政职务:",
    component: "BasicCheckbox",
    itemProps: {
      labelPosition: "top",
    },
    componentProps: {
      data: [{ label: "书记", value: "1" }],
      multiple: true,
      onChange(e: any) {
        console.log(e);
      }
    },
  },
  {
    field: "qtzw",
    label: "其他职务",
    component: "BasicCheckbox",
    itemProps: {
      labelPosition: "top",
    },
    componentProps: {
      data: [{ label: "书记", value: "1" }],
      multiple: true,
      onChange(e: any) {
        console.log(e);
      }
    },
  },
  {
    field: "njId",
    label: "所属年级",
    component: "BasicTree",
    componentProps: {
      api: findAllXxXqNjTree,
      rangeKey: "title",
      savaKey: "key",
    },
  },
  // {
  //   field: "bzrBjId",
  //   label: "班主任班级",
  //   component: "BasicTree",
  //   componentProps: {
  //     api: findAllNjBjTreeApi,
  //     rangeKey: "title",
  //     savaKey: "key",
  //   },
  // },
];
const [register, { getValue, setValue, setSchema }] = useForm({
  schema: formSchema
});

let dzZw: any = [];
let qtZw: any = [];
if (getJs.dzzw && typeof getJs.dzzw == "string") {
  dzZw = getJs.dzzw.split(",");
}
if (getJs.qtzw && typeof getJs.qtzw == "string") {
  qtZw = getJs.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(',');
    }
    
    // 保存数据
    setJs({ ...getJs, ...value });
    
    // 显示加载提示
    showLoading("正在加载职称信息...");
    
    // 延迟跳转，让用户看到加载提示
    setTimeout(() => {
      hideLoading();
      navigateTo("/pages/view/hr/teacherProfile/TitleInfo");
    }, 500);
    
  } catch (error) { 
    hideLoading();
    console.error("提交失败:", error);
  }
}

onMounted(async () => {
  const updateSchema: any = [];
  {
    const res = await zwGetListByLxApi({ zwlx: '党政职务' });
    dzZwList = res.result.map((item: any) => {
      return {
        label: item.zwmc,
        value: item.id
      };
    });
    updateSchema.push({
      field: "dzzw",
      label: "党政职务:",
      component: "BasicCheckbox",
      itemProps: {
        labelPosition: "top",
      },
      componentProps: {
        data: dzZwList,
        multiple: true,
        onChange(e: any) {
          console.log(e);
        }
      },
    })
  };
  {
    const res = await zwGetListByLxApi({ zwlx: '其他职务' });
    qtZwList = res.result.map((item: any) => {
      return {
        label: item.zwmc,
        value: item.id
      };
    });
    updateSchema.push({
      field: "qtzw",
      label: "其他职务:",
      component: "BasicCheckbox",
      itemProps: {
        labelPosition: "top",
      },
      componentProps: {
        data: qtZwList,
        multiple: true,
        onChange(e: any) {
          console.log(e);
        }
      },
    })
  };
  setSchema(updateSchema);
  nextTick(() => {
    setValue({
      ...getJs,
      dzzw: dzZw,
      qtzw: qtZw
    });
  });
});

</script>

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