const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    init: {
      type: Object
    },
    width: {
      type: String
    },
    left: {
      type: String
    }
  },

  /**数据监听 */
  observers: {
    init: function (val) {
      if (val) {
        this.setData({
          item: val,
        })
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    index: 0,
    item: '',
    columns: [],
    projectList: [],
  },

  created() {
    if (this.data.init && this.data.init.id == "") {
      this.setData({
        projectList: this.data.init.concat(app.globalData.projectInfoList)
      })
    } else {
      this.setData({
        projectList: app.globalData.projectInfoList
      })
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showPopup() {
      if (this.data.projectList.length > 1) {
        let data = this.data.projectList;
        let initData = this.data.init;
        let selectColumns = [];
        data.forEach((item, i) => {
          selectColumns.push({
            id: item.id,
            text: item.text
          });
          if (initData.id == item.id) {
            this.setData({
              index: i
            })
          }
        });
        console.log("default-index=" + this.data.index);
        this.setData({
          columns: selectColumns,
          show: true
        });
      }
    },

    onClose() {
      this.setData({
        show: false
      });
    },

    onSelectChange(e) {
      this.setData({
        item: e.detail.value
      })
    },

    onConfirm() {
      this.setData({
        value: this.data.item.text
      })
      this.triggerEvent('change', this.data.item)
      this.setData({
        show: false
      });
    },

  }
})