<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 {
  hideLoading,
  navigateBack,
  showLoading,
  showToast,
} from "@/utils/uniapp";
const signCompRef = ref<any>(null);
import { imagUrl } from "@/utils";
import { useForm } from "@/components/BasicForm/hooks/useForm";
import { useDataStore } from "@/store/modules/data";
import { jsConfirmJsDataApi } from "@/api/base/server";
import { useUserStore } from "@/store/modules/user";
const { getJs, setJs } = useUserStore();
const { getGlobal } = useDataStore();

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

const componentProps = {
  multiple: false, // 禁止多选
  maxCount: 1      // 限制最多显示一个文件
}

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

const srcData = {
  sfzrxy: imagUrl(getJs.sfzrxy),
  sfzghy: imagUrl(getJs.sfzghy),
  dyxlbyz: imagUrl(getJs.dyxlbyz),
  dyxlxwz: imagUrl(getJs.dyxlxwz),
  zgxlbyz: imagUrl(getJs.zgxlbyz),
  zgxlxwz: imagUrl(getJs.zgxlxwz),
  pthzm: imagUrl(getJs.pthzm),
  jszgz: imagUrl(getJs.jszgz),
}

setValue(srcData);

// 显示数据加载完成提示
showToast({
  title: "档案材料信息已加载",
  icon: "success",
  duration: 2000
});

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 = {
    ...getJs,
    ...value,
    signFile: sign_file.value,
  };
  setJs(params);
  const res = await jsConfirmJsDataApi(params);
  hideLoading();
  if (res.resultCode === 1) {
    uni.showToast({
      title: "提交成功",
      icon: "success",
    });

    setTimeout(() => {
      uni.redirectTo({
        url:
          "/pages/system/launchPage/launchPage?openId=" +
          getGlobal.openId +
          (getGlobal.type ? "&type=" + getGlobal.type : ""),
      });
    }, 1500);
  } else {
    uni.showToast({
      title: "提交失败",
      icon: "none",
    });
  }
}
</script>

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