<template>
  <BasicLayout>
    <view class="p-15">
      <view v-if="education.xl.length > 0">
        <template v-for="(item, index) in education.xl" :key="index">
          <view class="po-re mb-15">
            <BasicForm
              v-model="item.value"
              :schema="schema"
              :formsProps="{ labelWidth: 100 }"
            />
            <view
              @click="deleteMemberFamily(index as number, item.value)"
              class="delete-icon"
            >
              <BasicIcon type="clear" size="30" />
            </view>
          </view>
        </template>
      </view>
      <view
        class="flex-row items-center justify-center pb-10 pt-5"
        style="border: 1px solid #e8e8e8"
        @click="addEducation"
      >
        <uni-icons type="plus" size="16" color="#447ADE"></uni-icons>
        <view class="ml-5 cor-447ADE">新增</view>
      </view>
    </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 lang="ts" setup>
import {
  findAllNjBjTreeApi,
  xqgwFindAllApi,
  xqxjFindAllApi,
} from "@/api/base/server";
import { useForm } from "@/components/BasicForm/hooks/useForm";
import { useDataStore } from "@/store/modules/data";
import { navigateTo, navigateBack } from "@/utils/uniapp";
import { cloneDeep, map } from "lodash";

const schema = reactive<FormsSchema[]>([
  {
    field: "gwjbId",
    label: "岗位级别",
    component: "BasicPicker",
    componentProps: {
      api: xqgwFindAllApi,
      rangeKey: "gwmc",
      savaKey: "id",
    },
  },
  {
    field: "gwrztime",
    label: "岗位任职时间",
    component: "BasicDateTimes",
    componentProps: {},
  },
  {
    field: "gwrznx",
    label: "岗位任职年限",
    component: "BasicInput",
    componentProps: {},
  },
  {
    field: "xj",
    label: "薪级",
    component: "BasicPicker",
    componentProps: {
      api: xqxjFindAllApi,
      rangeKey: "xjmc",
      savaKey: "id",
    },
  },
  {
    field: "zrxk",
    label: "主任学科",
    component: "BasicInput",
    componentProps: {},
  },
  {
    field: "jrxk",
    label: "兼任学科",
    component: "BasicInput",
    componentProps: {},
  },
  {
    field: "bzrBjId",
    label: "班主任班级",
    component: "BasicDataPicker",
    componentProps: {
      api: findAllNjBjTreeApi,
      rangeKey: "title",
      savaKey: "key",
    },
  },
  {
    field: "dzzw",
    label: "党政职务",
    component: "BasicInput",
    componentProps: {},
  },
]);

const education = reactive<any>({
  xl: [{ value: {} }],
});

const { getFile, setFile } = useDataStore();
if (getFile.rgqkList && getFile.rgqkList.length > 0) {
  education.xl = map(getFile.rgqkList, (item) => {
    return { value: item };
  });
}

function addEducation() {
  education.xl.push({ value: {} });
}

function deleteMemberFamily(index: number, item: any) {
  const list = cloneDeep(education.xl);
  list.splice(index, 1);
  education.xl = list;
}

function submit() {
  setFile({
    ...getFile,
    rgqkList: map(education.xl, (item) => {
      return item.value;
    }),
  });
  navigateTo("/pages/view/hr/teacherProfile/FamilyInfo");
}
</script>
<style>
.delete-icon {
  position: absolute;
  right: -13px;
  top: -14px;
  z-index: 1;
}
</style>
