<template>
  <div class="adb-filter-button-group filters-buttons-wrapper select-container">
    <!-- show -->
    <div
      v-if="arrButtons.length"
      class="adb-filter-button-group__list"
      :class="!isAllButton ? 'adb-filter-button-group__list_btn' : ''"
    >
      <adb-filter-button-item
        v-for="item in arrButtons"
        :key="item.key"
        :item="item"
        v-model="filters[item.key]"
      >
        <span slot="optionItem" slot-scope="{ option }">
          <slot :name="item.slot" :option="option"></slot>
        </span>
      </adb-filter-button-item>
    </div>
    <!-- hidden -->
    <el-button
      v-if="!isShowAll"
      class="adb-filter-button-group__filter-btn"
      :title="$tc('adb-filter.filterTitle')"
      size="medium"
      @click="visibleFilters = true"
      :class="!isAllFilters ? 'border-left_none' : ''"
    >
      <adb-icon icon-class="spb-filter" />
      <span v-if="isAllFilters"> {{ $tc('adb-filter.filterTitle') }}</span>
    </el-button>
    <adb-filter-drawer
      v-if="visibleFilters"
      :show.sync="visibleFilters"
      v-model="filters"
      :options="drawerFilterOptions"
    />
  </div>
</template>

<script>
import AdbFilterButtonItem from '@adber/shipber-ui/packages/custom/filter-button-item';
import AdbIcon from '@adber/shipber-ui/packages/base/adb-icon';
import AdbFilterDrawer from '@adber/shipber-ui/packages/custom/filter-drawer';
import commonMessage from '@adber/shipber-ui/packages/locale';

export default {
  name: 'adb-filter-button-group',
  i18n: {
    sharedMessages: commonMessage,
  },
  components: { AdbFilterButtonItem, AdbIcon, AdbFilterDrawer },
  props: {
    filterOptions: {
      type: Array,
      default: () => ({}),
    },
    value: {
      default: undefined,
    },
  },
  data() {
    return {
      filters: this.$_.cloneDeep(this.value),
      visibleFilters: false,
    };
  },
  computed: {
    validateFilterOption() {
      return this.filterOptions.filter((option) => !option.is_hide);
    },
    arrButtons() {
      return this.validateFilterOption.filter((item) => item.is_button);
    },

    isAllFilters() {
      return this.validateFilterOption.every((item) => !item.is_button);
    },

    isAllButton() {
      return this.validateFilterOption.every((item) => item.is_button);
    },

    isShowAll() {
      return this.validateFilterOption.every((item) => item.is_button);
    },
    drawerFilterOptions() {
      return this.filterOptions.filter(
        (option) => !option.is_hide && !option.is_button
      );
    },
  },
  watch: {
    value: {
      handler(newVal) {
        if (this.$_.isEqual(newVal, this.filters)) return;
        this.filters = this.$_.cloneDeep(newVal);
      },
      deep: true,
    },
    filters: {
      handler(newVal) {
        if (this.$_.isEqual(newVal, this.value)) return;
        // const query = formatDate2ViewDate(newVal, this.filterOptions);
        this.$emit('input', this.$_.cloneDeep(newVal));
        this.$emit('commit');
      },
      deep: true,
    },
  },
  methods: {
    onChangeOption(key, value) {
      console.log(key, value);
    },
  },
};
</script>
<style lang="scss" scoped>
.adb-filter-button-group {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  /deep/ .el-button {
    border-radius: 6px;
    border-color: #c6cdd5;
  }
  &__list {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    color: #32363b;
    span {
      color: #32363b;
    }
    > *:not(:first-child) {
      margin-left: -1px;
    }

    > *:hover {
      z-index: 10;
    }
    > *:first-child {
      border-top-left-radius: 8px;
      border-bottom-left-radius: 8px;
      box-sizing: border-box;
    }

    > *:not(:first-child) {
      /deep/ * {
        border-top-left-radius: 0;
        border-bottom-left-radius: 0;
      }
    }

    > *:not(:last-child) {
      /deep/ * {
        border-top-right-radius: 0;
        border-bottom-right-radius: 0;
      }
    }
    &_btn {
      > *:not(:first-child) {
        /deep/ * {
          border-top-right-radius: 0;
          border-bottom-right-radius: 0;
          border-right: none;
        }
      }
    }

    font-family: Helvetica Neue;
    font-style: normal;
    font-weight: normal;
    font-size: 14px;
    line-height: 17px;

    /deep/ .el-button {
      height: 36px;
      span {
        color: #32363b;
        font-family: Helvetica Neue;
        font-size: 14px;
        font-weight: normal;
      }
    }
  }

  &__filter-btn {
    // width: 36px;
    /deep/ &.el-button {
      padding: 9px;
      svg {
        font-size: 16px;
      }
      span {
        font-size: 14px;
        color: #32363b;
        font-weight: normal;
      }
    }
  }
  .border-left_none {
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
  }
  .border-right_none {
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
  }
}
</style>
