import ColResult from '@/components/col-result'
import ColSticky from '@/components/col-sticky'
import request from '@/helpers/request'
import { state } from '@/state'
import { Button, Dialog, Toast } from 'vant'
import { defineComponent } from 'vue'
import teacherCert from '..'
import CertThree from '../cert-three'
import { teacherState } from '../teacherState'
import ColHeader from '@/components/col-header'

export default defineComponent({
  name: 'teacher-cert-update',
  data() {
    return {
      authStatus: false
    }
  },
  async mounted() {
    try {
      // 判断是否达人认证
      if (!state.user.data?.entryFlag) {
        Dialog.alert({
          message: '您尚未完成达人认证，达人认证后才可更新资料哦！',
          confirmButtonColor: '#2DC7AA'
        }).then(() => {
          this.$router.replace('/teacherCert')
        })
        return
      }
      const teacherInfo = await request.get(
        '/api-teacher/teacherAuthEntryRecord/getLastRecordByUserId'
      )
      const info = teacherInfo.data || {}
      // 老师入驻状态 0、未申请 UNPAALY、未申请 DOING、审核中 PASS、通过 UNPASS、不通过
      const entryStatus = info.teacherAuthStatus || 0
      this.authStatus = entryStatus === 'DOING' ? true : false
      teacherState.teacherCert.graduateSchool = info.graduateSchool
      teacherState.teacherCert.subject = info.subject
      teacherState.teacherCert.subjectId = info.subjectId
      teacherState.teacherCert.introduction = info.introduction
      teacherState.teacherCert.styleVideoJson = info.styleVideoJson
      teacherState.teacherCert.gradCertificate = info.gradCertificate
      teacherState.teacherCert.degreeCertificate = info.degreeCertificate
      teacherState.teacherCert.teacherCertificate = info.teacherCertificate
    } catch {
      //
    }
  },
  methods: {
    async onSubmit() {
      const teacherCert = teacherState.teacherCert
      if (!teacherCert.graduateSchool) {
        Toast('请输入您的毕业院校')
        return
      }
      if (!teacherCert.subject) {
        Toast('请输入您的专业')
        return
      }

      await request.post('/api-teacher/teacherAuthEntryRecord/doApply', {
        data: {
          graduateSchool: teacherCert.graduateSchool,
          subject: teacherCert.subject,
          subjectId: teacherCert.subjectId,
          introduction: teacherCert.introduction,
          styleVideoJson: teacherCert.styleVideoJson,
          gradCertificate: teacherCert.gradCertificate,
          degreeCertificate: teacherCert.degreeCertificate,
          teacherCertificate: teacherCert.teacherCertificate
        }
      })
      Toast('提交成功')
      this.authStatus = true
    }
  },
  render() {
    return (
      <>
        {this.authStatus ? (
          <>
            <ColHeader />
            <ColResult
              type="teacherCert"
              style={{ paddingTop: '60px' }}
              classImgSize="CERT"
              tips="感谢您的申请，小酷将在24小时内完成审核，请留意APP消息及短信获取审核结果。"
            />
          </>
        ) : (
          <>
            <ColHeader />
            <div
              style={{
                paddingTop: '12px'
              }}
            >
              <CertThree />
              <ColSticky position="bottom">
                <div class={['btnGroup']} style={{ paddingTop: '12px' }}>
                  <Button
                    block
                    round
                    onClick={this.onSubmit}
                    type="primary"
                    text="提交审核"
                  />
                </div>
              </ColSticky>
            </div>
          </>
        )}
      </>
    )
  }
})
