<template>
  <div class="scope-component-group">
    <div
      class="scope-item"
      :class="{ active: item.checked, disabled: item.disabled }"
      v-for="(item, index) in getOptions"
      :key="index"
      @click="onCellClick(item)"
    >
      <div class="scope-item-label">
        <span class="item-label">{{ item.label }}</span>
        <span v-if="item.vip && item.disabled" class="v-vip-icon"></span>
        <slot :item="item"></slot>
      </div>
      <div class="scope-item-action">
        <span class="jy-tag" v-if="item.tag && item.disabled">{{
          item.tag
        }}</span>
        <van-checkbox :value="item.checked" v-else>
          <template #icon>
            <slot name="icon" :item="item">
              <AppIcon
                :name="item.checked ? checkedIcon : defaultIcon"
                size="22"
                :color="item.checked ? checkedColor : defaultColor"
              />
            </slot>
          </template>
        </van-checkbox>
      </div>
    </div>
  </div>
</template>

<script>
import { Checkbox } from 'vant'
import { AppIcon } from '@/ui'
export default {
  name: 'scopeSelectorContent',
  components: {
    [Checkbox.name]: Checkbox,
    AppIcon
  },
  props: {
    /**
     * 数据源
     */
    options: {
      type: Array,
      default() {
        return []
      }
    },
    /**
     * 是否多选
     */
    multiple: {
      type: Boolean,
      default: true
    },
    /**
     * 最小选中数量
     */
    minSelect: {
      type: Number,
      default: 1
    },
    /**
     * 默认选中的数据(不同场景默认数据可能不一致)
     */
    defaultVal: {
      type: Array,
      default() {
        return []
      }
    },
    /**
     * 未选中展示的icon 默认为unchoose1(灰色圆) 传入AppIcon的name 传空即不展示
     */
    defaultIcon: {
      type: String,
      default: 'unchoose1'
    },
    /**
     * 选中的icon图标 默认为choose1(圆形checkbox）传入AppIcon的name
     */
    checkedIcon: {
      type: String,
      default: 'choose1'
    },
    /**
     * 选中图标的颜色
     */
    checkedColor: {
      type: String,
      default: '#2ABED1'
    },
    /**
     * 默认图标的颜色
     */
    defaultColor: {
      type: String,
      default: '#C0C4CC'
    }
  },
  computed: {
    getCheckedValue() {
      return this.getOptions.filter((v) => v.checked).map((s) => s.key)
    }
  },
  data() {
    return {
      value: this.defaultVal,
      getOptions: this.options
    }
  },
  watch: {
    options() {
      this.initData(this.options)
    }
  },
  created() {
    this.initData(this.getOptions)
  },
  methods: {
    initData(data = []) {
      const list = JSON.parse(JSON.stringify(data))
      list.forEach((v) => {
        v.checked = false
      })
      this.getOptions = list
      if (this.value.length) {
        this.setState(this.value)
      }
    },
    setState(data) {
      if (!data) return
      const list = this.getOptions
      list.forEach((v) => {
        if (data.indexOf(v.key) > -1) {
          v.checked = true
        } else {
          v.checked = false
        }
      })
      this.value = data
    },
    getState() {
      return this.getCheckedValue
    },
    onCellClick(item) {
      if (item.disabled) {
        this.$emit('open-tag', item)
      } else {
        if (this.multiple) {
          if (item.checked) {
            const canCancelSelect =
              this.getOptions.filter((v) => v.checked).length > this.minSelect
            if (canCancelSelect) {
              item.checked = !item.checked
            }
          } else {
            item.checked = !item.checked
            if (item.label === '全部') {
              this.getOptions.forEach((v) => {
                v.checked = false
              })
              item.checked = !item.checked
            } else {
              this.getOptions.forEach((v) => {
                if (v.label === '全部') {
                  v.checked = false
                }
              })
            }
          }
        } else {
          this.getOptions.forEach((v) => {
            v.checked = false
          })
          item.checked = true
        }
      }
      /**
       * 点击事件 (item, values)
       * @event change
       */
      this.$emit('change', {
        item,
        value: this.getCheckedValue
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.scope-component-group {
  .scope-item {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    font-size: 14px;
    font-weight: 500;
    color: $gray_7;
    line-height: 20px;
    box-sizing: border-box;
    &.active {
      color: $main;
    }
    &:not(:last-child)::after {
      position: absolute;
      content: '';
      bottom: 0;
      left: 16px;
      width: 100%;
      height: 1px;
      background: $border_color_3;
    }
    .v-vip-icon{
      display: inline-block;
      width: 14px;
      height: 14px;
      margin-left: 4px;
      background: url(@/assets/image/icon/vip/v_icon.png) no-repeat center;
      background-size: contain;
      margin-bottom: 2px;
    }
  }

  ::v-deep {
    .van-checkbox__icon {
      height: 1.1em;
      line-height: 1.1em;
    }
  }
}
</style>
