import OHeader from '@/components/o-header'
import { defineComponent, onMounted, reactive, ref } from 'vue'
import iconStudent from '@common/images/icon_student.png'
import styles from './apply-withdrawal.module.less'
import { Button, Dialog, Field, Image, showToast } from 'vant'
import OSticky from '@/components/o-sticky'
import { useRoute, useRouter } from 'vue-router'
import request from '@/helpers/request'
import { state } from '@/state'

export default defineComponent({
  name: 'apply-withdrawal',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const headColor = reactive({
      headBg: 'transparent',
      textColor: '#fff'
    })
    const forms = reactive({
      status: false,
      reason: '',
      id: route.query.id,
      dataInfo: {} as any,
      isClick: false
    })

    const getDetails = async () => {
      try {
        const { data } = await request.post('/api-student/student/getOrchestraDate/' + forms.id)
        forms.dataInfo = data
      } catch {
        //
      }
    }

    const onSubmit = async () => {
      if (!forms.reason) {
        showToast('请输入退团原因')
        return
      }
      forms.status = true
    }

    //
    const onConfirm = async () => {
      try {
        forms.isClick = true
        await request.post('/api-student/orchestra/leaveOrchestra', {
          data: {
            studentId: forms.dataInfo.studentId,
            orchestraId: forms.id,
            reason: forms.reason
          }
        })

        forms.isClick = false
        setTimeout(() => {
          showToast('申请成功')
        }, 100)
        setTimeout(() => {
          router.replace('/my-orchestra')
        }, 1100)
      } catch {
        //
        forms.isClick = false
      }
    }

    onMounted(() => {
      getDetails()
    })
    return () => (
      <div class={styles.applyWithdrawal}>
        <div class={styles.headers}>
          <OHeader
            background={headColor.headBg}
            color={headColor.textColor}
            border={false}
            backIconColor="white"
          />

          <div class={styles.userInfo}>
            <Image
              src={forms.dataInfo.studentAvatar || iconStudent}
              class={styles.img}
              fit="contain"
            />
            <div class={styles.userName}>{forms.dataInfo.studentName} 同学</div>
            <div class={styles.timer}>
              您已在{forms.dataInfo.orchestraName}训练<span>{forms.dataInfo.joinDays}</span>天
            </div>
          </div>
        </div>
        <div class={styles.content}>
          <span>在乐团训练的日子里，</span>
          有付出有汗水，有从0到1的喜悦也有攻克难关的坚持。你的付出为你带来成长，你的成长让老师感到欣喜。
        </div>

        <div class={styles.resion}>
          <div class={styles.title}>
            <div class={styles.name}>
              <i></i>退团原因<span>*</span>
            </div>
            <div class={styles.nums}>{forms.reason.length || 0}/400</div>
          </div>
          <Field v-model={forms.reason} placeholder="请输入退团详细原因" type="textarea" rows={3} />
        </div>

        <OSticky position="bottom">
          <div class={'btnGroup'}>
            <Button block round type="primary" onClick={onSubmit} disabled={forms.isClick}>
              确定
            </Button>
          </div>
        </OSticky>

        <Dialog
          v-model:show={forms.status}
          message={'确定要提交退团申请吗？'}
          messageAlign="left"
          confirmButtonText="确定"
          cancelButtonText="取消"
          showCancelButton
          onConfirm={() => onConfirm()}
        >
          {{
            title: () => (
              <div class={styles.dialogTitle}>
                <i></i>
                提示
              </div>
            )
          }}
        </Dialog>
      </div>
    )
  }
})
