<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>-->
            <div style="position: relative">
              自定义
              <div class="popper_box" style="position: absolute" v-if="value === '自定义'">
                <div class="popper_wrap">
                  从
                  <el-input class="popper_input" type="number" clearable v-model="startMoney"></el-input>
                  万
                </div>
                <div class="popper_wrap">
                  到
                  <el-input class="popper_input" type="number" clearable v-model="endMoney"></el-input>
                  万
                </div>
                <div style="">
                  <el-button size="mini" @click.stop="cancellation">取消</el-button>
                  <el-button type="primary" size="mini" @click.stop="handleConfirm">确定</el-button>
                </div>
              </div>
            </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: '请选择'
    },
    'moneyValue': {
      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
    }
    if(this.moneyValue){
      this.value = this.moneyValue
    }
  },
  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 '10亿以上':
            moneyStr = [100000]
            break;
          case '5亿以上':
            moneyStr = [50000]
            break;
          case '2亿以上':
            moneyStr = [20000]
            break;
          case '一亿以上':
            moneyStr = [10000]
            break;
          case '5亿-10亿':
            moneyStr = [50000, 100000]
            break;
          case '1亿-10亿':
            moneyStr = [10000, 100000]
            break;
          case '1亿-5亿':
            moneyStr = [10000, 50000]
            break;
          case '5000万-1亿':
            moneyStr = [5000, 10000]
            break;
          case '1000万-5000万':
            moneyStr = [1000, 5000]
            break;
          case '2000万-1亿':
            moneyStr = [2000, 10000]
            break;
          case '400万-2000万':
            moneyStr = [400, 2000]
            break;
          case '5000万以下':
            moneyStr = [, 5000]
            break;
          case '1000万以下':
            moneyStr = [, 1000]
            break;
          case '400万以下':
            moneyStr = [, 400]
            break;
          default:
            moneyStr = [, 1000]
            break;
        }
        this.$emit('input', moneyStr)
        this.$emit('handle-search')
      }
    },
    // 自定义取消
    cancellation(){
      this.isSelectOption = false
      this.value = ''
      this.$emit('input', '')
      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: 4px;
          }
        }
      }

      .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: 3px;
      min-width: 120px;
      font-size: 14px;
      color: #666666;
      background-color: #fff;
      border: 1px solid #E4E7ED;
      padding: 6px 0;
      border-radius: 4px;
      z-index: 10;


      // .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: 140px;
        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;
            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;
    }

    .popper_box {
      position: absolute;
      left: 128px;
      bottom: -15px;
      background: #ffffff;
      width: 186px;
      color: #606266;
      text-indent: 0;
      padding: 14px;
      padding-top: 0px;
      border: 1px solid #e0e0e0;
      .popper_wrap {
        margin-top: 16px;
        display: inline-block;
        .popper_input {
          width: 90px;
          display: inline-block;
          margin: 0px 8px;
          line-height: 34px;
        }
        .el-input__inner {
          width: 90px;
        }
      }
      input::-webkit-outer-spin-button,
      input::-webkit-inner-spin-button {
        -webkit-appearance: none !important;
      }
      input[type='number'] {
        -moz-appearance: textfield;
      }
    }
    .popper_box div:last-child {
      display: flex;
      justify-content: center;
      margin-top: 16px;
    }




  }
</style>
