<template>
  <view class="u-select" @click="close" :class="{ disabled: disabled }">
    <view class="u-select__content">
      <view class="u-select__label" @click.stop="openSelect">
        <slot name="text">
          <trans class="u-select__text" :_t="label" />
        </slot>
        <slot name="icon" v-if="!icon">
          <u-icon
            name="arrow-down"
            :size="iconSize"
            :color="iconColor"
          ></u-icon>
        </slot>
      </view>
      <view class="u-select__options" :style="{ zIndex: zIndex }" v-if="isOpen">
        <slot name="options">
          <view class="options" v-if="options.length">
            <view
              class="u-select__options_item"
              :class="current == index ? 'active' : ''"
              :key="index"
              v-for="(item, index) in options"
              @click.stop="selectItem(item, index)"
            >
              <slot name="optionItem" :item="item">
                <trans
                  class="u-select__item_text"
                  :style="{ color: itemColor }"
                  :_t="item[keyName]"
                  v-if="!isTran"
                />
                <text
                  class="u-select__item_text"
                  :style="{ color: itemColor }"
                  v-else
                >
                  {{ item[keyName] }}
                </text>
              </slot>
            </view>
          </view>
          <view class="_no" v-else>
            <trans class="no_text" _t="无数据" />
          </view>
        </slot>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: "up-select",
  emits: ["update:current", "select", "selectClick"],
  props: {
    label: {
      type: String,
      default: "选项",
    },
    options: {
      type: Array,
      default: () => {
        return [];
      },
    },
    keyName: {
      type: String,
      default: "name",
    },
    current: {
      type: [String, Number],
      default: "",
    },
    zIndex: {
      type: Number,
      default: 10,
    },
    itemColor: {
      type: String,
      default: "#333333",
    },
    iconColor: {
      type: String,
      default: "",
    },
    iconSize: {
      type: [String],
      default: "13px",
    },
    icon: Boolean,
    isTran: Boolean,
    disabled: {
      // 新增的禁用属性
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      isOpen: false,
    };
  },
  methods: {
    openSelect() {
      this.isOpen = !this.isOpen;
      this.$emit("selectClick");
    },
    close() {
      this.isOpen = false;
    },
    selectItem(item, index) {
      this.isOpen = false;
      this.$emit("update:current", index);
      this.$emit("select", item);
    },
  },
};
</script>

<style lang="scss" scoped>
.u-select__content {
  position: relative;

  .u-select__label {
    display: flex;
    font-size: 28rpx;
    font-weight: 500;
    line-height: 60rpx;
    margin: 0 8rpx;
    color: var(--text-02);

    /* #ifdef H5 */
    &:hover {
      cursor: pointer;
    }

    /* #endif */
  }

  .u-select__text {
    margin-right: 2px;
  }

  .u-select__options {
    min-width: 300rpx;
    box-sizing: border-box;
    border-radius: 4px;
    border: 1px solid #f1f1f1;
    background-color: #fff;
    position: absolute;
    top: auto;
    left: -10px;
    padding: 10rpx 0;
    box-shadow: 0px 0px 12px #0000001f;
    margin-top: 18rpx;
    overflow: hidden scroll;

    .options {
      max-height: 274px;
      overflow: hidden scroll;
    }

    .u-select__options_item {
      box-sizing: border-box;
      width: 100%;
      min-width: 300rpx;

      &:hover {
        background-color: #f7f7f7;
      }

      /* #ifdef H5 */
      &:hover {
        cursor: pointer;
      }

      .u-select__item_text {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        &:hover {
          cursor: pointer;
        }
      }

      /* #endif */
    }
  }

  .u-select__options::after {
    content: "";
    position: absolute;
    left: 60rpx;
    top: -12rpx;
    // transform: translateX(-30%);
    border-left: 14rpx solid transparent;
    border-right: 14rpx solid transparent;
    border-bottom: 14rpx solid var(--light);
    z-index: 10;
  }
}

._no {
  text-align: center;

  .no_text {
    padding: 20rpx 0;
    text-align: center;
    font-size: 24rpx;
    color: #909399;
  }
}

.disabled {
  pointer-events: none;
  border: 2rpx solid #dadbde !important;
  background-color: rgb(245, 247, 250) !important;
  box-shadow: 0 0 0 1px var(--borderColor) inset;
  color: #606266;
}
</style>
