import ColHeader from '@/components/col-header'
import ColSearch from '@/components/col-search'
import { Sticky, Image, List, Popup, Icon, Area, Field, Form, CellGroup, Button, Toast, Picker, DatetimePicker  } from 'vant'
import { defineComponent, onMounted, reactive } from 'vue'
import styles from './index.module.less'
import bg from './images/bg.png'
import rejectLogo from './images/rejectLogo.png'
import rejectSchool from './images/rejest-school.png'
import subTitle from './images/subTitle.png'
import centerLogo from './images/center.png'
import studentText from './images/studentText.png'
import { useRoute } from 'vue-router'
import icon_arrow from './images/icon_arrow.png'
import rejectBtn from './images/rejectBtn.png'
import request from '@/helpers/request'
import dayjs from 'dayjs'
export default defineComponent({
  name: 'tenantStudentRejest',
  setup() {
    const route = useRoute();
    const forms = reactive({
      gender: '',
      name: '',
      phone: '',
      subjectId: '',
      tenantId: '',
      birthdate: '',
      code: '',
      genderName:''
    });

    const data = reactive({
      schoolName: route.query.schoolName || '',
      cityName: '', // 所属城市
      showArea: false,
      checked: true,
      success: false,
      areaList: {} as any,
      sendMsg: '获取验证码',
      imgCodeStatus: false,
      subjectList: [],
      searchStatus: false,
      openStatus: false,
      dateState: false,
      genderState:false,
      genderList:[{text:'男',value:'1'},{text:'女',value:'0'}]
    });
    const handleSubmit = () => {
      console.log(forms, 'forms')
      if (!forms.name) {
        Toast('请输入姓名')
      }
      if (!forms.gender) {
        Toast('请选择性别')
      }
      if (!forms.phone) {
        Toast('请输入手机号')
      }
      if (!forms.birthdate) {
        Toast('请选择出生年月')
      }
      if (!forms.code) {
        Toast('请输入验证码')
      }
      if (!forms.subjectId) {
        Toast('请选择声部')
      }
    }
    const getSubjectList = async () => {
      try {
        const res = await request.get('/api-student/open/subject/subjectSelect')
        data.subjectList = res.data || []
      } catch (e) {
        console.log(e)
      }
    }
    const confirmSubject = (val: any) => {
      console.log(val, 'confirmSubject')
    }

    const confirmDate = (val:any)=>{
      forms.birthdate = dayjs(val).format('YYYY-MM-DD')
     data.genderState = false
    }
    onMounted(() => {
      getSubjectList()
    })
    const confirmGender = (val:any)=>{
      if(val.value){
        forms.gender = val.value;
        forms.genderName = val.text
      }else{
        forms.gender = '';
        forms.genderName = ''
      }
      data.genderState = false
    }
    /** 发送验证码 */
    const onSendSms = () => {
      if (!forms.phone) {
        Toast('请输入手机号码');
        return;
      }
      if (!/^1[3456789]\d{9}$/.test(forms.phone)) {
        Toast('手机号码格式不正确');
        return;
      }
      data.imgCodeStatus = true;
    };
    return () =>
      <>< div class={styles.videoClass} >
        <ColHeader
          class={styles.classHeader}
          border={false}
          isFixed={false}
          background="#fff"
        />
        <div class={styles.resjetStudentWrap}>
          <img src={rejectLogo} class={styles.rejectLogo} alt="" />
          <img src={studentText} class={styles.studentText} alt="" />
          <img src={bg} class={styles.bgWrap} alt="" />
          <div class={styles.schoolNameWrap}>
            <img src={rejectSchool} class={styles.rejectSchool} alt="" />
            <p>武汉星星小学</p>

          </div>
          <img class={styles.centerLogo} src={centerLogo} alt="" />
          <div class={styles.infoWrap}>
            <div class={styles.infoWrapCore}>
              <img src={subTitle} class={styles.subTitle} alt="" />

              <Form onSubmit={() => handleSubmit()}>
                <CellGroup class={styles.group} border={false}>
                  <Field
                    class={styles.noArrow}
                    inputAlign="right"
                    label="姓名"
                    placeholder="请输入姓名"
                    maxlength={20}
                    v-model={forms.name}

                  // onUpdate: modelValue={(val: string) => {
                  //   forms.nickname = val.trim();
                  // }}
                  />
                  <Field
                    readonly
                    inputAlign="right"
                    label="性别"
                    placeholder="请选择性别"
                    v-model={forms.genderName}
                    onClick={()=>{
                      data.genderState = true
                    }}
                  // onUpdate: modelValue={(val: string) => {
                  //   forms.nickname = val.trim();
                  // }}
                  >
                    {{
                      button: () => (
                        <img
                          style={{
                            display: 'block',
                            width: '12px',
                            height: '12px',

                          }}
                          src={icon_arrow}
                        />
                      )
                    }}
                  </Field>
                  <Field
                    readonly
                    inputAlign="right"
                    label="出生日期"
                    placeholder="请选择出生日期"
                    maxlength={20}
                    v-model={forms.birthdate}
                    onClick={()=>{data.dateState = true}}
                  // onUpdate: modelValue={(val: string) => {
                  //   forms.nickname = val.trim();
                  // }}
                  >
                    {{
                      button: () => (
                        <img
                          style={{
                            display: 'block',
                            width: '12px',
                            height: '12px',

                          }}
                          src={icon_arrow}
                        />
                      )
                    }}
                  </Field>
                  <Field
                    inputAlign="right"
                    label="手机号"
                    class={styles.noArrow}
                    maxlength={11}
                    placeholder="请输入手机号码"
                    v-model={forms.phone}
                  />
                  <div class={styles.tips}>
                    手机号码为酷乐秀机构版登录账号
                  </div>

                  <Field
                    class={styles.inputCode}
                    inputAlign="left"
                    label="请输入验证码"
                    labelWidth={0}
                    v-model={forms.code}
                    maxlength={6}>
                    {{
                      button: () => (
                        <Button

                          disabled={data.sendMsg.includes('s')}
                          class={styles.sendBtn}
                          onClick={() => onSendSms()}>
                          {data.sendMsg}
                        </Button>
                      )
                    }}
                  </Field>
                  <Field
                    border={false}
                    inputAlign="right"
                    label="声部"
                    placeholder="请选择声部"
                    readonly
                    v-model={data.cityName}
                    onClick={() => (data.searchStatus = true)}>
                    {{
                      button: () => (
                        <img
                          style={{
                            display: 'block',
                            width: '12px',
                            height: '12px',

                          }}
                          src={icon_arrow}
                        />
                      )
                    }}
                  </Field>
                </CellGroup>
              </Form>
            </div>
            <img src={rejectBtn} onClick={() => { handleSubmit() }} class={styles.rejectBtn} alt="" />
          </div>
        </div>
        <Popup
          show={data.searchStatus}
          position="bottom"
          round
          columns-field-names={{ text: '' }}
          safe-area-inset-bottom
          onClose={() => (data.searchStatus = false)}
          onClosed={() => (data.openStatus = false)}
        >
          <Picker columns={data.subjectList} onCancel={() => { data.searchStatus = false }} onConfirm={confirmSubject}></Picker>
        </Popup>
        <Popup
          show={data.dateState}
          position="bottom"
          round
          columns-field-names={{ text: '' }}
          safe-area-inset-bottom
          onClose={() => (data.dateState = false)}
          onClosed={() => (data.dateState = false)}
        >
          <DatetimePicker  type="date" v-model:value={forms.birthdate} onCancel={() => { data.dateState = false }} onConfirm={confirmDate}></DatetimePicker>
        </Popup>

        <Popup
          show={data.genderState}
          position="bottom"
          round
          columns-field-names={{ text: '' }}
          safe-area-inset-bottom
          onClose={() => (data.genderState = false)}
          onClosed={() => (data.genderState = false)}
        >
          <Picker columns={data.genderList} onCancel={() => { data.genderState = false }} onConfirm={confirmGender}></Picker>
        </Popup>
      </div ></>

  }

})
