<template>
  <div class="section-header-title-contianer">
    <div class="title">{{title}}</div>
    <div class="filter" v-if="filter">
      <template v-if="(filterType === 'select' || filterType === 'year' || filterType === 'default') && !!optionList && optionList.length > 0" >
        <el-select v-model="modelValue" placeholder="请选择" :class="[filterType === 'year' ? 'date-select' : 'other-select']" @change="selectChange">
          <el-option
            v-for="item in optionList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </template>
      <template v-if="filterType === 'yearMonth' && !!optionList && optionList.length > 0" >
        <el-select v-model="modelValue[0]" placeholder="请选择" class="date-select" @change="yearChange">
          <el-option
            v-for="item in optionList[0]"
            :key="item"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
        <el-select v-model="modelValue[1]" placeholder="请选择" class="date-select" @change="monthChange">
          <el-option
            v-for="item in optionList[1]"
            :key="item"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </template>

      <!-- 自定义slot -->
    </div>
    <div v-if="slotDefault">
      <slot></slot>
    </div>
  </div>
</template>

<script lang="ts" setup name="DzSectionHeader">
  import { ElSelect, ElOption } from 'element-plus'
  import { ref, watch, onMounted, useSlots } from 'vue'

  type selectItemValue  = string | number

  interface selectItem {
    value: selectItemValue,
    label: string,
  }

  interface Props {
    title: string,
    filterType?: 'year' | 'yearMonth' | 'select' | 'default',
    filter?: boolean,
    options?: selectItemValue[] | Array<selectItemValue[]> | selectItem[],
    modelValue?: string | number | selectItemValue[],
  }

  const props = withDefaults(defineProps<Props>(),
  {
    filterType: 'default',
    filter: false,
    options: () => [],
  })


  const curYear = ref('' as any)
  const curMonth = ref('' as any)
  const optionList = ref( [] as any[])

  const slotDefault = !!useSlots().default;

  watch(props.options, (newVal: selectItemValue[] | Array<selectItemValue[]> | selectItem[]) => {
    updateOptionList(newVal)
  })

  onMounted(() => {
    updateOptionList(props.options)
  })

  const emit = defineEmits(['change', 'chooseMenu'])

  const yearChange = (val: string | number) => {
    curYear.value = val
    emit('change', [val, curMonth.value])
  }
  const monthChange = (val: string | number) => {
    curMonth.value = val
    emit('change', [curYear.value, val])
  }
  const selectChange = (val:string | number) => {
    emit('change', val)
  }

  const updateOptionList = (val:any) => {
    if (!props.filter) return false
    if (props.filterType === 'year' && !!val) {
      optionList.value = val?.map((item:any) => {
        return typeof item === 'string' || typeof item === 'number' ? {
          value: item,
          label: item,
        } : item
      })
    } else {
      optionList.value = val
    }
  }

</script>
<style lang="less" scoped>
.section-header-title-contianer {
  width: 100%;
  height: 42px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-image: linear-gradient(270deg, #072856 0%, #0E5FFF 100%);
  position: relative;
  z-index: 99;
  .title {
    margin-left: 20px;
    font-family: PingFangSC-Medium;
    font-size: 22px;
    color: #D1F9FE;
    font-weight: 500;
  }

  .filter {
    display: flex;
    flex: 1;
    height: 100%;
    justify-content: flex-end;
    align-items: center;
    margin-right: 48px;
    .date-select {
      width: 90px;
    }


    .date-select:last-child {
      margin-left: 10px;
    }

    .other-select {
      width: 180px;
    }


    
    :deep(.el-input__inner ){
      border: 1px solid #e43179;
    }

    :deep(.el-input--suffix .el-input__inner) {
      background: transparent;
      color: #fff;
      border: none;
      text-align: right;
      padding-right: 0px;
      font-weight: 600;
    }

    :deep(.el-input__wrapper) {
      background: transparent;
      border: none !important;
      box-shadow: none !important;
    }

    :deep(.el-select .el-input .el-select__caret.el-icon) {
      color: #fff;
    }


    .nav-btn {
      margin-left: 10px;
      margin-top: 8px;
      width: 40px;
      height: 40px;
    }

  }
  .svg-btn {
    margin: 0 10px;
  }
}



</style>
