<template>
  <div class="picker-wrap">
    <div class="picker-wrap-txt" @click="show = true">
      <input type="text" readonly :value="pickerValShow" :placeholder="placeholder" />
      <svg-icon icon-class="triangle-right" slot="button" />
    </div>
    <cr-popup v-model="show" position="bottom" get-container="body">
      <cr-picker
        ref="picker"
        :columns="pickerData"
        value-key="title"
        show-toolbar
        @cancel="onCancel"
        @confirm="onConfirm"
      />
    </cr-popup>
  </div>
</template>

<script>
// TODO 目前没有用到的业务场景，默认值还未做处理
import popupPickerMixin from "../mixins/popupPicker.mixin.js";
export default {
  name: "PopupPicker",
  mixins: [popupPickerMixin],
  watch: {
    value(val) {
      if (val) {
        this.setDefaultVal();
        this.handleResult([this.pickerData.find(item => item.value === this.value)]);
      }
    }
  },
  methods: {
    handleResult(res) {
      this.pickerValShow = res[0].title;
      this.pickerVal = res[0].value;
    },
    setDefaultVal() {
      setTimeout(() => {
        const $picker = this.$refs.picker;
        $picker.setColumnIndex(
          0,
          this.pickerData.findIndex(item => item.value === this.value)
        );
      });
    }
  }
};
</script>
<style lang="less">
.picker-wrap {
  width: 100%;
  &-txt {
    height: 24px;
    color: #333;
    font-size: 14px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  input {
    color: #333;
    width: 90%;
  }
  .svg-icon {
    height: 16px;
    width: 16px;
  }
}
</style>
