PopupSelect.js 5.55 KB
// components/CarBrands/CarBrands.js
// import api from '../../api/api.js'
import { optionsSelect } from '../constant/stepData'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      observer: function (n) {
        if (n) {
          // console.log(n)
          // n & this.initOptions()
        }
      }
    },
    typeName: {
      type: String,
      observer: function (n) {
        n & this.initOptions()
        // this.initOptions()
      }
      // value: 'companyType'
    },
    popObj: {
      type: Object,
      observer: function (n) {
        // console.log(n)
        // console.log(n.popValue)
        // n?.popValue && this.changeOptions()
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    orderInfo: {},
    companytype: {
      name: '保险公司',
      height: '85%',
      optionsList: [
        {
          value: '中国人保'
        },
        {
          value: '中国平安'
        },
        {
          value: '太平洋保险'
        },
        {
          value: '中国人寿财险'
        },
        {
          value: '大地保险'
        },
        {
          value: '中华保险'
        },
        {
          label: '自定义',
          value: '',
          type: 'input'
          // active: true
        }
      ]
    },
    threetype: {
      name: '第三方责任险',
      height: '85%',
      optionsList: [
        {
          value: '100万'
        },
        {
          value: '200万'
        },
        {
          value: '300万'
        },
        {
          value: '400万'
        },
        {
          value: '500万'
        },
        {
          label: '自定义',
          value: '',
          type: 'input'
        }
      ]
    },
    drivertype: {
      name: '车上人员责任险(司机)',
      height: '85%',
      optionsList: [
        {
          value: '1万'
        },
        {
          value: '3万'
        },
        {
          value: '5万'
        },
        {
          label: '自定义',
          value: '',
          type: 'input'
        }
      ]
    },
    passengertype: {
      name: '车上人员责任险(乘客)',
      height: '85%',
      optionsList: [
        {
          value: '1万'
        },
        {
          value: '3万'
        },
        {
          value: '5万'
        },
        {
          label: '自定义',
          value: '',
          type: 'input'
        }
      ]
    },
    gearboxtype: {
      name: '变速箱类型',
      height: '85%',
      optionsList: [
        {
          value: 'MT变速箱'
        },
        {
          value: 'AMT变速箱'
        },
        {
          value: 'AT变速箱'
        },
        {
          value: 'CVT变速箱'
        },
        {
          value: 'DCT变速箱'
        },
        {
          label: '自定义',
          value: '',
          type: 'input'
        }
      ]
    },
    optionsList: []
  },
  pageLifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      // console.log('attached', attached)
      this.initOptions()
    },
    show: function () { },
    hide: function () { }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // init() {},
    initOptions() {
      // 根据字段名取对应的对象
      let { typeName } = this.data
      console.log(typeName, 'typeName')
      let optionsObj = this.data[typeName]
      // console.log()
      console.log('optionsList', optionsObj)
      this.setData({
        optionsObj,
        optionsList: optionsObj.optionsList
      })
      this.changeOptions()
    },
    changeOptions() {
      // 看值是否在列表中,没有认为是自定义
      let { popValue } = this.data.popObj
      console.log(this.data.popObj)
      let { optionsList } = this.data
      console.log(optionsList, 'optionsList')
      let flag = true
      optionsList.forEach((item, index) => {
        // item.value == popValue
        if (item.value == popValue) {
          flag = false
          item.active = true
        } else if (item?.type == 'input' && flag) {
          item.active = true
          item.value = popValue
        } else {
          item.active = false
        }
      })
      this.setData({
        optionsList
      })
    },
    handleInput(e) {
      let { item, index } = e.currentTarget.dataset
      let { optionsList } = this.data
      console.log(e.detail.value, index)
      this.setData({
        ['optionsList[' + index + '].value']: e.detail.value
      })
    },
    choose(e) {
      let { item, index } = e.currentTarget.dataset
      console.log(item, index)
      let { optionsList } = this.data
      optionsList = optionsList.map(item => {
        item.active = false
        return item
      })
      optionsList[index].active = true

      this.setData({
        optionsList
      })
    },
    confirm() {
      let { attrname, attrtype } = this.data.popObj
      let { optionsList } = this.data
      let flag = optionsList.find(item => {
        return item.active
      })
      if (!flag) {
        return wx.$util.showToast('请选择')
      }
      // this.triggerEvent('confirm', orderInfo)
      let thisPage = wx.$router.getPageInstance()
      // let popValue = thisPage.data[attrname][attrtype]
      console.log(flag.value, attrname, attrtype, 'flag.value')
      if (attrtype || attrname) {
        thisPage.setData({
          ['orderInfo' + '.' + attrtype + '.' + attrname]: flag.value
        })
      }
      this.setData({
        show: false
      })
      this.triggerEvent('confirm', {value: flag.value})
    }
  }
})