<template>
  <BasicLayout>
    <view class="p-15">
      <BasicForm @register="register"> </BasicForm>
      <BasicSign ref="signCompRef" :title="signTitle"></BasicSign>
    </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 { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import { hideLoading, navigateBack, navigateTo, showLoading } from "@/utils/uniapp";
import { cloneDeep } from "lodash";
const leaveId = ref<string | null>(null);
const signCompRef = ref<any>(null);
import { useForm } from "@/components/BasicForm/hooks/useForm";
import { useDataStore } from "@/store/modules/data";
import { jsConfirmJsDataApi } from "@/api/base/server";

const signTitle = ref<string>("签名");

const [register, { getValue, setValue }] = useForm({
  formsProps: {
    labelWidth: 120,
  },
  schema: [
    {
      field: "sfzrxy",
      label: "身份证人像页",
      component: "BasicUpload",
      itemProps: {
        labelPosition: "top",
      },
      componentProps: {},
    },
    {
      field: "sfzghy",
      label: "身份证国徽页",
      component: "BasicUpload",
      itemProps: {
        labelPosition: "top",
      },
      componentProps: {},
    },
    {
      field: "dyxlbyz",
      label: "第一学历毕业证",
      component: "BasicUpload",
      itemProps: {
        labelPosition: "top",
      },
      componentProps: {},
    },
    {
      field: "dyxlxwz",
      label: "第一学历学位证",
      component: "BasicUpload",
      itemProps: {
        labelPosition: "top",
      },
      componentProps: {},
    },
    {
      field: "zgxlbyz",
      label: "最高学历毕业证",
      component: "BasicUpload",
      itemProps: {
        labelPosition: "top",
      },
      componentProps: {},
    },
    {
      field: "zgxlxwz",
      label: "最高学历学位证",
      component: "BasicUpload",
      itemProps: {
        labelPosition: "top",
      },
      componentProps: {},
    },
    {
      field: "pthzm",
      label: "普通话证明",
      component: "BasicUpload",
      itemProps: {
        labelPosition: "top",
      },
      componentProps: {},
    },
    {
      field: "jszgz",
      label: "教师资格证",
      component: "BasicUpload",
      itemProps: {
        labelPosition: "top",
      },
      componentProps: {},
    },
  ],
});

const { getFile, setFile } = useDataStore();
setValue(getFile);

const sign_file = ref<string>("");

async function submit() {
  const value = await getValue();
  if (!sign_file.value) {
    const data = await signCompRef.value.getSyncSignature();
    sign_file.value = data.base64;
  }
  showLoading("提交中...");
  const params = {
    ...getFile,
    ...value,
    sign_file: sign_file.value,
  };
  setFile(params);
  const res = await jsConfirmJsDataApi(params);
  hideLoading();
  if (res.resultCode === 1) {
    uni.switchTab({
      url: "/pages/base/message/index",
    });
    uni.showToast({
      title: "提交成功",
      icon: "success",
    });
  } else {
    uni.showToast({
      title: "提交失败",
      icon: "none",
    });
  }
}
</script>

<style lang="scss" scoped></style>
