<template>
  <div :ref="refStr" class="custom-money-select screen-popper" id="custom-money-select">
    <div :class="['input-block', isSelectOption?'rote':'']">
      <div class="block" @click="isSelectOption=!isSelectOption" @mouseenter="handleMouseenter" @mouseleave="handleMouseleave">
        <el-input class="custom-money-input" v-model="value" :placeholder="placeholder" readonly>
          <template slot="suffix">
            <span @click.stop="handleClear" :class="[isClear&&isHover?'el-icon-circle-close':'el-icon-arrow-down']"></span>
          </template>
        </el-input>
      </div>
      <div class="options-block" v-if="isSelectOption">
        <div class="arrow"></div>
        <div @click="handleClick(option)" :class="['option', value==option?'active':'']" :key="i" v-for="(option, i) in options">
          <template v-if="option == '自定义'">
            <div class="number-box">
              <input type="number" v-model="startMoney" class="number-input" clearable>&nbsp;&nbsp;至&nbsp;&nbsp;<input v-model="endMoney" class="number-input" type="text" clearable>&nbsp;&nbsp;万元&nbsp;&nbsp;<el-button @click.stop="handleConfirm" class="number-button" type="primary">确定</el-button>
            </div>
          </template>
          <template v-else>
            <span>{{option}}</span> <span v-if="value==option" class="el-icon-check"></span>
          </template>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    'placeholder': {
      type: String,
      default: '请选择'
    },
    'ref-str': {
      type: String,
      default: `timeselect${String(Math.random(0, 100)).slice(2)}`,
    },
    permissions: {  //文本权限相关字段
      type: Array,
      default: () => {},
    },
    powerLabel: {
      type: String,
      default: ''
    },
    moneyList: {
      type: Array,
      default: () => [],
    }
  },
  computed: {
    isClear() {
      if(!this.isSelectOption && this.value) {
        return true
      }else {
        return false
      }
    }
  },
  data() {
    return {
      value: '',
      options: ['一亿以上', '5000万-1亿', '1000万-5000万', '1000万以下', '自定义'],
      isSelectOption: false,
      startMoney: '',
      endMoney: '',
      isHover: false
    }
  },
  mounted() {
    this.handleAppClick()
    if(this.moneyList&&this.moneyList.length>0){
      this.options = this.moneyList
    }
  },
  destroyed() {
    const app = document.getElementById('app')
    app.removeEventListener('click', ()=>{}, true)
  },
  methods: {
    // 判断是否点击的为组件内部
    handleAppClick() {
      const app = document.getElementById('app')
      app.addEventListener('click', (e) => {
        const dom = this.$refs[this.refStr]
        const flag = dom && dom.contains(e.target)
        // const flag = document.getElementById('custom-money-select').contains(e.target)
        !flag ? this.isSelectOption = false : ''

        if(this.value == '自定义' && !this.startMoney && !this.endMoney) {
          this.value = ''
          this.$emit('input', '')
          this.$emit('handle-search')
        }
      }, true)
    },
    // 清除
    handleClear() {
      if(this.isClear && this.isHover) {
        this.value = ''
        this.startMoney = ''
        this.endMoney = ''
        this.$emit('input', '')
        this.$emit('handle-search')
      }else {
        this.isSelectOption = true
      }
    },
    // 鼠标移入后的回调
    handleMouseenter() {
      this.isHover = true
    },
    // 鼠标离开后的回调
    handleMouseleave() {
      this.isHover = false
    },
    // 选项点击后的回调
    handleClick(value) {
      this.value = value
      let moneyStr = ''
      if(value == '自定义') {
        this.value = '自定义'
      }else {
        this.startMoney = ''
        this.endMoney = ''
        this.isSelectOption = false
        switch (value) {
          case '一亿以上':
            moneyStr = [10000]
            break;
          case '5000万-1亿':
            moneyStr = [5000, 10000]
            break;
          case '1000万-5000万':
            moneyStr = [1000, 5000]
            break;
          case '10亿以上':
            moneyStr = [100000]
            break;
          case '1亿-10亿':
            moneyStr = [10000, 100000]
            break;
          case '2000万-1亿':
            moneyStr = [2000, 10000]
            break;
          case '400万-2000万':
            moneyStr = [400, 2000]
            break;
          case '400万以下':
            moneyStr = [, 400]
            break;
          default:
            moneyStr = [, 1000]
            break;
        }
        this.$emit('input', moneyStr)
        this.$emit('handle-search')
      }
    },
    // 自定义确认点击后的回调
    handleConfirm() {
      this.isSelectOption = false

      if(!this.startMoney && !this.endMoney) {
        this.value = ''
        this.$emit('input', '')
      }else {
        if(this.endMoney && this.startMoney) {
          this.value = `${this.startMoney}-${this.endMoney}万`
        }else {
          if(this.startMoney) {
            this.value = `大于等于${this.startMoney}万`
          }

          if(this.endMoney) {
            this.value = `小于等于${this.endMoney}万`
          }
        }
        let moneyStr = [this.startMoney, this.endMoney]
        this.$emit('input', moneyStr)
      }
      this.$emit('handle-search')
    }
  }
}
</script>

<style lang="scss">
  .custom-money-select {
    width: 120px;
    height: 34px;
    position: relative;

    .input-block {
      margin: 0;
      width: 100%;
      height: 100%;
      cursor: pointer;

      .block {
        width: 100%;
        height: 100%;

        >.custom-money-input.el-input {
          width: 100%;
          height: 100%;
          >input {
            width: 100%;
            height: 100%;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            border-radius: 2px;
          }
        }
      }

      .el-input__suffix {
        transform: rotateZ(0);
        width: 25px;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
      }

      &.rote {
        .el-input__suffix {
          transform: rotateZ(180deg);
        }
      }
    }

    .options-block {
      position: absolute;
      margin-top: 12px;
      min-width: 120px;
      font-size: 14px;
      color: #666666;
      background-color: #fff;
      border: 1px solid #E4E7ED;
      padding: 6px 0;
      border-radius: 4px;
      z-index: 9;


      // .arrow {
      //   position: absolute;
      //   width: 0;
      //   height: 0;
      //   top: -12px;
      //   left: 35px;
      //   border: 6px solid transparent;
      //   border-bottom-color: #E4E7ED;

      //   &::after {
      //     position: absolute;
      //     display: inline-block;
      //     left: -4px;
      //     top: -2px;
      //     content: '';
      //     width: 0;
      //     height: 0;
      //     border: 4px solid transparent;
      //     border-bottom-color: #fff;
      //     z-index: 9;
      //   }
      // }

      .option {
        padding: 0 24px 0 16px;
        box-sizing: border-box;
        width: 400px;
        height: 36px;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .number-box {
          display: flex;
          align-items: center;

          >span {
            margin: 0 10px;
          }

          .number-input {
            padding: 0 24px 0 10px;
            width: 100px !important;
            height: 30px;
            line-height: 30px;
            border: none;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            width: 206px;
            border: 1px solid #DCDCDC;
            border-radius: 2px;

            &::-webkit-outer-spin-button,
            &::-webkit-inner-spin-button {
              -webkit-appearance: none;
            }
            &[type="number"]{
              -moz-appearance: textfield;
            }
          }

          .number-button {
            padding: 0;
            width: 60px;
            height: 30px;
            line-height: 30px;
            margin-left: 10px;
          }
        }

        >span {
          display: inline-block;
        }

        &.active {
          background-color: #F2F7FF;
          color: #0381FA;
        }

        &:hover {
          background-color: #F5F7FA;
        }
      }
    }

    .number-input {
      padding: 0 24px 0 10px;
      width: 60px !important;
      height: 30px;
      line-height: 30px;
      border: none;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      width: 206px;
      border: 1px solid #DCDCDC;
      border-radius: 2px;

      &::-webkit-outer-spin-button,
      &::-webkit-inner-spin-button {
        -webkit-appearance: none;
      }
      &[type="number"]{
        -moz-appearance: textfield;
      }
    }

    .number-button {
      padding: 0;
      width: 60px;
      height: 30px;
      line-height: 30px;
      margin-left: 10px;
    }
  }
</style>
