// components/input-search-select/input-search-select.js
var myRequest = require("../../utils/api.js");
var utils = require("../../utils/utils.js");
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    inputLabel:String,
    value:null,
    parentData:null,
    index:Number

  },

  /**
   * 组件的初始数据
   */
  data: {
    selectDraw:false,
    HandlersData:[],
    inputFouce:false,
    searchName:'-1',
    isScroll:false,
    selectFlag:false,


  },

  /**
   * 组件的方法列表
   */
  methods: {
    clearvalue(){
      this.setData({
        value:{dictName:''}
      })
    },
    clickSelect(e){
     
      let item = e.currentTarget.dataset.val.item
      console.log(item)
      this.setData({
        selectDraw:false,
        value:{
          dictName: this.properties.index!=4?item.dictName:item.prodName
        },
        selectFlag:true
      })
      this.triggerEvent('setDist',{
        index:this.properties.index,
        val:{
          value:item.dictId,
          label:item.dictName,
          item:item
        }
      })

    },
    getlableData(dictName) {
      var that = this;


      return new Promise((resole, reject) => {
        if(that.data.index==4){
          myRequest.myRequest('prodInfo/list', {
            dictId:that.properties.parentData.value,
            prodName:dictName
          }, 'get').then(function (res) {
            if (res.data.data && res.data.data.length != 0) {
              that.setData({
                proList: res.data.data
              })
              let HandlersData = res.data.data.map(item => {
                return {
                  label: item.prodName,
                  value: item.prodId,
                  item:item
                }
              })
              resole(HandlersData)
            } else {
              reject('暂无当前数据，请配置')
            }
    
          }).catch(function (res) {
            console.log(res);
          })

        }else{
          myRequest.myRequest('renovationDict/list', {
            parentId:that.properties.parentData.value,
            dictName
          }, 'get').then(function (res) {
            if (res.data.data.length) {
              let HandlersData = res.data.data.map(item => {
                return {
                  label: item.dictName,
                  value: item.dictId,
                  item: item
                }
              })
              resole(HandlersData)
    
            } else {
              reject([])
            }
    
          }).catch(function (res) {
            console.log(res);
          })

        }
     
  
      })
  
    },

    clickHandler(e) {
      e=e[0]
    
   
      if(this.data.selectFlag){
        this.setData({
          selectFlag:false,
        })
        return
      }
      if(e.type=="change"){
        this.triggerEvent('setDist',{
          index:this.properties.index,
          val:{
            value:'',
            label:e.detail.value
          }
        })
      }
      console.log(this.data.searchName,e.detail.value)
      if(this.data.searchName!=e.detail.value){
        if(this.properties?.parentData?.value){
        
          this.getlableData(e.detail.value).then(res => {
          console.log(111111111111)
            this.setData({
              selectDraw:true,
              HandlersData: res,
              searchName:e.detail.value
            })
      
          }).catch((error) => {
            console.log(3)
            this.setData({
              selectDraw:false,
              searchName:e.detail.value,
              HandlersData: [],
            })
          })
  
        }

      }
      
    },
    focusHandle:utils.debounce(function (e) {
        this.clickHandler(e)
    }),
      
  
    
    blurHandle(){
      if( !this.data.isScroll){
        this.setData({
        selectDraw:false,
        searchName:'-1',
        HandlersData: [],
      })
      }
   
    },
    bindScrollStart(){
      this.setData({
        isScroll:true,
      })
    },
    bindScrollEnd(e){
       this.setData({
        inputFouce:true,
        isScroll:false
      })
    },
    

  }
})