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'

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)
          ? Number(props.searchVal.subject)
          : ''
      },
      {
        deep: true
      }
    )
    const getSubjectList = async () => {
      try {
        const res = await request.get(
          '/api-website/open/subject/subjectSelect',
          {}
        )

        state.subjectList = res.data
      } 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: 150px"
              class="subSelect"
              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>
          <ElInput
            clearable
            v-model={state.search}
            placeholder={state.holder}
            class={classes.inputSelect}
            v-slots={
              {
                // append: () => (
                //   <ElButton
                //     onClick={startSearch}
                //     v-slots={{
                //       icon: () => (
                //         <ElIcon class={classStyle.searchIcon}>
                //           <Search></Search>
                //         </ElIcon>
                //       )
                //     }}
                //     style={{ color: '#2DC7AA', font: '20px' }}
                //   />
                // )
              }
            }
          ></ElInput>
        </div>
      </>
    )
  }
})
