<template>
    <div class="myedit">
        <div class="editable-cell">
        <div v-if="editable" class="editable-cell-input-wrapper">
          <a-input v-model="value" @blur="blurChange" @change="handleChange" @pressEnter="check" /><a-icon
            type="check"
            class="editable-cell-icon-check"
            @click.stop="check"
          />
        </div>
        <div v-else class="editable-cell-text-wrapper">
          <span v-if="slotName=='budget'">{{ value*1? `日预算：${value*1}` :value }}</span>
          <span v-if="slotName=='bid'">{{ value*1? `${value*1}` :value }}</span>
          <span v-if="slotName=='cpaBid'">{{ value*1? `${value*1}` :value }}</span>
          <span v-if="slotName=='name'">{{value}}</span>
          <a-icon type="edit" class="editable-cell-icon" @click.stop="edit" />
        </div>
      </div>
    </div>
</template>

<script>
    export default {
        props: ["text",'slotName'],
        
        data() {
            return {
              value: this.text,
              editable: false,
            };
        },
        watch:{
          text:{
            handler(n,o){
              this.value=n
            },
            deep:true
          }
        },
        methods: {
            blurChange(e) {
              console.log(e)
              const value = e.target.value;
              if(this.slotName=='budget'){
                if(this.value*1){
                  if(value*1 && value*1>300 &&value*1<9999999.99){
                    this.value = value;
                  }else{
                    this.$message.error('请输入300-9999999.99的数')
                  }
                }else{
                  this.value='不限'
                }    
              }else if(this.slotName=='bid'){
                if(this.value*1){
                  if(value*1 && value*1>0.1 &&value*1<9999999.99){
                    this.value = value;
                  }else{
                    this.$message.error('请输入0.1-9999999.99的数')
                  }
                }else{
                  this.value='不限'
                }   
              }else if(this.slotName=='cpaBid'){
                if(this.value*1){
                  if(value*1 && value*1>0.1 &&value*1<10000){
                    this.value = value;
                  }else{
                    this.$message.error('请输入0.1-10000的数')
                  }
                }else{
                  this.value='不限'
                }   
              }
              
            },
            handleChange(e){
              console.log(e)
            },
            check() {
              this.editable = false;
              this.$emit('change', this.value);
            },
            edit() {
              this.editable = true;
            },
        },
    }
</script>

<style lang="scss" scoped>
.myedit{
  .editable-cell {
  position: relative;
}

.editable-cell-input-wrapper,
.editable-cell-text-wrapper {
  padding-right: 24px;
}

.editable-cell-text-wrapper {
  padding: 5px 24px 5px 5px;
}

.editable-cell-icon,
.editable-cell-icon-check {
 position: absolute;
  right: 0;
  top: 6px;
  width: 20px;
  cursor: pointer;
}

.editable-cell-icon {
  line-height: 18px;
  display: none;
}

.editable-cell-icon-check {
  line-height: 28px;
}

.editable-cell:hover .editable-cell-icon {
  display: inline-block;
}

.editable-cell-icon:hover,
.editable-cell-icon-check:hover {
  color: #108ee9;
}

.editable-add-btn {
  margin-bottom: 8px;
}
}
</style>