<route lang="json">
{
  "style": {
    "navigationBarTitleText": "设计师认证",
    "navigationStyle": "custom"
  }
}
</route>
<script lang="ts" setup>
import Card from '@/components/card.vue'
import DataForm from '@/components/data-form.vue'
import NavBarEvo from '@/components/navbar-evo.vue'
import SectionHeading from '@/components/section-heading.vue'
import {
  createUserAuthInfo,
  getByDictType,
  getUserAuthInfo,
  updateUserAuthInfo,
  validateReferrerCode,
} from '../../../core/libs/requests'
import { DictType } from '../../../core/libs/models'
import { useUserStore } from '../../../store'
import pageHeaderFilter from '@designer-hub/assets/src/assets/svgs/pageHeaderFilter'
import { storeToRefs } from 'pinia'
import { useMessage, useToast } from 'wot-design-uni'
import { useRouter } from '../../../core/utils/router'
import { requestToast } from '../../../core/utils/common'
import { omit, pick } from 'radash'
import UploadEvo from '@/components/upload-evo.vue'

const { alert } = useMessage()
const router = useRouter()
const userStore = useUserStore()
const { userInfo, isDesigner } = storeToRefs(userStore)
const { error } = useToast()
const formData = ref<any>({})
const attachment = ref()
const formInited = ref(false)
const a = async () => ({ data: null, code: 0, msg: '' })
const { data: userAuthInfo, run: setUserAuthInfo } = useRequest(() => getUserAuthInfo())
// const { data: userAuthInfo, run: setUserAuthInfo } = useRequest(() => a())
const schema = ref({
  channelSource: {
    type: 'Select',
    label: '来源',
    props: {
      labelWidth: '126rpx',
      placeholder: '请选择通过哪个渠道入驻的筑巢荟',
      columns: [],
      disabled: userAuthInfo.value != null,
      'onUpdate:modelValue': (value) => {
        console.log(value)

        if (value === '4') {
          schema.value.referrer.existing = false
        } else {
          schema.value.referrer.existing = true
        }
      },
    },
  },
  referrer: {
    type: 'TextField',
    label: '推荐人',
    existing: true,
    props: {
      labelWidth: '126rpx',
      placeholder: '请如实填写推荐人编号，设计师会员编号或渠道编号',
      disabled: userAuthInfo.value != null,
    },
  },
  designerName: {
    type: 'TextField',
    label: '姓名',
    props: {
      labelWidth: '126rpx',
      placeholder: '请输入真实姓名',
    },
  },
  mobile: {
    type: 'TextField',
    label: '电话',
    props: {
      labelWidth: '126rpx',
      placeholder: '请输入电话号码',
    },
  },
  employer: {
    type: 'TextField',
    label: '公司',
    props: {
      labelWidth: '126rpx',
      placeholder: '请输入所在公司或自己公司名称',
    },
  },
  spatialExpertiseType: {
    type: 'Radio',
    label: '擅长空间类型',
    props: {
      labelWidth: '170rpx',
      placeholder: ' ',
      columns: [],
    },
  },
})
const handleSubmit = async () => {
  console.log(formData.value)
  if (!userAuthInfo.value) {
    if (formData.value.channelSource !== '4') {
      const { data, code: status } = await requestToast(() =>
        validateReferrerCode({ code: formData.value.referrer }),
      )
      if (data === false || status !== 0) {
        uni.showToast({ title: '推荐人编号不正确', icon: 'none' })
        return
      }
    }
    const { code, msg } = await createUserAuthInfo({
      gender: userInfo.value.sex,
      attachment: 'https://via.placeholder.com/319x204',
      userId: userInfo.value.userId,
      ...formData.value,
    })
    if (code === 0) {
      router.replace(`/pages/mine/authentication/submit/success/index`)
    } else {
      error(msg)
    }
  } else {
    const toBeUpdate = {
      ...omit(formData.value, []),
      attachment: attachment.value,
      id: userAuthInfo.value.id,
      gender: userInfo.value.sex,
    }
    const { code } = await requestToast(() => updateUserAuthInfo({ ...toBeUpdate, auditStatus: 1 }))
    if (code === 0) {
      router.replace(`/pages/mine/authentication/submit/success/index`)
    }
  }
}
onMounted(async () => {
  await setUserAuthInfo()
  console.log(userAuthInfo.value)
  console.log(userAuthInfo.value)

  if (userAuthInfo.value) {
    console.log(userAuthInfo.value)
    formData.value = pick(userAuthInfo.value, [
      'channelSource',
      'referrer',
      'designerName',
      'designerName',
      'mobile',
      'employer',
      'spatialExpertiseType',
    ])
    attachment.value = userAuthInfo.value.attachment
  }
  const { data } = await getByDictType('member_channel_source')
  const { data: res } = await getByDictType(DictType.memberSpatialExpertiseType)
  console.log(res)
  schema.value.channelSource.props.columns = data
  schema.value.spatialExpertiseType.props.columns = res
  formInited.value = true
  if (userInfo.value.userAuthStatus === 1) {
    alert({
      msg: '您的认证申请已提交，请耐心等待审核，审核通过后您将获得通知',
      title: '提示',
      confirmButtonText: '我知道了',
    })
  }
  if (userInfo.value.userAuthStatus === 2) {
    alert({
      title: '审核不通过',
      msg: '由于系统原因，您提交的认证暂时无法通过，请修改后重新提交',
    })
  }
})
defineExpose({})
</script>
<template>
  <div class="flex-grow">
    <div class="relative aspect-[3.6/1]">
      <div class="absolute top-0 left--1 right--1 bottom--18.5">
        <wd-img
          width="100%"
          height="100%"
          :src="'https://image.zhuchaohui.com/zhucaohui/21d6584cb711834f037a7126855d05572433f776b308596d94aca97dd37e6cfa.png'"
        ></wd-img>
      </div>
      <div class="absolute top-0 left-0 right-0 bottom--18.5">
        <wd-img width="100%" height="100%" :src="pageHeaderFilter"></wd-img>
      </div>
    </div>
    <NavBarEvo transparent dark title="设计师认证"></NavBarEvo>
    <div class="flex-grow flex flex-col p-3.5 gap-3.5 relative">
      <Card>
        <SectionHeading size="base" title="基本信息" custom-class="mb-4"></SectionHeading>
        <template v-if="formInited">
          <DataForm v-model="formData" :schema="schema" direction="horizontal"></DataForm>
        </template>
      </Card>
      <Card>
        <SectionHeading
          size="base"
          title="上传附件"
          subtitle="请上传名片或者获奖信息凭证"
          custom-class="mb-4"
        ></SectionHeading>
        <div class="h-0.25 bg-[#e1e1e1] mb-5"></div>
        <!-- <wd-upload></wd-upload> -->
        <UploadEvo v-if="formInited" v-model="attachment"></UploadEvo>
      </Card>
      <div class="flex-1"></div>
      <div>
        <wd-button
          block
          :round="false"
          :disabled="isDesigner || userInfo.userAuthStatus === 1"
          @click="handleSubmit"
        >
          提交
        </wd-button>
        <!-- <wd-button @click="handleSubmit">test</wd-button> -->
      </div>
    </div>
  </div>
</template>
