import { Search } from '@element-plus/icons-vue'
import { defineComponent, toRefs, reactive, onMounted, ref, watch } from 'vue'
import {
  ElButton,
  ElForm,
  ElFormItem,
  ElInput,
  ElSelect,
  ElOption,
  ElIcon
} from 'element-plus'

// import white from './while.module.less'
import classes from './index.module.less'
import request from '@/helpers/request'
import searchIcon from './images/searchIcon.png'
export default defineComponent({
  name: 'searchInput',
  props: {
    title: {
      type: String,
      default: ''
    },
    isWhile: {
      type: Boolean,
      default: true
    },
    searchVal: {
      type: Object,
      default: {}
    },
    holder: {
      type: String,
      default: '搜一搜你想练习的曲目'
    }
  },
  emits: ['startSearch'],
  setup(props, conent) {
    const state = reactive({
      title: props.title,
      search: props.searchVal.search,
      subject: null as null | number | string,
      subjectList: [],
      holder: props.holder
    })
    watch(
      () => props.searchVal,
      searchVal => {
        // console.log(searchVal,'searchVal')
        state.search = searchVal.search ? searchVal.search : ''
        state.subject = Number(props.searchVal.subject || props.searchVal.lessonSubject)
          ? Number(props.searchVal.subject|| props.searchVal.lessonSubject)
          : ''
      },
      {
        deep: true
      }
    )
    const getSubjectList = async () => {
      console.log('调用')
      try {
        const res = await request.get(
          '/api-website/open/subject/queryPage',
          {params:{rows:9999,page:1}}
        )

        state.subjectList = res.data.rows
      } catch (e) {
        console.log(e)
      }
    }

    const startSearch = () => {
      conent.emit('startSearch', {
        search: state.search,
        subject: state.subject
      })
    }
    onMounted(() => {
      getSubjectList()
    })
    // let classStyle = classes

    return () => (
      <>
        {/* props.isWhile ? classStyle.While : '' */}
        <div class={[classes.wrap]}>
          <div class={classes.selectWrap} id="selectWrap">
            <ElSelect
              clearable
              v-model={state.subject}
              placeholder="请选择声部"
              style="width: 175px"
              class={["subSelect",state.subject?'hasVal':'']}
              v-slots={{
                suffix: () => <div class={classes.san}></div>
              }}
            >
              {state.subjectList.map((item: any) => (
                <ElOption label={item.name} value={item.id} />
              ))}
            </ElSelect>
            <div class={classes.line}></div>
          </div>
          <div class={classes.searchWrap} >
            <img src={searchIcon} alt="" />
          </div>
          <ElInput
            v-model={state.search}
            placeholder={state.holder}
            clearable
            class={classes.inputSelect}
            // onKeyup={e => {
            //   if (e.keyCode === 13) {
            //     startSearch()
            //   }
            // }}
          ></ElInput>
            <div class={classes.searchline}></div>
          <div class={classes.searchBtn} onClick={startSearch}>搜索</div >
        </div>
      </>
    )
  }
})
